-
Notifications
You must be signed in to change notification settings - Fork 923
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: abbyhu2000 <[email protected]>
- Loading branch information
1 parent
a360b38
commit f9fdd1b
Showing
20 changed files
with
242 additions
and
63 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,12 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Any modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
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,74 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Any modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
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
70 changes: 70 additions & 0 deletions
70
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,70 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* The OpenSearch Contributors require contributions made to | ||
* this file be licensed under the Apache-2.0 license or a | ||
* compatible open source license. | ||
* | ||
* Any modifications Copyright OpenSearch Contributors. See | ||
* GitHub history for details. | ||
*/ | ||
|
||
import { DataStorage } 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: string, 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.