Skip to content

Commit

Permalink
add redux logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rileyajones committed Jun 15, 2023
1 parent 56c51b0 commit 17a28b7
Show file tree
Hide file tree
Showing 16 changed files with 866 additions and 6 deletions.
2 changes: 2 additions & 0 deletions tensorboard/webapp/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ tf_ng_web_test_suite(
"//tensorboard/webapp/metrics:integration_test",
"//tensorboard/webapp/metrics:test_lib",
"//tensorboard/webapp/metrics:utils_test",
"//tensorboard/webapp/metrics/data_source:card_interactions_data_source_test",
"//tensorboard/webapp/metrics/data_source:metrics_data_source_test",
"//tensorboard/webapp/metrics/effects:effects_test",
"//tensorboard/webapp/metrics/store:store_test",
Expand Down Expand Up @@ -358,6 +359,7 @@ tf_svg_bundle(
"@com_google_material_design_icon//:notifications_none_24px.svg",
"@com_google_material_design_icon//:palette_24px.svg",
"@com_google_material_design_icon//:refresh_24px.svg",
"@com_google_material_design_icon//:science_24px.svg",
"@com_google_material_design_icon//:search_24px.svg",
"@com_google_material_design_icon//:settings_24px.svg",
"@com_google_material_design_icon//:settings_backup_restore_24px.svg",
Expand Down
1 change: 1 addition & 0 deletions tensorboard/webapp/metrics/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ tf_ng_module(
"//tensorboard/webapp/core",
"//tensorboard/webapp/metrics/actions",
"//tensorboard/webapp/metrics/data_source",
"//tensorboard/webapp/metrics/data_source:card_interactions_data_source",
"//tensorboard/webapp/metrics/effects",
"//tensorboard/webapp/metrics/store",
"//tensorboard/webapp/metrics/store:metrics_initial_state_provider",
Expand Down
13 changes: 11 additions & 2 deletions tensorboard/webapp/metrics/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ import {
TimeSeriesRequest,
TimeSeriesResponse,
} from '../data_source';
import {CardState} from '../store/metrics_types';
import {CardInteractions, CardState} from '../store/metrics_types';
import {
CardId,
HeaderEditInfo,
HeaderToggleInfo,
HistogramMode,
MinMaxStep,
PluginType,
TooltipSort,
XAxisType,
Expand Down Expand Up @@ -272,5 +271,15 @@ export const metricsHideEmptyCardsToggled = createAction(
'[Metrics] Hide Empty Cards Changed'
);

export const metricsPreviousCardInteractionsChanged = createAction(
'[Metrics] Card Interactions Changed',
props<{cardInteractions: CardInteractions}>()
);

export const metricsCardClicked = createAction(
'[Metrics] Card Clicked',
props<{cardId: string}>()
);

// TODO(jieweiwu): Delete after internal code is updated.
export const stepSelectorTimeSelectionChanged = timeSelectionChanged;
26 changes: 26 additions & 0 deletions tensorboard/webapp/metrics/data_source/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ tf_ng_module(
],
)

tf_ng_module(
name = "card_interactions_data_source",
srcs = [
"card_interactions_data_source.ts",
"card_interactions_data_source_module.ts",
],
deps = [
"//tensorboard/webapp/metrics/store:types",
"@npm//@angular/core",
],
)

tf_ts_library(
name = "types",
srcs = [
Expand Down Expand Up @@ -70,3 +82,17 @@ tf_ts_library(
"@npm//rxjs",
],
)

tf_ts_library(
name = "card_interactions_data_source_test",
testonly = True,
srcs = [
"card_interactions_data_source_test.ts",
],
deps = [
":card_interactions_data_source",
"//tensorboard/webapp/angular:expect_angular_core_testing",
"//tensorboard/webapp/metrics:internal_types",
"@npm//@types/jasmine",
],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {Injectable} from '@angular/core';
import {CardInteractions} from '../store/metrics_types';

const CARD_INTERACTIONS_KEY = 'tb-card-interactions';

const MAX_RECORDS: Record<keyof CardInteractions, number> = {
pins: 10,
clicks: 10,
tagFilters: 10,
};

@Injectable()
export class CardInteractionsDataSource {
saveCardInteractions(cardInteractions: CardInteractions) {
const trimmedInteractions: CardInteractions = {
pins: cardInteractions.pins.slice(
cardInteractions.pins.length - MAX_RECORDS.pins
),
clicks: cardInteractions.clicks.slice(
cardInteractions.clicks.length - MAX_RECORDS.clicks
),
tagFilters: cardInteractions.tagFilters.slice(
cardInteractions.tagFilters.length - MAX_RECORDS.tagFilters
),
};
localStorage.setItem(
CARD_INTERACTIONS_KEY,
JSON.stringify(trimmedInteractions)
);
}

getCardInteractions(): CardInteractions {
const existingInteractions = localStorage.getItem(CARD_INTERACTIONS_KEY);
if (existingInteractions) {
return JSON.parse(existingInteractions) as CardInteractions;
}
return {
tagFilters: [],
pins: [],
clicks: [],
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright 2023 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 {NgModule} from '@angular/core';
import {CardInteractionsDataSource} from './card_interactions_data_source';

@NgModule({
imports: [],
providers: [CardInteractionsDataSource],
})
export class MetricsCardInteractionsDataSourceModule {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* Copyright 2023 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 {TestBed} from '@angular/core/testing';
import {CardInteractionsDataSource} from './card_interactions_data_source';
import {PluginType} from '../internal_types';

describe('CardInteractionsDataSource Test', () => {
let mockStorage: Record<string, string>;
let dataSource: CardInteractionsDataSource;

beforeEach(async () => {
await TestBed.configureTestingModule({
providers: [CardInteractionsDataSource],
});

dataSource = TestBed.inject(CardInteractionsDataSource);

mockStorage = {};
spyOn(window.localStorage, 'setItem').and.callFake(
(key: string, value: string) => {
if (key !== 'tb-card-interactions') {
throw new Error('incorrect key used');
}

mockStorage[key] = value;
}
);

spyOn(window.localStorage, 'getItem').and.callFake((key: string) => {
if (key !== 'tb-card-interactions') {
throw new Error('incorrect key used');
}

return mockStorage[key];
});
});

describe('saveCardInteractions', () => {
it('only saves 10 pins', () => {
dataSource.saveCardInteractions({
clicks: [],
tagFilters: [],
pins: Array.from({length: 12}).map((_, index) => ({
cardId: `card-${index}`,
runId: null,
tag: 'foo',
plugin: PluginType.SCALARS,
})),
});

expect(dataSource.getCardInteractions().pins.length).toEqual(10);
});

it('only saves 10 clicks', () => {
dataSource.saveCardInteractions({
pins: [],
tagFilters: [],
clicks: Array.from({length: 12}).map((_, index) => ({
cardId: `card-${index}`,
runId: null,
tag: 'foo',
plugin: PluginType.SCALARS,
})),
});

expect(dataSource.getCardInteractions().clicks.length).toEqual(10);
});

it('only saves 10 tagFilgers', () => {
dataSource.saveCardInteractions({
clicks: [],
tagFilters: Array.from({length: 12}).map((_, index) =>
index.toString()
),
pins: [],
});

expect(dataSource.getCardInteractions().tagFilters.length).toEqual(10);
});
});

describe('getCardInteractions', () => {
it('returns all default state when key is not set', () => {
expect(dataSource.getCardInteractions()).toEqual({
tagFilters: [],
pins: [],
clicks: [],
});
});

it('returns previously written value', () => {
dataSource.saveCardInteractions({
tagFilters: ['foo'],
clicks: [
{cardId: '1', runId: null, tag: 'foo', plugin: PluginType.SCALARS},
],
pins: [
{cardId: '2', runId: null, tag: 'bar', plugin: PluginType.SCALARS},
],
});

expect(dataSource.getCardInteractions()).toEqual({
tagFilters: ['foo'],
clicks: [
{cardId: '1', runId: null, tag: 'foo', plugin: PluginType.SCALARS},
],
pins: [
{cardId: '2', runId: null, tag: 'bar', plugin: PluginType.SCALARS},
],
});
});
});
});
16 changes: 14 additions & 2 deletions tensorboard/webapp/metrics/effects/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ package(default_visibility = ["//tensorboard:internal"])

tf_ng_module(
name = "effects",
srcs = ["index.ts"],
srcs = [
"card_interaction_effects.ts",
"index.ts",
],
deps = [
"//tensorboard/webapp:app_state",
"//tensorboard/webapp:selectors",
"//tensorboard/webapp/app_routing:types",
"//tensorboard/webapp/app_routing/actions",
"//tensorboard/webapp/app_routing/store",
"//tensorboard/webapp/core/actions",
"//tensorboard/webapp/core/store",
"//tensorboard/webapp/metrics:types",
"//tensorboard/webapp/metrics/actions",
"//tensorboard/webapp/metrics/data_source",
"//tensorboard/webapp/metrics/data_source:card_interactions_data_source",
"//tensorboard/webapp/metrics/store",
"//tensorboard/webapp/types",
"@npm//@angular/core",
Expand All @@ -27,7 +32,10 @@ tf_ng_module(
tf_ts_library(
name = "effects_test",
testonly = True,
srcs = ["metrics_effects_test.ts"],
srcs = [
"card_interactions_effects_test.ts",
"metrics_effects_test.ts",
],
deps = [
":effects",
"//tensorboard/webapp:app_state",
Expand All @@ -37,14 +45,18 @@ tf_ts_library(
"//tensorboard/webapp/app_routing:testing",
"//tensorboard/webapp/app_routing:types",
"//tensorboard/webapp/app_routing/actions",
"//tensorboard/webapp/app_routing/store",
"//tensorboard/webapp/core/actions",
"//tensorboard/webapp/core/store",
"//tensorboard/webapp/core/testing",
"//tensorboard/webapp/metrics:internal_types",
"//tensorboard/webapp/metrics:test_lib",
"//tensorboard/webapp/metrics:types",
"//tensorboard/webapp/metrics/actions",
"//tensorboard/webapp/metrics/data_source",
"//tensorboard/webapp/metrics/data_source:card_interactions_data_source",
"//tensorboard/webapp/metrics/store",
"//tensorboard/webapp/testing:utils",
"//tensorboard/webapp/types",
"//tensorboard/webapp/util:dom",
"//tensorboard/webapp/webapp_data_source:http_client_testing",
Expand Down
Loading

0 comments on commit 17a28b7

Please sign in to comment.