Skip to content

Commit

Permalink
fix min/max/sum/average aggregate of elements having only null/undefi…
Browse files Browse the repository at this point in the history
…ned values
  • Loading branch information
scampi committed Jan 3, 2017
1 parent 67088c8 commit 0791be7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 10 deletions.
70 changes: 60 additions & 10 deletions src/ui/public/agg_types/__tests__/metrics/top_hit.js
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
}
}
]
Expand All @@ -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)
}
}
]
Expand Down
3 changes: 3 additions & 0 deletions src/ui/public/agg_types/metrics/top_hit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 0791be7

Please sign in to comment.