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

[Discover-next] dataframes respects datasources #7092

Closed
wants to merge 3 commits into from
Closed
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/7092.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Onboard dataframes to MDS ([#7092](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7092))
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"start": "scripts/use_node scripts/opensearch_dashboards --dev",
"start:docker": "scripts/use_node scripts/opensearch_dashboards --dev --opensearch.hosts=$OPENSEARCH_HOSTS --opensearch.ignoreVersionMismatch=true --server.host=$SERVER_HOST",
"start:security": "scripts/use_node scripts/opensearch_dashboards --dev --security",
"start:enhancements": "scripts/use_node scripts/opensearch_dashboards --dev --data.enhancements.enabled=true --uiSettings.overrides['query:enhancements:enabled']=true --uiSettings.overrides['query:dataSource:readOnly']=false",
"start:enhancements": "scripts/use_node scripts/opensearch_dashboards --dev --data.enhancements.enabled=true --data_source.enabled=true --uiSettings.overrides['query:enhancements:enabled']=true --uiSettings.overrides['query:dataSource:readOnly']=false",
"debug": "scripts/use_node --nolazy --inspect scripts/opensearch_dashboards --dev",
"debug-break": "scripts/use_node --nolazy --inspect-brk scripts/opensearch_dashboards --dev",
"lint": "yarn run lint:es && yarn run lint:style",
Expand Down
24 changes: 24 additions & 0 deletions src/plugins/data/common/data_frames/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,29 @@
);
};

/**
* Parses a raw query string and extracts the query string and data source.
* @param rawQueryString - The raw query string to parse.
* @returns An object containing the parsed query string and data source (if found).
*/
export const parseRawQueryString = (rawQueryString: string) => {
const rawDataSource = rawQueryString.match(/::(.*?)::/);
return {

Check warning on line 55 in src/plugins/data/common/data_frames/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/utils.ts#L54-L55

Added lines #L54 - L55 were not covered by tests
qs: rawQueryString.replace(/::.*?::/, ''),
formattedQs(key: string = '.'): string {
const parts = rawQueryString.split('::');

Check warning on line 58 in src/plugins/data/common/data_frames/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/utils.ts#L58

Added line #L58 was not covered by tests
if (parts.length > 1) {
return (parts.slice(0, 1).join('') + parts.slice(1).join(key)).replace(

Check warning on line 60 in src/plugins/data/common/data_frames/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/utils.ts#L60

Added line #L60 was not covered by tests
new RegExp(key + '$'),
''
);
}
return rawQueryString;

Check warning on line 65 in src/plugins/data/common/data_frames/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/utils.ts#L65

Added line #L65 was not covered by tests
},
...(rawDataSource && { dataSource: rawDataSource[1] }),
};
};

/**
* Returns the raw aggregations from the search request.
*
Expand Down Expand Up @@ -379,6 +402,7 @@
getAggQsFn: GetDataFrameAggQsFn;
}) => {
dataFrame.meta = {
...dataFrame.meta,
aggs: aggConfig,
aggsQs: {
[aggConfig.id]: getAggQsFn({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,25 @@
return await this.savedObjectsClient.get<DataSourceAttributes>('data-source', id);
};

/**
* Finds a data source by its title.
*
* @param title - The title of the data source to find.
* @param size - The number of results to return. Defaults to 10.
* @returns The first matching data source or undefined if not found.
*/
findDataSourceByTitle = async (title: string, size: number = 10) => {
const savedObjectsResponse = await this.savedObjectsClient.find<DataSourceAttributes>({

Check warning on line 152 in src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/index_patterns/index_patterns/index_patterns.ts#L152

Added line #L152 was not covered by tests
type: 'data-source',
fields: ['title'],
search: title,
searchFields: ['title'],
perPage: size,
});

return savedObjectsResponse[0] || undefined;
};

