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

[ui/timeBuckets/calcAutoInterval] Refactor #24669

Merged
merged 5 commits into from
Oct 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 0 additions & 88 deletions src/ui/public/time_buckets/calc_auto_interval.js

This file was deleted.

116 changes: 116 additions & 0 deletions src/ui/public/time_buckets/calc_auto_interval.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import moment from 'moment';

import { calcAutoIntervalLessThan, calcAutoIntervalNear } from './calc_auto_interval';

describe('calcAutoIntervalNear', () => {
test('100 buckets/1ms = 1ms buckets', () => {
spalger marked this conversation as resolved.
Show resolved Hide resolved
const interval = calcAutoIntervalNear(100, moment.duration(1, 'ms'));
expect(interval.asMilliseconds()).toBe(1);
});

test('100 buckets/200ms = 2ms buckets', () => {
const interval = calcAutoIntervalNear(100, moment.duration(200, 'ms'));
expect(interval.asMilliseconds()).toBe(2);
});

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

test('1000 buckets/1000h = 1h buckets', () => {
const interval = calcAutoIntervalNear(1000, moment.duration(1000, 'hours'));
expect(interval.asHours()).toBe(1);
});

test('100 buckets/1h = 30s buckets', () => {
const interval = calcAutoIntervalNear(100, moment.duration(1, 'hours'));
expect(interval.asSeconds()).toBe(30);
});

test('25 buckets/1d = 1h buckets', () => {
const interval = calcAutoIntervalNear(25, moment.duration(1, 'day'));
expect(interval.asHours()).toBe(1);
});

test('1000 buckets/1y = 12h buckets', () => {
const interval = calcAutoIntervalNear(1000, moment.duration(1, 'year'));
expect(interval.asHours()).toBe(12);
});

test('10000 buckets/1y = 1h buckets', () => {
const interval = calcAutoIntervalNear(10000, moment.duration(1, 'year'));
expect(interval.asHours()).toBe(1);
});

test('100000 buckets/1y = 5m buckets', () => {
const interval = calcAutoIntervalNear(100000, moment.duration(1, 'year'));
expect(interval.asMinutes()).toBe(5);
});
});

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

test('100 buckets/200ms = 2ms buckets', () => {
const interval = calcAutoIntervalLessThan(100, moment.duration(200, 'ms'));
expect(interval.asMilliseconds()).toBe(2);
});

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

test('1000 buckets/1000h = 1h buckets', () => {
const interval = calcAutoIntervalLessThan(1000, moment.duration(1000, 'hours'));
expect(interval.asHours()).toBe(1);
});

test('100 buckets/1h = 30s buckets', () => {
const interval = calcAutoIntervalLessThan(100, moment.duration(1, 'hours'));
expect(interval.asSeconds()).toBe(30);
});

test('25 buckets/1d = 30m buckets', () => {
const interval = calcAutoIntervalLessThan(25, moment.duration(1, 'day'));
expect(interval.asMinutes()).toBe(30);
});

test('1000 buckets/1y = 3h buckets', () => {
const interval = calcAutoIntervalLessThan(1000, moment.duration(1, 'year'));
expect(interval.asHours()).toBe(3);
});

test('10000 buckets/1y = 30m buckets', () => {
const interval = calcAutoIntervalLessThan(10000, moment.duration(1, 'year'));
expect(interval.asMinutes()).toBe(30);
});

test('100000 buckets/1y = 5m buckets', () => {
const interval = calcAutoIntervalLessThan(100000, moment.duration(1, 'year'));
expect(interval.asMinutes()).toBe(5);
});
});
129 changes: 129 additions & 0 deletions src/ui/public/time_buckets/calc_auto_interval.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import moment, { Duration } from 'moment';

