timepicker.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import angular from 'angular';
  4. import moment from 'moment';
  5. import * as rangeUtil from 'app/core/utils/rangeutil';
  6. export class TimePickerCtrl {
  7. static tooltipFormat = 'MMM D, YYYY HH:mm:ss';
  8. static defaults = {
  9. time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'],
  10. refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d'],
  11. };
  12. dashboard: any;
  13. panel: any;
  14. absolute: any;
  15. timeRaw: any;
  16. tooltip: string;
  17. rangeString: string;
  18. timeOptions: any;
  19. refresh: any;
  20. isOpen: boolean;
  21. isUtc: boolean;
  22. firstDayOfWeek: number;
  23. /** @ngInject */
  24. constructor(private $scope, private $rootScope, private timeSrv) {
  25. $scope.ctrl = this;
  26. $rootScope.onAppEvent('shift-time-forward', () => this.move(1), $scope);
  27. $rootScope.onAppEvent('shift-time-backward', () => this.move(-1), $scope);
  28. $rootScope.onAppEvent('refresh', () => this.init(), $scope);
  29. $rootScope.onAppEvent('dash-editor-hidden', () => this.isOpen = false, $scope);
  30. this.init();
  31. }
  32. init() {
  33. this.panel = this.dashboard.timepicker;
  34. _.defaults(this.panel, TimePickerCtrl.defaults);
  35. this.firstDayOfWeek = moment.localeData().firstDayOfWeek();
  36. var time = angular.copy(this.timeSrv.timeRange());
  37. var timeRaw = angular.copy(time.raw);
  38. if (!this.dashboard.isTimezoneUtc()) {
  39. time.from.local();
  40. time.to.local();
  41. if (moment.isMoment(timeRaw.from)) {
  42. timeRaw.from.local();
  43. }
  44. if (moment.isMoment(timeRaw.to)) {
  45. timeRaw.to.local();
  46. }
  47. this.isUtc = false;
  48. } else {
  49. this.isUtc = true;
  50. }
  51. this.rangeString = rangeUtil.describeTimeRange(timeRaw);
  52. this.absolute = {fromJs: time.from.toDate(), toJs: time.to.toDate()};
  53. this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
  54. this.tooltip += this.dashboard.formatDate(time.to);
  55. // do not update time raw when dropdown is open
  56. // as auto refresh will reset the from/to input fields
  57. if (!this.isOpen) {
  58. this.timeRaw = timeRaw;
  59. }
  60. }
  61. zoom(factor) {
  62. this.$rootScope.appEvent('zoom-out', 2);
  63. }
  64. move(direction) {
  65. var range = this.timeSrv.timeRange();
  66. var timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  67. var to, from;
  68. if (direction === -1) {
  69. to = range.to.valueOf() - timespan;
  70. from = range.from.valueOf() - timespan;
  71. } else if (direction === 1) {
  72. to = range.to.valueOf() + timespan;
  73. from = range.from.valueOf() + timespan;
  74. if (to > Date.now() && range.to < Date.now()) {
  75. to = Date.now();
  76. from = range.from.valueOf();
  77. }
  78. } else {
  79. to = range.to.valueOf();
  80. from = range.from.valueOf();
  81. }
  82. this.timeSrv.setTime({from: moment.utc(from), to: moment.utc(to)});
  83. }
  84. openDropdown() {
  85. this.init();
  86. this.isOpen = true;
  87. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  88. this.refresh = {
  89. value: this.dashboard.refresh,
  90. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  91. return {text: interval, value: interval};
  92. })
  93. };
  94. this.refresh.options.unshift({text: 'off'});
  95. this.$rootScope.appEvent('show-dash-editor', {
  96. editview: 'timepicker',
  97. scope: this.$scope,
  98. cssClass: 'gf-timepicker-dropdown',
  99. });
  100. }
  101. applyCustom() {
  102. if (this.refresh.value !== this.dashboard.refresh) {
  103. this.timeSrv.setAutoRefresh(this.refresh.value);
  104. }
  105. this.timeSrv.setTime(this.timeRaw);
  106. this.$rootScope.appEvent('hide-dash-editor');
  107. }
  108. absoluteFromChanged() {
  109. this.timeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  110. }
  111. absoluteToChanged() {
  112. this.timeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  113. }
  114. getAbsoluteMomentForTimezone(jsDate) {
  115. return this.dashboard.isTimezoneUtc() ? moment(jsDate).utc() : moment(jsDate);
  116. }
  117. setRelativeFilter(timespan) {
  118. var range = {from: timespan.from, to: timespan.to};
  119. if (this.panel.nowDelay && range.to === 'now') {
  120. range.to = 'now-' + this.panel.nowDelay;
  121. }
  122. this.timeSrv.setTime(range);
  123. this.$rootScope.appEvent('hide-dash-editor');
  124. }
  125. }
  126. export function settingsDirective() {
  127. return {
  128. restrict: 'E',
  129. templateUrl: 'public/app/features/dashboard/timepicker/settings.html',
  130. controller: TimePickerCtrl,
  131. bindToController: true,
  132. controllerAs: 'ctrl',
  133. scope: {
  134. dashboard: "="
  135. }
  136. };
  137. }
  138. export function timePickerDirective() {
  139. return {
  140. restrict: 'E',
  141. templateUrl: 'public/app/features/dashboard/timepicker/timepicker.html',
  142. controller: TimePickerCtrl,
  143. bindToController: true,
  144. controllerAs: 'ctrl',
  145. scope: {
  146. dashboard: "="
  147. }
  148. };
  149. }
  150. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  151. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  152. import {inputDateDirective} from './input_date';
  153. angular.module("grafana.directives").directive('inputDatetime', inputDateDirective);