From 253495b6d76fd29f57f3a93f4eac99dd64cc88e7 Mon Sep 17 00:00:00 2001 From: Alexey Antonov Date: Mon, 16 Nov 2020 10:45:36 +0300 Subject: [PATCH] TSVB doesn't communicate it's index-patterns to dashboard (#82964) * TSVB doesn't communicate it's index-patterns to dashboard Closes: #81476 * useCustomSearchSource -> getUsedIndexPattern * fix CI Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- .../vis_type_timeseries/common/constants.ts | 1 + .../public/metrics_type.ts | 20 ++++++++++++++++++- .../create_vis_embeddable_from_object.ts | 11 ++++++++-- src/plugins/visualizations/public/vis.ts | 1 - .../public/vis_types/base_vis_type.ts | 3 +++ .../visualizations/public/vis_types/types.ts | 10 +++++++++- 6 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/plugins/vis_type_timeseries/common/constants.ts b/src/plugins/vis_type_timeseries/common/constants.ts index fc402d6ab7db5..92dfc6d522957 100644 --- a/src/plugins/vis_type_timeseries/common/constants.ts +++ b/src/plugins/vis_type_timeseries/common/constants.ts @@ -18,3 +18,4 @@ */ export const MAX_BUCKETS_SETTING = 'metrics:max_buckets'; +export const INDEXES_SEPARATOR = ','; diff --git a/src/plugins/vis_type_timeseries/public/metrics_type.ts b/src/plugins/vis_type_timeseries/public/metrics_type.ts index 682517ab1a996..01b6ea0768338 100644 --- a/src/plugins/vis_type_timeseries/public/metrics_type.ts +++ b/src/plugins/vis_type_timeseries/public/metrics_type.ts @@ -25,7 +25,9 @@ import { EditorController } from './application'; // @ts-ignore import { PANEL_TYPES } from '../common/panel_types'; import { VisEditor } from './application/components/vis_editor_lazy'; -import { VIS_EVENT_TO_TRIGGER, VisGroups } from '../../visualizations/public'; +import { VIS_EVENT_TO_TRIGGER, VisGroups, VisParams } from '../../visualizations/public'; +import { getDataStart } from './services'; +import { INDEXES_SEPARATOR } from '../common/constants'; export const metricsVisDefinition = { name: 'metrics', @@ -84,5 +86,21 @@ export const metricsVisDefinition = { return [VIS_EVENT_TO_TRIGGER.applyFilter]; }, inspectorAdapters: {}, + getUsedIndexPattern: async (params: VisParams) => { + const { indexPatterns } = getDataStart(); + const indexes: string = params.index_pattern; + + if (indexes) { + const cachedIndexes = await indexPatterns.getIdsWithTitle(); + const ids = indexes + .split(INDEXES_SEPARATOR) + .map((title) => cachedIndexes.find((i) => i.title === title)?.id) + .filter((id) => id); + + return Promise.all(ids.map((id) => indexPatterns.get(id!))); + } + + return []; + }, responseHandler: 'none', }; diff --git a/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts b/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts index 36f3f7d6ed22e..c064298658fff 100644 --- a/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts +++ b/src/plugins/visualizations/public/embeddable/create_vis_embeddable_from_object.ts @@ -41,6 +41,7 @@ import { import { VisualizeEmbeddableFactoryDeps } from './visualize_embeddable_factory'; import { VISUALIZE_ENABLE_LABS_SETTING } from '../../common/constants'; import { SavedVisualizationsLoader } from '../saved_visualizations'; +import { IndexPattern } from '../../../data/public'; export const createVisEmbeddableFromObject = (deps: VisualizeEmbeddableFactoryDeps) => async ( vis: Vis, @@ -69,8 +70,14 @@ export const createVisEmbeddableFromObject = (deps: VisualizeEmbeddableFactoryDe return new DisabledLabEmbeddable(vis.title, input); } - const indexPattern = vis.data.indexPattern; - const indexPatterns = indexPattern ? [indexPattern] : []; + let indexPatterns: IndexPattern[] = []; + + if (vis.type.getUsedIndexPattern) { + indexPatterns = await vis.type.getUsedIndexPattern(vis.params); + } else if (vis.data.indexPattern) { + indexPatterns = [vis.data.indexPattern]; + } + const editable = getCapabilities().visualize.save as boolean; return new VisualizeEmbeddable( diff --git a/src/plugins/visualizations/public/vis.ts b/src/plugins/visualizations/public/vis.ts index 5c3233a8de896..cae9058071b6c 100644 --- a/src/plugins/visualizations/public/vis.ts +++ b/src/plugins/visualizations/public/vis.ts @@ -137,7 +137,6 @@ export class Vis { if (state.params || typeChanged) { this.params = this.getParams(state.params); } - if (state.data && state.data.searchSource) { this.data.searchSource = await getSearch().searchSource.create(state.data.searchSource!); this.data.indexPattern = this.data.searchSource.getField('index'); diff --git a/src/plugins/visualizations/public/vis_types/base_vis_type.ts b/src/plugins/visualizations/public/vis_types/base_vis_type.ts index 807582723172d..e52cd4d2b2d56 100644 --- a/src/plugins/visualizations/public/vis_types/base_vis_type.ts +++ b/src/plugins/visualizations/public/vis_types/base_vis_type.ts @@ -44,6 +44,7 @@ interface CommonBaseVisTypeOptions | 'editorConfig' | 'hidden' | 'stage' + | 'getUsedIndexPattern' | 'useCustomNoDataScreen' | 'visConfig' | 'group' @@ -96,6 +97,7 @@ export class BaseVisType implements VisType public readonly responseHandler; public readonly hierarchicalData; public readonly setup; + public readonly getUsedIndexPattern; public readonly useCustomNoDataScreen; public readonly inspectorAdapters; public readonly toExpressionAst; @@ -126,6 +128,7 @@ export class BaseVisType implements VisType this.responseHandler = opts.responseHandler ?? 'none'; this.setup = opts.setup; this.hierarchicalData = opts.hierarchicalData ?? false; + this.getUsedIndexPattern = opts.getUsedIndexPattern; this.useCustomNoDataScreen = opts.useCustomNoDataScreen ?? false; this.inspectorAdapters = opts.inspectorAdapters; this.toExpressionAst = opts.toExpressionAst; diff --git a/src/plugins/visualizations/public/vis_types/types.ts b/src/plugins/visualizations/public/vis_types/types.ts index ee804e5677243..6ea44dc360559 100644 --- a/src/plugins/visualizations/public/vis_types/types.ts +++ b/src/plugins/visualizations/public/vis_types/types.ts @@ -20,10 +20,11 @@ import { IconType } from '@elastic/eui'; import React from 'react'; import { Adapters } from 'src/plugins/inspector'; +import { IndexPattern } from 'src/plugins/data/public'; import { VisEditorConstructor } from 'src/plugins/visualize/public'; import { ISchemas } from 'src/plugins/vis_default_editor/public'; import { TriggerContextMapping } from '../../../ui_actions/public'; -import { Vis, VisToExpressionAst, VisualizationControllerConstructor } from '../types'; +import { Vis, VisParams, VisToExpressionAst, VisualizationControllerConstructor } from '../types'; export interface VisTypeOptions { showTimePicker: boolean; @@ -64,6 +65,13 @@ export interface VisType { * If given, it will return the supported triggers for this vis. */ readonly getSupportedTriggers?: () => Array; + + /** + * Some visualizations are created without SearchSource and may change the used indexes during the visualization configuration. + * Using this method we can rewrite the standard mechanism for getting used indexes + */ + readonly getUsedIndexPattern?: (visParams: VisParams) => IndexPattern[] | Promise; + readonly isAccessible?: boolean; readonly requestHandler?: string | unknown; readonly responseHandler?: string | unknown;