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',
|
2014-09-10 03:46:04 -05:00
|
|
|
'kbn',
|
|
|
|
'moment'
|
|
|
|
], function (angular, _, config, kbn, moment) {
|
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) {
|
|
|
|
this.set_interval(this.dashboard.refresh);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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) {
|
|
|
|
this.time.from = new Date(this.time.from);
|
|
|
|
}
|
|
|
|
if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
|
|
|
|
this.time.to = new Date(this.time.to);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-10 03:46:04 -05:00
|
|
|
this._parseUrlParam = function(value) {
|
|
|
|
if (value.indexOf('now') !== -1) {
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
if (value.length === 8) {
|
|
|
|
return moment.utc(value, 'YYYYMMDD').toDate();
|
|
|
|
}
|
|
|
|
if (value.length === 15) {
|
|
|
|
return moment.utc(value, 'YYYYMMDDTHHmmss').toDate();
|
|
|
|
}
|
|
|
|
var epoch = parseInt(value);
|
|
|
|
if (!_.isNaN(epoch)) {
|
|
|
|
return new Date(epoch);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
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
|
2015-05-26 09:48:23 -05:00
|
|
|
if (time.to && time.to.indexOf('now') === -1) {
|
2015-02-03 02:27:00 -06:00
|
|
|
this.old_refresh = this.dashboard.refresh || this.old_refresh;
|
2014-08-27 10:58:49 -05:00
|
|
|
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-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();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (_.isDate(range.from)) { range.from = range.from.getTime(); }
|
|
|
|
if (_.isDate(range.to)) { range.to = range.to.getTime(); }
|
|
|
|
|
|
|
|
return range;
|
|
|
|
};
|
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
this.timeRange = function(parse) {
|
|
|
|
var _t = this.time;
|
2015-01-25 06:45:08 -06:00
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
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
|
|
|
});
|