Skip to content

Commit

Permalink
[2.x Manual Backport] Follow up on 8723 comment (opensearch-project#8733
Browse files Browse the repository at this point in the history
)

This is a backport PR for opensearch-project#8733

The original PR is a follow up fix for the comment:
opensearch-project#8723 (comment)

Signed-off-by: Anan Zhuang <[email protected]>
  • Loading branch information
ananzh committed Oct 29, 2024
1 parent 21e84cb commit b9241df
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
20 changes: 10 additions & 10 deletions src/plugins/data/public/query/query_string/query_history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ export const HISTORY_KEY_PREFIX = 'query_';
export class QueryHistory {
private changeEmitter: BehaviorSubject<any[]>;

constructor(private readonly sessionStorage: DataStorage) {
constructor(private readonly storage: DataStorage) {
this.changeEmitter = new BehaviorSubject<any[]>(this.getHistory());
}

public getHistoryKeys(): string[] {
return this.sessionStorage
return this.storage
.keys()
.filter((key: string) => key.startsWith(HISTORY_KEY_PREFIX))
.sort((a, b) => {
Expand All @@ -31,7 +31,7 @@ export class QueryHistory {

public getHistory(): any[] {
return this.getHistoryKeys()
.map((key) => this.sessionStorage.get(key))
.map((key) => this.storage.get(key))
.sort((a, b) => b.time - a.time);
}

Expand All @@ -45,13 +45,13 @@ export class QueryHistory {

// Check if the query already exists
const existingKey = existingKeys.find((key) => {
const item = this.sessionStorage.get(key);
const item = this.storage.get(key);
return item && item.query.query === query.query && item.query.language === query.language;
});

if (existingKey) {
// If the query exists, remove it from its current position
this.sessionStorage.remove(existingKey);
this.storage.remove(existingKey);
existingKeys.splice(existingKeys.indexOf(existingKey), 1);
}

Expand All @@ -64,24 +64,24 @@ export class QueryHistory {
dateRange,
id: uuid.v4(),
};
this.sessionStorage.set(newKey, newItem);
this.storage.set(newKey, newItem);

// Trim the history if it exceeds the maximum size
if (existingKeys.length >= MAX_HISTORY_SIZE) {
const keysToRemove = existingKeys.slice(MAX_HISTORY_SIZE - 1);
keysToRemove.forEach((key) => this.sessionStorage.remove(key));
keysToRemove.forEach((key) => this.storage.remove(key));
}

// Emit the updated history
this.changeEmitter.next(this.getHistory());
}

public clearHistory(): void {
this.getHistoryKeys().forEach((key) => this.sessionStorage.remove(key));
this.getHistoryKeys().forEach((key) => this.storage.remove(key));
this.changeEmitter.next([]);
}
}

export function createHistory(deps: { sessionStorage: DataStorage }): QueryHistory {
return new QueryHistory(deps.sessionStorage);
export function createHistory(deps: { storage: DataStorage }): QueryHistory {
return new QueryHistory(deps.storage);
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export class QueryStringManager {
private readonly notifications: NotificationsSetup
) {
this.query$ = new BehaviorSubject<Query>(this.getDefaultQuery());
this.queryHistory = new QueryHistory(this.sessionStorage);
this.queryHistory = createHistory({ storage: this.sessionStorage });
this.datasetService = new DatasetService(uiSettings, this.sessionStorage);
this.languageService = new LanguageService(this.defaultSearchInterceptor, this.storage);
}
Expand Down

0 comments on commit b9241df

Please sign in to comment.