From fb73ff2cc3bcd1f6b0825dda98b1415fec325fcd Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 1 Nov 2024 23:18:41 +0000 Subject: [PATCH] Fix data source info is missing in default query when click Discover from other pages (#8583) When we click Discover sidebar from other pages, we don't fetch the data source info and enrich the default dataset, so that some unexpected behavior happens. Issues Resolved #8582 Signed-off-by: gaobinlong (cherry picked from commit 97f4734181c72fa90b4bd8390f06a0f6fac7566d) Signed-off-by: github-actions[bot] --- changelogs/fragments/8583.yml | 2 + src/plugins/data/public/mocks.ts | 12 ++++- .../dataset_service/dataset_service.test.ts | 49 +++++++++++++++++++ .../dataset_service/dataset_service.ts | 13 +++++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 changelogs/fragments/8583.yml diff --git a/changelogs/fragments/8583.yml b/changelogs/fragments/8583.yml new file mode 100644 index 000000000000..1acb9c52d43a --- /dev/null +++ b/changelogs/fragments/8583.yml @@ -0,0 +1,2 @@ +fix: +- Fix data source info is missing in default query when click Discover from other pages ([#8583](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8583)) diff --git a/src/plugins/data/public/mocks.ts b/src/plugins/data/public/mocks.ts index d838c983665d..f0dc11ca9e54 100644 --- a/src/plugins/data/public/mocks.ts +++ b/src/plugins/data/public/mocks.ts @@ -83,7 +83,17 @@ const createStartContract = (isEnhancementsEnabled: boolean = false): Start => { fetchForWildcard: jest.fn(), }, }), - get: jest.fn().mockReturnValue(Promise.resolve({})), + get: jest.fn().mockReturnValue( + Promise.resolve({ + id: 'id', + name: 'name', + dataSourceRef: { + id: 'id', + type: 'datasource', + name: 'datasource', + }, + }) + ), getDefault: jest.fn().mockReturnValue( Promise.resolve({ name: 'Default name', diff --git a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.test.ts b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.test.ts index 014a4acbc0b5..43dcc78e9bc3 100644 --- a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.test.ts +++ b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.test.ts @@ -11,6 +11,7 @@ import { IDataPluginServices } from '../../../types'; import { indexPatternTypeConfig } from './lib'; import { dataPluginMock } from '../../../mocks'; import { IndexPatternsContract } from '../../..'; +import { waitFor } from '@testing-library/dom'; describe('DatasetService', () => { let service: DatasetService; @@ -182,4 +183,52 @@ describe('DatasetService', () => { } expect(service.getRecentDatasets().length).toEqual(4); }); + + test('test get default dataset ', async () => { + jest.clearAllMocks(); + uiSettings = coreMock.createSetup().uiSettings; + uiSettings.get = jest.fn().mockImplementation((setting: string) => { + if (setting === UI_SETTINGS.SEARCH_MAX_RECENT_DATASETS) { + return 4; + } else if (setting === 'defaultIndex') { + return 'id'; + } else if (setting === UI_SETTINGS.QUERY_ENHANCEMENTS_ENABLED) { + return true; + } + }); + sessionStorage = new DataStorage(window.sessionStorage, 'opensearchDashboards.'); + mockDataPluginServices = {} as jest.Mocked; + service = new DatasetService(uiSettings, sessionStorage); + indexPatterns = { + ...dataPluginMock.createStartContract().indexPatterns, + getDataSource: jest.fn().mockReturnValue( + Promise.resolve({ + id: 'id', + attributes: { + title: 'datasource', + dataSourceEngineType: 'OpenSearch', + }, + }) + ), + }; + service.init(indexPatterns); + + await waitFor(() => { + expect(service.getDefault()?.dataSource).toMatchObject({ + id: 'id', + title: 'datasource', + type: 'OpenSearch', + }); + }); + + indexPatterns = { + ...dataPluginMock.createStartContract().indexPatterns, + getDataSource: jest.fn().mockReturnValue(Promise.resolve()), + }; + service.init(indexPatterns); + + await waitFor(() => { + expect(service.getDefault()?.dataSource).toBe(undefined); + }); + }); }); diff --git a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts index 5012055d1b18..55d77c2ccc36 100644 --- a/src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts +++ b/src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts @@ -256,6 +256,11 @@ export class DatasetService { return undefined; } + let dataSource; + if (indexPattern.dataSourceRef) { + dataSource = await this.indexPatterns?.getDataSource(indexPattern.dataSourceRef?.id); + } + const dataType = this.typesRegistry.get(DEFAULT_DATA.SET_TYPES.INDEX_PATTERN); if (dataType) { const dataset = dataType.toDataset([ @@ -263,8 +268,16 @@ export class DatasetService { id: indexPattern.id, title: indexPattern.title, type: DEFAULT_DATA.SET_TYPES.INDEX_PATTERN, + parent: dataSource + ? { + id: dataSource.id, + title: dataSource.attributes?.title, + type: dataSource.attributes?.dataSourceEngineType, + } + : undefined, }, ]); + return { ...dataset, timeFieldName: indexPattern.timeFieldName }; }