Skip to content
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

2.0 query bar refactoring #7667

Closed
wants to merge 9 commits into from
Closed
8 changes: 4 additions & 4 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public';
import { ConfigSchema } from '../config';
import {
Storage,
IStorageWrapper,
createStartServicesGetter,
} from '../../opensearch_dashboards_utils/public';
Expand All @@ -49,6 +48,7 @@
import { UiService } from './ui/ui_service';
import { FieldFormatsService } from './field_formats';
import { QueryService } from './query';
import { createStorage, QueryStorage } from './ui';
import {
IndexPatternsService,
onRedirectNoIndexPattern,
Expand Down Expand Up @@ -117,15 +117,15 @@
private readonly uiService: UiService;
private readonly fieldFormatsService: FieldFormatsService;
private readonly queryService: QueryService;
private readonly storage: IStorageWrapper;
private readonly storage: QueryStorage;

constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
this.searchService = new SearchService(initializerContext);
this.uiService = new UiService(initializerContext);
this.queryService = new QueryService();
this.fieldFormatsService = new FieldFormatsService();
this.autocomplete = new AutocompleteService(initializerContext);
this.storage = new Storage(window.localStorage);
this.storage = createStorage({ engine: window.localStorage, prefix: 'opensearchDashboards.' });

Check warning on line 128 in src/plugins/data/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/plugin.ts#L128

Added line #L128 was not covered by tests
}

public setup(
Expand Down Expand Up @@ -179,7 +179,7 @@
query: queryService,
__enhance: (enhancements: DataPublicPluginEnhancements) => {
if (enhancements.search) searchService.__enhance(enhancements.search);
if (enhancements.ui) uiService.__enhance(enhancements.ui);
if (enhancements.ui) queryService.queryString.__enhance(enhancements.ui);
},
};
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/data/public/query/lib/add_to_query_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ import { IUiSettingsClient } from 'src/core/public';
import { IStorageWrapper } from 'src/plugins/opensearch_dashboards_utils/public';
import { Query } from '../../../common';
import { getQueryLog } from './get_query_log';
import { QueryStorage } from '../../ui';

interface AddToQueryLogDependencies {
uiSettings: IUiSettingsClient;
storage: IStorageWrapper;
storage: QueryStorage;
}

