Skip to content
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

Update cardToPinnedCopyCache when resolving unresolved pins. #5837

Merged
merged 2 commits into from
Aug 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions tensorboard/webapp/metrics/store/metrics_reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ const reducer = createReducer(
state.cardList,
state.cardMetadataMap,
state.cardToPinnedCopy,
state.cardToPinnedCopyCache,
state.pinnedCardToOriginal,
state.cardStepIndex
);
Expand All @@ -396,7 +397,6 @@ const reducer = createReducer(
const newState = {
...state,
...resolvedResult,
cardToPinnedCopyCache: resolvedResult.cardToPinnedCopy,
settingOverrides: newSettings,
};

Expand Down Expand Up @@ -567,6 +567,7 @@ const reducer = createReducer(
nextCardList,
nextCardMetadataMap,
nextCardToPinnedCopy,
state.cardToPinnedCopyCache,
nextPinnedCardToOriginal,
nextCardStepIndex
);
Expand Down Expand Up @@ -922,12 +923,13 @@ const reducer = createReducer(
const resolvedResult = buildOrReturnStateWithPinnedCopy(
cardId,
nextCardToPinnedCopy,
nextCardToPinnedCopyCache,
nextPinnedCardToOriginal,
nextCardStepIndexMap,
nextCardMetadataMap
);
nextCardToPinnedCopy = resolvedResult.cardToPinnedCopy;
nextCardToPinnedCopyCache = resolvedResult.cardToPinnedCopy;
nextCardToPinnedCopyCache = resolvedResult.cardToPinnedCopyCache;
nextPinnedCardToOriginal = resolvedResult.pinnedCardToOriginal;
nextCardMetadataMap = resolvedResult.cardMetadataMap;
nextCardStepIndexMap = resolvedResult.cardStepIndex;
Expand Down
5 changes: 5 additions & 0 deletions tensorboard/webapp/metrics/store/metrics_reducers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,7 @@ describe('metrics reducers', () => {
cardList,
cardStepIndex,
cardToPinnedCopy,
cardToPinnedCopyCache,
pinnedCardToOriginal,
unresolvedImportedPinnedCards,
} = nextState;
Expand All @@ -723,6 +724,7 @@ describe('metrics reducers', () => {
cardList,
cardStepIndex,
cardToPinnedCopy,
cardToPinnedCopyCache,
pinnedCardToOriginal,
unresolvedImportedPinnedCards,
}).toEqual({
Expand All @@ -738,6 +740,9 @@ describe('metrics reducers', () => {
}),
},
cardToPinnedCopy: new Map([[expectedCardId, expectedPinnedCopyId]]),
cardToPinnedCopyCache: new Map([
[expectedCardId, expectedPinnedCopyId],
]),
pinnedCardToOriginal: new Map([[expectedPinnedCopyId, expectedCardId]]),
unresolvedImportedPinnedCards: [
{plugin: PluginType.SCALARS, tag: 'tagB'},
Expand Down
10 changes: 10 additions & 0 deletions tensorboard/webapp/metrics/store/metrics_store_internal_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type ResolvedPinPartialState = Pick<
MetricsState,
| 'cardMetadataMap'
| 'cardToPinnedCopy'
| 'cardToPinnedCopyCache'
| 'pinnedCardToOriginal'
| 'cardStepIndex'
>;
Expand Down Expand Up @@ -203,6 +204,7 @@ export function buildOrReturnStateWithUnresolvedImportedPins(
nonPinnedCards: NonPinnedCardId[],
cardMetadataMap: CardMetadataMap,
cardToPinnedCopy: CardToPinnedCard,
cardToPinnedCopyCache: CardToPinnedCard,
pinnedCardToOriginal: PinnedCardToCard,
cardStepIndexMap: CardStepIndexMap
): ResolvedPinPartialState & {unresolvedImportedPinnedCards: CardUniqueInfo[]} {
Expand All @@ -224,13 +226,15 @@ export function buildOrReturnStateWithUnresolvedImportedPins(
unresolvedImportedPinnedCards,
cardMetadataMap,
cardToPinnedCopy,
cardToPinnedCopyCache,
pinnedCardToOriginal,
cardStepIndex: cardStepIndexMap,
};
}

let stateWithResolvedPins = {
cardToPinnedCopy,
cardToPinnedCopyCache,
pinnedCardToOriginal,
cardStepIndex: cardStepIndexMap,
cardMetadataMap,
Expand All @@ -239,6 +243,7 @@ export function buildOrReturnStateWithUnresolvedImportedPins(
stateWithResolvedPins = buildOrReturnStateWithPinnedCopy(
cardToPin,
stateWithResolvedPins.cardToPinnedCopy,
stateWithResolvedPins.cardToPinnedCopyCache,
stateWithResolvedPins.pinnedCardToOriginal,
stateWithResolvedPins.cardStepIndex,
stateWithResolvedPins.cardMetadataMap
Expand All @@ -258,6 +263,7 @@ export function buildOrReturnStateWithUnresolvedImportedPins(
export function buildOrReturnStateWithPinnedCopy(
cardId: NonPinnedCardId,
cardToPinnedCopy: CardToPinnedCard,
cardToPinnedCopyCache: CardToPinnedCard,
pinnedCardToOriginal: PinnedCardToCard,
cardStepIndexMap: CardStepIndexMap,
cardMetadataMap: CardMetadataMap
Expand All @@ -266,20 +272,23 @@ export function buildOrReturnStateWithPinnedCopy(
if (cardToPinnedCopy.has(cardId)) {
return {
cardToPinnedCopy,
cardToPinnedCopyCache,
pinnedCardToOriginal,
cardStepIndex: cardStepIndexMap,
cardMetadataMap,
};
}

const nextCardToPinnedCopy = new Map(cardToPinnedCopy);
const nextCardToPinnedCopyCache = new Map(cardToPinnedCopyCache);
const nextPinnedCardToOriginal = new Map(pinnedCardToOriginal);
const nextCardStepIndexMap = {...cardStepIndexMap};
const nextCardMetadataMap = {...cardMetadataMap};

// Create a pinned copy. Copies step index from the original card.
const pinnedCardId = getPinnedCardId(cardId);
nextCardToPinnedCopy.set(cardId, pinnedCardId);
nextCardToPinnedCopyCache.set(cardId, pinnedCardId);
nextPinnedCardToOriginal.set(pinnedCardId, cardId);
if (cardStepIndexMap.hasOwnProperty(cardId)) {
nextCardStepIndexMap[pinnedCardId] = cardStepIndexMap[cardId];
Expand All @@ -293,6 +302,7 @@ export function buildOrReturnStateWithPinnedCopy(

return {
cardToPinnedCopy: nextCardToPinnedCopy,
cardToPinnedCopyCache: nextCardToPinnedCopyCache,
pinnedCardToOriginal: nextPinnedCardToOriginal,
cardStepIndex: nextCardStepIndexMap,
cardMetadataMap: nextCardMetadataMap,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ describe('metrics store utils', () => {
{card1: {plugin: PluginType.SCALARS, tag: 'accuracy', runId: null}},
new Map(),
new Map(),
new Map(),
{card1: buildStepIndexMetadata({index: 2})}
);

Expand All @@ -381,6 +382,9 @@ describe('metrics store utils', () => {
expect(result.cardToPinnedCopy).toEqual(
new Map([['card1', pinnedCardId]])
);
expect(result.cardToPinnedCopyCache).toEqual(
new Map([['card1', pinnedCardId]])
);
expect(result.pinnedCardToOriginal).toEqual(
new Map([[pinnedCardId, 'card1']])
);
Expand All @@ -391,19 +395,22 @@ describe('metrics store utils', () => {
it('adds a pinned copy properly', () => {
const {
cardToPinnedCopy,
cardToPinnedCopyCache,
pinnedCardToOriginal,
cardStepIndex,
cardMetadataMap,
} = buildOrReturnStateWithPinnedCopy(
'card1',
new Map(),
new Map(),
new Map(),
{card1: buildStepIndexMetadata({index: 2})},
{card1: createCardMetadata()}
);
const pinnedCardId = getPinnedCardId('card1');

expect(cardToPinnedCopy).toEqual(new Map([['card1', pinnedCardId]]));
expect(cardToPinnedCopyCache).toEqual(new Map([['card1', pinnedCardId]]));
expect(pinnedCardToOriginal).toEqual(new Map([[pinnedCardId, 'card1']]));
expect(cardStepIndex).toEqual({
card1: buildStepIndexMetadata({index: 2}),
Expand All @@ -417,17 +424,26 @@ describe('metrics store utils', () => {

it('throws if the original card does not have metadata', () => {
expect(() => {
buildOrReturnStateWithPinnedCopy('card1', new Map(), new Map(), {}, {});
buildOrReturnStateWithPinnedCopy(
'card1',
new Map(),
new Map(),
new Map(),
{},
{}
);
}).toThrow();
});

it('no-ops if the card already has a pinned copy', () => {
const cardToPinnedCopy = new Map([['card1', 'card-pin1']]);
const cardToPinnedCopyCache = new Map([['card1', 'card-pin1']]);
const pinnedCardToOriginal = new Map([['card-pin1', 'card1']]);
const cardStepIndexMap = {};
const cardMetadataMap = {card1: createCardMetadata()};
const originals = {
cardToPinnedCopy: new Map(cardToPinnedCopy),
cardToPinnedCopyCache: new Map(cardToPinnedCopyCache),
pinnedCardToOriginal: new Map(pinnedCardToOriginal),
cardStepIndexMap: {...cardStepIndexMap},
cardMetadataMap: {...cardMetadataMap},
Expand All @@ -436,12 +452,16 @@ describe('metrics store utils', () => {
const result = buildOrReturnStateWithPinnedCopy(
'card1',
cardToPinnedCopy,
cardToPinnedCopyCache,
pinnedCardToOriginal,
cardStepIndexMap,
cardMetadataMap
);

expect(result.cardToPinnedCopy).toEqual(originals.cardToPinnedCopy);
expect(result.cardToPinnedCopyCache).toEqual(
originals.cardToPinnedCopyCache
);
expect(result.pinnedCardToOriginal).toEqual(
originals.pinnedCardToOriginal
);
Expand Down