Skip to content

Commit

Permalink
[calcAutoInterval] return 0ms when duration is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
spalger committed Oct 26, 2018
1 parent a071868 commit 3ea56af
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
20 changes: 20 additions & 0 deletions src/ui/public/time_buckets/calc_auto_interval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ import moment from 'moment';
import { calcAutoIntervalLessThan, calcAutoIntervalNear } from './calc_auto_interval';

describe('calcAutoIntervalNear', () => {
test('0 buckets/1h = 0ms buckets', () => {
const interval = calcAutoIntervalNear(0, moment.duration(1, 'h'));
expect(interval.asMilliseconds()).toBe(0);
});

test('100 buckets/undefined = 0ms buckets', () => {
const interval = calcAutoIntervalNear(0, undefined as any);
expect(interval.asMilliseconds()).toBe(0);
});

test('100 buckets/1ms = 1ms buckets', () => {
const interval = calcAutoIntervalNear(100, moment.duration(1, 'ms'));
expect(interval.asMilliseconds()).toBe(1);
Expand Down Expand Up @@ -69,6 +79,16 @@ describe('calcAutoIntervalNear', () => {
});

describe('calcAutoIntervalLessThan', () => {
test('0 buckets/1h = 0ms buckets', () => {
const interval = calcAutoIntervalLessThan(0, moment.duration(1, 'h'));
expect(interval.asMilliseconds()).toBe(0);
});

test('100 buckets/undefined = 0ms buckets', () => {
const interval = calcAutoIntervalLessThan(0, undefined as any);
expect(interval.asMilliseconds()).toBe(0);
});

test('100 buckets/1ms = 1ms buckets', () => {
const interval = calcAutoIntervalLessThan(100, moment.duration(1, 'ms'));
expect(interval.asMilliseconds()).toBe(1);
Expand Down
11 changes: 8 additions & 3 deletions src/ui/public/time_buckets/calc_auto_interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,13 @@ const rules = [
},
];

function estimateBucketMs(count: number, duration: Duration) {
const ms = Number(duration) / count;
return isFinite(ms) ? ms : NaN;
}

function defaultInterval(targetMs: number) {
return moment.duration(Math.max(Math.floor(targetMs), 1), 'ms');
return moment.duration(isNaN(targetMs) ? 0 : Math.max(Math.floor(targetMs), 1), 'ms');
}

/**
Expand All @@ -98,7 +103,7 @@ function defaultInterval(targetMs: number) {
* @param duration time range the agg covers
*/
export function calcAutoIntervalNear(targetBucketCount: number, duration: Duration) {
const targetMs = Number(duration) / targetBucketCount;
const targetMs = estimateBucketMs(targetBucketCount, duration);

for (let i = 0; i < rules.length - 1; i++) {
if (Number(rules[i + 1].bound) <= targetMs) {
Expand All @@ -117,7 +122,7 @@ export function calcAutoIntervalNear(targetBucketCount: number, duration: Durati
* @param duration amount of time covered by the agg
*/
export function calcAutoIntervalLessThan(maxBucketCount: number, duration: Duration) {
const maxMs = Number(duration) / maxBucketCount;
const maxMs = estimateBucketMs(maxBucketCount, duration);

for (const { interval } of rules) {
if (Number(interval) <= maxMs) {
Expand Down

0 comments on commit 3ea56af

Please sign in to comment.