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 10 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
2 changes: 1 addition & 1 deletion src/plugins/data/public/query/query_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
indexPatterns,
application,
}: QueryServiceStartDependencies): IQueryStart {
this.queryStringManager.getDatasetService().init(indexPatterns);
this.queryStringManager.getDatasetService().init(indexPatterns, savedObjectsClient);

Check warning on line 104 in src/plugins/data/public/query/query_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_service.ts#L104

Added line #L104 was not covered by tests
return {
addToQueryLog: createAddToQueryLog({
storage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ import { IDataPluginServices } from '../../../types';
import { indexPatternTypeConfig } from './lib';
import { dataPluginMock } from '../../../mocks';
import { IndexPatternsContract } from '../../..';
import { SavedObjectsClientContract } from 'opensearch-dashboards/public';
import { waitFor } from '@testing-library/dom';

describe('DatasetService', () => {
let service: DatasetService;
let uiSettings: ReturnType<typeof coreMock.createSetup>['uiSettings'];
let sessionStorage: DataStorage;
let mockDataPluginServices: jest.Mocked<IDataPluginServices>;
let indexPatterns: IndexPatternsContract;
let savedObjectsClient: jest.Mocked<SavedObjectsClientContract>;

beforeEach(() => {
uiSettings = coreMock.createSetup().uiSettings;
Expand All @@ -30,7 +33,8 @@ describe('DatasetService', () => {
mockDataPluginServices = {} as jest.Mocked<IDataPluginServices>;
service = new DatasetService(uiSettings, sessionStorage);
indexPatterns = dataPluginMock.createStartContract().indexPatterns;
service.init(indexPatterns);
savedObjectsClient = (undefined as unknown) as jest.Mocked<SavedObjectsClientContract>;
service.init(indexPatterns, savedObjectsClient);
});

const mockResult = {
Expand Down Expand Up @@ -182,4 +186,61 @@ describe('DatasetService', () => {
}
expect(service.getRecentDatasets().length).toEqual(4);
});

test('test empty savedObjectsClient', () => {
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';
}
});
sessionStorage = new DataStorage(window.sessionStorage, 'opensearchDashboards.');
mockDataPluginServices = {} as jest.Mocked<IDataPluginServices>;
service = new DatasetService(uiSettings, sessionStorage);
indexPatterns = dataPluginMock.createStartContract().indexPatterns;
savedObjectsClient = (undefined as unknown) as jest.Mocked<SavedObjectsClientContract>;
service.init(indexPatterns, savedObjectsClient);

expect(service.getDefault()?.dataSource).toEqual(undefined);
});

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;
const mockedDataSource = {
id: 'id',
attributes: {
title: 'datasource',
dataSourceEngineType: 'OpenSearch',
},
};
savedObjectsClient = ({
get: jest.fn().mockReturnValue(Promise.resolve(mockedDataSource)),
} as unknown) as jest.Mocked<SavedObjectsClientContract>;
service.init(indexPatterns, savedObjectsClient);

await waitFor(() => {
expect(service.getDefault()?.dataSource).toMatchObject({
id: 'id',
title: 'datasource',
type: 'OpenSearch',
});
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { CoreStart } from 'opensearch-dashboards/public';
import { CoreStart, SavedObjectsClientContract } from 'opensearch-dashboards/public';
import LRUCache from 'lru-cache';
import {
Dataset,
Expand All @@ -24,6 +24,7 @@
private defaultDataset?: Dataset;
private typesRegistry: Map<string, DatasetTypeConfig> = new Map();
private recentDatasets: LRUCache<string, Dataset>;
private savedObjectsClient?: SavedObjectsClientContract;

constructor(
private readonly uiSettings: CoreStart['uiSettings'],
Expand All @@ -46,8 +47,12 @@
this.registerType(indexTypeConfig);
}

public async init(indexPatterns: IndexPatternsContract): Promise<void> {
public async init(
indexPatterns: IndexPatternsContract,
savedObjectsClient: SavedObjectsClientContract
): Promise<void> {
this.indexPatterns = indexPatterns;
this.savedObjectsClient = savedObjectsClient;

Check warning on line 55 in src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts#L55

Added line #L55 was not covered by tests
this.defaultDataset = await this.fetchDefaultDataset();
}

Expand Down Expand Up @@ -227,15 +232,31 @@
return undefined;
}

let dataSource;
if (indexPattern.dataSourceRef) {
dataSource = await this.savedObjectsClient?.get(

Check warning on line 237 in src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/dataset_service/dataset_service.ts#L237

Added line #L237 was not covered by tests
gaobinlong marked this conversation as resolved.
Show resolved Hide resolved
indexPattern.dataSourceRef?.type,
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