Permalink
Browse files

Add auto interval support

  • Loading branch information...
1 parent a7f78ce commit 44acf3de5ff02fe8d1c0ad2c15fb4250e73ac555 @rashidkpc rashidkpc committed Jun 30, 2016
View
@@ -1,6 +1,6 @@
{
"name": "timelion",
- "version": "0.1.349",
+ "version": "0.1.350",
"kibanas": [
"5.0.0-alpha2",
"5.0.0-alpha3",
@@ -2,6 +2,8 @@ var _ = require('lodash');
var $ = require('jquery');
var moment = require('moment-timezone');
var observeResize = require('./observe_resize');
+var configFile = require('../../../timelion.json');
+var calculateInterval = require('../../lib/calculate_interval');
require('flot');
require('flotTime');
@@ -12,7 +14,6 @@ require('flotSymbol');
require('flotStack');
require('flotAxisLabels');
-
require('./observe_resize');
var app = require('ui/modules').get('apps/timelion', []);
@@ -187,9 +188,15 @@ app.directive('chart', function ($compile, $rootScope, timefilter, $timeout, Pri
var options = _.cloneDeep(defaultOptions);
-
// Get the X-axis tick format
- var format = getxAxisFormatter($scope.interval);
+ var time = timefilter.getBounds();
+ var interval = calculateInterval(
+ time.min.valueOf(),
+ time.max.valueOf(),
+ configFile.target_buckets || 200,
+ $scope.interval
+ );
+ var format = getxAxisFormatter(interval);
// Use moment to format ticks so we get timezone correction
options.xaxis.tickFormatter = function (val) {
@@ -13,7 +13,7 @@ app.directive('timelionInterval', function ($compile, $timeout, timefilter) {
},
template: html,
link: function ($scope, $elem) {
- $scope.intervalOptions = ['1s', '1m', '1h', '1d', '1w', '1M', '1y', 'other'];
+ $scope.intervalOptions = ['auto', '1s', '1m', '1h', '1d', '1w', '1M', '1y', 'other'];
$scope.$watch('model', function (newVal, oldVal) {
// Only run this on initialization
@@ -34,7 +34,6 @@ app.directive('timelionInterval', function ($compile, $timeout, timefilter) {
if (newVal === oldVal) return;
if (newVal === 'other') {
-
$scope.otherInterval = oldVal;
$scope.model = $scope.otherInterval;
$timeout(function () {
@@ -0,0 +1,43 @@
+module.exports = function calculateInterval(from, to, size, interval) {
+ if (interval !== 'auto') return interval;
+ return roundInterval((to - from) / size);
+};
+
+// Totally cribbed this from Kibana 3.
+// I bet there's something similar in the Kibana 4 code. Somewhere. Somehow.
+function roundInterval(interval) {
+ switch (true) {
+ case (interval <= 500): // <= 0.5s
+ return '100ms';
+ case (interval <= 5000): // <= 5s
+ return '1s';
+ case (interval <= 7500): // <= 7.5s
+ return '5s';
+ case (interval <= 15000): // <= 15s
+ return '10s';
+ case (interval <= 45000): // <= 45s
+ return '30s';
+ case (interval <= 180000): // <= 3m
+ return '1m';
+ case (interval <= 450000): // <= 9m
+ return '5m';
+ case (interval <= 1200000): // <= 20m
+ return '10m';
+ case (interval <= 2700000): // <= 45m
+ return '30m';
+ case (interval <= 7200000): // <= 2h
+ return '1h';
+ case (interval <= 21600000): // <= 6h
+ return '3h';
+ case (interval <= 86400000): // <= 24h
+ return '12h';
+ case (interval <= 604800000): // <= 1w
+ return '24h';
+ case (interval <= 1814400000): // <= 3w
+ return '1w';
+ case (interval < 3628800000): // < 2y
+ return '30d';
+ default:
+ return '1y';
+ }
+};
@@ -53,8 +53,8 @@
<p>
<h4>Intervals</h4>
You might already have one nice chart, but I'm going to operate on the assumption you don't for educational purposes. The input bar at the top has two inputs. On the left, is your expression, leave that alone for now, we'll get to it. On the right is the <strong>interval selector</strong>, which is currently set to <code>{{state.interval}}</code>.
- <span ng-show="state.interval == '1d'"><strong>Looks good!</strong></span>
- <span ng-show="state.interval != '1d'">Set it to <code>1d</code>. That's pretty safe</span>
+ <span ng-show="state.interval == 'auto'"><strong>Looks good!</strong></span>
+ <span ng-show="state.interval != 'auto'">Set it to <code>auto</code></span>.
If timelion thinks your combination of time range and interval will produce too many data points it will throw an error. You can configure that limit in <code>timelion.json</code>
</p>
<p>
@@ -190,7 +190,3 @@
</tr>
</table>
</div>
-
-
-
-
@@ -3,8 +3,6 @@ define(function (require) {
var _ = require('lodash');
var moment = require('moment');
var configFile = require('../../timelion.json');
- console.log(configFile);
-
// Used only by the savedSheets service, usually no reason to change this
module.factory('SavedSheet', function (courier) {
@@ -27,8 +25,7 @@ define(function (require) {
hits: 0,
description: '',
timelion_sheet: ['.es(*)'],
- timelion_interval: '1m',
- timelion_other_interval: '1d',
+ timelion_interval: 'auto',
timelion_chart_height: 275,
timelion_columns: configFile.default_columns || 2,
timelion_rows: configFile.default_rows || 2,
@@ -5,6 +5,8 @@ var Promise = require('bluebird');
var parseSheet = require('./lib/parse_sheet.js');
var parseDateMath = require('../lib/date_math.js');
+var calculateInterval = require('../../public/lib/calculate_interval.js');
+
var loadFunctions = require('../lib/load_functions.js');
var repositionArguments = require('./lib/reposition_arguments.js');
var indexArguments = require('./lib/index_arguments.js');
@@ -169,6 +171,13 @@ module.exports = function (tlConfig) {
tlConfig.time = request.time;
tlConfig.time.to = parseDateMath(request.time.to, true).valueOf();
tlConfig.time.from = parseDateMath(request.time.from).valueOf();
+ tlConfig.time.interval = calculateInterval(
+ tlConfig.time.from,
+ tlConfig.time.to,
+ tlConfig.file.target_buckets || 200,
+ tlConfig.time.interval
+ );
+
tlConfig.setTargetSeries();
stats.invokeTime = (new Date()).getTime();
View
@@ -9,8 +9,8 @@
"graphite": {
"url": "https://www.hostedgraphite.com/UID/ACCESS_KEY/graphite"
},
- "default_interval": "1h",
"default_rows": 2,
"default_columns": 2,
- "max_buckets": 2000
+ "max_buckets": 2000,
+ "target_buckets": 200
}

0 comments on commit 44acf3d

Please sign in to comment.