From 6742fea09c03d1d93d9f2ad06a9325c076dec5d8 Mon Sep 17 00:00:00 2001 From: Kawika Avilla Date: Sun, 28 Apr 2024 06:02:28 +0000 Subject: [PATCH] clean up Signed-off-by: Kawika Avilla --- src/plugins/data/common/data_frames/types.ts | 6 +++ src/plugins/data/common/data_frames/utils.ts | 7 ++- .../search_source/fetch/get_search_params.ts | 40 ++++++++++++++-- .../search/search_source/fetch/index.ts | 7 ++- .../search/search_source/search_source.ts | 33 ++++++++----- src/plugins/data/common/types.ts | 4 ++ .../data/public/search/search_service.ts | 47 +++++++++++-------- src/plugins/data/public/search/types.ts | 2 + .../query_string_input/language_switcher.tsx | 4 ++ .../data/server/search/search_service.ts | 44 +++++++++-------- .../utils/update_search_source.ts | 9 ++-- 11 files changed, 143 insertions(+), 60 deletions(-) diff --git a/src/plugins/data/common/data_frames/types.ts b/src/plugins/data/common/data_frames/types.ts index 730055dd7efd..a1d935a2863d 100644 --- a/src/plugins/data/common/data_frames/types.ts +++ b/src/plugins/data/common/data_frames/types.ts @@ -13,6 +13,12 @@ export enum DATA_FRAME_TYPES { POLLING = 'data_frame_polling', } +export interface DataFrameService { + get: () => IDataFrame | undefined; + set: (dataFrame: IDataFrame) => Promise; + clear: () => void; +} + export interface IDataFrame { type?: DATA_FRAME_TYPES.DEFAULT; name?: string; diff --git a/src/plugins/data/common/data_frames/utils.ts b/src/plugins/data/common/data_frames/utils.ts index d218c704c4d1..c9ccfb4ee2f0 100644 --- a/src/plugins/data/common/data_frames/utils.ts +++ b/src/plugins/data/common/data_frames/utils.ts @@ -22,6 +22,10 @@ export interface IDataFrameResponse extends SearchResponse { took: number; } +export const getRawDataFrame = (searchRequest: IOpenSearchDashboardsSearchRequest) => { + return searchRequest.params?.body?.df; +}; + export const getRawQueryString = ( searchRequest: IOpenSearchDashboardsSearchRequest ): string | undefined => { @@ -63,8 +67,7 @@ export const convertResult = (response: IDataFrameResponse): SearchResponse hit[field.name] = field.values[index]; }); hits.push({ - _index: data.name ?? '', - _id: '', + _index: data.name, _source: hit, }); } diff --git a/src/plugins/data/common/search/search_source/fetch/get_search_params.ts b/src/plugins/data/common/search/search_source/fetch/get_search_params.ts index a25d6e530bad..d9bd7721d6cb 100644 --- a/src/plugins/data/common/search/search_source/fetch/get_search_params.ts +++ b/src/plugins/data/common/search/search_source/fetch/get_search_params.ts @@ -29,7 +29,7 @@ */ import { UI_SETTINGS } from '../../../constants'; -import { GetConfigFn } from '../../../types'; +import { GetConfigFn, GetDataFrameFn, DestroyDataFrameFn } from '../../../types'; import { ISearchRequestParams } from '../../index'; import { SearchRequest } from './types'; @@ -49,16 +49,50 @@ export function getPreference(getConfig: GetConfigFn) { : undefined; } +export function getExternalSearchParamsFromRequest( + searchRequest: SearchRequest, + dependencies: { + getConfig: GetConfigFn; + getDataFrame: GetDataFrameFn; + } +): ISearchRequestParams { + const { getConfig, getDataFrame } = dependencies; + const searchParams = getSearchParams(getConfig); + const dataFrame = getDataFrame(); + const indexTitle = searchRequest.index.title || searchRequest.index; + + return { + index: indexTitle, + body: { + ...searchRequest.body, + ...(dataFrame && dataFrame?.name === indexTitle ? { df: dataFrame } : {}), + }, + ...searchParams, + }; +} + /** @public */ // TODO: Could provide this on runtime contract with dependencies // already wired up. export function getSearchParamsFromRequest( searchRequest: SearchRequest, - dependencies: { getConfig: GetConfigFn } + dependencies: { + getConfig: GetConfigFn; + getDataFrame?: GetDataFrameFn; + destroyDataFrame?: DestroyDataFrameFn; + } ): ISearchRequestParams { - const { getConfig } = dependencies; + const { getConfig, getDataFrame, destroyDataFrame } = dependencies; const searchParams = getSearchParams(getConfig); + if (getDataFrame && destroyDataFrame) { + if (getDataFrame()) { + delete searchRequest.body.df; + delete searchRequest.indexType; + destroyDataFrame(); + } + } + return { index: searchRequest.index.title || searchRequest.index, body: searchRequest.body, diff --git a/src/plugins/data/common/search/search_source/fetch/index.ts b/src/plugins/data/common/search/search_source/fetch/index.ts index bb432ec0d833..f171ca38738e 100644 --- a/src/plugins/data/common/search/search_source/fetch/index.ts +++ b/src/plugins/data/common/search/search_source/fetch/index.ts @@ -28,6 +28,11 @@ * under the License. */ -export { getSearchParams, getSearchParamsFromRequest, getPreference } from './get_search_params'; +export { + getSearchParams, + getExternalSearchParamsFromRequest, + getSearchParamsFromRequest, + getPreference, +} from './get_search_params'; export { RequestFailure } from './request_error'; export * from './types'; diff --git a/src/plugins/data/common/search/search_source/search_source.ts b/src/plugins/data/common/search/search_source/search_source.ts index 57968f47c11a..f086984cac33 100644 --- a/src/plugins/data/common/search/search_source/search_source.ts +++ b/src/plugins/data/common/search/search_source/search_source.ts @@ -91,7 +91,13 @@ import { DATA_FRAME_TYPES, IDataFrame, IDataFrameResponse, convertResult } from import { IOpenSearchSearchRequest, IOpenSearchSearchResponse, ISearchOptions } from '../..'; import { IOpenSearchDashboardsSearchRequest, IOpenSearchDashboardsSearchResponse } from '../types'; import { ISearchSource, SearchSourceOptions, SearchSourceFields } from './types'; -import { FetchHandlers, RequestFailure, getSearchParamsFromRequest, SearchRequest } from './fetch'; +import { + FetchHandlers, + RequestFailure, + getExternalSearchParamsFromRequest, + getSearchParamsFromRequest, + SearchRequest, +} from './fetch'; import { getOpenSearchQueryConfig, @@ -286,17 +292,26 @@ export class SearchSource { /** * Set the data frame of this SearchSource + * + * @async * @return {undefined|IDataFrame} */ async setDataFrame(dataFrame: IDataFrame | undefined) { if (dataFrame) { await this.dependencies.df.set(dataFrame); } else { - this.dependencies.df.clear(); + this.destroyDataFrame(); } return this.getDataFrame(); } + /** + * Clear the data frame of this SearchSource + */ + destroyDataFrame() { + this.dependencies.df.clear(); + } + /** * Fetch this source and reject the returned Promise on error * @@ -367,13 +382,10 @@ export class SearchSource { private fetchSearch(searchRequest: SearchRequest, options: ISearchOptions) { const { search, getConfig, onResponse } = this.dependencies; - if (this.getDataFrame()) { - delete searchRequest.body!.df; - this.setDataFrame(undefined); - } - const params = getSearchParamsFromRequest(searchRequest, { getConfig, + getDataFrame: this.getDataFrame.bind(this), + destroyDataFrame: this.destroyDataFrame.bind(this), }); return search( @@ -389,14 +401,11 @@ export class SearchSource { private async fetchExternalSearch(searchRequest: SearchRequest, options: ISearchOptions) { const { search, getConfig, onResponse } = this.dependencies; - const params = getSearchParamsFromRequest(searchRequest, { + const params = getExternalSearchParamsFromRequest(searchRequest, { getConfig, + getDataFrame: this.getDataFrame.bind(this), }); - if (this.getDataFrame() && this.getDataFrame()!.name === searchRequest.index) { - params.body!.df = this.getDataFrame(); - } - return search({ params }, options).then(async (response: any) => { if (response.hasOwnProperty('type')) { if ((response as IDataFrameResponse).type === DATA_FRAME_TYPES.DEFAULT) { diff --git a/src/plugins/data/common/types.ts b/src/plugins/data/common/types.ts index fba5473624dc..0a87a8c97357 100644 --- a/src/plugins/data/common/types.ts +++ b/src/plugins/data/common/types.ts @@ -28,6 +28,8 @@ * under the License. */ +import { IDataFrame } from './data_frames'; + export * from './query/types'; export * from './osd_field_types/types'; export * from './index_patterns/types'; @@ -44,3 +46,5 @@ export * from './data_frames/types'; * not possible. */ export type GetConfigFn = (key: string, defaultOverride?: T) => T; +export type GetDataFrameFn = () => IDataFrame | undefined; +export type DestroyDataFrameFn = () => void; diff --git a/src/plugins/data/public/search/search_service.ts b/src/plugins/data/public/search/search_service.ts index 23589746da3b..340c007963af 100644 --- a/src/plugins/data/public/search/search_service.ts +++ b/src/plugins/data/public/search/search_service.ts @@ -58,11 +58,12 @@ import { } from '../../common/search/aggs/buckets/shard_delay'; import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn'; import { + DataFrameService, IDataFrame, IDataFrameResponse, createDataFrameCache, dataFrameToSpec, -} from '../../common'; +} from '../../common/data_frames'; /** @internal */ export interface SearchServiceSetupDependencies { @@ -138,6 +139,29 @@ export class SearchService implements Plugin { const loadingCount$ = new BehaviorSubject(0); http.addLoadingCountSource(loadingCount$); + const dfService: DataFrameService = { + get: () => this.dfCache.get(), + set: async (dataFrame: IDataFrame) => { + if (this.dfCache.get() && this.dfCache.get()?.name !== dataFrame.name) { + indexPatterns.clearCache(this.dfCache.get()!.name, false); + } + this.dfCache.set(dataFrame); + const existingIndexPattern = indexPatterns.getByTitle(dataFrame.name!, true); + const dataSet = await indexPatterns.create( + dataFrameToSpec(dataFrame, existingIndexPattern?.id), + !existingIndexPattern?.id + ); + // save to cache by title because the id is not unique for temporary index pattern created + indexPatterns.saveToCache(dataSet.title, dataSet); + }, + clear: () => { + if (this.dfCache.get() === undefined) return; + // name because the id is not unique for temporary index pattern created + indexPatterns.clearCache(this.dfCache.get()!.name, false); + this.dfCache.clear(); + }, + }; + const searchSourceDependencies: SearchSourceDependencies = { getConfig: uiSettings.get.bind(uiSettings), search: < @@ -156,25 +180,7 @@ export class SearchService implements Plugin { callMsearch: getCallMsearch({ http }), loadingCount$, }, - df: { - get: () => this.dfCache.get(), - set: async (dataFrame: IDataFrame) => { - this.dfCache.set(dataFrame); - const existingIndexPattern = indexPatterns.getByTitle(dataFrame.name!, true); - const dataSet = await indexPatterns.create( - dataFrameToSpec(dataFrame, existingIndexPattern?.id), - !existingIndexPattern?.id - ); - // save to cache by title because the id is not unique for temporary index pattern created - indexPatterns.saveToCache(dataSet.title, dataSet); - }, - clear: () => { - if (this.dfCache.get() === undefined) return; - // name because the id is not unique for temporary index pattern created - indexPatterns.clearCache(this.dfCache.get()!.name, false); - this.dfCache.clear(); - }, - }, + df: dfService, }; return { @@ -188,6 +194,7 @@ export class SearchService implements Plugin { this.searchInterceptor = enhancements.searchInterceptor; }, getDefaultSearchInterceptor: () => this.defaultSearchInterceptor, + df: dfService, }; } diff --git a/src/plugins/data/public/search/types.ts b/src/plugins/data/public/search/types.ts index 679c6624bad4..29dc37b41c91 100644 --- a/src/plugins/data/public/search/types.ts +++ b/src/plugins/data/public/search/types.ts @@ -35,6 +35,7 @@ import { AggsSetup, AggsSetupDependencies, AggsStartDependencies, AggsStart } fr import { ISearchGeneric, ISearchStartSearchSource } from '../../common/search'; import { IndexPatternsContract } from '../../common/index_patterns/index_patterns'; import { UsageCollectionSetup } from '../../../usage_collection/public'; +import { DataFrameService } from '../../common/data_frames'; export { ISearchStartSearchSource }; @@ -80,6 +81,7 @@ export interface ISearchStart { searchSource: ISearchStartSearchSource; __enhance: (enhancements: SearchEnhancements) => void; getDefaultSearchInterceptor: () => ISearchInterceptor; + df: DataFrameService; } export { SEARCH_EVENT_TYPE } from './collectors'; diff --git a/src/plugins/data/public/ui/query_string_input/language_switcher.tsx b/src/plugins/data/public/ui/query_string_input/language_switcher.tsx index f3ee1f0fb916..877041369d4f 100644 --- a/src/plugins/data/public/ui/query_string_input/language_switcher.tsx +++ b/src/plugins/data/public/ui/query_string_input/language_switcher.tsx @@ -86,6 +86,10 @@ export function QueryLanguageSwitcher(props: Props) { ? queryEnhancement.search : searchService.getDefaultSearchInterceptor(), }); + + if (!queryEnhancement) { + searchService.df.clear(); + } uiService.Settings.setUiOverridesByUserQueryLanguage(queryLanguage); }; diff --git a/src/plugins/data/server/search/search_service.ts b/src/plugins/data/server/search/search_service.ts index 54c1716129d2..2eef461b94da 100644 --- a/src/plugins/data/server/search/search_service.ts +++ b/src/plugins/data/server/search/search_service.ts @@ -74,6 +74,7 @@ import { import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn'; import { ConfigSchema } from '../../config'; import { + DataFrameService, IDataFrame, IDataFrameResponse, createDataFrameCache, @@ -211,6 +212,29 @@ export class SearchService implements Plugin { searchSourceRequiredUiSettings ); + const dfService: DataFrameService = { + get: () => this.dfCache.get(), + set: async (dataFrame: IDataFrame) => { + if (this.dfCache.get() && this.dfCache.get()?.name !== dataFrame.name) { + scopedIndexPatterns.clearCache(this.dfCache.get()!.name, false); + } + this.dfCache.set(dataFrame); + const existingIndexPattern = scopedIndexPatterns.getByTitle(dataFrame.name!, true); + const dataSet = await scopedIndexPatterns.create( + dataFrameToSpec(dataFrame, existingIndexPattern?.id), + !existingIndexPattern?.id + ); + // save to cache by title because the id is not unique for temporary index pattern created + scopedIndexPatterns.saveToCache(dataSet.title, dataSet); + }, + clear: () => { + if (this.dfCache.get() === undefined) return; + // name because the id is not unique for temporary index pattern created + scopedIndexPatterns.clearCache(this.dfCache.get()!.name, false); + this.dfCache.clear(); + }, + }; + const searchSourceDependencies: SearchSourceDependencies = { getConfig: (key: string): T => uiSettingsCache[key], search: (searchRequest, options) => { @@ -245,25 +269,7 @@ export class SearchService implements Plugin { }), loadingCount$: new BehaviorSubject(0), }, - df: { - get: () => this.dfCache.get(), - set: async (dataFrame: IDataFrame) => { - this.dfCache.set(dataFrame); - const existingIndexPattern = scopedIndexPatterns.getByTitle(dataFrame.name!, true); - const dataSet = await scopedIndexPatterns.create( - dataFrameToSpec(dataFrame, existingIndexPattern?.id), - !existingIndexPattern?.id - ); - // save to cache by title because the id is not unique for temporary index pattern created - scopedIndexPatterns.saveToCache(dataSet.title, dataSet); - }, - clear: () => { - if (this.dfCache.get() === undefined) return; - // name because the id is not unique for temporary index pattern created - scopedIndexPatterns.clearCache(this.dfCache.get()!.name, false); - this.dfCache.clear(); - }, - }, + df: dfService, }; return this.searchSourceService.start(scopedIndexPatterns, searchSourceDependencies); diff --git a/src/plugins/discover/public/application/view_components/utils/update_search_source.ts b/src/plugins/discover/public/application/view_components/utils/update_search_source.ts index 9fd021d69d29..a8480fdad18a 100644 --- a/src/plugins/discover/public/application/view_components/utils/update_search_source.ts +++ b/src/plugins/discover/public/application/view_components/utils/update_search_source.ts @@ -31,12 +31,15 @@ export const updateSearchSource = async ({ }: Props) => { const { uiSettings, data } = services; let dataSet = indexPattern; + const dataFrame = searchSource?.getDataFrame(); if ( searchSource && - searchSource.getDataFrame() && - dataSet.title !== searchSource.getDataFrame()?.name + dataFrame && + dataFrame.name && + dataFrame.name !== '' && + dataSet.title !== dataFrame.name ) { - dataSet = data.indexPatterns.getByTitle(searchSource.getDataFrame()?.name!, true) ?? dataSet; + dataSet = data.indexPatterns.getByTitle(dataFrame.name, true) ?? dataSet; searchSource.setField('index', dataSet); }