-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Suggested Cards: Create data source for persisting card interactions to localStorage #6439
Merged
rileyajones
merged 3 commits into
tensorflow:master
from
rileyajones:card-interactions-datasource
Sep 14, 2023
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
tensorboard/webapp/metrics/data_source/card_interactions_data_source.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* 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 {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: [], | ||
}; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
tensorboard/webapp/metrics/data_source/card_interactions_data_source_module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/* 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({ | ||
providers: [CardInteractionsDataSource], | ||
}) | ||
export class MetricsCardInteractionsDataSourceModule {} |
125 changes: 125 additions & 0 deletions
125
tensorboard/webapp/metrics/data_source/card_interactions_data_source_test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}, | ||
], | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is fine. However, I would prefer to add this state into the reducer when it is starting to be used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would need to define the state property as optional in order to make typescript happy and I would rather not do that. It's also inconsistent with the way we define other defaults.