Skip to content

Commit

Permalink
Fix data source info is missing in default query when click Discover …
Browse files Browse the repository at this point in the history
…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 <[email protected]>
  • Loading branch information
gaobinlong authored Nov 1, 2024
1 parent affd265 commit 97f4734
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelogs/fragments/8583.yml
Original file line number Diff line number Diff line change
@@ -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))
12 changes: 11 additions & 1 deletion src/plugins/data/public/mocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<IDataPluginServices>;
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);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,28 @@ 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([
{
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 };
}

Expand Down

0 comments on commit 97f4734

Please sign in to comment.