Skip to content

Commit

Permalink
progress
Browse files Browse the repository at this point in the history
Signed-off-by: abbyhu2000 <[email protected]>
  • Loading branch information
abbyhu2000 committed Aug 19, 2024
1 parent 9a84202 commit f424b16
Show file tree
Hide file tree
Showing 19 changed files with 132 additions and 197 deletions.
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
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 @@ export class DataPublicPlugin
constructor(initializerContext: PluginInitializerContext<ConfigSchema>) {
this.searchService = new SearchService(initializerContext);
this.uiService = new UiService(initializerContext);
this.queryService = new QueryService();
this.queryService = new QueryService(initializerContext);
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 @@ export class DataPublicPlugin
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);
},
};
}
Expand Down
16 changes: 13 additions & 3 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 @@ -44,7 +48,9 @@ import {
IndexPatternsService,
} from '../../common';
import { getUiSettings } from '../services';
import { IndexPattern } from '..';
import { IndexPattern, ISearchStart } from '..';
import { ConfigSchema } from '../../config';
import { SearchService } from '../search/search_service';

/**
* Query Service
Expand All @@ -71,6 +77,8 @@ export class QueryService {

state$!: ReturnType<typeof createQueryStateObservable>;

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

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

Expand All @@ -80,7 +88,9 @@ export class QueryService {
storage,
});

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

this.queryStringManager = new QueryStringManager(storage, uiSettings, supportedAppNames);
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 { QueryEditorExtensionConfig } from '../../../ui';
import { ConfigSchema } from '../../../../config';
import { DataStorage, setOverrides as setFieldOverrides } from '../../../../common';

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

export class Settings {
export class LanguageManager {
private isEnabled = false;
private enabledQueryEnhancementsUpdated$ = new BehaviorSubject<boolean>(this.isEnabled);
private enhancedAppNames: string[] = [];
private queryEnhancements: Map<string, QueryEnhancement>;
private queryEditorExtensionMap: Record<string, QueryEditorExtensionConfig>;

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 = {};
}

public __enhance = (enhancements: UiEnhancements) => {
if (!enhancements) return;
if (enhancements.query && enhancements.query.language) {
this.queryEnhancements.set(enhancements.query.language, enhancements.query);
}
if (enhancements.queryEditorExtension) {
this.queryEditorExtensionMap[enhancements.queryEditorExtension.id] =
enhancements.queryEditorExtension;
}
};

public getAllQueryEnhancements() {
return this.queryEnhancements;
}

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

public getQueryEditorExtensionMap() {
return this.queryEditorExtensionMap;
}

supportsEnhancementsEnabled(appName: string) {
Expand All @@ -58,18 +81,6 @@ export class Settings {
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,16 +98,16 @@ export class Settings {
}

setUserQueryLanguage(language: string) {
if (language !== this.getUserQueryLanguage()) {
this.search.df.clear();
}
// 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.search.__enhance({
// searchInterceptor: queryEnhancement
// ? queryEnhancement.search
// : this.search.getDefaultSearchInterceptor(),
// });
this.setUiOverridesByUserQueryLanguage(language);

return true;
Expand Down Expand Up @@ -167,20 +178,4 @@ export class Settings {
}
}

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,23 @@ import { skip } from 'rxjs/operators';
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';
import { SearchService } from '../../search/search_service';

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

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

public getLanguageManager() {
return this.languageManager;
}
}

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 @@ import {
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 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
{ fieldFormats, indexPatterns }: SearchServiceStartDependencies
): ISearchStart {
const search = ((request, options) => {
const queryString = getQueryService().queryString;
const selectedLanguage = getQueryService().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);
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 @@ export const DataSetNavigator: React.FC<DataSetNavigatorProps> = ({
}) => {
const searchService = getSearchService();
const queryService = getQueryService();
const languageManager = queryService.queryString.getLanguageManager();
const uiService = getUiService();
const indexPatternsService = getIndexPatterns();
const notifications = getNotifications();
Expand Down Expand Up @@ -107,14 +108,14 @@ export const DataSetNavigator: React.FC<DataSetNavigatorProps> = ({
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);
const initialInput = queryEnhancements?.searchBar?.queryStringInput?.initialValue;

const query = initialInput
? initialInput.replace('<data_source>', selectedDataSet.title!)
: '';
uiService.Settings.setUserQueryString(query);
languageManager.setUserQueryString(query);
queryService.queryString.setQuery({ query, language });

queryService.dataSetManager.setDataSet(selectedDataSet);
Expand Down Expand Up @@ -143,7 +144,7 @@ export const DataSetNavigator: React.FC<DataSetNavigatorProps> = ({

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

useEffect(() => {
Expand Down
15 changes: 7 additions & 8 deletions src/plugins/data/public/ui/query_editor/language_selector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@elastic/eui';
import { i18n } from '@osd/i18n';
import React, { useState } from 'react';
import { getUiService } from '../../services';
import { getQueryService } from '../../services';

export interface QueryLanguageSelectorProps {
language: string;
Expand Down Expand Up @@ -53,17 +53,16 @@ export const QueryLanguageSelector = (props: QueryLanguageSelectorProps) => {
},
];

const uiService = getUiService();
const queryService = getQueryService();
const languageManager = queryService.queryString.getLanguageManager();

const queryEnhancements = uiService.Settings.getAllQueryEnhancements();
const queryEnhancements = languageManager.getAllQueryEnhancements();
queryEnhancements.forEach((enhancement) => {
if (
(enhancement.supportedAppNames &&
props.appName &&
!enhancement.supportedAppNames.includes(props.appName)) ||
uiService.Settings.getUserQueryLanguageBlocklist().includes(
enhancement.language.toLowerCase()
)
languageManager.getUserQueryLanguageBlocklist().includes(enhancement.language.toLowerCase())
)
return;
languageOptions.unshift(mapExternalLanguageToOptions(enhancement.language));
Expand All @@ -78,10 +77,10 @@ export const QueryLanguageSelector = (props: QueryLanguageSelectorProps) => {

const handleLanguageChange = (newLanguage: string) => {
props.onSelectLanguage(newLanguage);
uiService.Settings.setUserQueryLanguage(newLanguage);
languageManager.setUserQueryLanguage(newLanguage);
};

uiService.Settings.setUserQueryLanguage(props.language);
languageManager.setUserQueryLanguage(props.language);

const languageOptionsMenu = languageOptions
.sort((a, b) => {
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/ui/query_editor/no_data_popover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
import { ReactElement, useEffect, useState } from 'react';
import React from 'react';
import { EuiButtonEmpty, EuiText, EuiTourStep } from '@elastic/eui';
import { IStorageWrapper } from 'src/plugins/opensearch_dashboards_utils/public';
import { i18n } from '@osd/i18n';
import { DataStorage } from 'src/plugins/data/common';

const NO_DATA_POPOVER_STORAGE_KEY = 'data.noDataPopover';

Expand All @@ -17,7 +17,7 @@ export function NoDataPopover({
children,
}: {
showNoDataPopover?: boolean;
storage: IStorageWrapper;
storage: DataStorage;
children: ReactElement;
}) {
const [noDataPopoverDismissed, setNoDataPopoverDismissed] = useState(() =>
Expand Down
Loading

0 comments on commit f424b16

Please sign in to comment.