Skip to content

Commit

Permalink
Implement histogram interval scaling in discover. Closes #49
Browse files Browse the repository at this point in the history
  • Loading branch information
Rashid Khan committed Apr 21, 2014
1 parent ce23f98 commit 32bee8d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 30 deletions.
29 changes: 22 additions & 7 deletions src/kibana/apps/discover/controllers/discover.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ define(function (require) {
var angular = require('angular');
var moment = require('moment');
var settingsHtml = require('text!../partials/settings.html');
var interval = require('utils/interval');
var datemath = require('utils/datemath');

require('notify/notify');
require('directives/timepicker');
Expand Down Expand Up @@ -78,11 +80,19 @@ define(function (require) {
indexPatternList: indexPatternList,
};

// So we can watch it.
$scope.time = timefilter.time;

// TODO: Switch this to watching time.string when we implement it
$scope.$watchCollection('time', function () {
updateDataSource();
$scope.fetch();
});

// stores the complete list of fields
$scope.fields = null;

var init = _.once(function () {
console.log(moment.duration(7.5, 's').valueOf());
return setFields()
.then(updateDataSource)
.then(function () {
Expand Down Expand Up @@ -132,7 +142,6 @@ define(function (require) {
}
]
}]}]} : undefined;
console.log($scope.chart);

return searchSource.onResults().then(onResults);
}).catch(function (err) {
Expand Down Expand Up @@ -183,6 +192,8 @@ define(function (require) {
};

function updateDataSource() {
var chartOptions;

if ($scope.opts.index !== searchSource.get('index')) {
// set the index on the savedSearch
searchSource.index($scope.opts.index);
Expand All @@ -192,9 +203,6 @@ define(function (require) {
delete $scope.columns;

setFields();

// clear the columns and fields, then refetch when we do a savedSearch
//$scope.state.columns = $scope.fields = null;
}

searchSource
Expand All @@ -208,13 +216,20 @@ define(function (require) {

if (!!$scope.opts.timefield) {
timefilter.enabled(true);
chartOptions = interval.calculate(timefilter.time.from, timefilter.time.to, 100);

searchSource
.aggs({
events: {
date_histogram: {
field: $scope.opts.timefield,
interval: '30m',
format: 'yyyy-MM-dd HH:mm'
interval: chartOptions.interval + 'ms',
format: chartOptions.format,
min_doc_count: 0,
extended_bounds: {
min: datemath.parse(timefilter.time.from).valueOf(),
max: datemath.parse(timefilter.time.to, true).valueOf()
}
}
}
});
Expand Down
5 changes: 0 additions & 5 deletions src/kibana/controllers/kibana.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ define(function (require) {
$scope.configureTemplateUrl = require('text!../partials/global_config.html');
};

// TODO: Switch this to watching timefilter.string when we implement it
$scope.$watchCollection('opts.timefilter.time', function () {
courier.fetch();
});

// expose the notification services list of notifs on the $scope so that the
// notification directive can show them on the screen
$scope.notifList = notify._notifs;
Expand Down
37 changes: 19 additions & 18 deletions src/kibana/utils/interval.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
define(function (require) {
var _ = require('lodash');
var moment = require('moment');
var datemath = require('utils/datemath');

/**
* Calculate a graph interval
Expand All @@ -13,46 +14,46 @@ define(function (require) {
*
*/
var calculate = function (from, to, target) {
from = from.valueOf();
to = to.valueOf();
from = datemath.parse(from).valueOf();
to = datemath.parse(to, true).valueOf();
return roundInterval((to - from) / target);
};

// interval should be passed in ms
var roundInterval = function (interval) {
switch (true) {
case (interval <= toMS('500ms')):
return {interval: toMS('100ms'), format: 'HH:mm:ss.SSS'};
return {interval: toMS('100ms'), format: 'hh:mm:ss.SSS'};
case (interval <= toMS('5s')):
return {interval: toMS('1s'), format: 'HH:mm:ss'};
return {interval: toMS('1s'), format: 'hh:mm:ss'};
case (interval <= toMS('7.5s')):
return {interval: toMS('5s'), format: 'HH:mm:ss'};
return {interval: toMS('5s'), format: 'hh:mm:ss'};
case (interval <= toMS('15s')):
return {interval: toMS('10s'), format: 'HH:mm:ss'};
return {interval: toMS('10s'), format: 'hh:mm:ss'};
case (interval <= toMS('45s')):
return {interval: toMS('30s'), format: 'HH:mm:ss'};
return {interval: toMS('30s'), format: 'hh:mm:ss'};
case (interval <= toMS('3m')):
return {interval: toMS('1m'), format: 'HH:mm'};
return {interval: toMS('1m'), format: 'hh:mm'};
case (interval <= toMS('9m')):
return {interval: toMS('5m'), format: 'HH:mm'};
return {interval: toMS('5m'), format: 'hh:mm'};
case (interval <= toMS('20m')):
return {interval: toMS('10m'), format: 'HH:mm'};
return {interval: toMS('10m'), format: 'hh:mm'};
case (interval <= toMS('45m')):
return {interval: toMS('30m'), format: 'YYYY-MM-DD HH:mm'};
return {interval: toMS('30m'), format: 'yyyy-MM-dd hh:mm'};
case (interval <= toMS('2h')):
return {interval: toMS('1h'), format: 'YYYY-MM-DD HH:mm'};
return {interval: toMS('1h'), format: 'yyyy-MM-dd hh:mm'};
case (interval <= toMS('6h')):
return {interval: toMS('3h'), format: 'YYYY-MM-DD HH:mm'};
return {interval: toMS('3h'), format: 'yyyy-MM-dd hh:mm'};
case (interval <= toMS('24h')):
return {interval: toMS('12h'), format: 'YYYY-MM-DD HH:mm'};
return {interval: toMS('12h'), format: 'yyyy-MM-dd hh:mm'};
case (interval <= toMS('1w')):
return {interval: toMS('1d'), format: 'YYYY-MM-DD'};
return {interval: toMS('1d'), format: 'yyyy-MM-dd'};
case (interval <= toMS('3w')):
return {interval: toMS('1w'), format: 'YYYY-MM-DD'};
return {interval: toMS('1w'), format: 'yyyy-MM-dd'};
case (interval < toMS('1y')):
return {interval: toMS('1M'), format: 'YYYY-MM'};
return {interval: toMS('1M'), format: 'yyyy-MM'};
default:
return {interval: toMS('1y'), format: 'YYYY'};
return {interval: toMS('1y'), format: 'yyyy'};
}
};

Expand Down

0 comments on commit 32bee8d

Please sign in to comment.