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

Fix data source info is missing in default query when click Discover from other pages #8583

Merged
merged 15 commits into from
Nov 1, 2024
Merged
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/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
Loading