export function createAddToQueryLog({ storage, uiSettings }: AddToQueryLogDependencies) {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/query/lib/get_query_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
*/

import { IUiSettingsClient } from 'src/core/public';
import { IStorageWrapper } from 'src/plugins/opensearch_dashboards_utils/public';
import { PersistedLog } from '../persisted_log';
import { UI_SETTINGS } from '../../../common';
import { QueryStorage } from '../../ui';

/** @internal */
export function getQueryLog(
uiSettings: IUiSettingsClient,
storage: IStorageWrapper,
storage: QueryStorage,
appName: string,
language: string
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import _ from 'lodash';
import * as Rx from 'rxjs';
import { map } from 'rxjs/operators';
import { IStorageWrapper } from 'src/plugins/opensearch_dashboards_utils/public';
import { QueryStorage } from '../../ui';

const defaultIsDuplicate = (oldItem: any, newItem: any) => {
return _.isEqual(oldItem, newItem);
Expand All @@ -48,12 +49,12 @@ export class PersistedLog<T = any> {
public maxLength?: number;
public filterDuplicates?: boolean;
public isDuplicate: (oldItem: T, newItem: T) => boolean;
public storage: IStorageWrapper;
public storage: QueryStorage;
public items: T[];

private update$ = new Rx.BehaviorSubject(undefined);

constructor(name: string, options: PersistedLogOptions<T> = {}, storage: IStorageWrapper) {
constructor(name: string, options: PersistedLogOptions<T> = {}, storage: QueryStorage) {
this.name = name;
this.maxLength =
typeof options.maxLength === 'string'
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/data/public/query/query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,21 @@ import { DataSetContract, DataSetManager } from './dataset_manager';
import { buildOpenSearchQuery, getOpenSearchQueryConfig, IndexPatternsService } from '../../common';
import { getUiSettings } from '../services';
import { IndexPattern } from '..';
import { QueryStorage } from '../ui';

/**
* Query Service
* @internal
*/

interface QueryServiceSetupDependencies {
storage: IStorageWrapper;
storage: QueryStorage;
uiSettings: IUiSettingsClient;
}

interface QueryServiceStartDependencies {
savedObjectsClient: SavedObjectsClientContract;
storage: IStorageWrapper;
storage: QueryStorage;
uiSettings: IUiSettingsClient;
indexPatterns: IndexPatternsService;
}
Expand Down
91 changes: 91 additions & 0 deletions src/plugins/data/public/query/query_string/history.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* 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 { BehaviorSubject } from 'rxjs';
import { QueryStorage } from '../../ui/history/storage';
import { Query, TimeRange } from '../..';

export class QueryHistory {
constructor(private readonly storage: QueryStorage) {}

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();

Check warning on line 40 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L39-L40

Added lines #L39 - L40 were not covered by tests
}

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);

Check warning on line 47 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L44-L47

Added lines #L44 - L47 were not covered by tests
});

const timestamp = new Date().getTime();
const k = 'query_' + timestamp;
this.storage.set(k, {

Check warning on line 52 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L50-L52

Added lines #L50 - L52 were not covered by tests
dataSet,
time: timestamp,
query,
dateRange,
});

this.changeEmitter.next(this.getHistory());

Check warning on line 59 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L59

Added line #L59 was not covered by tests
}

updateCurrentState(content: any) {
const timestamp = new Date().getTime();
this.storage.set('editor_state', {

Check warning on line 64 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L63-L64

Added lines #L63 - L64 were not covered by tests
time: timestamp,
content,
});
}

getLegacySavedEditorState() {
const saved = this.storage.get('editor_state');

Check warning on line 71 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L71

Added line #L71 was not covered by tests
if (!saved) return;
const { time, content } = saved;
return { time, content };

Check warning on line 74 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L73-L74

Added lines #L73 - L74 were not covered by tests
}

/**
* This function should only ever be called once for a user if they had legacy state.
*/
deleteLegacySavedEditorState() {
this.storage.remove('editor_state');

Check warning on line 81 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L81

Added line #L81 was not covered by tests
}

clearHistory() {
this.getHistoryKeys().forEach((key) => this.storage.remove(key));

Check warning on line 85 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L85

Added line #L85 was not covered by tests
}
}

export function createHistory(deps: { storage: QueryStorage }) {
return new QueryHistory(deps.storage);

Check warning on line 90 in src/plugins/data/public/query/query_string/history.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/history.ts#L90

Added line #L90 was not covered by tests
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { BehaviorSubject } from 'rxjs';
import { CoreStart } from 'opensearch-dashboards/public';
import { skip } from 'rxjs/operators';
import {
IndexPattern,
SIMPLE_DATA_SET_TYPES,
SimpleDataSet,
SimpleDataSource,
UI_SETTINGS,
} from '../../../../common';
import { IndexPatternsContract } from '../../../index_patterns';
import { QueryEnhancement, UiEnhancements } from '../../../ui/types';
import { QueryEditorExtensionConfig } from '../../../ui';
import { DataPublicPluginEnhancements } from '../../../types';

export class LanguageManager {
private queryEnhancements: Map<string, QueryEnhancement>;
private queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig>;
//TODO

Check failure on line 24 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
//private queryEditorUISettings: Map<string, queryEditorUISettings>;

Check failure on line 25 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment

constructor(private readonly uiSettings: CoreStart['uiSettings']) {
this.queryEnhancements = new Map();
this.queryEditorExtensionMap = {};
//TODO

Check failure on line 30 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
//this.queryEditorUISettings = new Map();

Check failure on line 31 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
}

public __enhance = (enhancements: UiEnhancements) => {
if (!enhancements) return;
if (enhancements.query && enhancements.query.language) {
this.queryEnhancements.set(enhancements.query.language, enhancements.query);

Check warning on line 37 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/language_manager/language_manager.ts#L37

Added line #L37 was not covered by tests
}
if (enhancements.queryEditorExtension) {
this.queryEditorExtensionMap[enhancements.queryEditorExtension.id] =

Check warning on line 40 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/language_manager/language_manager.ts#L40

Added line #L40 was not covered by tests
enhancements.queryEditorExtension;
}

//TODO

Check failure on line 44 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
//settings related to query editor UI

Check failure on line 45 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View workflow job for this annotation

GitHub Actions / Build and Verify on Linux (ciGroup1)

Expected exception block, space or tab after '//' in comment
// if (enhancements.queryEditorUISettings && enhancements.queryEditorUISettings.language) {
// this.queryEditorUISettings.set(
// enhancements.queryEditorUISettings.language,
// enhancements.queryEditorUISettings
// );
// }
};

// public initWithIndexPattern = (indexPattern: IndexPattern | null) => {
// if (!this.uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED)) return;
// if (!indexPattern || !indexPattern.id) {
// return undefined;
// }

// this.defaultDataSet = {
// id: indexPattern.id,
// title: indexPattern.title,
// type: SIMPLE_DATA_SET_TYPES.INDEX_PATTERN,
// timeFieldName: indexPattern.timeFieldName,
// fields: indexPattern.fields,
// ...(indexPattern.dataSourceRef
// ? {
// dataSourceRef: {
// id: indexPattern.dataSourceRef?.id,
// name: indexPattern.dataSourceRef?.name,
// type: indexPattern.dataSourceRef?.type,
// } as SimpleDataSource,
// }
// : {}),
// };
// };

// public getUpdates$ = () => {
// return this.dataSet$.asObservable().pipe(skip(1));
// };

public getQueryEnhancement = () => {
return this.queryEnhancements;

Check warning on line 83 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/language_manager/language_manager.ts#L83

Added line #L83 was not covered by tests
};

public getQueryEditorExtension = () => {
return this.queryEditorExtensionMap;

Check warning on line 87 in src/plugins/data/public/query/query_string/language_manager/language_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/language_manager/language_manager.ts#L87

Added line #L87 was not covered by tests
};

// public getDefaultDataSet = () => {
// return this.defaultDataSet;
// };

// public fetchDefaultDataSet = async (): Promise<SimpleDataSet | undefined> => {
// const defaultIndexPatternId = this.uiSettings.get('defaultIndex');
// if (!defaultIndexPatternId) {
// return undefined;
// }

// const indexPattern = await this.indexPatterns?.get(defaultIndexPatternId);
// if (!indexPattern || !indexPattern.id) {
// return undefined;
// }

// return {
// id: indexPattern.id,
// title: indexPattern.title,
// type: SIMPLE_DATA_SET_TYPES.INDEX_PATTERN,
// timeFieldName: indexPattern.timeFieldName,
// ...(indexPattern.dataSourceRef
// ? {
// dataSourceRef: {
// id: indexPattern.dataSourceRef?.id,
// name: indexPattern.dataSourceRef?.name,
// type: indexPattern.dataSourceRef?.type,
// } as SimpleDataSource,
// }
// : {}),
// };
// };
}

export type LanguageContract = PublicMethodsOf<LanguageManager>;
Loading
Loading