const rules = [
{
bound: Infinity,
interval: moment.duration(1, 'year'),
},
{
bound: moment.duration(1, 'year'),
interval: moment.duration(1, 'month'),
},
{
bound: moment.duration(3, 'week'),
interval: moment.duration(1, 'week'),
},
{
bound: moment.duration(1, 'week'),
interval: moment.duration(1, 'd'),
},
{
bound: moment.duration(24, 'hour'),
interval: moment.duration(12, 'hour'),
},
{
bound: moment.duration(6, 'hour'),
interval: moment.duration(3, 'hour'),
},
{
bound: moment.duration(2, 'hour'),
interval: moment.duration(1, 'hour'),
},
{
bound: moment.duration(45, 'minute'),
interval: moment.duration(30, 'minute'),
},
{
bound: moment.duration(20, 'minute'),
interval: moment.duration(10, 'minute'),
},
{
bound: moment.duration(9, 'minute'),
interval: moment.duration(5, 'minute'),
},
{
bound: moment.duration(3, 'minute'),
interval: moment.duration(1, 'minute'),
},
{
bound: moment.duration(45, 'second'),
interval: moment.duration(30, 'second'),
},
{
bound: moment.duration(15, 'second'),
interval: moment.duration(10, 'second'),
},
{
bound: moment.duration(7.5, 'second'),
interval: moment.duration(5, 'second'),
},
{
bound: moment.duration(5, 'second'),
interval: moment.duration(1, 'second'),
},
{
bound: moment.duration(500, 'ms'),
interval: moment.duration(100, 'ms'),
},
];

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

/**
* Using some simple rules we pick a "pretty" interval that will
* produce around the number of buckets desired given a time range.
*
* @param targetBucketCount desired number of buckets
* @param duration time range the agg covers
*/
export function calcAutoIntervalNear(targetBucketCount: number, duration: Duration) {
spalger marked this conversation as resolved.
Show resolved Hide resolved
const targetMs = Number(duration) / targetBucketCount;

for (let i = 0; i < rules.length - 1; i++) {
if (Number(rules[i + 1].bound) <= targetMs) {
spalger marked this conversation as resolved.
Show resolved Hide resolved
return rules[i].interval.clone();
}
}

return defaultInterval(targetMs);
}

/**
* Pick a "pretty" interval that produces no more than the maxBucketCount
* for the given time range.
*
* @param maxBucketCount maximum number of buckets to create
* @param duration amount of time covered by the agg
*/
export function calcAutoIntervalLessThan(maxBucketCount: number, duration: Duration) {
spalger marked this conversation as resolved.
Show resolved Hide resolved
const maxMs = Number(duration) / maxBucketCount;

for (const { interval } of rules) {
if (Number(interval) <= maxMs) {
return interval.clone();
}
}

return defaultInterval(maxMs);
}
6 changes: 3 additions & 3 deletions src/ui/public/time_buckets/time_buckets.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import _ from 'lodash';
import moment from 'moment';
import chrome from '../chrome';
import { parseInterval } from '../utils/parse_interval';
import { calcAutoInterval } from './calc_auto_interval';
import { calcAutoIntervalLessThan, calcAutoIntervalNear } from './calc_auto_interval';
import {
convertDurationToNormalizedEsInterval,
convertIntervalToEsInterval,
Expand Down Expand Up @@ -231,7 +231,7 @@ TimeBuckets.prototype.getInterval = function (useNormalizedEsInterval = true) {
function readInterval() {
const interval = self._i;
if (moment.isDuration(interval)) return interval;
return calcAutoInterval.near(config.get('histogram:barTarget'), duration);
return calcAutoIntervalNear(config.get('histogram:barTarget'), duration);
}

// check to see if the interval should be scaled, and scale it if so
Expand All @@ -243,7 +243,7 @@ TimeBuckets.prototype.getInterval = function (useNormalizedEsInterval = true) {
let scaled;

if (approxLen > maxLength) {
scaled = calcAutoInterval.lessThan(maxLength, duration);
scaled = calcAutoIntervalLessThan(maxLength, duration);
} else {
return interval;
}
Expand Down