Skip to content

Commit

Permalink
useCustomSearchSource -> getUsedIndexPattern
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed Nov 10, 2020
1 parent e2e02fe commit fbec631
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 23 deletions.
1 change: 1 addition & 0 deletions src/plugins/vis_type_timeseries/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@
*/

export const MAX_BUCKETS_SETTING = 'metrics:max_buckets';
export const INDEXES_SEPARATOR = ',';
21 changes: 11 additions & 10 deletions src/plugins/vis_type_timeseries/public/metrics_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { PANEL_TYPES } from '../common/panel_types';
import { VisEditor } from './application/components/vis_editor_lazy';
import { VIS_EVENT_TO_TRIGGER, VisGroups, VisParams } from '../../visualizations/public';
import { getSavedObjectsClient, getDataStart } from './services';
import { INDEXES_SEPARATOR } from '../common/constants';

export const metricsVisDefinition = {
name: 'metrics',
Expand Down Expand Up @@ -85,19 +86,19 @@ export const metricsVisDefinition = {
return [VIS_EVENT_TO_TRIGGER.applyFilter];
},
inspectorAdapters: {},
useCustomSearchSource: async (params: VisParams) => {
getUsedIndexPattern: async (params: VisParams) => {
const { indexPatterns } = getDataStart();

if (params.index_pattern) {
const savedObjects = await getSavedObjectsClient().client.find({
type: 'index-pattern',
fields: ['title'],
search: params.index_pattern,
});
const cachedIndexes = await indexPatterns.getIdsWithTitle();
const ids = params.index_pattern
.split(INDEXES_SEPARATOR)
.map((title) => cachedIndexes.find((i) => i.title === title)?.id)
.filter((id) => id);

if (savedObjects.total) {
return { index: await getDataStart().indexPatterns.get(savedObjects.savedObjects[0].id) };
}
return Promise.all(ids.map((is) => indexPatterns.get(is)));
}
return {};
return [];
},
responseHandler: 'none',
};
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,14 @@ export const createVisEmbeddableFromObject = (deps: VisualizeEmbeddableFactoryDe
return new DisabledLabEmbeddable(vis.title, input);
}

const indexPattern = vis.data.indexPattern;
const indexPatterns = indexPattern ? [indexPattern] : [];
let indexPatterns = [];

if (vis.type.getUsedIndexPattern) {
indexPatterns = await vis.type.getUsedIndexPattern(vis.params);
} else if (vis.data.indexPattern) {
indexPatterns = [vis.data.indexPatter];
}

const editable = getCapabilities().visualize.save as boolean;

return new VisualizeEmbeddable(
Expand Down
3 changes: 0 additions & 3 deletions src/plugins/visualizations/public/vis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,6 @@ 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
4 changes: 2 additions & 2 deletions src/plugins/visualizations/public/vis_types/base_vis_type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export class BaseVisType<TVisParams = VisParams> implements VisType<TVisParams>
public readonly responseHandler;
public readonly hierarchicalData;
public readonly setup;
public readonly useCustomSearchSource;
public readonly getUsedIndexPattern;
public readonly useCustomNoDataScreen;
public readonly inspectorAdapters;
public readonly toExpressionAst;
Expand Down Expand Up @@ -128,7 +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.getUsedIndexPattern = opts.getUsedIndexPattern;
this.useCustomNoDataScreen = opts.useCustomNoDataScreen ?? false;
this.inspectorAdapters = opts.inspectorAdapters;
this.toExpressionAst = opts.toExpressionAst;
Expand Down
10 changes: 4 additions & 6 deletions src/plugins/visualizations/public/vis_types/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { IconType } from '@elastic/eui';
import React from 'react';
import { Adapters } from 'src/plugins/inspector';
import { SearchSourceFields } from 'src/plugins/data/public';
import { IIndexPattern } 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';
Expand Down Expand Up @@ -67,12 +67,10 @@ export interface VisType<TVisParams = unknown> {
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
* 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 useCustomSearchSource?: (
visParams: VisParams
) => SearchSourceFields | Promise<SearchSourceFields>;
readonly getUsedIndexPattern?: (visParams: VisParams) => IIndexPattern[];

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

0 comments on commit fbec631

Please sign in to comment.