Skip to content

Commit

Permalink
Basic data frames cache implementation
Browse files Browse the repository at this point in the history
Signed-off-by: abbyhu2000 <[email protected]>
  • Loading branch information
abbyhu2000 committed Jun 21, 2024
1 parent 4f54049 commit 2b580fd
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 91 deletions.
32 changes: 18 additions & 14 deletions src/plugins/data/common/data_frames/_df_cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,28 @@

import { IDataFrame } from '..';

export interface DfCache {
get: () => IDataFrame | undefined;
set: (value: IDataFrame) => IDataFrame;
clear: () => void;
export interface DfsCache {
get: (name: string) => IDataFrame | undefined;
set: (name: string, prom: IDataFrame) => void;
clear: (name: string) => void;
clearAll: () => void;
}

export function createDataFrameCache(): DfCache {
let df: IDataFrame | undefined;
const cache: DfCache = {
get: () => {
return df;
export function createDataFramesCache(): DfsCache {
const dfs: Record<string, IDataFrame> = {};
const cache: DfsCache = {
get: (name: string) => {
return dfs[name];

Check warning on line 19 in src/plugins/data/common/data_frames/_df_cache.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/_df_cache.ts#L19

Added line #L19 was not covered by tests
},
set: (prom: IDataFrame) => {
df = prom;
return prom;
set: (name: string, df: IDataFrame) => {
dfs[name] = df;
return;

Check warning on line 23 in src/plugins/data/common/data_frames/_df_cache.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/_df_cache.ts#L22-L23

Added lines #L22 - L23 were not covered by tests
},
clear: () => {
df = undefined;
clear: (name: string) => {
delete dfs[name];

Check warning on line 26 in src/plugins/data/common/data_frames/_df_cache.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/_df_cache.ts#L26

Added line #L26 was not covered by tests
},
clearAll: () => {
Object.keys(dfs).forEach((name) => delete dfs[name]);

Check warning on line 29 in src/plugins/data/common/data_frames/_df_cache.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/common/data_frames/_df_cache.ts#L29

Added line #L29 was not covered by tests
},
};
return cache;
Expand Down
9 changes: 5 additions & 4 deletions src/plugins/data/common/data_frames/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ export enum DATA_FRAME_TYPES {
POLLING = 'data_frame_polling',
}

export interface DataFrameService {
get: () => IDataFrame | undefined;
set: (dataFrame: IDataFrame) => Promise<void>;
clear: () => void;
export interface DataFramesService {
get: (name: string) => IDataFrame | undefined;
set: (name: string, df: IDataFrame) => void;
clear: (name: string) => void;
clearAll: () => void;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,9 @@ export function getSearchParamsFromRequest(
destroyDataFrame?: DestroyDataFrameFn;
}
): ISearchRequestParams {
const { getConfig, getDataFrame, destroyDataFrame } = dependencies;
const { getConfig } = 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,
Expand Down
34 changes: 20 additions & 14 deletions 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,13 @@ import { normalizeSortRequest } from './normalize_sort_request';
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,
DataFramesService,
IDataFrame,
IDataFrameResponse,
convertResult,
} from '../../data_frames';
import { IOpenSearchSearchRequest, IOpenSearchSearchResponse, ISearchOptions } from '../..';
import { IOpenSearchDashboardsSearchRequest, IOpenSearchDashboardsSearchResponse } from '../types';
import { ISearchSource, SearchSourceOptions, SearchSourceFields } from './types';
Expand Down Expand Up @@ -138,11 +144,7 @@ export interface SearchSourceDependencies extends FetchHandlers {
request: SearchStrategyRequest,
options: ISearchOptions
) => Promise<SearchStrategyResponse>;
df: {
get: () => IDataFrame | undefined;
set: (dataFrame: IDataFrame) => Promise<void>;
clear: () => void;
};
dfs: DataFramesService;
}

/** @public **/
Expand Down Expand Up @@ -286,8 +288,8 @@ export class SearchSource {
* Get the data frame of this SearchSource
* @return {undefined|IDataFrame}
*/
getDataFrame() {
return this.dependencies.df.get();
getDataFrame(dfName: string) {
return this.dependencies.dfs.get(dfName);

Check warning on line 292 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#L292

Added line #L292 was not covered by tests
}

/**
Expand All @@ -296,20 +298,24 @@ export class SearchSource {
* @async
* @return {undefined|IDataFrame}
*/
async setDataFrame(dataFrame: IDataFrame | undefined) {
async setDataFrame(dfName: string, dataFrame: IDataFrame | undefined) {
if (dataFrame) {
await this.dependencies.df.set(dataFrame);
await this.dependencies.dfs.set(dfName, dataFrame);

Check warning on line 303 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#L303

Added line #L303 was not covered by tests
} else {
this.destroyDataFrame();
this.destroyDataFrame(dfName);

Check warning on line 305 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#L305

Added line #L305 was not covered by tests
}
return this.getDataFrame();
return this.getDataFrame(dfName);

Check warning on line 307 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#L307

Added line #L307 was not covered by tests
}

/**
* Clear the data frame of this SearchSource
*/
destroyDataFrame() {
this.dependencies.df.clear();
destroyDataFrame(dfName: string) {
this.dependencies.dfs.clear(dfName);

Check warning on line 314 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#L314

Added line #L314 was not covered by tests
}

clearDataFramesCache() {
this.dependencies.dfs.clearAll();

Check warning on line 318 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#L318

Added line #L318 was not covered by tests
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export * from './data_frames/types';
* not possible.
*/
export type GetConfigFn = <T = any>(key: string, defaultOverride?: T) => T;
export type GetDataFrameFn = () => IDataFrame | undefined;
export type GetDataFrameFn = (dfName: string) => IDataFrame | undefined;
export type GetDataFrameAggQsFn = ({
qs,
aggConfig,
Expand All @@ -60,5 +60,5 @@ export type GetDataFrameAggQsFn = ({
timeFilter: any;
}) => any;

export type DestroyDataFrameFn = () => void;
export type DestroyDataFrameFn = (dfName: string) => void;
export type GetAggTypeFn = (id: string) => BucketAggType<any> | MetricAggType<any>;
43 changes: 25 additions & 18 deletions src/plugins/data/public/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,11 @@ import {
} from '../../common/search/aggs/buckets/shard_delay';
import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn';
import {
DataFrameService,
DataFramesService,
IDataFrame,
IDataFrameResponse,
createDataFrameCache,
createDataFrame,
createDataFramesCache,
dataFrameToSpec,
} from '../../common/data_frames';

Expand All @@ -80,7 +81,7 @@ export interface SearchServiceStartDependencies {
export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
private readonly aggsService = new AggsService();
private readonly searchSourceService = new SearchSourceService();
private readonly dfCache = createDataFrameCache();
private readonly dfsCache = createDataFramesCache();
private searchInterceptor!: ISearchInterceptor;
private defaultSearchInterceptor!: ISearchInterceptor;
private usageCollector?: SearchUsageCollector;
Expand Down Expand Up @@ -139,26 +140,32 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
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);
const dfsService: DataFramesService = {
get: (dfName: string) => {
if (this.dfsCache.get(dfName)) {
return this.dfsCache.get(dfName);

Check warning on line 146 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#L146

Added line #L146 was not covered by tests
} else {
const df = createDataFrame({ name: dfName, fields: [] });
this.dfsCache.set(dfName, df);
return df;

Check warning on line 150 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#L148-L150

Added lines #L148 - L150 were not covered by tests
}
this.dfCache.set(dataFrame);
const existingIndexPattern = indexPatterns.getByTitle(dataFrame.name!, true);
},
set: async (dfName: string, df: IDataFrame) => {
this.dfsCache.set(dfName, df);
const existingIndexPattern = indexPatterns.getByTitle(dfName, true);

Check warning on line 155 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#L154-L155

Added lines #L154 - L155 were not covered by tests
const dataSet = await indexPatterns.create(
dataFrameToSpec(dataFrame, existingIndexPattern?.id),
dataFrameToSpec(df, 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();
clear: (dfName: string) => {
indexPatterns.clearCache(dfName, false);
this.dfsCache.clear(dfName);

Check warning on line 165 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#L164-L165

Added lines #L164 - L165 were not covered by tests
},
clearAll: () => {
this.dfsCache.clearAll();

Check warning on line 168 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#L168

Added line #L168 was not covered by tests
},
};

Expand All @@ -180,7 +187,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
callMsearch: getCallMsearch({ http }),
loadingCount$,
},
df: dfService,
dfs: dfsService,
};

return {
Expand All @@ -194,7 +201,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
this.searchInterceptor = enhancements.searchInterceptor;
},
getDefaultSearchInterceptor: () => this.defaultSearchInterceptor,
df: dfService,
dfs: dfsService,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/data/public/search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +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';
import { DataFramesService } from '../../common/data_frames';

export { ISearchStartSearchSource };

Expand Down Expand Up @@ -81,7 +81,7 @@ export interface ISearchStart {
searchSource: ISearchStartSearchSource;
__enhance: (enhancements: SearchEnhancements) => void;
getDefaultSearchInterceptor: () => ISearchInterceptor;
df: DataFrameService;
dfs: DataFramesService;
}

export { SEARCH_EVENT_TYPE } from './collectors';
Expand Down
1 change: 0 additions & 1 deletion src/plugins/data/public/ui/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ export class Settings {

setUserQueryLanguage(language: string) {
this.storage.set('opensearchDashboards.userQueryLanguage', language);
this.search.df.clear();
const queryEnhancement = this.queryEnhancements.get(language);
this.search.__enhance({
searchInterceptor: queryEnhancement
Expand Down
29 changes: 14 additions & 15 deletions src/plugins/data/server/search/search_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ import {
import { aggShardDelay } from '../../common/search/aggs/buckets/shard_delay_fn';
import { ConfigSchema } from '../../config';
import {
DataFrameService,
DataFramesService,
IDataFrame,
IDataFrameResponse,
createDataFrameCache,
createDataFramesCache,
dataFrameToSpec,
} from '../../common';

Expand Down Expand Up @@ -105,7 +105,7 @@ export interface SearchRouteDependencies {
export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
private readonly aggsService = new AggsService();
private readonly searchSourceService = new SearchSourceService();
private readonly dfCache = createDataFrameCache();
private readonly dfsCache = createDataFramesCache();
private defaultSearchStrategyName: string = OPENSEARCH_SEARCH_STRATEGY;
private searchStrategies: StrategyMap = {};

Expand Down Expand Up @@ -212,13 +212,10 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
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 dfsService: DataFramesService = {
get: (dfName: string) => this.dfsCache.get(dfName),

Check warning on line 216 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#L215-L216

Added lines #L215 - L216 were not covered by tests
set: async (dfName: string, dataFrame: IDataFrame) => {
this.dfsCache.set(dfName, dataFrame);

Check warning on line 218 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#L218

Added line #L218 was not covered by tests
const existingIndexPattern = scopedIndexPatterns.getByTitle(dataFrame.name!, true);
const dataSet = await scopedIndexPatterns.create(
dataFrameToSpec(dataFrame, existingIndexPattern?.id),
Expand All @@ -227,11 +224,13 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
// 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;
clear: (dfName: string) => {
// name because the id is not unique for temporary index pattern created
scopedIndexPatterns.clearCache(this.dfCache.get()!.name, false);
this.dfCache.clear();
scopedIndexPatterns.clearCache(this.dfsCache.get(dfName)!.name, false);
this.dfsCache.clear(dfName);

Check warning on line 230 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-L230

Added lines #L229 - L230 were not covered by tests
},
clearAll: () => {
this.dfsCache.clearAll();

Check warning on line 233 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#L233

Added line #L233 was not covered by tests
},
};

Expand Down Expand Up @@ -269,7 +268,7 @@ export class SearchService implements Plugin<ISearchSetup, ISearchStart> {
}),
loadingCount$: new BehaviorSubject(0),
},
df: dfService,
dfs: dfsService,
};

return this.searchSourceService.start(scopedIndexPatterns, searchSourceDependencies);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,7 @@ export const updateSearchSource = async ({
histogramConfigs,
}: Props) => {
const { uiSettings, data } = services;
let dataSet = indexPattern;
const dataFrame = searchSource?.getDataFrame();
if (
searchSource &&
dataFrame &&
dataFrame.name &&
dataFrame.name !== '' &&
dataSet.title !== dataFrame.name
) {
dataSet = data.indexPatterns.getByTitle(dataFrame.name, true) ?? dataSet;
searchSource.setField('index', dataSet);
}
const dataSet = indexPattern;

const sortForSearchSource = getSortForSearchSource(
sort,
Expand Down

0 comments on commit 2b580fd

Please sign in to comment.