Skip to content

Commit

Permalink
[Metrics UI] Fix Metrics Explorer API to return empty results when fi…
Browse files Browse the repository at this point in the history
…eld 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>
  • Loading branch information
3 people authored Oct 26, 2020
1 parent 96fd83b commit 43b31e1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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',
},
},
},
};
};
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 43b31e1

Please sign in to comment.