Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Aggs] Force return 0 on empty buckets on count if null flag is disabled #207308

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the "Elastic License
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
* Public License v 1"; you may not use this file except in compliance with, at
* your election, the "Elastic License 2.0", the "GNU Affero General Public
* License v3.0 only", or the "Server Side Public License, v 1".
*/

import moment from 'moment';
import { IMetricAggConfig } from './metric_agg_type';
import { getCountMetricAgg } from './count';

function makeAgg(emptyAsNull: boolean = false, timeShift?: moment.Duration): IMetricAggConfig {
return {
getTimeShift() {
return timeShift;
},
params: {
emptyAsNull,
},
} as IMetricAggConfig;
}

function getBucket(value: number | undefined, timeShift?: moment.Duration) {
const suffix = timeShift ? `_${timeShift.asMilliseconds()}` : '';
return { ['doc_count' + suffix]: value };
}

describe('Count', () => {
it('should return the value for a non-shifted bucket', () => {
const agg = getCountMetricAgg();
expect(agg.getValue(makeAgg(), getBucket(1000))).toBe(1000);
});

it('should return the value for a shifted bucket', () => {
const agg = getCountMetricAgg();
const shift = moment.duration(1800000);
expect(agg.getValue(makeAgg(false, shift), getBucket(1000, shift))).toBe(1000);
});

describe('emptyAsNull', () => {
it('should return null if the value is 0 and the flag is enabled', () => {
const agg = getCountMetricAgg();
expect(agg.getValue(makeAgg(true), getBucket(0))).toBe(null);
});

it('should return null if the value is undefined and the flag is enabled', () => {
const agg = getCountMetricAgg();
expect(agg.getValue(makeAgg(true), getBucket(undefined))).toBe(null);
});

it('should return null if the value is 0 and the flag is enabled on a shifted count', () => {
const agg = getCountMetricAgg();
const shift = moment.duration(1800000);
expect(agg.getValue(makeAgg(true, shift), getBucket(0, shift))).toBe(null);
});

it('should return null if the value is undefined and the flag is enabled on a shifted count', () => {
const agg = getCountMetricAgg();
const shift = moment.duration(1800000);
expect(agg.getValue(makeAgg(true, shift), getBucket(undefined, shift))).toBe(null);
});

it('should return 0 if the value is 0 and the flag is disabled', () => {
const agg = getCountMetricAgg();
expect(agg.getValue(makeAgg(false), getBucket(0))).toBe(0);
});

it('should return 0 if the value is undefined and the flag is disabled', () => {
const agg = getCountMetricAgg();
expect(agg.getValue(makeAgg(false), getBucket(undefined))).toBe(0);
});

it('should return 0 if the value is 0 and the flag is disabled on a shifted count', () => {
const agg = getCountMetricAgg();
const shift = moment.duration(1800000);
expect(agg.getValue(makeAgg(false, shift), getBucket(undefined, shift))).toBe(0);
});

it('should return 0 if the value is undefined and the flag is disabled on a shifted count', () => {
const agg = getCountMetricAgg();
const shift = moment.duration(1800000);
expect(agg.getValue(makeAgg(false, shift), getBucket(undefined, shift))).toBe(0);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export const getCountMetricAgg = () =>
if (value === 0 && agg.params.emptyAsNull) {
return null;
}
if (value == null) {
dej611 marked this conversation as resolved.
Show resolved Hide resolved
// if the value is undefined, respect the emptyAsNull flag
return agg.params.emptyAsNull ? null : 0;
}
return value;
},
isScalable() {
Expand Down