[Metrics UI] Fix Metrics Explorer API to return empty results when field is missing from aggregation (#80919)

* [Metrics UI] Fix Metrics Explorer API to return empty results when field is missing from aggregation

* Update x-pack/plugins/infra/server/routes/metrics_explorer/lib/convert_request_to_metrics_api_options.ts

Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com>

* Fixing type

Co-authored-by: Felix Stürmer <weltenwort@users.noreply.github.com>
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Chris Cowan 2020-10-26 08:59:46 -07:00 committed by GitHub
parent 96fd83b1a4
commit 43b31e1b2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 14 deletions

View file

@ -9,7 +9,7 @@ import { MetricsAPIMetric, MetricsExplorerMetric } from '../../../../common/http
export const convertMetricToMetricsAPIMetric = (
metric: MetricsExplorerMetric,
index: number
): MetricsAPIMetric => {
): MetricsAPIMetric | undefined => {
const id = `metric_${index}`;
if (metric.aggregation === 'rate' && metric.field) {
return {
@ -44,19 +44,21 @@ export const convertMetricToMetricsAPIMetric = (
};
}
return {
id,
aggregations: {
[id]: {
bucket_script: {
buckets_path: { count: '_count' },
script: {
source: 'count * 1',
lang: 'expression',
if (metric.aggregation === 'count') {
return {
id,
aggregations: {
[id]: {
bucket_script: {
buckets_path: { count: '_count' },
script: {
source: 'count * 1',
lang: 'expression',
},
gap_policy: 'skip',
},
gap_policy: 'skip',
},
},
},
};
};
}
};

View file

@ -120,4 +120,16 @@ describe('convertRequestToMetricsAPIOptions', () => {
metrics: [],
});
});
it('should work with empty field', () => {
expect(
convertRequestToMetricsAPIOptions({
...BASE_REQUEST,
metrics: [{ aggregation: 'avg' }],
})
).toEqual({
...BASE_METRICS_UI_OPTIONS,
metrics: [],
});
});
});

View file

@ -15,7 +15,9 @@ import { convertMetricToMetricsAPIMetric } from './convert_metric_to_metrics_api
export const convertRequestToMetricsAPIOptions = (
options: MetricsExplorerRequestBody
): MetricsAPIRequest => {
const metrics = options.metrics.map(convertMetricToMetricsAPIMetric);
const metrics = options.metrics
.map(convertMetricToMetricsAPIMetric)
.filter(<M>(m: M): m is NonNullable<M> => !!m);
const { limit, timerange, indexPattern } = options;
const metricsApiOptions: MetricsAPIRequest = {