From c1d1e888793315a8d5521861359abe22867c81c2 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 3 Sep 2024 21:53:17 +0000 Subject: [PATCH] fix(discover): switch language based on dataset selection (#7924) * fix(discover): switch language based on dataset selection Signed-off-by: Joshua Li * keep currently selected language as default option Signed-off-by: Joshua Li * update supportedLanguages mock Signed-off-by: Joshua Li --------- Signed-off-by: Joshua Li (cherry picked from commit edabe3bda9b0da800ab9b96891569399d97b20f6) Signed-off-by: github-actions[bot] --- .../dataset_service/dataset_service.mock.ts | 2 +- .../dataset_service/lib/index_pattern_type.ts | 4 +-- .../query_string/dataset_service/types.ts | 2 +- .../language_service/language_service.ts | 4 +-- .../query_string/query_string_manager.ts | 8 +++--- .../ui/dataset_selector/configurator.tsx | 27 +++++++++++-------- .../ui/query_editor/language_selector.tsx | 2 +- 7 files changed, 27 insertions(+), 22 deletions(-) diff --git a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts index ecf7c74410ca..0b9372b73092 100644 --- a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts +++ b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.mock.ts @@ -18,7 +18,7 @@ const createSetupDatasetServiceMock = (): jest.Mocked => toDataset: jest.fn(), fetch: jest.fn(), fetchFields: jest.fn(), - supportedLanguages: jest.fn().mockReturnValue(['DQL', 'Lucene', 'PPL', 'SQL']), + supportedLanguages: jest.fn().mockReturnValue(['kuery', 'lucene', 'PPL', 'SQL']), }; const defaultDataset: Dataset = { diff --git a/src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts b/src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts index b92fb70631f6..e8064715f7af 100644 --- a/src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts +++ b/src/plugins/data/public/query/query_string/dataset_service/lib/index_pattern_type.ts @@ -65,9 +65,9 @@ export const indexPatternTypeConfig: DatasetTypeConfig = { supportedLanguages: (dataset): string[] => { if (dataset.dataSource?.type === 'OpenSearch Serverless') { - return ['DQL', 'Lucene']; + return ['kuery', 'lucene']; } - return ['DQL', 'Lucene', 'PPL', 'SQL']; + return ['kuery', 'lucene', 'PPL', 'SQL']; }, }; diff --git a/src/plugins/data/public/query/query_string/dataset_service/types.ts b/src/plugins/data/public/query/query_string/dataset_service/types.ts index 0ea84d67f2a0..ad2b50aeb1cf 100644 --- a/src/plugins/data/public/query/query_string/dataset_service/types.ts +++ b/src/plugins/data/public/query/query_string/dataset_service/types.ts @@ -41,7 +41,7 @@ export interface DatasetTypeConfig { fetchFields: (dataset: Dataset) => Promise; /** * Retrieves the supported query languages for this dataset type. - * @returns {Promise} A promise that resolves to an array of supported language names. + * @returns {Promise} A promise that resolves to an array of supported language ids. */ supportedLanguages: (dataset: Dataset) => string[]; } diff --git a/src/plugins/data/public/query/query_string/language_service/language_service.ts b/src/plugins/data/public/query/query_string/language_service/language_service.ts index 113a85e25e10..899915248e52 100644 --- a/src/plugins/data/public/query/query_string/language_service/language_service.ts +++ b/src/plugins/data/public/query/query_string/language_service/language_service.ts @@ -57,8 +57,8 @@ export class LanguageService { this.languages.set(config.id, config); } - public getLanguage(language: string): LanguageConfig | undefined { - return this.languages.get(language); + public getLanguage(languageId: string): LanguageConfig | undefined { + return this.languages.get(languageId); } public getLanguages(): LanguageConfig[] { diff --git a/src/plugins/data/public/query/query_string/query_string_manager.ts b/src/plugins/data/public/query/query_string/query_string_manager.ts index b44628980ce0..723461224453 100644 --- a/src/plugins/data/public/query/query_string/query_string_manager.ts +++ b/src/plugins/data/public/query/query_string/query_string_manager.ts @@ -175,14 +175,14 @@ export class QueryStringManager { public getInitialQueryByDataset = (newDataset: Dataset) => { const curQuery = this.query$.getValue(); - const languageId = curQuery.language; + const languageId = newDataset.language || curQuery.language; const language = this.languageService.getLanguage(languageId); - const newQuery = { ...curQuery, dataset: newDataset }; - const input = language?.getQueryString(newQuery) || ''; + const newQuery = { ...curQuery, language: languageId, dataset: newDataset }; + const newQueryString = language?.getQueryString(newQuery) || ''; return { ...newQuery, - query: input, + query: newQueryString, }; }; diff --git a/src/plugins/data/public/ui/dataset_selector/configurator.tsx b/src/plugins/data/public/ui/dataset_selector/configurator.tsx index e59683dffa28..527d5f7e66c8 100644 --- a/src/plugins/data/public/ui/dataset_selector/configurator.tsx +++ b/src/plugins/data/public/ui/dataset_selector/configurator.tsx @@ -3,7 +3,6 @@ * SPDX-License-Identifier: Apache-2.0 */ -import React, { useEffect, useState } from 'react'; import { EuiButton, EuiButtonEmpty, @@ -19,8 +18,9 @@ import { } from '@elastic/eui'; import { i18n } from '@osd/i18n'; import { FormattedMessage } from '@osd/i18n/react'; +import React, { useEffect, useState } from 'react'; import { BaseDataset, Dataset, DatasetField } from '../../../common'; -import { getQueryService, getIndexPatterns } from '../../services'; +import { getIndexPatterns, getQueryService } from '../../services'; export const Configurator = ({ baseDataset, @@ -35,14 +35,20 @@ export const Configurator = ({ }) => { const queryService = getQueryService(); const queryString = queryService.queryString; + const languageService = queryService.queryString.getLanguageService(); const indexPatternsService = getIndexPatterns(); const type = queryString.getDatasetService().getType(baseDataset.type); const languages = type?.supportedLanguages(baseDataset) || []; - const [dataset, setDataset] = useState(baseDataset); const [timeFields, setTimeFields] = useState(); - const [timeField, setTimeField] = useState(dataset.timeFieldName); - const [language, setLanguage] = useState(languages[0]); + const [timeFieldName, setTimeFieldName] = useState(); + const [language, setLanguage] = useState(() => { + const currentLanguage = queryString.getQuery().language; + if (languages.includes(currentLanguage)) { + return currentLanguage; + } + return languages[0]; + }); useEffect(() => { const fetchFields = async () => { @@ -88,7 +94,7 @@ export const Configurator = ({ } )} > - + {timeFields && timeFields.length > 0 && ( { const value = e.target.value === 'undefined' ? undefined : e.target.value; - setTimeField(value); - setDataset({ ...dataset, timeFieldName: value }); + setTimeFieldName(value); }} /> @@ -127,7 +132,7 @@ export const Configurator = ({ > ({ - text: languageId, + text: languageService.getLanguage(languageId)?.title || languageId, value: languageId, }))} value={language} @@ -149,7 +154,7 @@ export const Configurator = ({ defaultMessage="Previous" /> - onConfirm(dataset)} fill> + onConfirm({ ...baseDataset, language, timeFieldName })} fill> { iconSize="s" onClick={onButtonClick} className="languageSelector__button" - iconType={'arrowDown'} + iconType="arrowDown" > {selectedLanguage.label}