2013-09-13 15:52:13 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2013-10-05 18:41:20 -05:00
|
|
|
'config',
|
|
|
|
'kbn'
|
|
|
|
], function (angular, _, config, kbn) {
|
2013-09-13 15:52:13 -05:00
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.services');
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
module.service('timeSrv', function($rootScope, $timeout, timer) {
|
|
|
|
var self = this;
|
2014-06-06 23:38:33 -05:00
|
|
|
|
2014-08-27 09:29:48 -05:00
|
|
|
this.init = function(dashboard) {
|
2014-08-27 10:58:49 -05:00
|
|
|
timer.cancel_all();
|
|
|
|
|
|
|
|
this.dashboard = dashboard;
|
|
|
|
this.time = dashboard.time;
|
|
|
|
|
|
|
|
if(this.dashboard.refresh) {
|
|
|
|
this.set_interval(this.dashboard.refresh);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.set_interval = function (interval) {
|
|
|
|
this.dashboard.refresh = interval;
|
|
|
|
if (interval) {
|
|
|
|
var _i = kbn.interval_to_ms(interval);
|
|
|
|
this.start_scheduled_refresh(_i);
|
|
|
|
} else {
|
|
|
|
this.cancel_scheduled_refresh();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.refreshDashboard = function() {
|
|
|
|
$rootScope.$broadcast('refresh');
|
|
|
|
};
|
|
|
|
|
|
|
|
this.start_scheduled_refresh = function (after_ms) {
|
|
|
|
self.cancel_scheduled_refresh();
|
|
|
|
self.refresh_timer = timer.register($timeout(function () {
|
|
|
|
self.start_scheduled_refresh(after_ms);
|
|
|
|
self.refreshDashboard();
|
|
|
|
}, after_ms));
|
2014-08-27 09:29:48 -05:00
|
|
|
};
|
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
this.cancel_scheduled_refresh = function () {
|
|
|
|
timer.cancel(this.refresh_timer);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.setTime = function(time) {
|
|
|
|
_.extend(this.time, time);
|
|
|
|
|
|
|
|
// disable refresh if we have an absolute time
|
|
|
|
if (time.to !== 'now') {
|
|
|
|
this.old_refresh = this.dashboard.refresh;
|
|
|
|
this.set_interval(false);
|
|
|
|
}
|
|
|
|
else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
|
|
|
|
this.set_interval(this.old_refresh);
|
|
|
|
this.old_refresh = null;
|
|
|
|
}
|
|
|
|
|
2014-09-05 05:07:48 -05:00
|
|
|
$rootScope.emitAppEvent('time-range-changed', this.time);
|
2014-08-27 10:58:49 -05:00
|
|
|
$timeout(this.refreshDashboard, 0);
|
|
|
|
};
|
|
|
|
|
|
|
|
this.timeRange = function(parse) {
|
|
|
|
var _t = this.time;
|
|
|
|
if(_.isUndefined(_t) || _.isUndefined(_t.from)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if(parse === false) {
|
|
|
|
return {
|
|
|
|
from: _t.from,
|
|
|
|
to: _t.to
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
var _from = _t.from;
|
|
|
|
var _to = _t.to || new Date();
|
|
|
|
|
|
|
|
return {
|
|
|
|
from: kbn.parseDate(_from),
|
|
|
|
to: kbn.parseDate(_to)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
2014-06-08 09:08:12 -05:00
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
});
|
|
|
|
|
2014-05-19 08:31:30 -05:00
|
|
|
});
|