-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds mechanism to include feature flags in Polymer HTTP requests #5727
Changes from all commits
15fe0aa
d6f531f
a79470b
abb4835
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
load("//tensorboard/defs:defs.bzl", "tf_ng_web_test_suite", "tf_ts_library") | ||
|
||
package(default_visibility = ["//tensorboard:internal"]) | ||
|
||
licenses(["notice"]) | ||
|
||
tf_ts_library( | ||
name = "tf_feature_flags", | ||
srcs = [ | ||
"feature-flags.ts", | ||
"tf-feature-flags-polymer.ts", | ||
], | ||
deps = [ | ||
"//tensorboard/webapp/feature_flag:types", | ||
"@npm//@polymer/decorators", | ||
"@npm//@polymer/polymer", | ||
], | ||
) | ||
|
||
tf_ts_library( | ||
name = "test_lib", | ||
testonly = True, | ||
srcs = [ | ||
"feature-flags-test.ts", | ||
], | ||
deps = [ | ||
":tf_feature_flags", | ||
"//tensorboard/webapp/feature_flag:testing", | ||
"@npm//@types/jasmine", | ||
], | ||
) | ||
|
||
tf_ng_web_test_suite( | ||
name = "karma_test", | ||
deps = [ | ||
":test_lib", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
import {buildFeatureFlag} from '../../webapp/feature_flag/testing'; | ||
import * as feature_flags from './feature-flags'; | ||
|
||
describe('feature-flags', () => { | ||
beforeEach(() => { | ||
feature_flags.initializeFeatureFlags(); | ||
}); | ||
|
||
it('sets and gets FeatureFlags', () => { | ||
feature_flags.setFeatureFlags(buildFeatureFlag({inColab: true})); | ||
expect(feature_flags.getFeatureFlags()).toEqual( | ||
buildFeatureFlag({inColab: true}) | ||
); | ||
}); | ||
|
||
it('throws Error if getFeatureFlags is called before setFeatureFlags', () => { | ||
expect(() => feature_flags.getFeatureFlags()).toThrow( | ||
new Error('FeatureFlags have not yet been determined by TensorBoard.') | ||
); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
import {FeatureFlags} from '../../webapp/feature_flag/types'; | ||
|
||
// FeatureFlags are managed and injected by the Angular portion of the TB code | ||
// base. In practice they are set one time soon after application load and never | ||
// again for the lifetime of the application. | ||
let _featureFlags: FeatureFlags | null; | ||
initializeFeatureFlags(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems strange. So _featureFlags is initialized by this call in the application but this is called manually in tests? Is this s typical structure in the polymer code? |
||
|
||
export function initializeFeatureFlags(): void { | ||
_featureFlags = null; | ||
} | ||
|
||
/** | ||
* Sets the FeatureFlags for use in the Polymer portion of the TB code base. | ||
* | ||
* In practice this should only be called by the Angular portion of the TB code | ||
* base immediately after it determines the final set of FeatureFlags. | ||
*/ | ||
export function setFeatureFlags(featureFlags: FeatureFlags): void { | ||
_featureFlags = featureFlags; | ||
} | ||
|
||
/** | ||
* Retrieves FeatureFlags. | ||
* | ||
* @throws Error if FeatureFlags have not yet been set. In practice they should | ||
* be set soon after application load before any Polymer code is invoked. | ||
* This runtime check acts as a sanity check to enforce that assumption. | ||
*/ | ||
export function getFeatureFlags(): FeatureFlags { | ||
if (_featureFlags === null) { | ||
throw Error('FeatureFlags have not yet been determined by TensorBoard.'); | ||
} | ||
return _featureFlags; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
import {customElement} from '@polymer/decorators'; | ||
import {PolymerElement} from '@polymer/polymer'; | ||
import * as tf_feature_flags from './feature-flags'; | ||
|
||
/** | ||
* This Polymer component allows the underlying API to be accessible from | ||
* Angular TensorBoard by exposing otherwise mangled smybols. | ||
*/ | ||
@customElement('tf-feature-flags') | ||
class TfFeatureFlags extends PolymerElement { | ||
override _template = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means that this is an element with no UI component? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Honestly I don't know. I'm following the pattern established for similar files, like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, that's what it means. The more above-board way to do this would be the static method described here: Overriding
bmd3k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tf_feature_flags = tf_feature_flags; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this even do? Assigning a variable to itself? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, sorry, honestly I don't know. I'm following the pattern established for similar files, like: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might look confusing but syntactically it's not assigning a variable to itself; it's defining a property on the element and assigning its value to be the imported module from You can see how this mechanism works a bit better with the existing tf_globals code:
bmd3k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,17 +16,27 @@ limitations under the License. | |
import {Injectable} from '@angular/core'; | ||
import {Actions, createEffect, ofType} from '@ngrx/effects'; | ||
import {Action, createAction, Store} from '@ngrx/store'; | ||
import {combineLatestWith, map} from 'rxjs/operators'; | ||
import {combineLatestWith, map, tap, withLatestFrom} from 'rxjs/operators'; | ||
import '../../tb_polymer_interop_types'; | ||
import {TBFeatureFlagDataSource} from '../../webapp_data_source/tb_feature_flag_data_source_types'; | ||
import {partialFeatureFlagsLoaded} from '../actions/feature_flag_actions'; | ||
import {ForceSvgDataSource} from '../force_svg_data_source'; | ||
import {getIsAutoDarkModeAllowed} from '../store/feature_flag_selectors'; | ||
import { | ||
getFeatureFlags, | ||
getIsAutoDarkModeAllowed, | ||
} from '../store/feature_flag_selectors'; | ||
import {State} from '../store/feature_flag_types'; | ||
|
||
const effectsInitialized = createAction('[FEATURE FLAG] Effects Init'); | ||
|
||
@Injectable() | ||
export class FeatureFlagEffects { | ||
// Ngrx assumes all Effect classes have properties that inherit from the base | ||
// JS Object. `tf_feature_flags` does not, so we wrap it. | ||
private readonly tfFeatureFlags = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this mean that this object is owned by the FeatureFlagEffects class? Does that |initializeFeatureFlags| function get called when creating this element? Is that what the self assigned variable does? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is also quite a bit of a mystery to me but I believe this just exposes the interop leayer (tf-feature-flags) to the effects class. I'm not certain at what point, exactly, the class gets initialized. I'm following the pattern established here:
bmd3k marked this conversation as resolved.
Show resolved
Hide resolved
|
||
ref: document.createElement('tf-feature-flags').tf_feature_flags, | ||
}; | ||
|
||
/** @export */ | ||
readonly getFeatureFlags$ = createEffect(() => | ||
this.actions$.pipe( | ||
|
@@ -46,6 +56,27 @@ export class FeatureFlagEffects { | |
) | ||
); | ||
|
||
/** | ||
* Pass FeatureFlags to the Polymer portion of the code base immediately after | ||
* feature flags have been finalized. | ||
* | ||
* @export | ||
*/ | ||
readonly updatePolymerFeatureFlags$ = createEffect( | ||
() => | ||
this.actions$.pipe( | ||
// partialFeatureFlagsLoaded triggers this effect but the actual | ||
// feature flag values used are from the Store, given that it contains | ||
// the finalized merged feature flags. | ||
ofType(partialFeatureFlagsLoaded), | ||
withLatestFrom(this.store.select(getFeatureFlags)), | ||
tap(([, featureFlags]) => { | ||
this.tfFeatureFlags.ref.setFeatureFlags(featureFlags); | ||
}) | ||
), | ||
{dispatch: false} | ||
); | ||
|
||
constructor( | ||
private readonly actions$: Actions, | ||
private readonly store: Store<State>, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* Copyright 2022 The TensorFlow Authors. All Rights Reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
==============================================================================*/ | ||
|
||
export const FEATURE_FLAGS_HEADER_NAME = 'X-TensorBoard-Feature-Flags'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems strange. So _featureFlags is initialized by this call in the application but this is called manually in tests? Is this s typical structure in the polymer code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if it is typical. Without testing I would have likely just merged lines 21 to 26 into the following one line:
let _featureFlags: FeatureFlags | null = null;
But I added the initializeFeatureFlags() function to facilitate testing. Unfortunately we don't really test much of the polymer code so I just picked a pattern that worked fo rme.