2017-12-20 05:33:33 -06:00
|
|
|
import _ from 'lodash';
|
|
|
|
import angular from 'angular';
|
|
|
|
import moment from 'moment';
|
2015-12-22 06:59:11 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import * as rangeUtil from 'app/core/utils/rangeutil';
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-12 04:49:14 -05:00
|
|
|
export class TimePickerCtrl {
|
2017-12-20 05:33:33 -06:00
|
|
|
static tooltipFormat = 'MMM D, YYYY HH:mm:ss';
|
2015-09-12 05:52:50 -05:00
|
|
|
static defaults = {
|
2017-12-20 05:33:33 -06:00
|
|
|
time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'],
|
2017-12-21 01:39:31 -06:00
|
|
|
refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d'],
|
2015-09-12 05:52:50 -05:00
|
|
|
};
|
2014-01-06 12:00:08 -06:00
|
|
|
|
2015-09-18 05:41:32 -05:00
|
|
|
dashboard: any;
|
|
|
|
panel: any;
|
|
|
|
absolute: any;
|
|
|
|
timeRaw: any;
|
2017-07-24 05:21:30 -05:00
|
|
|
editTimeRaw: any;
|
2015-09-18 05:41:32 -05:00
|
|
|
tooltip: string;
|
|
|
|
rangeString: string;
|
|
|
|
timeOptions: any;
|
|
|
|
refresh: any;
|
2015-09-18 14:01:13 -05:00
|
|
|
isUtc: boolean;
|
2016-07-05 09:08:44 -05:00
|
|
|
firstDayOfWeek: number;
|
2017-12-08 07:32:15 -06:00
|
|
|
isOpen: boolean;
|
2018-08-30 04:52:31 -05:00
|
|
|
isAbsolute: boolean;
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2015-12-21 09:34:18 -06:00
|
|
|
/** @ngInject */
|
2015-09-18 05:41:32 -05:00
|
|
|
constructor(private $scope, private $rootScope, private timeSrv) {
|
2017-12-03 00:53:47 -06:00
|
|
|
this.$scope.ctrl = this;
|
2014-08-27 10:58:49 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
$rootScope.onAppEvent('shift-time-forward', () => this.move(1), $scope);
|
|
|
|
$rootScope.onAppEvent('shift-time-backward', () => this.move(-1), $scope);
|
2018-03-29 02:15:15 -05:00
|
|
|
$rootScope.onAppEvent('closeTimepicker', this.openDropdown.bind(this), $scope);
|
2018-08-25 10:49:39 -05:00
|
|
|
|
|
|
|
this.dashboard.on('refresh', this.onRefresh.bind(this), $scope);
|
2015-01-25 06:45:08 -06:00
|
|
|
|
2017-07-24 05:21:30 -05:00
|
|
|
// init options
|
2015-09-18 05:41:32 -05:00
|
|
|
this.panel = this.dashboard.timepicker;
|
|
|
|
_.defaults(this.panel, TimePickerCtrl.defaults);
|
2016-07-05 09:08:44 -05:00
|
|
|
this.firstDayOfWeek = moment.localeData().firstDayOfWeek();
|
|
|
|
|
2017-07-24 05:21:30 -05:00
|
|
|
// init time stuff
|
|
|
|
this.onRefresh();
|
|
|
|
}
|
|
|
|
|
|
|
|
onRefresh() {
|
2018-08-26 13:19:23 -05:00
|
|
|
const time = angular.copy(this.timeSrv.timeRange());
|
|
|
|
const timeRaw = angular.copy(time.raw);
|
2015-09-18 14:01:13 -05:00
|
|
|
|
2016-04-03 08:05:43 -05:00
|
|
|
if (!this.dashboard.isTimezoneUtc()) {
|
2015-09-18 14:01:13 -05:00
|
|
|
time.from.local();
|
|
|
|
time.to.local();
|
|
|
|
if (moment.isMoment(timeRaw.from)) {
|
|
|
|
timeRaw.from.local();
|
|
|
|
}
|
|
|
|
if (moment.isMoment(timeRaw.to)) {
|
|
|
|
timeRaw.to.local();
|
|
|
|
}
|
2017-07-03 02:20:55 -05:00
|
|
|
this.isUtc = false;
|
2015-09-18 14:01:13 -05:00
|
|
|
} else {
|
|
|
|
this.isUtc = true;
|
|
|
|
}
|
2015-08-19 06:25:20 -05:00
|
|
|
|
2015-09-18 06:54:31 -05:00
|
|
|
this.rangeString = rangeUtil.describeTimeRange(timeRaw);
|
2017-12-19 09:06:54 -06:00
|
|
|
this.absolute = { fromJs: time.from.toDate(), toJs: time.to.toDate() };
|
2017-12-20 05:33:33 -06:00
|
|
|
this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
|
2015-09-18 05:41:32 -05:00
|
|
|
this.tooltip += this.dashboard.formatDate(time.to);
|
2017-07-24 05:21:30 -05:00
|
|
|
this.timeRaw = timeRaw;
|
2018-08-30 04:52:31 -05:00
|
|
|
this.isAbsolute = moment.isMoment(this.timeRaw.to);
|
2015-09-18 05:41:32 -05:00
|
|
|
}
|
2015-09-18 01:17:19 -05:00
|
|
|
|
2015-09-18 07:04:53 -05:00
|
|
|
zoom(factor) {
|
2017-12-20 05:33:33 -06:00
|
|
|
this.$rootScope.appEvent('zoom-out', 2);
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2016-04-07 03:30:49 -05:00
|
|
|
move(direction) {
|
2018-08-26 13:19:23 -05:00
|
|
|
const range = this.timeSrv.timeRange();
|
2016-04-07 03:30:49 -05:00
|
|
|
|
2018-08-26 13:19:23 -05:00
|
|
|
const timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
|
2018-08-30 01:58:43 -05:00
|
|
|
let to, from;
|
2016-04-07 03:30:49 -05:00
|
|
|
if (direction === -1) {
|
|
|
|
to = range.to.valueOf() - timespan;
|
|
|
|
from = range.from.valueOf() - timespan;
|
|
|
|
} else if (direction === 1) {
|
|
|
|
to = range.to.valueOf() + timespan;
|
|
|
|
from = range.from.valueOf() + timespan;
|
|
|
|
if (to > Date.now() && range.to < Date.now()) {
|
|
|
|
to = Date.now();
|
|
|
|
from = range.from.valueOf();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
to = range.to.valueOf();
|
|
|
|
from = range.from.valueOf();
|
|
|
|
}
|
|
|
|
|
2017-12-19 09:06:54 -06:00
|
|
|
this.timeSrv.setTime({ from: moment.utc(from), to: moment.utc(to) });
|
2016-04-07 03:30:49 -05:00
|
|
|
}
|
|
|
|
|
2015-09-18 01:17:19 -05:00
|
|
|
openDropdown() {
|
2017-12-08 07:32:15 -06:00
|
|
|
if (this.isOpen) {
|
2018-04-04 09:20:01 -05:00
|
|
|
this.closeDropdown();
|
2017-12-08 07:32:15 -06:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-24 05:21:30 -05:00
|
|
|
this.onRefresh();
|
|
|
|
this.editTimeRaw = this.timeRaw;
|
2017-12-21 01:39:31 -06:00
|
|
|
this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
|
2015-09-18 05:41:32 -05:00
|
|
|
this.refresh = {
|
|
|
|
value: this.dashboard.refresh,
|
|
|
|
options: _.map(this.panel.refresh_intervals, (interval: any) => {
|
2017-12-19 09:06:54 -06:00
|
|
|
return { text: interval, value: interval };
|
2017-12-20 05:33:33 -06:00
|
|
|
}),
|
2015-09-18 03:36:47 -05:00
|
|
|
};
|
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
this.refresh.options.unshift({ text: 'off' });
|
2017-12-08 07:32:15 -06:00
|
|
|
this.isOpen = true;
|
2018-04-04 09:20:01 -05:00
|
|
|
this.$rootScope.appEvent('timepickerOpen');
|
|
|
|
}
|
|
|
|
|
|
|
|
closeDropdown() {
|
|
|
|
this.isOpen = false;
|
|
|
|
this.$rootScope.appEvent('timepickerClosed');
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2013-09-13 15:52:13 -05:00
|
|
|
|
2015-09-18 03:36:47 -05:00
|
|
|
applyCustom() {
|
2015-09-18 05:41:32 -05:00
|
|
|
if (this.refresh.value !== this.dashboard.refresh) {
|
|
|
|
this.timeSrv.setAutoRefresh(this.refresh.value);
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2015-01-25 06:45:08 -06:00
|
|
|
|
2017-07-24 05:21:30 -05:00
|
|
|
this.timeSrv.setTime(this.editTimeRaw);
|
2018-04-04 09:20:01 -05:00
|
|
|
this.closeDropdown();
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2014-08-14 15:30:19 -05:00
|
|
|
|
2015-09-18 04:01:37 -05:00
|
|
|
absoluteFromChanged() {
|
2017-12-21 01:39:31 -06:00
|
|
|
this.editTimeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
|
2015-09-18 04:01:37 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
absoluteToChanged() {
|
2017-07-24 05:21:30 -05:00
|
|
|
this.editTimeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
|
2015-09-18 14:01:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
getAbsoluteMomentForTimezone(jsDate) {
|
2017-12-21 01:39:31 -06:00
|
|
|
return this.dashboard.isTimezoneUtc() ? moment(jsDate).utc() : moment(jsDate);
|
2015-09-18 04:01:37 -05:00
|
|
|
}
|
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
setRelativeFilter(timespan) {
|
2018-08-26 13:19:23 -05:00
|
|
|
const range = { from: timespan.from, to: timespan.to };
|
2014-08-15 01:02:16 -05:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
if (this.panel.nowDelay && range.to === 'now') {
|
|
|
|
range.to = 'now-' + this.panel.nowDelay;
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2013-10-09 03:36:41 -05:00
|
|
|
|
2015-09-12 05:52:50 -05:00
|
|
|
this.timeSrv.setTime(range);
|
2018-04-04 09:20:01 -05:00
|
|
|
this.closeDropdown();
|
2015-09-12 05:52:50 -05:00
|
|
|
}
|
2015-09-12 04:49:14 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export function settingsDirective() {
|
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'E',
|
|
|
|
templateUrl: 'public/app/features/dashboard/timepicker/settings.html',
|
2015-09-12 04:49:14 -05:00
|
|
|
controller: TimePickerCtrl,
|
2015-09-19 05:20:24 -05:00
|
|
|
bindToController: true,
|
2017-12-20 05:33:33 -06:00
|
|
|
controllerAs: 'ctrl',
|
2015-09-19 05:20:24 -05:00
|
|
|
scope: {
|
2017-12-20 05:33:33 -06:00
|
|
|
dashboard: '=',
|
|
|
|
},
|
2015-09-12 04:49:14 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function timePickerDirective() {
|
|
|
|
return {
|
2017-12-20 05:33:33 -06:00
|
|
|
restrict: 'E',
|
|
|
|
templateUrl: 'public/app/features/dashboard/timepicker/timepicker.html',
|
2015-09-12 04:49:14 -05:00
|
|
|
controller: TimePickerCtrl,
|
2015-09-18 05:41:32 -05:00
|
|
|
bindToController: true,
|
2017-12-20 05:33:33 -06:00
|
|
|
controllerAs: 'ctrl',
|
2015-09-18 05:41:32 -05:00
|
|
|
scope: {
|
2017-12-20 05:33:33 -06:00
|
|
|
dashboard: '=',
|
|
|
|
},
|
2015-09-12 04:49:14 -05:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:39:31 -06:00
|
|
|
angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
|
|
|
|
angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
|
2017-12-19 09:06:54 -06:00
|
|
|
|
2017-12-20 05:33:33 -06:00
|
|
|
import { inputDateDirective } from './input_date';
|
2017-12-21 01:39:31 -06:00
|
|
|
angular.module('grafana.directives').directive('inputDatetime', inputDateDirective);
|