From 69cf758f209db362f0bbc622010805031ba6d787 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 29 Sep 2021 09:10:42 -0400 Subject: [PATCH] Fixes error with percentiles on index with many docs (#113216) (#113385) * Fixes error with metric viz and percentiles on index with many docs * Unskip the metric viz suite * Adds a unit test Co-authored-by: Stratoula Kalafateli --- .../search/aggs/metrics/percentiles.test.ts | 51 +++++++++++++++++++ .../aggs/metrics/percentiles_get_value.ts | 2 +- .../apps/visualize/_metric_chart.ts | 3 +- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts b/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts index c6cc73f6c0fac..26189e022e7c6 100644 --- a/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts +++ b/src/plugins/data/common/search/aggs/metrics/percentiles.test.ts @@ -10,6 +10,7 @@ import { IPercentileAggConfig, getPercentilesMetricAgg } from './percentiles'; import { AggConfigs, IAggConfigs } from '../agg_configs'; import { mockAggTypesRegistry } from '../test_helpers'; import { METRIC_TYPES } from './metric_agg_types'; +import { IResponseAggConfig } from './lib/get_response_agg_config_class'; describe('AggTypesMetricsPercentilesProvider class', () => { let aggConfigs: IAggConfigs; @@ -92,4 +93,54 @@ describe('AggTypesMetricsPercentilesProvider class', () => { } `); }); + + it('returns NaN if bucket has zero documents', () => { + const agg = { + id: '1.5', + key: 5, + parentId: '1', + } as IResponseAggConfig; + const percentileValue: any = getPercentilesMetricAgg().getValue(agg, { + doc_count: 0, + }); + + expect(percentileValue).toBe(NaN); + }); + + it('computes the value correctly', () => { + const agg = { + id: '1.5', + key: 5, + parentId: '1', + } as IResponseAggConfig; + const percentileValue: any = getPercentilesMetricAgg().getValue(agg, { + doc_count: 0, + 1: { + values: [ + { + key: 1, + value: 0, + }, + { + key: 5, + value: 306.5442142237532, + }, + { + key: 75, + value: 8014.248827201506, + }, + { + key: 95, + value: 10118.560640759324, + }, + { + key: 99, + value: 18028.720727798096, + }, + ], + }, + }); + + expect(percentileValue).toBe(306.5442142237532); + }); }); diff --git a/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts b/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts index 1044269df3476..90585909db42a 100644 --- a/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts +++ b/src/plugins/data/common/search/aggs/metrics/percentiles_get_value.ts @@ -13,7 +13,7 @@ export const getPercentileValue = ( agg: TAggConfig, bucket: any ) => { - const { values } = bucket[agg.parentId]; + const { values } = bucket[agg.parentId] ?? {}; const percentile: any = find(values, ({ key }) => key === agg.key); diff --git a/test/functional/apps/visualize/_metric_chart.ts b/test/functional/apps/visualize/_metric_chart.ts index 6f6767619f486..7853a3a845bfc 100644 --- a/test/functional/apps/visualize/_metric_chart.ts +++ b/test/functional/apps/visualize/_metric_chart.ts @@ -17,8 +17,7 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) { const inspector = getService('inspector'); const PageObjects = getPageObjects(['visualize', 'visEditor', 'visChart', 'timePicker']); - // FLAKY: https://github.com/elastic/kibana/issues/106121 - describe.skip('metric chart', function () { + describe('metric chart', function () { before(async function () { await PageObjects.visualize.initTests(); log.debug('navigateToApp visualize');