Skip to content

Commit aae9be4

Browse files
committed
add loadSavedPins in metrics effect
1 parent 3e5b052 commit aae9be4

File tree

2 files changed

+91
-21
lines changed

2 files changed

+91
-21
lines changed

tensorboard/webapp/metrics/effects/index.ts

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -269,27 +269,40 @@ export class MetricsEffects implements OnInitEffects {
269269
this.getVisibleCardFetchInfos(),
270270
this.store.select(selectors.getEnableGlobalPins)
271271
),
272-
map(
273-
([
274-
{cardId, canCreateNewPins, wasPinned},
275-
fetchInfos,
276-
enableGlobalPins,
277-
]) => {
278-
if (!enableGlobalPins) {
279-
return;
280-
}
281-
const card = fetchInfos.find((value) => value.id === cardId);
282-
// Saving only scalar pinned cards.
283-
if (!card || card.plugin !== PluginType.SCALARS) {
284-
return;
285-
}
286-
if (wasPinned) {
287-
this.savedPinsDataSource.removeScalarPin(card.tag);
288-
} else if (canCreateNewPins) {
289-
this.savedPinsDataSource.saveScalarPin(card.tag);
290-
}
272+
filter(([, , enableGlobalPins]) => enableGlobalPins),
273+
map(([{cardId, canCreateNewPins, wasPinned}, fetchInfos]) => {
274+
const card = fetchInfos.find((value) => value.id === cardId);
275+
// Saving only scalar pinned cards.
276+
if (!card || card.plugin !== PluginType.SCALARS) {
277+
return;
278+
}
279+
if (wasPinned) {
280+
this.savedPinsDataSource.removeScalarPin(card.tag);
281+
} else if (canCreateNewPins) {
282+
this.savedPinsDataSource.saveScalarPin(card.tag);
291283
}
292-
)
284+
})
285+
);
286+
287+
private readonly loadSavedPins$ = this.actions$.pipe(
288+
ofType(initAction, coreActions.pluginsListingLoaded),
289+
withLatestFrom(this.store.select(selectors.getEnableGlobalPins)),
290+
filter(([, enableGlobalPins]) => enableGlobalPins),
291+
map(() => {
292+
const tags = this.savedPinsDataSource.getSavedScalarPins();
293+
if (!tags || tags.length === 0) {
294+
return;
295+
}
296+
const unresolvedPinnedCards = tags.map((tag) => ({
297+
plugin: PluginType.SCALARS,
298+
tag: tag,
299+
}));
300+
this.store.dispatch(
301+
actions.metricsUnresolvedPinnedCardsAdded({
302+
cards: unresolvedPinnedCards,
303+
})
304+
);
305+
})
293306
);
294307

295308
/**
@@ -328,7 +341,11 @@ export class MetricsEffects implements OnInitEffects {
328341
/**
329342
* Subscribes to: cardPinStateToggled.
330343
*/
331-
this.addOrRemovePin$
344+
this.addOrRemovePin$,
345+
/**
346+
* Subscribes to: dashboard shown.
347+
*/
348+
this.loadSavedPins$
332349
);
333350
},
334351
{dispatch: false}

tensorboard/webapp/metrics/effects/metrics_effects_test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,5 +903,58 @@ describe('metrics effects', () => {
903903
expect(removeScalarPinSpy).not.toHaveBeenCalled();
904904
});
905905
});
906+
907+
describe('loadSavedPins', () => {
908+
const fakeTagList = ['tagA', 'tagB'];
909+
const fakeUniqueCardInfos = fakeTagList.map((tag) => ({
910+
plugin: PluginType.SCALARS,
911+
tag: tag,
912+
}));
913+
let getSavedScalarPinsSpy: jasmine.Spy;
914+
915+
beforeEach(() => {
916+
store.overrideSelector(selectors.getEnableGlobalPins, true);
917+
store.refreshState();
918+
});
919+
920+
it('dispatches unresolvedPinnedCards action if tag is given', () => {
921+
getSavedScalarPinsSpy = spyOn(
922+
savedPinsDataSource,
923+
'getSavedScalarPins'
924+
).and.returnValue(['tagA', 'tagB']);
925+
926+
actions$.next(TEST_ONLY.initAction());
927+
928+
expect(actualActions).toEqual([
929+
actions.metricsUnresolvedPinnedCardsAdded({
930+
cards: fakeUniqueCardInfos,
931+
}),
932+
]);
933+
});
934+
935+
it('does not dispatch unresolvedPinnedCards action if tag is an empty list', () => {
936+
getSavedScalarPinsSpy = spyOn(
937+
savedPinsDataSource,
938+
'getSavedScalarPins'
939+
).and.returnValue([]);
940+
941+
actions$.next(TEST_ONLY.initAction());
942+
943+
expect(actualActions).toEqual([]);
944+
});
945+
946+
it('does not load saved pins if getEnableGlobalPins is false', () => {
947+
getSavedScalarPinsSpy = spyOn(
948+
savedPinsDataSource,
949+
'getSavedScalarPins'
950+
).and.returnValue(['tagA', 'tagB']);
951+
store.overrideSelector(selectors.getEnableGlobalPins, false);
952+
store.refreshState();
953+
954+
actions$.next(TEST_ONLY.initAction());
955+
956+
expect(actualActions).toEqual([]);
957+
});
958+
});
906959
});
907960
});

0 commit comments

Comments
 (0)