[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:
parent
96fd83b1a4
commit
43b31e1b2b
|
@ -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',
|
||||
},
|
||||
},
|
||||
},
|
||||
};
|
||||
};
|
||||
}
|
||||
};
|
||||
|
|
|
@ -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: [],
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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 = {
|
||||
|
|
Loading…
Reference in a new issue