/**
* Get list of index pattern ids
* @param refresh Force refresh of index pattern list
Expand Down
35 changes: 34 additions & 1 deletion src/plugins/data/common/search/search_source/search_source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,15 @@
import { filterDocvalueFields } from './filter_docvalue_fields';
import { fieldWildcardFilter } from '../../../../opensearch_dashboards_utils/common';
import { IIndexPattern } from '../../index_patterns';
import { DATA_FRAME_TYPES, IDataFrame, IDataFrameResponse, convertResult } from '../../data_frames';
import {
DATA_FRAME_TYPES,
IDataFrame,
IDataFrameResponse,
convertResult,
createDataFrame,
getRawQueryString,
parseRawQueryString,
} from '../../data_frames';
import { IOpenSearchSearchRequest, IOpenSearchSearchResponse, ISearchOptions } from '../..';
import { IOpenSearchDashboardsSearchRequest, IOpenSearchDashboardsSearchResponse } from '../types';
import { ISearchSource, SearchSourceOptions, SearchSourceFields } from './types';
Expand Down Expand Up @@ -305,6 +313,23 @@
return this.getDataFrame();
}

/**
* Create and set the data frame of this SearchSource
*
* @async
* @return {undefined|IDataFrame}
*/
async createDataFrame(searchRequest: SearchRequest) {
const rawQueryString = this.getRawQueryStringFromRequest(searchRequest);
const dataFrame = createDataFrame({

Check warning on line 324 in src/plugins/data/common/search/search_source/search_source.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/search/search_source/search_source.ts#L323-L324

Added lines #L323 - L324 were not covered by tests
name: searchRequest.index.title || searchRequest.index,
fields: [],
...(rawQueryString && { meta: { queryConfig: parseRawQueryString(rawQueryString) } }),
});
await this.setDataFrame(dataFrame);
return this.getDataFrame();

Check warning on line 330 in src/plugins/data/common/search/search_source/search_source.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/search/search_source/search_source.ts#L329-L330

Added lines #L329 - L330 were not covered by tests
}

/**
* Clear the data frame of this SearchSource
*/
Expand Down Expand Up @@ -401,6 +426,10 @@
private async fetchExternalSearch(searchRequest: SearchRequest, options: ISearchOptions) {
const { search, getConfig, onResponse } = this.dependencies;

if (!this.getDataFrame()) {
await this.createDataFrame(searchRequest);

Check warning on line 430 in src/plugins/data/common/search/search_source/search_source.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/search/search_source/search_source.ts#L430

Added line #L430 was not covered by tests
}

const params = getExternalSearchParamsFromRequest(searchRequest, {
getConfig,
getDataFrame: this.getDataFrame.bind(this),
Expand Down Expand Up @@ -444,6 +473,10 @@
return request.body!.query.hasOwnProperty('type') && request.body!.query.type === 'unsupported';
}

private getRawQueryStringFromRequest(request: SearchRequest): string | undefined {
return getRawQueryString({ params: request });

Check warning on line 477 in src/plugins/data/common/search/search_source/search_source.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/search/search_source/search_source.ts#L477

Added line #L477 was not covered by tests
}

/**
* Called by requests of this search source when they are started
* @param options
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,16 @@
if (this.dfCache.get() && this.dfCache.get()?.name !== dataFrame.name) {
indexPatterns.clearCache(this.dfCache.get()!.name, false);
}
if (
dataFrame.meta &&
dataFrame.meta.queryConfig &&
'dataSource' in dataFrame.meta.queryConfig
) {
const dataSource = await indexPatterns.findDataSourceByTitle(

Check warning on line 153 in src/plugins/data/public/search/search_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/search/search_service.ts#L153

Added line #L153 was not covered by tests
dataFrame.meta.queryConfig.dataSource
);
dataFrame.meta.queryConfig.dataSourceId = dataSource?.id;

Check warning on line 156 in src/plugins/data/public/search/search_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/search/search_service.ts#L156

Added line #L156 was not covered by tests
}
this.dfCache.set(dataFrame);
const existingIndexPattern = indexPatterns.getByTitle(dataFrame.name!, true);
const dataSet = await indexPatterns.create(
Expand Down
10 changes: 10 additions & 0 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@
if (this.dfCache.get() && this.dfCache.get()?.name !== dataFrame.name) {
scopedIndexPatterns.clearCache(this.dfCache.get()!.name, false);
}
if (
dataFrame.meta &&
dataFrame.meta.queryConfig &&
'dataSource' in dataFrame.meta.queryConfig
) {
const dataSource = await scopedIndexPatterns.findDataSourceByTitle(

Check warning on line 226 in src/plugins/data/server/search/search_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/server/search/search_service.ts#L226

Added line #L226 was not covered by tests
dataFrame.meta.queryConfig.dataSource
);
dataFrame.meta.queryConfig.dataSourceId = dataSource?.id;

Check warning on line 229 in src/plugins/data/server/search/search_service.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/server/search/search_service.ts#L229

Added line #L229 was not covered by tests
}
this.dfCache.set(dataFrame);
const existingIndexPattern = scopedIndexPatterns.getByTitle(dataFrame.name!, true);
const dataSet = await scopedIndexPatterns.create(
Expand Down
Loading