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

Created language manager inside query string service #7756

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7756.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Created language manager inside query string service ([#7756](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7756))
6 changes: 4 additions & 2 deletions src/plugins/data/public/antlr/dql/code_completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { getTokenPosition } from '../shared/cursor';
import { IndexPattern, IndexPatternField } from '../../index_patterns';
import { QuerySuggestionGetFnArgs } from '../../autocomplete';
import { DQLParserVisitor } from './.generated/DQLParserVisitor';
import { getUiService } from '../../services';
import { getQueryService } from '../../services';

const findCursorIndex = (
tokenStream: TokenStream,
Expand Down Expand Up @@ -132,7 +132,9 @@ export const getSuggestions = async ({
if (
!services ||
!services.appName ||
!getUiService().Settings.supportsEnhancementsEnabled(services.appName) ||
!getQueryService()
.queryString.getLanguageManager()
.supportsEnhancementsEnabled(services.appName) ||
!indexPattern
) {
return [];
Expand Down
1 change: 1 addition & 0 deletions src/plugins/data/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
} from '../common';

import { FilterLabel } from './ui';
export { createEditor, DefaultInput, DQLBody, SingleLineInput } from './ui';

import {
generateFilters,
Expand Down
5 changes: 3 additions & 2 deletions src/plugins/data/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
this.searchService = new SearchService(initializerContext);
this.uiService = new UiService(initializerContext);
this.queryService = new QueryService();
this.queryService = new QueryService(initializerContext);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L122 was not covered by tests
this.fieldFormatsService = new FieldFormatsService();
this.autocomplete = new AutocompleteService(initializerContext);
this.storage = createStorage({ engine: window.localStorage, prefix: 'opensearch_dashboards.' });
Expand Down Expand Up @@ -176,7 +176,8 @@
query: queryService,
__enhance: (enhancements: DataPublicPluginEnhancements) => {
if (enhancements.search) searchService.__enhance(enhancements.search);
if (enhancements.ui) uiService.__enhance(enhancements.ui);
if (enhancements.ui)
queryService.queryString.getLanguageManager().__enhance(enhancements.ui);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L180 was not covered by tests
},
};
}
Expand Down
13 changes: 11 additions & 2 deletions src/plugins/data/public/query/query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@
*/

import { share } from 'rxjs/operators';
import { IUiSettingsClient, SavedObjectsClientContract } from 'src/core/public';
import {
IUiSettingsClient,
PluginInitializerContext,
SavedObjectsClientContract,
} from 'src/core/public';
import { FilterManager } from './filter_manager';
import { createAddToQueryLog } from './lib';
import { TimefilterService, TimefilterSetup } from './timefilter';
Expand All @@ -45,6 +49,7 @@
} from '../../common';
import { getUiSettings } from '../services';
import { IndexPattern } from '..';
import { ConfigSchema } from '../../config';

/**
* Query Service
Expand All @@ -71,6 +76,8 @@

state$!: ReturnType<typeof createQueryStateObservable>;

constructor(private initializerContext: PluginInitializerContext<ConfigSchema>) {}

public setup({ storage, uiSettings }: QueryServiceSetupDependencies) {
this.filterManager = new FilterManager(uiSettings);

Expand All @@ -80,7 +87,9 @@
storage,
});

this.queryStringManager = new QueryStringManager(storage, uiSettings);
const { enhancements: supportedAppNames } = this.initializerContext.config.get<ConfigSchema>();

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

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_service.ts#L90

Added line #L90 was not covered by tests

this.queryStringManager = new QueryStringManager(storage, uiSettings, supportedAppNames);

Check warning on line 92 in src/plugins/data/public/query/query_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_service.ts#L92

Added line #L92 was not covered by tests
this.dataSetManager = new DataSetManager(uiSettings);

this.state$ = createQueryStateObservable({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
*/

import { BehaviorSubject } from 'rxjs';
import { DataStorage, setOverrides as setFieldOverrides } from '../../../common';
import { ConfigSchema } from '../../../config';
import { ISearchStart } from '../../search';
import { QueryEditorExtensionConfig } from '../query_editor/query_editor_extensions';
import { QueryEnhancement } from '../types';
import { QueryEnhancement, UiEnhancements } from '../../../ui/types';
import { createEditor, DQLBody, QueryEditorExtensionConfig, SingleLineInput } from '../../../ui';
import { ConfigSchema } from '../../../../config';
import { DataStorage, setOverrides as setFieldOverrides } from '../../../../common';

export interface DataSettings {
userQueryLanguage: string;
Expand All @@ -22,21 +21,49 @@
};
}

export class Settings {
export class LanguageManager {
private isEnabled = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this can be removed

private enabledQueryEnhancementsUpdated$ = new BehaviorSubject<boolean>(this.isEnabled);
private enhancedAppNames: string[] = [];
private queryEnhancements: Map<string, QueryEnhancement>;
private queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig>;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be apart of the query enhancement


constructor(
private readonly config: ConfigSchema['enhancements'],
private readonly search: ISearchStart,
private readonly storage: DataStorage,
private readonly queryEnhancements: Map<string, QueryEnhancement>,
private readonly queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig>
private readonly storage: DataStorage
) {
this.isEnabled = true;
this.setUserQueryEnhancementsEnabled(this.isEnabled);
this.enhancedAppNames = this.isEnabled ? this.config.supportedAppNames : [];
this.queryEnhancements = new Map();
this.queryEditorExtensionMap = {};

Check warning on line 39 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#L38-L39

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

public createDefaultQueryEditor() {
return createEditor(SingleLineInput, SingleLineInput, DQLBody);

Check warning on line 43 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#L43

Added line #L43 was not covered by tests
}

public __enhance = (enhancements: UiEnhancements) => {

Check warning on line 46 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#L46

Added line #L46 was not covered by tests
if (!enhancements) return;
if (enhancements.query && enhancements.query.language) {
this.queryEnhancements.set(enhancements.query.language, enhancements.query);

Check warning on line 49 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#L49

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

Check warning on line 52 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#L52

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

public getAllQueryEnhancements() {
return this.queryEnhancements;

Check warning on line 58 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#L58

Added line #L58 was not covered by tests
}

public getQueryEnhancements(language: string) {
return this.queryEnhancements.get(language);

Check warning on line 62 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#L62

Added line #L62 was not covered by tests
}

public getQueryEditorExtensionMap() {
return this.queryEditorExtensionMap;

Check warning on line 66 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#L66

Added line #L66 was not covered by tests
}

supportsEnhancementsEnabled(appName: string) {
Expand All @@ -58,18 +85,6 @@
return true;
}

getAllQueryEnhancements() {
return this.queryEnhancements;
}

getQueryEnhancements(language: string) {
return this.queryEnhancements.get(language);
}

getQueryEditorExtensionMap() {
return this.queryEditorExtensionMap;
}

getUserQueryLanguageBlocklist() {
return this.storage.get('userQueryLanguageBlocklist') || [];
}
Expand All @@ -87,18 +102,8 @@
}

setUserQueryLanguage(language: string) {
if (language !== this.getUserQueryLanguage()) {
this.search.df.clear();
}
this.storage.set('userQueryLanguage', language);
const queryEnhancement = this.queryEnhancements.get(language);
this.search.__enhance({
searchInterceptor: queryEnhancement
? queryEnhancement.search
: this.search.getDefaultSearchInterceptor(),
});
this.setUiOverridesByUserQueryLanguage(language);

return true;
}

Expand Down Expand Up @@ -129,10 +134,10 @@
setUiOverridesByUserQueryLanguage(language: string) {
const queryEnhancement = this.queryEnhancements.get(language);
if (queryEnhancement) {
const { fields = {}, showDocLinks } = queryEnhancement;
this.setUiOverrides({ fields, showDocLinks });
const { fields = {} } = queryEnhancement;
this.setUiOverrides({ fields });

Check warning on line 138 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#L138

Added line #L138 was not covered by tests
} else {
this.setUiOverrides({ fields: undefined, showDocLinks: undefined });
this.setUiOverrides({ fields: undefined });

Check warning on line 140 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#L140

Added line #L140 was not covered by tests
}
}

Expand Down Expand Up @@ -167,20 +172,4 @@
}
}

interface Deps {
config: ConfigSchema['enhancements'];
search: ISearchStart;
storage: DataStorage;
queryEnhancements: Map<string, QueryEnhancement>;
queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig>;
}

export function createSettings({
config,
search,
storage,
queryEnhancements,
queryEditorExtensionMap,
}: Deps) {
return new Settings(config, search, storage, queryEnhancements, queryEditorExtensionMap);
}
export type LanguageContract = PublicMethodsOf<LanguageManager>;
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,22 @@
import { CoreStart } from 'opensearch-dashboards/public';
import { DataStorage, Query, SimpleDataSet, TimeRange, UI_SETTINGS } from '../../../common';
import { createHistory, QueryHistory } from './query_history';
import { LanguageManager } from './language_manager/language_manager';
import { ConfigSchema } from '../../../config';

export class QueryStringManager {
private query$: BehaviorSubject<Query>;
private queryHistory: QueryHistory;
public languageManager: LanguageManager;

constructor(
private readonly storage: DataStorage,
private readonly uiSettings: CoreStart['uiSettings']
private readonly uiSettings: CoreStart['uiSettings'],
private readonly supportedAppNames: ConfigSchema['enhancements']
) {
this.query$ = new BehaviorSubject<Query>(this.getDefaultQuery());
this.queryHistory = createHistory({ storage });
this.languageManager = new LanguageManager(this.supportedAppNames, this.storage);

Check warning on line 51 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L51

Added line #L51 was not covered by tests
}

private getDefaultQueryString() {
Expand Down Expand Up @@ -121,6 +126,10 @@
public changeQueryHistory(listener: (reqs: any[]) => void) {
return this.queryHistory.change(listener);
}

public getLanguageManager() {
return this.languageManager;

Check warning on line 131 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L131

Added line #L131 was not covered by tests
}
}

export type QueryStringContract = PublicMethodsOf<QueryStringManager>;
8 changes: 4 additions & 4 deletions src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
createDataFrameCache,
dataFrameToSpec,
} from '../../common/data_frames';
import { getQueryService, getUiService } from '../services';
import { getQueryService } from '../services';
import { UI_SETTINGS } from '../../common';

/** @internal */
Expand Down Expand Up @@ -135,10 +135,10 @@
{ fieldFormats, indexPatterns }: SearchServiceStartDependencies
): ISearchStart {
const search = ((request, options) => {
const queryString = getQueryService().queryString;

Check warning on line 138 in src/plugins/data/public/search/search_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/search/search_service.ts#L138

Added line #L138 was not covered by tests
const selectedLanguage = getQueryService().queryString.getQuery().language;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can use the constant you create on queryString.getQuery().language

const uiService = getUiService();
const enhancement = uiService.Settings.getQueryEnhancements(selectedLanguage);
uiService.Settings.setUiOverridesByUserQueryLanguage(selectedLanguage);
const enhancement = queryString.getLanguageManager().getQueryEnhancements(selectedLanguage);
queryString.getLanguageManager().setUiOverridesByUserQueryLanguage(selectedLanguage);

Check warning on line 141 in src/plugins/data/public/search/search_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/search/search_service.ts#L140-L141

Added lines #L140 - L141 were not covered by tests
const isEnhancedEnabled = uiSettings.get(UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED);

if (enhancement) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
}) => {
const searchService = getSearchService();
const queryService = getQueryService();
const languageManager = queryService.queryString.getLanguageManager();

Check warning on line 64 in src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx#L64

Added line #L64 was not covered by tests
const uiService = getUiService();
const indexPatternsService = getIndexPatterns();
const notifications = getNotifications();
Expand Down Expand Up @@ -107,14 +108,14 @@
const selectedDataSet = ds ?? selectedDataSetState;
if (!selectedDataSet || !selectedDataSet.id) return;

const language = uiService.Settings.getUserQueryLanguage();
const queryEnhancements = uiService.Settings.getQueryEnhancements(language);
const language = languageManager.getUserQueryLanguage();
const queryEnhancements = languageManager.getQueryEnhancements(language);

Check warning on line 112 in src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx#L111-L112

Added lines #L111 - L112 were not covered by tests
const initialInput = queryEnhancements?.searchBar?.queryStringInput?.initialValue;

const query = initialInput
? initialInput.replace('<data_source>', selectedDataSet.title!)
: '';
uiService.Settings.setUserQueryString(query);
languageManager.setUserQueryString(query);

Check warning on line 118 in src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/ui/dataset_navigator/dataset_navigator.tsx#L118

Added line #L118 was not covered by tests
queryService.queryString.setQuery({ query, language });

queryService.dataSetManager.setDataSet(selectedDataSet);
Expand Down Expand Up @@ -143,7 +144,7 @@

closePopover();
},
[queryService, setSelectedDataSetState, selectedDataSetState, uiService.Settings]
[queryService, setSelectedDataSetState, selectedDataSetState, languageManager]
);

useEffect(() => {
Expand Down
13 changes: 5 additions & 8 deletions src/plugins/data/public/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@
* under the License.
*/

export {
UiEnhancements,
IUiStart,
IUiSetup,
createSettings,
Settings,
DataSettings,
} from './types';
export { UiEnhancements, IUiStart, IUiSetup } from './types';
export { IndexPatternSelectProps } from './index_pattern_select';
export { FilterLabel } from './filter_bar';
export { QueryStringInput, QueryStringInputProps } from './query_string_input';
Expand All @@ -46,6 +39,10 @@ export {
QueryEditorExtensions,
QueryEditorExtensionDependencies,
QueryEditorExtensionConfig,
createEditor,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how do we intend to use this? (sorry if it's explained later in PR).
exporting components which devs can handle the state and prop or do they access it by the ui service?

DefaultInput,
DQLBody,
SingleLineInput,
} from './query_editor';
export { SearchBar, SearchBarProps, StatefulSearchBarProps } from './search_bar';
export { SuggestionsComponent } from './typeahead';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ interface DefaultInputProps extends React.JSX.IntrinsicAttributes {
provideCompletionItems: monaco.languages.CompletionItemProvider['provideCompletionItems'];
}

const DefaultInput: React.FC<DefaultInputProps> = ({
export const DefaultInput: React.FC<DefaultInputProps> = ({
languageId,
value,
onChange,
Expand Down Expand Up @@ -95,5 +95,3 @@ const FooterItem: React.FC<{ item: FooterItem | string }> = ({ item }) => {
</EuiText>
);
};

export const createDefaultEditor = createEditor(SingleLineInput, null, DefaultInput);
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@
*/

import React from 'react';
import { createEditor, SingleLineInput } from '../shared';

interface DQLBodyProps extends React.JSX.IntrinsicAttributes {
filterBar?: any;
}

const DQLBody: React.FC<DQLBodyProps> = ({ filterBar }) => <div>{filterBar}</div>;

export const createDQLEditor = createEditor(SingleLineInput, SingleLineInput, DQLBody);
export const DQLBody: React.FC<DQLBodyProps> = ({ filterBar }) => <div>{filterBar}</div>;
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export interface Editor<TCollapsed, TExpanded, TBody> {
Body: BodyComponent<TBody>;
}

interface EditorInstance<TCollapsed, TExpanded, TBody> {
export interface EditorInstance<TCollapsed, TExpanded, TBody> {
TopBar: {
Collapsed: () => React.ReactElement;
Expanded: (() => React.ReactElement) | null;
Expand Down
Loading
Loading