-
Notifications
You must be signed in to change notification settings - Fork 919
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an unified storage class for data plugin (#7701)
* add an unified storage class Signed-off-by: abbyhu2000 <[email protected]> --------- Signed-off-by: abbyhu2000 <[email protected]>
- Loading branch information
1 parent
0a3bcd8
commit 9a84202
Showing
24 changed files
with
245 additions
and
85 deletions.
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
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,6 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
export { DataStorage, createStorage } from './storage'; |
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,68 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { transform, startsWith, keys } from 'lodash'; | ||
import { parse, stringify } from '@osd/std'; | ||
|
||
export enum StorageKeys { | ||
WIDTH = 'widths', | ||
} | ||
|
||
type IStorageEngine = typeof window.localStorage; | ||
export class DataStorage { | ||
constructor(private readonly engine: IStorageEngine, private readonly prefix: string) {} | ||
|
||
encode(val: any) { | ||
return stringify(val); | ||
} | ||
|
||
decode(val: any) { | ||
if (typeof val === 'string') { | ||
return parse(val); | ||
} | ||
} | ||
|
||
encodeKey(key: string) { | ||
return `${this.prefix}${key}`; | ||
} | ||
|
||
decodeKey(key: string) { | ||
if (startsWith(key, this.prefix)) { | ||
return `${key.slice(this.prefix.length)}`; | ||
} | ||
} | ||
|
||
set(key: string, val: any) { | ||
this.engine.setItem(this.encodeKey(key), this.encode(val)); | ||
return val; | ||
} | ||
|
||
has(key: string) { | ||
return this.engine.getItem(this.encodeKey(key)) != null; | ||
} | ||
|
||
get<T>(key: string, _default?: T) { | ||
if (this.has(key)) { | ||
return this.decode(this.engine.getItem(this.encodeKey(key))); | ||
} else { | ||
return _default; | ||
} | ||
} | ||
|
||
remove(key: string) { | ||
return this.engine.removeItem(this.encodeKey(key)); | ||
} | ||
|
||
keys(): string[] { | ||
return transform(keys(this.engine), (ours, key) => { | ||
const ourKey = this.decodeKey(key); | ||
if (ourKey != null) ours.push(ourKey); | ||
}); | ||
} | ||
} | ||
|
||
export function createStorage(deps: { engine: IStorageEngine; prefix: string }) { | ||
return new DataStorage(deps.engine, deps.prefix); | ||
} |
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
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
64 changes: 64 additions & 0 deletions
64
src/plugins/data/public/query/query_string/query_history.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,64 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { DataStorage, SimpleDataSet } from 'src/plugins/data/common'; | ||
import { BehaviorSubject } from 'rxjs'; | ||
import { Query, TimeRange } from '../..'; | ||
|
||
// Todo: Implement a more advanced QueryHistory class when needed for recent query history | ||
export class QueryHistory { | ||
constructor(private readonly storage: DataStorage) {} | ||
|
||
private changeEmitter = new BehaviorSubject<any[]>(this.getHistory() || []); | ||
|
||
getHistoryKeys() { | ||
return this.storage | ||
.keys() | ||
.filter((key: string) => key.indexOf('query_') === 0) | ||
.sort() | ||
.reverse(); | ||
} | ||
|
||
getHistory() { | ||
return this.getHistoryKeys().map((key) => this.storage.get(key)); | ||
} | ||
|
||
// This is used as an optimization mechanism so that different components | ||
// can listen for changes to history and update because changes to history can | ||
// be triggered from different places in the app. The alternative would be to store | ||
// this in state so that we hook into the React model, but it would require loading history | ||
// every time the application starts even if a user is not going to view history. | ||
change(listener: (reqs: any[]) => void) { | ||
const subscription = this.changeEmitter.subscribe(listener); | ||
return () => subscription.unsubscribe(); | ||
} | ||
|
||
addQueryToHistory(dataSet: SimpleDataSet, query: Query, dateRange?: TimeRange) { | ||
const keys = this.getHistoryKeys(); | ||
keys.splice(0, 500); // only maintain most recent X; | ||
keys.forEach((key) => { | ||
this.storage.remove(key); | ||
}); | ||
|
||
const timestamp = new Date().getTime(); | ||
const k = 'query_' + timestamp; | ||
this.storage.set(k, { | ||
dataSet, | ||
time: timestamp, | ||
query, | ||
dateRange, | ||
}); | ||
|
||
this.changeEmitter.next(this.getHistory()); | ||
} | ||
|
||
clearHistory() { | ||
this.getHistoryKeys().forEach((key) => this.storage.remove(key)); | ||
} | ||
} | ||
|
||
export function createHistory(deps: { storage: DataStorage }) { | ||
return new QueryHistory(deps.storage); | ||
} |
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
Oops, something went wrong.