TimePickerCtrl.ts 5.2 KB

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