From 0791be7914383103f85f183d668b3daf04a7ac5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Tue, 3 Jan 2017 10:40:16 +0000 Subject: [PATCH] fix min/max/sum/average aggregate of elements having only null/undefined values --- .../agg_types/__tests__/metrics/top_hit.js | 70 ++++++++++++++++--- src/ui/public/agg_types/metrics/top_hit.js | 3 + 2 files changed, 63 insertions(+), 10 deletions(-) diff --git a/src/ui/public/agg_types/__tests__/metrics/top_hit.js b/src/ui/public/agg_types/__tests__/metrics/top_hit.js index c23c95d0f7c0a..04429a447787c 100644 --- a/src/ui/public/agg_types/__tests__/metrics/top_hit.js +++ b/src/ui/public/agg_types/__tests__/metrics/top_hit.js @@ -1,3 +1,4 @@ +import _ from 'lodash'; import expect from 'expect.js'; import ngMock from 'ng_mock'; import TopHitProvider from 'ui/agg_types/metrics/top_hit'; @@ -237,21 +238,70 @@ describe('Top hit metric', function () { }); [ - { type: 'concat', result: [ 1, 2, 3 ] }, - { type: 'sum', result: 6 }, - { type: 'min', result: 1 }, - { type: 'max', result: 3 }, - { type: 'average', result: 2 } + { + description: 'concat values with a comma', + type: 'concat', + data: [ 1, 2, 3 ], + result: [ 1, 2, 3 ] + }, + { + description: 'sum up the values', + type: 'sum', + data: [ 1, 2, 3 ], + result: 6 + }, + { + description: 'take the minimum value', + type: 'min', + data: [ 1, 2, 3 ], + result: 1 + }, + { + description: 'take the maximum value', + type: 'max', + data: [ 1, 2, 3 ], + result: 3 + }, + { + description: 'take the average value', + type: 'average', + data: [ 1, 2, 3 ], + result: 2 + }, + { + description: 'support null/undefined', + type: 'min', + data: [ undefined, null ], + result: null + }, + { + description: 'support null/undefined', + type: 'max', + data: [ undefined, null ], + result: null + }, + { + description: 'support null/undefined', + type: 'sum', + data: [ undefined, null ], + result: null + }, + { + description: 'support null/undefined', + type: 'average', + data: [ undefined, null ], + result: null + } ] .forEach(agg => { - it(`should return the result of the ${agg.type} aggregation over the last doc`, function () { + it(`should return the result of the ${agg.type} aggregation over the last doc - ${agg.description}`, function () { const bucket = { '1': { hits: { hits: [ { _source: { - bytes: [ 1, 2, 3 ] + bytes: agg.data } } ] @@ -263,19 +313,19 @@ describe('Top hit metric', function () { expect(topHitMetric.getValue(aggConfig, bucket)).to.eql(agg.result); }); - it(`should return the result of the ${agg.type} aggregation over the last X docs`, function () { + it(`should return the result of the ${agg.type} aggregation over the last X docs - ${agg.description}`, function () { const bucket = { '1': { hits: { hits: [ { _source: { - bytes: [ 1, 2 ] + bytes: _.dropRight(agg.data, 1) } }, { _source: { - bytes: 3 + bytes: _.last(agg.data) } } ] diff --git a/src/ui/public/agg_types/metrics/top_hit.js b/src/ui/public/agg_types/metrics/top_hit.js index fd78ad5787be2..94c88578378f1 100644 --- a/src/ui/public/agg_types/metrics/top_hit.js +++ b/src/ui/public/agg_types/metrics/top_hit.js @@ -183,6 +183,9 @@ export default function AggTypeMetricTopProvider(Private) { } if (_.isArray(values)) { + if (!_.compact(values).length) { + return null; + } switch (agg.params.aggregate.val) { case 'max': return _.max(values);