2013-09-13 15:52:13 -05:00
|
|
|
define([
|
|
|
|
'angular',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2015-09-16 12:49:05 -05:00
|
|
|
'moment',
|
2015-10-30 08:19:02 -05:00
|
|
|
'app/core/config',
|
2015-10-30 08:44:40 -05:00
|
|
|
'app/core/utils/kbn',
|
2015-09-16 12:49:05 -05:00
|
|
|
'app/core/utils/datemath'
|
2015-10-30 08:44:40 -05:00
|
|
|
], function (angular, _, moment, config, kbn, dateMath) {
|
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-09-10 03:46:04 -05:00
|
|
|
module.service('timeSrv', function($rootScope, $timeout, $routeParams, timer) {
|
2014-08-27 10:58:49 -05:00
|
|
|
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;
|
|
|
|
|
2014-09-10 03:46:04 -05:00
|
|
|
this._initTimeFromUrl();
|
2015-01-26 03:57:08 -06:00
|
|
|
this._parseTime();
|
2014-09-10 03:46:04 -05:00
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
if(this.dashboard.refresh) {
|
2015-09-18 03:36:47 -05:00
|
|
|
this.setAutoRefresh(this.dashboard.refresh);
|
2014-08-27 10:58:49 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-01-26 03:57:08 -06:00
|
|
|
this._parseTime = function() {
|
|
|
|
// when absolute time is saved in json it is turned to a string
|
|
|
|
if (_.isString(this.time.from) && this.time.from.indexOf('Z') >= 0) {
|
2015-09-18 14:01:13 -05:00
|
|
|
this.time.from = moment(this.time.from).utc();
|
2015-01-26 03:57:08 -06:00
|
|
|
}
|
|
|
|
if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
|
2015-09-18 14:01:13 -05:00
|
|
|
this.time.to = moment(this.time.to).utc();
|
2015-01-26 03:57:08 -06:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-10 03:46:04 -05:00
|
|
|
this._parseUrlParam = function(value) {
|
|
|
|
if (value.indexOf('now') !== -1) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value.length === 8) {
|
2015-09-17 04:21:38 -05:00
|
|
|
return moment.utc(value, 'YYYYMMDD');
|
2014-09-10 03:46:04 -05:00
|
|
|
}
|
|
|
|
if (value.length === 15) {
|
2015-09-17 04:21:38 -05:00
|
|
|
return moment.utc(value, 'YYYYMMDDTHHmmss');
|
2014-09-10 03:46:04 -05:00
|
|
|
}
|
2015-12-04 03:32:23 -06:00
|
|
|
|
|
|
|
if (!isNaN(value)) {
|
|
|
|
var epoch = parseInt(value);
|
2015-09-17 04:21:38 -05:00
|
|
|
return moment(epoch);
|
2014-09-10 03:46:04 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|
|
|
|
|
|
|
|
this._initTimeFromUrl = function() {
|
|
|
|
if ($routeParams.from) {
|
|
|
|
this.time.from = this._parseUrlParam($routeParams.from) || this.time.from;
|
|
|
|
}
|
|
|
|
if ($routeParams.to) {
|
|
|
|
this.time.to = this._parseUrlParam($routeParams.to) || this.time.to;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-09-18 03:36:47 -05:00
|
|
|
this.setAutoRefresh = function (interval) {
|
2014-08-27 10:58:49 -05:00
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
2015-11-29 05:12:48 -06:00
|
|
|
this.setTime = function(time, enableRefresh) {
|
2014-08-27 10:58:49 -05:00
|
|
|
_.extend(this.time, time);
|
|
|
|
|
2015-11-29 05:12:48 -06:00
|
|
|
// disable refresh if zoom in or zoom out
|
|
|
|
if (!enableRefresh && moment.isMoment(time.to)) {
|
|
|
|
this.old_refresh = this.dashboard.refresh || this.old_refresh;
|
|
|
|
this.setAutoRefresh(false);
|
|
|
|
}
|
|
|
|
else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
|
2015-09-18 03:36:47 -05:00
|
|
|
this.setAutoRefresh(this.old_refresh);
|
2014-08-27 10:58:49 -05:00
|
|
|
this.old_refresh = null;
|
|
|
|
}
|
|
|
|
|
2014-09-24 09:26:39 -05:00
|
|
|
$rootScope.appEvent('time-range-changed', this.time);
|
2014-08-27 10:58:49 -05:00
|
|
|
$timeout(this.refreshDashboard, 0);
|
|
|
|
};
|
|
|
|
|
2014-11-11 04:48:27 -06:00
|
|
|
this.timeRangeForUrl = function() {
|
|
|
|
var range = this.timeRange(false);
|
|
|
|
if (_.isString(range.to) && range.to.indexOf('now')) {
|
|
|
|
range = this.timeRange();
|
|
|
|
}
|
|
|
|
|
2015-09-17 02:57:59 -05:00
|
|
|
if (moment.isMoment(range.from)) { range.from = range.from.valueOf(); }
|
|
|
|
if (moment.isMoment(range.to)) { range.to = range.to.valueOf(); }
|
2014-11-11 04:48:27 -06:00
|
|
|
|
|
|
|
return range;
|
|
|
|
};
|
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
this.timeRange = function(parse) {
|
2015-09-18 14:01:13 -05:00
|
|
|
// make copies if they are moment (do not want to return out internal moment, because they are mutable!)
|
|
|
|
var from = moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from ;
|
|
|
|
var to = moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to ;
|
2015-01-25 06:45:08 -06:00
|
|
|
|
2015-09-18 14:01:13 -05:00
|
|
|
if (parse !== false) {
|
|
|
|
from = dateMath.parse(from, false);
|
|
|
|
to = dateMath.parse(to, true);
|
2014-08-27 10:58:49 -05:00
|
|
|
}
|
2015-09-18 14:01:13 -05:00
|
|
|
|
|
|
|
return {from: from, to: to};
|
2014-08-27 10:58:49 -05:00
|
|
|
};
|
2014-06-08 09:08:12 -05:00
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
});
|
|
|
|
|
2014-05-19 08:31:30 -05:00
|
|
|
});
|