Skip to content

Commit 81b1ab0

Browse files
authored
Suggested Cards: Create data source for persisting card interactions to localStorage (#6439)
## Motivation for features / changes We are working on a feature to suggest cards to users at the top of the timeseries dashboard. Suggestions are generated based on previous interactions with TensorBoard. In order to determine what those previous interactions were we are storing them to localStorage. See #6437 for more information. Googlers see [go/tb-suggested-cards](go/tb-suggested-cards)
1 parent 4f6f6a2 commit 81b1ab0

7 files changed

+258
-0
lines changed

tensorboard/webapp/metrics/data_source/BUILD

+26
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,18 @@ tf_ng_module(
2525
],
2626
)
2727

28+
tf_ng_module(
29+
name = "card_interactions_data_source",
30+
srcs = [
31+
"card_interactions_data_source.ts",
32+
"card_interactions_data_source_module.ts",
33+
],
34+
deps = [
35+
"//tensorboard/webapp/metrics/store:types",
36+
"@npm//@angular/core",
37+
],
38+
)
39+
2840
tf_ts_library(
2941
name = "types",
3042
srcs = [
@@ -70,3 +82,17 @@ tf_ts_library(
7082
"@npm//rxjs",
7183
],
7284
)
85+
86+
tf_ts_library(
87+
name = "card_interactions_data_source_test",
88+
testonly = True,
89+
srcs = [
90+
"card_interactions_data_source_test.ts",
91+
],
92+
deps = [
93+
":card_interactions_data_source",
94+
"//tensorboard/webapp/angular:expect_angular_core_testing",
95+
"//tensorboard/webapp/metrics:internal_types",
96+
"@npm//@types/jasmine",
97+
],
98+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
import {Injectable} from '@angular/core';
16+
import {CardInteractions} from '../store/metrics_types';
17+
18+
const CARD_INTERACTIONS_KEY = 'tb-card-interactions';
19+
20+
const MAX_RECORDS: Record<keyof CardInteractions, number> = {
21+
pins: 10,
22+
clicks: 10,
23+
tagFilters: 10,
24+
};
25+
26+
@Injectable()
27+
export class CardInteractionsDataSource {
28+
saveCardInteractions(cardInteractions: CardInteractions) {
29+
const trimmedInteractions: CardInteractions = {
30+
pins: cardInteractions.pins.slice(
31+
cardInteractions.pins.length - MAX_RECORDS.pins
32+
),
33+
clicks: cardInteractions.clicks.slice(
34+
cardInteractions.clicks.length - MAX_RECORDS.clicks
35+
),
36+
tagFilters: cardInteractions.tagFilters.slice(
37+
cardInteractions.tagFilters.length - MAX_RECORDS.tagFilters
38+
),
39+
};
40+
localStorage.setItem(
41+
CARD_INTERACTIONS_KEY,
42+
JSON.stringify(trimmedInteractions)
43+
);
44+
}
45+
46+
getCardInteractions(): CardInteractions {
47+
const existingInteractions = localStorage.getItem(CARD_INTERACTIONS_KEY);
48+
if (existingInteractions) {
49+
return JSON.parse(existingInteractions) as CardInteractions;
50+
}
51+
return {
52+
tagFilters: [],
53+
pins: [],
54+
clicks: [],
55+
};
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
import {NgModule} from '@angular/core';
16+
import {CardInteractionsDataSource} from './card_interactions_data_source';
17+
18+
@NgModule({
19+
providers: [CardInteractionsDataSource],
20+
})
21+
export class MetricsCardInteractionsDataSourceModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
==============================================================================*/
15+
import {TestBed} from '@angular/core/testing';
16+
import {CardInteractionsDataSource} from './card_interactions_data_source';
17+
import {PluginType} from '../internal_types';
18+
19+
describe('CardInteractionsDataSource Test', () => {
20+
let mockStorage: Record<string, string>;
21+
let dataSource: CardInteractionsDataSource;
22+
23+
beforeEach(async () => {
24+
await TestBed.configureTestingModule({
25+
providers: [CardInteractionsDataSource],
26+
});
27+
28+
dataSource = TestBed.inject(CardInteractionsDataSource);
29+
30+
mockStorage = {};
31+
spyOn(window.localStorage, 'setItem').and.callFake(
32+
(key: string, value: string) => {
33+
if (key !== 'tb-card-interactions') {
34+
throw new Error('incorrect key used');
35+
}
36+
37+
mockStorage[key] = value;
38+
}
39+
);
40+
41+
spyOn(window.localStorage, 'getItem').and.callFake((key: string) => {
42+
if (key !== 'tb-card-interactions') {
43+
throw new Error('incorrect key used');
44+
}
45+
46+
return mockStorage[key];
47+
});
48+
});
49+
50+
describe('saveCardInteractions', () => {
51+
it('only saves 10 pins', () => {
52+
dataSource.saveCardInteractions({
53+
clicks: [],
54+
tagFilters: [],
55+
pins: Array.from({length: 12}).map((_, index) => ({
56+
cardId: `card-${index}`,
57+
runId: null,
58+
tag: 'foo',
59+
plugin: PluginType.SCALARS,
60+
})),
61+
});
62+
63+
expect(dataSource.getCardInteractions().pins.length).toEqual(10);
64+
});
65+
66+
it('only saves 10 clicks', () => {
67+
dataSource.saveCardInteractions({
68+
pins: [],
69+
tagFilters: [],
70+
clicks: Array.from({length: 12}).map((_, index) => ({
71+
cardId: `card-${index}`,
72+
runId: null,
73+
tag: 'foo',
74+
plugin: PluginType.SCALARS,
75+
})),
76+
});
77+
78+
expect(dataSource.getCardInteractions().clicks.length).toEqual(10);
79+
});
80+
81+
it('only saves 10 tagFilgers', () => {
82+
dataSource.saveCardInteractions({
83+
clicks: [],
84+
tagFilters: Array.from({length: 12}).map((_, index) =>
85+
index.toString()
86+
),
87+
pins: [],
88+
});
89+
90+
expect(dataSource.getCardInteractions().tagFilters.length).toEqual(10);
91+
});
92+
});
93+
94+
describe('getCardInteractions', () => {
95+
it('returns all default state when key is not set', () => {
96+
expect(dataSource.getCardInteractions()).toEqual({
97+
tagFilters: [],
98+
pins: [],
99+
clicks: [],
100+
});
101+
});
102+
103+
it('returns previously written value', () => {
104+
dataSource.saveCardInteractions({
105+
tagFilters: ['foo'],
106+
clicks: [
107+
{cardId: '1', runId: null, tag: 'foo', plugin: PluginType.SCALARS},
108+
],
109+
pins: [
110+
{cardId: '2', runId: null, tag: 'bar', plugin: PluginType.SCALARS},
111+
],
112+
});
113+
114+
expect(dataSource.getCardInteractions()).toEqual({
115+
tagFilters: ['foo'],
116+
clicks: [
117+
{cardId: '1', runId: null, tag: 'foo', plugin: PluginType.SCALARS},
118+
],
119+
pins: [
120+
{cardId: '2', runId: null, tag: 'bar', plugin: PluginType.SCALARS},
121+
],
122+
});
123+
});
124+
});
125+
});

tensorboard/webapp/metrics/store/metrics_reducers.ts

+10
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,16 @@ const {initialState, reducers: namespaceContextedReducer} =
453453
settings: METRICS_SETTINGS_DEFAULT,
454454
settingOverrides: {},
455455
visibleCardMap: new Map<ElementId, CardId>(),
456+
previousCardInteractions: {
457+
tagFilters: [],
458+
pins: [],
459+
clicks: [],
460+
},
461+
newCardInteractions: {
462+
tagFilters: [],
463+
pins: [],
464+
clicks: [],
465+
},
456466
},
457467

458468
/** onNavigated */

tensorboard/webapp/metrics/store/metrics_types.ts

+9
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import {
2727
} from '../data_source';
2828
import {
2929
CardId,
30+
CardIdWithMetadata,
3031
CardMetadata,
3132
CardUniqueInfo,
3233
HistogramMode,
@@ -166,6 +167,12 @@ export type CardStepIndexMap = Record<
166167
CardStepIndexMetaData | null
167168
>;
168169

170+
export type CardInteractions = {
171+
tagFilters: string[];
172+
pins: CardIdWithMetadata[];
173+
clicks: CardIdWithMetadata[];
174+
};
175+
169176
export type CardToPinnedCard = Map<NonPinnedCardId, PinnedCardId>;
170177

171178
export type PinnedCardToCard = Map<PinnedCardId, NonPinnedCardId>;
@@ -254,6 +261,8 @@ export interface MetricsNonNamespacedState {
254261
* Map from ElementId to CardId. Only contains all visible cards.
255262
*/
256263
visibleCardMap: Map<ElementId, CardId>;
264+
previousCardInteractions: CardInteractions;
265+
newCardInteractions: CardInteractions;
257266
}
258267

259268
export type MetricsState = NamespaceContextedState<

tensorboard/webapp/metrics/testing.ts

+10
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,16 @@ function buildBlankState(): MetricsState {
112112
isSettingsPaneOpen: false,
113113
isSlideoutMenuOpen: false,
114114
tableEditorSelectedTab: DataTableMode.SINGLE,
115+
previousCardInteractions: {
116+
tagFilters: [],
117+
pins: [],
118+
clicks: [],
119+
},
120+
newCardInteractions: {
121+
tagFilters: [],
122+
pins: [],
123+
clicks: [],
124+
},
115125
};
116126
}
117127

0 commit comments

Comments
 (0)