Add support for timelion:min_interval (#11476)

* Add support for timelion:min_interval

* import -> require
This commit is contained in:
Rashid Khan 2017-05-09 09:29:55 -07:00 committed by GitHub
parent 817c4b42a7
commit d04f393d91
6 changed files with 60 additions and 5 deletions

View file

@ -0,0 +1,44 @@
const filename = require('path').basename(__filename);
const fn = require(`../calculate_interval`);
const moment = require('moment');
const expect = require('chai').expect;
const from = (count, unit) => moment().subtract(count, unit).valueOf();
const to = moment().valueOf();
const size = 200;
const min = '1ms';
describe(filename, () => {
it('Exports a function', () => {
expect(fn).to.be.a('function');
});
it('Only calculates when interval = auto', () => {
const partialFn = (interval) => fn(from(1, 'y'), to, size, interval, min);
expect(partialFn('1ms')).to.equal('1ms');
expect(partialFn('bag_of_beans')).to.equal('bag_of_beans');
expect(partialFn('auto')).to.not.equal('auto');
});
it('Calculates nice round intervals', () => {
const partialFn = (count, unit) => fn(from(count, unit), to, size, 'auto', min);
expect(partialFn(15, 'm')).to.equal('1s');
expect(partialFn(1, 'h')).to.equal('30s');
expect(partialFn(3, 'd')).to.equal('30m');
expect(partialFn(1, 'w')).to.equal('1h');
expect(partialFn(1, 'y')).to.equal('24h');
expect(partialFn(100, 'y')).to.equal('1y');
});
it('Does not calculate an interval lower than the minimum', () => {
const partialFn = (count, unit) => fn(from(count, unit), to, size, 'auto', '1m');
expect(partialFn(5, 's')).to.equal('1m');
expect(partialFn(15, 'm')).to.equal('1m');
expect(partialFn(1, 'h')).to.equal('1m');
expect(partialFn(3, 'd')).to.equal('30m');
expect(partialFn(1, 'w')).to.equal('1h');
expect(partialFn(1, 'y')).to.equal('24h');
expect(partialFn(100, 'y')).to.equal('1y');
});
});

View file

@ -1,6 +1,10 @@
module.exports = function calculateInterval(from, to, size, interval) {
const toMS = require('../../server/lib/to_milliseconds.js');
module.exports = function calculateInterval(from, to, size, interval, min) {
if (interval !== 'auto') return interval;
return roundInterval((to - from) / size);
const dateMathInterval = roundInterval((to - from) / size);
if (toMS(dateMathInterval) < toMS(min)) return min;
return dateMathInterval;
};
// Totally cribbed this from Kibana 3.

View file

@ -187,7 +187,8 @@ module.exports = function timechartFn(Private, config, $rootScope, timefilter, $
time.min.valueOf(),
time.max.valueOf(),
config.get('timelion:target_buckets') || 200,
$scope.interval
$scope.interval,
config.get('timelion:min_interval') || '1ms',
);
const format = getxAxisFormatter(interval);

View file

@ -169,7 +169,8 @@ module.exports = function (tlConfig) {
tlConfig.time.from,
tlConfig.time.to,
tlConfig.settings['timelion:target_buckets'] || 200,
tlConfig.time.interval
tlConfig.time.interval,
tlConfig.settings['timelion:min_interval'] || '1ms',
);
tlConfig.setTargetSeries();

View file

@ -12,5 +12,6 @@
"default_rows": 2,
"default_columns": 2,
"max_buckets": 2000,
"target_buckets": 200
"target_buckets": 200,
"min_interval": "1ms"
}

View file

@ -297,6 +297,10 @@ export default function defaultSettingsProvider() {
value: 2,
description: 'Number of rows on a timelion sheet by default'
},
'timelion:min_interval': {
value: '1ms',
description: 'The smallest interval that will be calculated when using "auto"'
},
'timelion:graphite.url': {
value: 'https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite',
description: '<em>[experimental]</em> The URL of your graphite host'