Skip to content

Commit

Permalink
TSVB doesn't communicate it's index-patterns to dashboard
Browse files Browse the repository at this point in the history
Closes: #81476
  • Loading branch information
alexwizp committed Nov 9, 2020
1 parent 97e2dc8 commit e2e02fe
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 3 deletions.
17 changes: 16 additions & 1 deletion src/plugins/vis_type_timeseries/public/metrics_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ 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 { getSavedObjectsClient, getDataStart } from './services';

export const metricsVisDefinition = {
name: 'metrics',
Expand Down Expand Up @@ -84,5 +85,19 @@ export const metricsVisDefinition = {
return [VIS_EVENT_TO_TRIGGER.applyFilter];
},
inspectorAdapters: {},
useCustomSearchSource: async (params: VisParams) => {
if (params.index_pattern) {
const savedObjects = await getSavedObjectsClient().client.find({
type: 'index-pattern',
fields: ['title'],
search: params.index_pattern,
});

if (savedObjects.total) {
return { index: await getDataStart().indexPatterns.get(savedObjects.savedObjects[0].id) };
}
}
return {};
},
responseHandler: 'none',
};
4 changes: 3 additions & 1 deletion src/plugins/visualizations/public/vis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,9 @@ export class Vis<TVisParams = VisParams> {
if (state.params || typeChanged) {
this.params = this.getParams(state.params);
}

if (this.type.useCustomSearchSource) {
state.data.searchSource = await this.type.useCustomSearchSource(this.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');
Expand Down
3 changes: 3 additions & 0 deletions src/plugins/visualizations/public/vis_types/base_vis_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ interface CommonBaseVisTypeOptions<TVisParams>
| 'editorConfig'
| 'hidden'
| 'stage'
| 'useCustomSearchSource'
| 'useCustomNoDataScreen'
| 'visConfig'
| 'group'
Expand Down Expand Up @@ -96,6 +97,7 @@ export class BaseVisType<TVisParams = VisParams> implements VisType<TVisParams>
public readonly responseHandler;
public readonly hierarchicalData;
public readonly setup;
public readonly useCustomSearchSource;
public readonly useCustomNoDataScreen;
public readonly inspectorAdapters;
public readonly toExpressionAst;
Expand Down Expand Up @@ -126,6 +128,7 @@ export class BaseVisType<TVisParams = VisParams> implements VisType<TVisParams>
this.responseHandler = opts.responseHandler ?? 'none';
this.setup = opts.setup;
this.hierarchicalData = opts.hierarchicalData ?? false;
this.useCustomSearchSource = opts.useCustomSearchSource;
this.useCustomNoDataScreen = opts.useCustomNoDataScreen ?? false;
this.inspectorAdapters = opts.inspectorAdapters;
this.toExpressionAst = opts.toExpressionAst;
Expand Down
12 changes: 11 additions & 1 deletion src/plugins/visualizations/public/vis_types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@
import { IconType } from '@elastic/eui';
import React from 'react';
import { Adapters } from 'src/plugins/inspector';
import { SearchSourceFields } 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;
Expand Down Expand Up @@ -64,6 +65,15 @@ export interface VisType<TVisParams = unknown> {
* If given, it will return the supported triggers for this vis.
*/
readonly getSupportedTriggers?: () => Array<keyof TriggerContextMapping>;

/**
* Some visualizations are created without SearchSource and may change the index during the visualization configuration.
* Using this method we can rewrite the standard mechanism for getting SearchSource and set it directly from the visualization
*/
readonly useCustomSearchSource?: (
visParams: VisParams
) => SearchSourceFields | Promise<SearchSourceFields>;

readonly isAccessible?: boolean;
readonly requestHandler?: string | unknown;
readonly responseHandler?: string | unknown;
Expand Down

0 comments on commit e2e02fe

Please sign in to comment.