TimePickerCtrl.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import _ from 'lodash';
  2. import angular from 'angular';
  3. import moment from 'moment';
  4. import * as rangeUtil from '@grafana/ui/src/utils/rangeutil';
  5. export class TimePickerCtrl {
  6. static tooltipFormat = 'MMM D, YYYY HH:mm:ss';
  7. static defaults = {
  8. time_options: ['5m', '15m', '1h', '6h', '12h', '24h', '2d', '7d', '30d'],
  9. refresh_intervals: ['5s', '10s', '30s', '1m', '5m', '15m', '30m', '1h', '2h', '1d'],
  10. };
  11. dashboard: any;
  12. panel: any;
  13. absolute: any;
  14. timeRaw: any;
  15. editTimeRaw: any;
  16. tooltip: string;
  17. rangeString: string;
  18. timeOptions: any;
  19. refresh: any;
  20. isUtc: boolean;
  21. firstDayOfWeek: number;
  22. isOpen: boolean;
  23. isAbsolute: boolean;
  24. /** @ngInject */
  25. constructor(private $scope, private $rootScope, private timeSrv) {
  26. this.$scope.ctrl = this;
  27. $rootScope.onAppEvent('shift-time-forward', () => this.move(1), $scope);
  28. $rootScope.onAppEvent('shift-time-backward', () => this.move(-1), $scope);
  29. $rootScope.onAppEvent('closeTimepicker', this.openDropdown.bind(this), $scope);
  30. this.dashboard.on('refresh', this.onRefresh.bind(this), $scope);
  31. // init options
  32. this.panel = this.dashboard.timepicker;
  33. _.defaults(this.panel, TimePickerCtrl.defaults);
  34. this.firstDayOfWeek = moment.localeData().firstDayOfWeek();
  35. // init time stuff
  36. this.onRefresh();
  37. }
  38. onRefresh() {
  39. const time = angular.copy(this.timeSrv.timeRange());
  40. const timeRaw = angular.copy(time.raw);
  41. if (!this.dashboard.isTimezoneUtc()) {
  42. time.from.local();
  43. time.to.local();
  44. if (moment.isMoment(timeRaw.from)) {
  45. timeRaw.from.local();
  46. }
  47. if (moment.isMoment(timeRaw.to)) {
  48. timeRaw.to.local();
  49. }
  50. this.isUtc = false;
  51. } else {
  52. this.isUtc = true;
  53. }
  54. this.rangeString = rangeUtil.describeTimeRange(timeRaw);
  55. this.absolute = { fromJs: time.from.toDate(), toJs: time.to.toDate() };
  56. this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
  57. this.tooltip += this.dashboard.formatDate(time.to);
  58. this.timeRaw = timeRaw;
  59. this.isAbsolute = moment.isMoment(this.timeRaw.to);
  60. }
  61. zoom(factor) {
  62. this.$rootScope.appEvent('zoom-out', 2);
  63. }
  64. move(direction) {
  65. const range = this.timeSrv.timeRange();
  66. const timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  67. let 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. if (this.isOpen) {
  86. this.closeDropdown();
  87. return;
  88. }
  89. this.onRefresh();
  90. this.editTimeRaw = this.timeRaw;
  91. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  92. this.refresh = {
  93. value: this.dashboard.refresh,
  94. options: this.panel.refresh_intervals.map((interval: any) => {
  95. return { text: interval, value: interval };
  96. }),
  97. };
  98. this.refresh.options.unshift({ text: 'off' });
  99. this.isOpen = true;
  100. this.$rootScope.appEvent('timepickerOpen');
  101. }
  102. closeDropdown() {
  103. this.isOpen = false;
  104. this.$rootScope.appEvent('timepickerClosed');
  105. }
  106. applyCustom() {
  107. if (this.refresh.value !== this.dashboard.refresh) {
  108. this.timeSrv.setAutoRefresh(this.refresh.value);
  109. }
  110. this.timeSrv.setTime(this.editTimeRaw);
  111. this.closeDropdown();
  112. }
  113. absoluteFromChanged() {
  114. this.editTimeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  115. }
  116. absoluteToChanged() {
  117. this.editTimeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  118. }
  119. getAbsoluteMomentForTimezone(jsDate) {
  120. return this.dashboard.isTimezoneUtc() ? moment(jsDate).utc() : moment(jsDate);
  121. }
  122. setRelativeFilter(timespan) {
  123. const range = { from: timespan.from, to: timespan.to };
  124. if (this.panel.nowDelay && range.to === 'now') {
  125. range.to = 'now-' + this.panel.nowDelay;
  126. }
  127. this.timeSrv.setTime(range);
  128. this.closeDropdown();
  129. }
  130. }
  131. export function settingsDirective() {
  132. return {
  133. restrict: 'E',
  134. templateUrl: 'public/app/features/dashboard/components/TimePicker/settings.html',
  135. controller: TimePickerCtrl,
  136. bindToController: true,
  137. controllerAs: 'ctrl',
  138. scope: {
  139. dashboard: '=',
  140. },
  141. };
  142. }
  143. export function timePickerDirective() {
  144. return {
  145. restrict: 'E',
  146. templateUrl: 'public/app/features/dashboard/components/TimePicker/template.html',
  147. controller: TimePickerCtrl,
  148. bindToController: true,
  149. controllerAs: 'ctrl',
  150. scope: {
  151. dashboard: '=',
  152. },
  153. };
  154. }
  155. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  156. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  157. import { inputDateDirective } from './validation';
  158. angular.module('grafana.directives').directive('inputDatetime', inputDateDirective);