Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Visualizations] Pass 'aggs' parameter to custom request handlers #71423

Merged
merged 7 commits into from
Jul 16, 2020
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { get } from 'lodash';
import { i18n } from '@kbn/i18n';
import { VisResponseValue, PersistedState } from '../../../../plugins/visualizations/public';
import { ExpressionFunctionDefinition, Render } from '../../../../plugins/expressions/public';
import { getTypes, getIndexPatterns, getFilterManager } from '../services';
import { getTypes, getIndexPatterns, getFilterManager, getSearch } from '../services';

interface Arguments {
index?: string | null;
Expand All @@ -31,6 +31,7 @@ interface Arguments {
schemas?: string;
visConfig?: string;
uiState?: string;
aggConfigs?: string;
}

export type ExpressionFunctionVisualization = ExpressionFunctionDefinition<
Expand Down Expand Up @@ -84,6 +85,11 @@ export const visualization = (): ExpressionFunctionVisualization => ({
default: '"{}"',
help: 'User interface state',
},
aggConfigs: {
types: ['string'],
default: '"{}"',
help: 'Aggregation configurations',
},
},
async fn(input, args, { inspectorAdapters }) {
const visConfigParams = args.visConfig ? JSON.parse(args.visConfig) : {};
Expand All @@ -94,6 +100,9 @@ export const visualization = (): ExpressionFunctionVisualization => ({
const uiStateParams = args.uiState ? JSON.parse(args.uiState) : {};
const uiState = new PersistedState(uiStateParams);

const aggConfigsState = args.aggConfigs ? JSON.parse(args.aggConfigs) : {};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you want {} as a fallback here... createAggConfigs expects an array of serialized agg configs as the aggConfigsState:

createAggConfigs(
  indexPattern: IndexPattern,
  configStates: AggConfigSerialized[]
) => IAggConfigs;

So you can probably either fall back to [], or to undefined (in which case createAggConfigs will default to [] anyway).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, I'll put [] here.
Unless you prefer undefined?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes no difference IMHO. Inside createAggConfigs it will be [] either way

const aggConfigs = indexPattern ? getSearch().aggs.createAggConfigs(indexPattern, aggConfigsState) : null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than setting to null here, perhaps we should simply exclude the aggs from the requestHandler if for some reason no index pattern is present? Then they are basically an optional parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I set undefined as default, instead of null?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I guess that's what I'm proposing. Basically so that the argument to the request handler is aggs?: IAggConfigs instead of aggs: IAggConfigs | null

I don't feel strongly about this though. In the end it won't make a huge difference since the request handler will likely check if (aggs) {...} the same either way.


if (typeof visType.requestHandler === 'function') {
input = await visType.requestHandler({
partialRows: args.partialRows,
Expand All @@ -107,6 +116,7 @@ export const visualization = (): ExpressionFunctionVisualization => ({
inspectorAdapters,
queryFilter: getFilterManager(),
forceFetch: true,
aggConfigs,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we call this aggs here instead of aggConfigs, as this is consistent with the request handler inside of esaggs.ts, and also with our previous implementation?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I'll do that.

});
}

Expand Down
3 changes: 2 additions & 1 deletion src/plugins/visualizations/public/legacy/build_pipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,8 @@ export const buildPipeline = async (
pipeline += `visualization type='${vis.type.name}'
${prepareJson('visConfig', visConfig)}
metricsAtAllLevels=${vis.isHierarchical()}
partialRows=${vis.type.requiresPartialRows || vis.params.showPartialRows || false} `;
partialRows=${vis.type.requiresPartialRows || vis.params.showPartialRows || false}
${prepareJson('aggConfigs', vis.data.aggs!.aggs)} `;
if (indexPattern) {
pipeline += `${prepareString('index', indexPattern.id)}`;
}
Expand Down