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

Added framework to get default query string using dataset and language combination #8896

Merged
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/8896.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Added framework to get default query string using dataset and language combination ([#8896](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8896))
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DatasetField,
DatasetSearchOptions,
DataStructure,
Query,
SavedObject,
} from '../../../../common';
import { IDataPluginServices } from '../../../types';
Expand Down Expand Up @@ -106,4 +107,8 @@ export interface DatasetTypeConfig {
* Service used for indexedViews related operations
*/
indexedViewsService?: DatasetIndexedViewsService;
/**
* Returns the initial query that is added to the query editor when a dataset is selected.
*/
getInitialQueryString?: (query: Query) => string | void;
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,37 @@ describe('QueryStringManager', () => {
expect(result.dataset).toEqual(currentDataset);
expect(result.query).toBeDefined();
});

test('getInitialQueryByLanguage returns the initial query from the dataset config if present', () => {
service.getDatasetService().getType = jest.fn().mockReturnValue({
supportedLanguages: jest.fn(),
getInitialQueryString: jest.fn().mockImplementation(({ language }) => {
switch (language) {
case 'sql':
return 'default sql dataset query';
case 'ppl':
return 'default ppl dataset query';
}
}),
});

const sqlQuery = service.getInitialQueryByLanguage('sql');
expect(sqlQuery).toHaveProperty('query', 'default sql dataset query');

const pplQuery = service.getInitialQueryByLanguage('ppl');
expect(pplQuery).toHaveProperty('query', 'default ppl dataset query');
});

test('getInitialQueryByLanguage returns the initial query from the language config if dataset does not provide one', () => {
service.getDatasetService().getType = jest.fn().mockReturnValue({
supportedLanguages: jest.fn(),
});
service.getLanguageService().getLanguage = jest.fn().mockReturnValue({
getQueryString: jest.fn().mockReturnValue('default-language-service-query'),
});

const sqlQuery = service.getInitialQueryByLanguage('sql');
expect(sqlQuery).toHaveProperty('query', 'default-language-service-query');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@
return this.storage.get('userQueryString') || '';
}

private getInitialDatasetQueryString(query: Query) {
const { language, dataset } = query;

Check warning on line 67 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L67

Added line #L67 was not covered by tests

const languageConfig = this.languageService.getLanguage(language);

Check warning on line 69 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L69

Added line #L69 was not covered by tests
let typeConfig;

if (dataset) {
typeConfig = this.datasetService.getType(dataset.type);

Check warning on line 73 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L73

Added line #L73 was not covered by tests
}

return (

Check warning on line 76 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L76

Added line #L76 was not covered by tests
typeConfig?.getInitialQueryString?.(query) ?? (languageConfig?.getQueryString(query) || '')
);
}

public getDefaultQuery(): Query {
const defaultLanguageId = this.getDefaultLanguage();
const defaultQuery = this.getDefaultQueryString();
Expand All @@ -79,13 +94,11 @@
defaultDataset &&
this.languageService
) {
const language = this.languageService.getLanguage(defaultLanguageId);
const newQuery = { ...query, dataset: defaultDataset };
const newQueryString = language?.getQueryString(newQuery) || '';

return {
...newQuery,
query: newQueryString,
query: this.getInitialDatasetQueryString(newQuery),
};
}

Expand Down Expand Up @@ -244,13 +257,12 @@

// Both language and dataset provided - generate fresh query
if (language && dataset) {
const languageService = this.languageService.getLanguage(language);
const newQuery = {
language,
dataset,
query: '',
};
newQuery.query = languageService?.getQueryString(newQuery) || '';
newQuery.query = this.getInitialDatasetQueryString(newQuery);

Check warning on line 265 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L265

Added line #L265 was not covered by tests
return newQuery;
}

Expand All @@ -274,12 +286,12 @@
*/
public getInitialQueryByLanguage = (languageId: string) => {
const curQuery = this.query$.getValue();
const language = this.languageService.getLanguage(languageId);
const newQuery = {
...curQuery,
language: languageId,
};
const queryString = language?.getQueryString(newQuery) || '';

const queryString = this.getInitialDatasetQueryString(newQuery);

Check warning on line 294 in src/plugins/data/public/query/query_string/query_string_manager.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/query/query_string/query_string_manager.ts#L294

Added line #L294 was not covered by tests
this.languageService.setUserQueryString(queryString);

return {
Expand All @@ -296,17 +308,15 @@
const curQuery = this.query$.getValue();
// Use dataset's preferred language or fallback to current language
const languageId = newDataset.language || curQuery.language;
const language = this.languageService.getLanguage(languageId);
const newQuery = {
...curQuery,
language: languageId,
dataset: newDataset,
};
const queryString = language?.getQueryString(newQuery) || '';

return {
...newQuery,
query: queryString,
query: this.getInitialDatasetQueryString(newQuery, newDataset),
};
};

Expand Down
Loading