Skip to content

[Global pins] Add clear all pins button #6828

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

Merged
merged 7 commits into from
Apr 16, 2024
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
4 changes: 4 additions & 0 deletions tensorboard/webapp/metrics/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,5 +277,9 @@ export const metricsUnresolvedPinnedCardsFromLocalStorageAdded = createAction(
props<{cards: CardUniqueInfo[]}>()
);

export const metricsClearAllPinnedCards = createAction(
'[Metrics] Clear all pinned cards'
);

// TODO(jieweiwu): Delete after internal code is updated.
export const stepSelectorTimeSelectionChanged = timeSelectionChanged;
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,8 @@ export class SavedPinsDataSource {
}
return [];
}

removeAllScalarPins(): void {
window.localStorage.setItem(SAVED_SCALAR_PINS_KEY, JSON.stringify([]));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,15 @@ describe('SavedPinsDataSource Test', () => {
expect(dataSource.getSavedScalarPins()).toEqual(['tag1']);
});
});

describe('removeAllScalarPins', () => {
it('removes all existing pins', () => {
dataSource.saveScalarPin('tag3');
dataSource.saveScalarPin('tag4');

dataSource.removeAllScalarPins();

expect(dataSource.getSavedScalarPins().length).toEqual(0);
});
});
});
21 changes: 20 additions & 1 deletion tensorboard/webapp/metrics/effects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,21 @@ export class MetricsEffects implements OnInitEffects {
})
);

private readonly removeAllPins$ = this.actions$.pipe(
ofType(actions.metricsClearAllPinnedCards),
withLatestFrom(
this.store.select(selectors.getEnableGlobalPins),
this.store.select(selectors.getShouldPersistSettings)
),
filter(
([, enableGlobalPins, shouldPersistSettings]) =>
enableGlobalPins && shouldPersistSettings
),
tap(() => {
this.savedPinsDataSource.removeAllScalarPins();
})
);

/**
* In general, this effect dispatch the following actions:
*
Expand Down Expand Up @@ -356,7 +371,11 @@ export class MetricsEffects implements OnInitEffects {
/**
* Subscribes to: dashboard shown (initAction).
*/
this.loadSavedPins$
this.loadSavedPins$,
/**
* Subscribes to: metricsClearAllPinnedCards.
*/
this.removeAllPins$
);
},
{dispatch: false}
Expand Down
37 changes: 37 additions & 0 deletions tensorboard/webapp/metrics/effects/metrics_effects_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -985,5 +985,42 @@ describe('metrics effects', () => {
expect(actualActions).toEqual([]);
});
});

describe('removeAllPins', () => {
let removeAllScalarPinsSpy: jasmine.Spy;

beforeEach(() => {
removeAllScalarPinsSpy = spyOn(
savedPinsDataSource,
'removeAllScalarPins'
);
store.overrideSelector(selectors.getEnableGlobalPins, true);
store.refreshState();
});

it('removes all pins by calling removeAllScalarPins method', () => {
actions$.next(actions.metricsClearAllPinnedCards());

expect(removeAllScalarPinsSpy).toHaveBeenCalled();
});

it('does not remove pins if getEnableGlobalPins is false', () => {
store.overrideSelector(selectors.getEnableGlobalPins, false);
store.refreshState();

actions$.next(actions.metricsClearAllPinnedCards());

expect(removeAllScalarPinsSpy).not.toHaveBeenCalled();
});

it('does not remove pins if getShouldPersistSettings is false', () => {
store.overrideSelector(selectors.getShouldPersistSettings, false);
store.refreshState();

actions$.next(actions.metricsClearAllPinnedCards());

expect(removeAllScalarPinsSpy).not.toHaveBeenCalled();
});
});
});
});
25 changes: 24 additions & 1 deletion tensorboard/webapp/metrics/store/metrics_reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ import {
TagMetadata,
TimeSeriesData,
TimeSeriesLoadable,
CardToPinnedCard,
PinnedCardToCard,
} from './metrics_types';
import {dataTableUtils} from '../../widgets/data_table/utils';

Expand Down Expand Up @@ -1525,7 +1527,28 @@ const reducer = createReducer(
],
};
}
)
),
on(actions.metricsClearAllPinnedCards, (state) => {
const nextCardMetadataMap = {...state.cardMetadataMap};
const nextCardStepIndex = {...state.cardStepIndex};
const nextCardStateMap = {...state.cardStateMap};

for (const cardId of state.pinnedCardToOriginal.keys()) {
delete nextCardMetadataMap[cardId];
delete nextCardStepIndex[cardId];
delete nextCardStateMap[cardId];
}

return {
...state,
cardMetadataMap: nextCardMetadataMap,
cardStateMap: nextCardStateMap,
cardStepIndex: nextCardStepIndex,
cardToPinnedCopy: new Map() as CardToPinnedCard,
cardToPinnedCopyCache: new Map() as CardToPinnedCard,
pinnedCardToOriginal: new Map() as PinnedCardToCard,
};
})
);

export function reducers(state: MetricsState | undefined, action: Action) {
Expand Down
52 changes: 52 additions & 0 deletions tensorboard/webapp/metrics/store/metrics_reducers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4488,5 +4488,57 @@ describe('metrics reducers', () => {
expect(state2.unresolvedImportedPinnedCards).toEqual([fakePinnedCard]);
});
});

