2013-09-13 15:52:13 -05:00
|
|
|
/*
|
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
## Timepicker2
|
2013-09-13 15:52:13 -05:00
|
|
|
|
|
|
|
### Parameters
|
|
|
|
* mode :: The default mode of the panel. Options: 'relative', 'absolute' 'since' Default: 'relative'
|
|
|
|
* time_options :: An array of possible time options. Default: ['5m','15m','1h','6h','12h','24h','2d','7d','30d']
|
|
|
|
* timespan :: The default options selected for the relative view. Default: '15m'
|
|
|
|
* timefield :: The field in which time is stored in the document.
|
|
|
|
* refresh: Object containing refresh parameters
|
|
|
|
* enable :: true/false, enable auto refresh by default. Default: false
|
|
|
|
* interval :: Seconds between auto refresh. Default: 30
|
|
|
|
* min :: The lowest interval a user may set
|
|
|
|
*/
|
|
|
|
define([
|
|
|
|
'angular',
|
|
|
|
'app',
|
2014-08-07 07:35:19 -05:00
|
|
|
'lodash',
|
2013-09-13 15:52:13 -05:00
|
|
|
'moment',
|
|
|
|
'kbn'
|
|
|
|
],
|
|
|
|
function (angular, app, _, moment, kbn) {
|
|
|
|
'use strict';
|
|
|
|
|
2014-07-28 11:11:52 -05:00
|
|
|
var module = angular.module('grafana.panels.timepicker', []);
|
2013-09-13 15:52:13 -05:00
|
|
|
app.useModule(module);
|
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
module.controller('timepicker', function($scope, $rootScope, timeSrv) {
|
2014-08-27 09:29:48 -05:00
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
$scope.panelMeta = {
|
|
|
|
status : "Stable",
|
2014-08-27 09:29:48 -05:00
|
|
|
description : ""
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Set and populate defaults
|
|
|
|
var _d = {
|
|
|
|
status : "Stable",
|
|
|
|
time_options : ['5m','15m','1h','6h','12h','24h','2d','7d','30d'],
|
2013-10-09 03:36:41 -05:00
|
|
|
refresh_intervals : ['5s','10s','30s','1m','5m','15m','30m','1h','2h','1d'],
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
2014-01-06 12:00:08 -06:00
|
|
|
|
2014-02-20 12:40:29 -06:00
|
|
|
_.defaults($scope.panel,_d);
|
2013-10-09 03:36:41 -05:00
|
|
|
|
|
|
|
// ng-pattern regexs
|
|
|
|
$scope.patterns = {
|
|
|
|
date: /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/,
|
|
|
|
hour: /^([01]?[0-9]|2[0-3])$/,
|
|
|
|
minute: /^[0-5][0-9]$/,
|
|
|
|
second: /^[0-5][0-9]$/,
|
|
|
|
millisecond: /^[0-9]*$/
|
|
|
|
};
|
|
|
|
|
2014-08-27 10:58:49 -05:00
|
|
|
$scope.timeSrv = timeSrv;
|
|
|
|
|
2014-05-27 11:18:05 -05:00
|
|
|
$scope.$on('refresh', function() {
|
|
|
|
$scope.init();
|
|
|
|
});
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
$scope.init = function() {
|
2014-08-27 09:29:48 -05:00
|
|
|
var time = timeSrv.timeRange(true);
|
2015-01-25 06:45:08 -06:00
|
|
|
$scope.panel.now = false;
|
|
|
|
|
|
|
|
var unparsed = timeSrv.timeRange(false);
|
|
|
|
if (_.isString(unparsed.to) && unparsed.to.indexOf('now') === 0) {
|
|
|
|
$scope.panel.now = true;
|
2013-09-13 15:52:13 -05:00
|
|
|
}
|
2015-01-25 06:45:08 -06:00
|
|
|
|
|
|
|
$scope.time = getScopeTimeObj(time.from, time.to);
|
2015-03-28 11:53:52 -05:00
|
|
|
|
|
|
|
$scope.onAppEvent('zoom-out', function() {
|
|
|
|
$scope.zoom(2);
|
|
|
|
});
|
2013-10-09 03:36:41 -05:00
|
|
|
};
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
$scope.customTime = function() {
|
|
|
|
// Assume the form is valid since we're setting it to something valid
|
|
|
|
$scope.input.$setValidity("dummy", true);
|
|
|
|
$scope.temptime = cloneTime($scope.time);
|
2014-08-26 03:16:21 -05:00
|
|
|
$scope.temptime.now = $scope.panel.now;
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2014-08-18 06:17:18 -05:00
|
|
|
$scope.temptime.from.date.setHours(0,0,0,0);
|
|
|
|
$scope.temptime.to.date.setHours(0,0,0,0);
|
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
// Date picker needs the date to be at the start of the day
|
2014-08-18 06:17:18 -05:00
|
|
|
if(new Date().getTimezoneOffset() < 0) {
|
2014-11-11 06:57:35 -06:00
|
|
|
$scope.temptime.from.date = moment($scope.temptime.from.date).add(1, 'days').toDate();
|
|
|
|
$scope.temptime.to.date = moment($scope.temptime.to.date).add(1, 'days').toDate();
|
2014-08-18 06:17:18 -05:00
|
|
|
}
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2014-09-24 09:26:39 -05:00
|
|
|
$scope.appEvent('show-dash-editor', {src: 'app/panels/timepicker/custom.html', scope: $scope });
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
// Constantly validate the input of the fields. This function does not change any date variables
|
|
|
|
// outside of its own scope
|
|
|
|
$scope.validate = function(time) {
|
|
|
|
// Assume the form is valid. There is a hidden dummy input for invalidating it programatically.
|
|
|
|
$scope.input.$setValidity("dummy", true);
|
|
|
|
|
|
|
|
var _from = datepickerToLocal(time.from.date),
|
|
|
|
_to = datepickerToLocal(time.to.date),
|
|
|
|
_t = time;
|
|
|
|
|
|
|
|
if($scope.input.$valid) {
|
|
|
|
|
|
|
|
_from.setHours(_t.from.hour,_t.from.minute,_t.from.second,_t.from.millisecond);
|
|
|
|
_to.setHours(_t.to.hour,_t.to.minute,_t.to.second,_t.to.millisecond);
|
|
|
|
|
|
|
|
// Check that the objects are valid and to is after from
|
|
|
|
if(isNaN(_from.getTime()) || isNaN(_to.getTime()) || _from.getTime() >= _to.getTime()) {
|
|
|
|
$scope.input.$setValidity("dummy", false);
|
|
|
|
return false;
|
2013-09-13 15:52:13 -05:00
|
|
|
}
|
|
|
|
} else {
|
2013-10-09 03:36:41 -05:00
|
|
|
return false;
|
2013-09-13 15:52:13 -05:00
|
|
|
}
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2014-08-26 03:16:21 -05:00
|
|
|
return { from: _from, to:_to, now: time.now};
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
$scope.setNow = function() {
|
|
|
|
$scope.time.to = getTimeObj(new Date());
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
/*
|
|
|
|
time : {
|
|
|
|
from: Date
|
|
|
|
to: Date
|
2013-09-13 15:52:13 -05:00
|
|
|
}
|
2013-10-09 03:36:41 -05:00
|
|
|
*/
|
|
|
|
$scope.setAbsoluteTimeFilter = function (time) {
|
|
|
|
// Create filter object
|
|
|
|
var _filter = _.clone(time);
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2014-08-26 03:16:21 -05:00
|
|
|
if(time.now) {
|
2013-10-09 03:36:41 -05:00
|
|
|
_filter.to = "now";
|
|
|
|
}
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
// Set the filter
|
2014-08-27 09:29:48 -05:00
|
|
|
$scope.panel.filter_id = timeSrv.setTime(_filter);
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
// Update our representation
|
|
|
|
$scope.time = getScopeTimeObj(time.from,time.to);
|
|
|
|
};
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
$scope.setRelativeFilter = function(timespan) {
|
|
|
|
$scope.panel.now = true;
|
2013-12-30 17:27:17 -06:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
var _filter = {
|
|
|
|
from : "now-"+timespan,
|
|
|
|
to: "now"
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
|
|
|
|
2015-01-25 06:45:08 -06:00
|
|
|
if ($scope.panel.nowDelay) {
|
|
|
|
_filter.to = 'now-' + $scope.panel.nowDelay;
|
|
|
|
}
|
|
|
|
|
2014-08-27 09:29:48 -05:00
|
|
|
timeSrv.setTime(_filter);
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
$scope.time = getScopeTimeObj(kbn.parseDate(_filter.from),new Date());
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
2013-10-01 14:19:57 -05:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
var pad = function(n, width, z) {
|
|
|
|
z = z || '0';
|
2014-06-06 23:38:33 -05:00
|
|
|
n = n.toString();
|
2013-10-09 03:36:41 -05:00
|
|
|
return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
|
|
|
|
};
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2013-10-09 03:36:41 -05:00
|
|
|
var cloneTime = function(time) {
|
|
|
|
var _n = {
|
|
|
|
from: _.clone(time.from),
|
|
|
|
to: _.clone(time.to)
|
|
|
|
};
|
|
|
|
// Create new dates as _.clone is shallow.
|
|
|
|
_n.from.date = new Date(_n.from.date);
|
|
|
|
_n.to.date = new Date(_n.to.date);
|
|
|
|
return _n;
|
|
|
|
};
|
|
|
|
|
|
|
|
var getScopeTimeObj = function(from,to) {
|
2014-08-15 01:02:16 -05:00
|
|
|
var model = { from: getTimeObj(from), to: getTimeObj(to), };
|
2014-08-14 15:30:19 -05:00
|
|
|
|
|
|
|
if (model.from.date) {
|
2014-08-18 06:17:18 -05:00
|
|
|
model.tooltip = $scope.dashboard.formatDate(model.from.date) + ' <br>to<br>';
|
|
|
|
model.tooltip += $scope.dashboard.formatDate(model.to.date);
|
2014-08-14 15:30:19 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
model.tooltip = 'Click to set time filter';
|
|
|
|
}
|
|
|
|
|
2014-08-27 09:29:48 -05:00
|
|
|
if (timeSrv.time) {
|
2014-08-15 01:02:16 -05:00
|
|
|
if ($scope.panel.now) {
|
|
|
|
model.rangeString = moment(model.from.date).fromNow() + ' to ' +
|
|
|
|
moment(model.to.date).fromNow();
|
|
|
|
}
|
|
|
|
else {
|
2014-08-19 04:12:48 -05:00
|
|
|
model.rangeString = $scope.dashboard.formatDate(model.from.date, 'MMM D, YYYY HH:mm:ss') + ' to ' +
|
|
|
|
$scope.dashboard.formatDate(model.to.date, 'MMM D, YYYY HH:mm:ss');
|
2014-08-15 01:02:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-14 15:30:19 -05:00
|
|
|
return model;
|
2013-10-09 03:36:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
var getTimeObj = function(date) {
|
|
|
|
return {
|
|
|
|
date: new Date(date),
|
|
|
|
hour: pad(date.getHours(),2),
|
|
|
|
minute: pad(date.getMinutes(),2),
|
|
|
|
second: pad(date.getSeconds(),2),
|
|
|
|
millisecond: pad(date.getMilliseconds(),3)
|
2013-09-13 15:52:13 -05:00
|
|
|
};
|
2013-10-09 03:36:41 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
// Do not use the results of this function unless you plan to use setHour/Minutes/etc on the result
|
|
|
|
var datepickerToLocal = function(date) {
|
|
|
|
date = moment(date).clone().toDate();
|
|
|
|
return moment(new Date(date.getTime() + date.getTimezoneOffset() * 60000)).toDate();
|
|
|
|
};
|
|
|
|
|
2015-03-28 11:53:52 -05:00
|
|
|
$scope.zoom = function(factor) {
|
|
|
|
var range = timeSrv.timeRange();
|
|
|
|
|
|
|
|
var timespan = (range.to.valueOf() - range.from.valueOf());
|
|
|
|
var center = range.to.valueOf() - timespan/2;
|
|
|
|
|
|
|
|
var to = (center + (timespan*factor)/2);
|
|
|
|
var from = (center - (timespan*factor)/2);
|
|
|
|
|
|
|
|
if(to > Date.now() && range.to <= Date.now()) {
|
|
|
|
var offset = to - Date.now();
|
|
|
|
from = from - offset;
|
|
|
|
to = Date.now();
|
|
|
|
}
|
|
|
|
|
|
|
|
timeSrv.setTime({
|
|
|
|
from: moment.utc(from).toDate(),
|
|
|
|
to: moment.utc(to).toDate(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2013-09-13 15:52:13 -05:00
|
|
|
});
|
2013-10-09 03:36:41 -05:00
|
|
|
});
|