From d2abc03251f7c66526216f8d49d7bab9a7e63510 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Wed, 24 Feb 2021 13:12:55 +0300 Subject: [PATCH] [TSVB] Enable `dual mode`, support index patterns and strings (#92395) Part of #91367 Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../server/lib/get_fields.ts | 1 - .../server/lib/get_vis_data.ts | 1 - .../lib/get_index_pattern.ts | 32 +++++++++++++ .../abstract_search_strategy.test.ts | 1 + .../strategies/abstract_search_strategy.ts | 18 ++++--- .../annotations/get_request_params.js | 11 +++-- .../server/lib/vis_data/get_table_data.js | 7 ++- .../lib/vis_data/helpers/get_index_pattern.js | 48 ------------------- .../lib/vis_data/series/get_request_params.js | 7 ++- 9 files changed, 59 insertions(+), 67 deletions(-) create mode 100644 src/plugins/vis_type_timeseries/server/lib/search_strategies/lib/get_index_pattern.ts delete mode 100644 src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_index_pattern.js diff --git a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts index 4b58624a7d1a4..dd74a6c780f4b 100644 --- a/src/plugins/vis_type_timeseries/server/lib/get_fields.ts +++ b/src/plugins/vis_type_timeseries/server/lib/get_fields.ts @@ -47,7 +47,6 @@ export async function getFields( ), }, getUiSettingsService: () => requestContext.core.uiSettings.client, - getSavedObjectsClient: () => requestContext.core.savedObjects.client, getEsShardTimeout: async () => { return await framework.globalConfig$ .pipe( diff --git a/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts b/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts index 4c4e1d4603763..f99d02d00b2ef 100644 --- a/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts +++ b/src/plugins/vis_type_timeseries/server/lib/get_vis_data.ts @@ -52,7 +52,6 @@ export function getVisData( pre: {}, payload: request.body, getUiSettingsService: () => requestContext.core.uiSettings.client, - getSavedObjectsClient: () => requestContext.core.savedObjects.client, getEsShardTimeout: async () => { return await framework.globalConfig$ .pipe( diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/lib/get_index_pattern.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/lib/get_index_pattern.ts new file mode 100644 index 0000000000000..512494de290fd --- /dev/null +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/lib/get_index_pattern.ts @@ -0,0 +1,32 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0 and the Server Side Public License, v 1; you may not use this file except + * in compliance with, at your election, the Elastic License 2.0 or the Server + * Side Public License, v 1. + */ + +import { IndexPatternsService, IndexPattern } from '../../../../../data/server'; + +interface IndexPatternObjectDependencies { + indexPatternsService: IndexPatternsService; +} +export async function getIndexPatternObject( + indexPatternString: string, + { indexPatternsService }: IndexPatternObjectDependencies +) { + let indexPatternObject: IndexPattern | undefined | null; + + if (!indexPatternString) { + indexPatternObject = await indexPatternsService.getDefault(); + } else { + indexPatternObject = (await indexPatternsService.find(indexPatternString)).find( + (index) => index.title === indexPatternString + ); + } + + return { + indexPatternObject, + indexPatternString: indexPatternObject?.title || indexPatternString || '', + }; +} diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.test.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.test.ts index 017fa6837f562..774ef19ef1cd7 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.test.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.test.ts @@ -36,6 +36,7 @@ describe('AbstractSearchStrategy', () => { getIndexPatternsService: jest.fn(() => Promise.resolve({ find: jest.fn(() => []), + getDefault: jest.fn(() => {}), }) ), } as unknown) as ReqFacade; diff --git a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts index 620f494021f0f..dec0330bde6fc 100644 --- a/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts +++ b/src/plugins/vis_type_timeseries/server/lib/search_strategies/strategies/abstract_search_strategy.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import type { FakeRequest, IUiSettingsClient, SavedObjectsClientContract } from 'kibana/server'; +import type { FakeRequest, IUiSettingsClient } from 'kibana/server'; import { indexPatterns, IndexPatternsFetcher } from '../../../../../data/server'; @@ -14,6 +14,7 @@ import type { Framework } from '../../../plugin'; import type { FieldSpec, IndexPatternsService } from '../../../../../data/common'; import type { VisPayload, SanitizedFieldType } from '../../../../common/types'; import type { VisTypeTimeseriesRequestHandlerContext } from '../../../types'; +import { getIndexPatternObject } from '../lib/get_index_pattern'; /** * ReqFacade is a regular KibanaRequest object extended with additional service @@ -29,7 +30,6 @@ export interface ReqFacade extends FakeRequest { indexPatternsFetcher?: IndexPatternsFetcher; }; getUiSettingsService: () => IUiSettingsClient; - getSavedObjectsClient: () => SavedObjectsClientContract; getEsShardTimeout: () => Promise; getIndexPatternsService: () => Promise; } @@ -89,16 +89,14 @@ export abstract class AbstractSearchStrategy { rollupIndex: string; }> ) { - const { indexPatternsFetcher } = req.pre; - const indexPatternsService = await req.getIndexPatternsService(); - const kibanaIndexPattern = (await indexPatternsService.find(indexPattern)).find( - (index) => index.title === indexPattern - ); + const { indexPatternObject } = await getIndexPatternObject(indexPattern, { + indexPatternsService: await req.getIndexPatternsService(), + }); return toSanitizedFieldType( - kibanaIndexPattern - ? kibanaIndexPattern.getNonScriptedFields() - : await indexPatternsFetcher!.getFieldsForWildcard({ + indexPatternObject + ? indexPatternObject.getNonScriptedFields() + : await req.pre.indexPatternsFetcher!.getFieldsForWildcard({ pattern: indexPattern, fieldCapsOptions: { allow_no_indices: true }, metaFields: [], diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/annotations/get_request_params.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/annotations/get_request_params.js index b3287957801a3..49697205a8582 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/annotations/get_request_params.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/annotations/get_request_params.js @@ -8,7 +8,7 @@ import { buildAnnotationRequest } from './build_request_body'; import { getEsShardTimeout } from '../helpers/get_es_shard_timeout'; -import { getIndexPatternObject } from '../helpers/get_index_pattern'; +import { getIndexPatternObject } from '../../search_strategies/lib/get_index_pattern'; export async function getAnnotationRequestParams( req, @@ -19,8 +19,13 @@ export async function getAnnotationRequestParams( ) { const uiSettings = req.getUiSettingsService(); const esShardTimeout = await getEsShardTimeout(req); - const indexPattern = annotation.index_pattern; - const { indexPatternObject, indexPatternString } = await getIndexPatternObject(req, indexPattern); + const { indexPatternObject, indexPatternString } = await getIndexPatternObject( + annotation.index_pattern, + { + indexPatternsService: await req.getIndexPatternsService(), + } + ); + const request = await buildAnnotationRequest( req, panel, diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/get_table_data.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/get_table_data.js index 97015db5d435c..e0604145eef33 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/get_table_data.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/get_table_data.js @@ -11,7 +11,7 @@ import { handleErrorResponse } from './handle_error_response'; import { get } from 'lodash'; import { processBucket } from './table/process_bucket'; import { getEsQueryConfig } from './helpers/get_es_query_uisettings'; -import { getIndexPatternObject } from './helpers/get_index_pattern'; +import { getIndexPatternObject } from '../search_strategies/lib/get_index_pattern'; import { createFieldsFetcher } from './helpers/fields_fetcher'; import { extractFieldLabel } from '../../../common/calculate_label'; @@ -23,7 +23,10 @@ export async function getTableData(req, panel) { capabilities, } = await req.framework.searchStrategyRegistry.getViableStrategy(req, panelIndexPattern); const esQueryConfig = await getEsQueryConfig(req); - const { indexPatternObject } = await getIndexPatternObject(req, panelIndexPattern); + const { indexPatternObject } = await getIndexPatternObject(panelIndexPattern, { + indexPatternsService: await req.getIndexPatternsService(), + }); + const extractFields = createFieldsFetcher(req, searchStrategy, capabilities); const calculatePivotLabel = async () => { diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_index_pattern.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_index_pattern.js deleted file mode 100644 index bddd7a7011612..0000000000000 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/helpers/get_index_pattern.js +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0 and the Server Side Public License, v 1; you may not use this file except - * in compliance with, at your election, the Elastic License 2.0 or the Server - * Side Public License, v 1. - */ - -import { get } from 'lodash'; - -export async function getIndexPatternObject(req, indexPatternString) { - let defaultIndex; - - if (!indexPatternString) { - defaultIndex = await req.getUiSettingsService().get('defaultIndex'); - } - - // getting the matching index pattern - const savedObjectClient = req.getSavedObjectsClient(); - const indexPatternObjects = await savedObjectClient.find({ - type: 'index-pattern', - fields: ['title', 'fields', 'timeFieldName'], - search: indexPatternString ? `"${indexPatternString}"` : null, - search_fields: ['title'], - }); - - // getting the index pattern fields - const indexPatterns = indexPatternObjects.saved_objects - .filter( - (obj) => - obj.attributes.title === indexPatternString || (defaultIndex && obj.id === defaultIndex) - ) - .map((indexPattern) => { - const { title, fields, timeFieldName } = indexPattern.attributes; - return { - title, - timeFieldName, - fields: JSON.parse(fields), - }; - }); - - const indexPatternObject = indexPatterns.length === 1 ? indexPatterns[0] : null; - - return { - indexPatternObject, - indexPatternString: indexPatternString || get(indexPatternObject, 'title', ''), - }; -} diff --git a/src/plugins/vis_type_timeseries/server/lib/vis_data/series/get_request_params.js b/src/plugins/vis_type_timeseries/server/lib/vis_data/series/get_request_params.js index cb9561847b90b..ca4676d363a15 100644 --- a/src/plugins/vis_type_timeseries/server/lib/vis_data/series/get_request_params.js +++ b/src/plugins/vis_type_timeseries/server/lib/vis_data/series/get_request_params.js @@ -8,13 +8,16 @@ import { buildRequestBody } from './build_request_body'; import { getEsShardTimeout } from '../helpers/get_es_shard_timeout'; -import { getIndexPatternObject } from '../helpers/get_index_pattern'; +import { getIndexPatternObject } from '../../../lib/search_strategies/lib/get_index_pattern'; export async function getSeriesRequestParams(req, panel, series, esQueryConfig, capabilities) { const uiSettings = req.getUiSettingsService(); const indexPattern = (series.override_index_pattern && series.series_index_pattern) || panel.index_pattern; - const { indexPatternObject, indexPatternString } = await getIndexPatternObject(req, indexPattern); + + const { indexPatternObject, indexPatternString } = await getIndexPatternObject(indexPattern, { + indexPatternsService: await req.getIndexPatternsService(), + }); const request = await buildRequestBody( req,