Skip to content

Commit 43b31e1

Browse files
simianhackerweltenwortkibanamachine
authored
[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>
1 parent 96fd83b commit 43b31e1

File tree

3 files changed

+30
-14
lines changed

3 files changed

+30
-14
lines changed

x-pack/plugins/infra/server/routes/metrics_explorer/lib/convert_metric_to_metrics_api_metric.ts

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { MetricsAPIMetric, MetricsExplorerMetric } from '../../../../common/http
99
export const convertMetricToMetricsAPIMetric = (
1010
metric: MetricsExplorerMetric,
1111
index: number
12-
): MetricsAPIMetric => {
12+
): MetricsAPIMetric | undefined => {
1313
const id = `metric_${index}`;
1414
if (metric.aggregation === 'rate' && metric.field) {
1515
return {
@@ -44,19 +44,21 @@ export const convertMetricToMetricsAPIMetric = (
4444
};
4545
}
4646

47-
return {
48-
id,
49-
aggregations: {
50-
[id]: {
51-
bucket_script: {
52-
buckets_path: { count: '_count' },
53-
script: {
54-
source: 'count * 1',
55-
lang: 'expression',
47+
if (metric.aggregation === 'count') {
48+
return {
49+
id,
50+
aggregations: {
51+
[id]: {
52+
bucket_script: {
53+
buckets_path: { count: '_count' },
54+
script: {
55+
source: 'count * 1',
56+
lang: 'expression',
57+
},
58+
gap_policy: 'skip',
5659
},
57-
gap_policy: 'skip',
5860
},
5961
},
60-
},
61-
};
62+
};
63+
}
6264
};

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,4 +120,16 @@ describe('convertRequestToMetricsAPIOptions', () => {
120120
metrics: [],
121121
});
122122
});
123+
124+
it('should work with empty field', () => {
125+
expect(
126+
convertRequestToMetricsAPIOptions({
127+
...BASE_REQUEST,
128+
metrics: [{ aggregation: 'avg' }],
129+
})
130+
).toEqual({
131+
...BASE_METRICS_UI_OPTIONS,
132+
metrics: [],
133+
});
134+
});
123135
});

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ import { convertMetricToMetricsAPIMetric } from './convert_metric_to_metrics_api
1515
export const convertRequestToMetricsAPIOptions = (
1616
options: MetricsExplorerRequestBody
1717
): MetricsAPIRequest => {
18-
const metrics = options.metrics.map(convertMetricToMetricsAPIMetric);
18+
const metrics = options.metrics
19+
.map(convertMetricToMetricsAPIMetric)
20+
.filter(<M>(m: M): m is NonNullable<M> => !!m);
1921
const { limit, timerange, indexPattern } = options;
2022

2123
const metricsApiOptions: MetricsAPIRequest = {

0 commit comments

Comments
 (0)