describe('#metricsClearAllPinnedCards', () => {
it('unpins all pinned cards', () => {
const beforeState = buildMetricsState({
cardMetadataMap: {
card1: createScalarCardMetadata(),
pinnedCopy1: createScalarCardMetadata(),
card2: createScalarCardMetadata(),
pinnedCopy2: createScalarCardMetadata(),
},
cardList: ['card1', 'card2'],
cardStepIndex: {
card1: buildStepIndexMetadata({index: 10}),
pinnedCopy1: buildStepIndexMetadata({index: 20}),
card2: buildStepIndexMetadata({index: 11}),
pinnedCopy2: buildStepIndexMetadata({index: 21}),
},
cardToPinnedCopy: new Map([
['card1', 'pinnedCopy1'],
['card2', 'pinnedCopy2'],
]),
cardToPinnedCopyCache: new Map([
['card1', 'pinnedCopy1'],
['card2', 'pinnedCopy2'],
]),
pinnedCardToOriginal: new Map([
['pinnedCopy1', 'card1'],
['pinnedCopy2', 'card2'],
]),
});
const nextState = reducers(
beforeState,
actions.metricsClearAllPinnedCards()
);

const expectedState = buildMetricsState({
cardMetadataMap: {
card1: createScalarCardMetadata(),
card2: createScalarCardMetadata(),
},
cardList: ['card1', 'card2'],
cardStepIndex: {
card1: buildStepIndexMetadata({index: 10}),
card2: buildStepIndexMetadata({index: 11}),
},
cardToPinnedCopy: new Map(),
cardToPinnedCopyCache: new Map(),
pinnedCardToOriginal: new Map(),
});
expect(nextState).toEqual(expectedState);
});
});
});
});
2 changes: 2 additions & 0 deletions tensorboard/webapp/metrics/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,8 @@ export class TestingSavedPinsDataSource {
getSavedScalarPins() {
return [];
}

removeAllScalarPins() {}
}

export function provideTestingSavedPinsDataSource() {
Expand Down
68 changes: 67 additions & 1 deletion tensorboard/webapp/metrics/views/main_view/main_view_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ import {MainViewComponent, SHARE_BUTTON_COMPONENT} from './main_view_component';
import {MainViewContainer} from './main_view_container';
import {PinnedViewComponent} from './pinned_view_component';
import {PinnedViewContainer} from './pinned_view_container';
import {buildMockState} from '../../../testing/utils';

@Component({
selector: 'card-view',
Expand Down Expand Up @@ -182,7 +183,11 @@ describe('metrics main view', () => {
],
providers: [
provideMockStore({
initialState: appStateFromMetricsState(buildMetricsState()),
initialState: {
...buildMockState({
...appStateFromMetricsState(buildMetricsState()),
}),
},
}),
],
// Skip errors for card renderers, which are tested separately.
Expand Down Expand Up @@ -1606,6 +1611,67 @@ describe('metrics main view', () => {
expect(indicator).toBeTruthy();
});
});

describe('clear all pins button', () => {
beforeEach(() => {
store.overrideSelector(selectors.getEnableGlobalPins, true);
});

it('does not show the button if getEnableGlobalPins is false', () => {
store.overrideSelector(selectors.getEnableGlobalPins, false);
store.overrideSelector(selectors.getPinnedCardsWithMetadata, []);
const fixture = TestBed.createComponent(MainViewContainer);
fixture.detectChanges();

const clearAllButton = fixture.debugElement.query(
By.css('[aria-label="Clear all pinned cards"]')
);
expect(clearAllButton).toBeNull();
});

it('does not show the button if there is no pinned card', () => {
store.overrideSelector(selectors.getPinnedCardsWithMetadata, []);
const fixture = TestBed.createComponent(MainViewContainer);
fixture.detectChanges();

const clearAllButton = fixture.debugElement.query(
By.css('[aria-label="Clear all pinned cards"]')
);
expect(clearAllButton).toBeNull();
});

it('shows the button if there is a pinned card', () => {
store.overrideSelector(selectors.getPinnedCardsWithMetadata, [
{cardId: 'card1', ...createCardMetadata(PluginType.SCALARS)},
{cardId: 'card2', ...createCardMetadata(PluginType.IMAGES)},
]);
const fixture = TestBed.createComponent(MainViewContainer);
fixture.detectChanges();

const clearAllButton = fixture.debugElement.query(
By.css('[aria-label="Clear all pinned cards"]')
);
expect(clearAllButton).toBeTruthy();
});

it('dispatch clear all action when the button is clicked', () => {
store.overrideSelector(selectors.getPinnedCardsWithMetadata, [
{cardId: 'card1', ...createCardMetadata(PluginType.SCALARS)},
{cardId: 'card2', ...createCardMetadata(PluginType.IMAGES)},
]);
const fixture = TestBed.createComponent(MainViewContainer);
fixture.detectChanges();

const clearAllButton = fixture.debugElement.query(
By.css('[aria-label="Clear all pinned cards"]')
);
clearAllButton.nativeElement.click();

expect(dispatchedActions).toEqual([
actions.metricsClearAllPinnedCards(),
]);
});
});
});

describe('slideout menu', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,25 @@ mat-icon {
margin-right: 5px;
}

.group-toolbar {
justify-content: space-between;
}

.left-items {
display: flex;
align-items: center;
}

.right-items {
button {
$_height: 25px;
font-size: 12px;
font-weight: normal;
height: $_height;
line-height: $_height;
}
}

.group-text {
display: flex;
align-items: baseline;
Expand Down
Loading
Loading