timepicker.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. import _ from 'lodash';
  2. import angular from 'angular';
  3. import moment from 'moment';
  4. import * as rangeUtil from 'app/core/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. /** @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('refresh', this.onRefresh.bind(this), $scope);
  29. $rootScope.onAppEvent('closeTimepicker', this.openDropdown.bind(this), $scope);
  30. // init options
  31. this.panel = this.dashboard.timepicker;
  32. _.defaults(this.panel, TimePickerCtrl.defaults);
  33. this.firstDayOfWeek = moment.localeData().firstDayOfWeek();
  34. // init time stuff
  35. this.onRefresh();
  36. }
  37. onRefresh() {
  38. var time = angular.copy(this.timeSrv.timeRange());
  39. var timeRaw = angular.copy(time.raw);
  40. if (!this.dashboard.isTimezoneUtc()) {
  41. time.from.local();
  42. time.to.local();
  43. if (moment.isMoment(timeRaw.from)) {
  44. timeRaw.from.local();
  45. }
  46. if (moment.isMoment(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. }
  59. zoom(factor) {
  60. this.$rootScope.appEvent('zoom-out', 2);
  61. }
  62. move(direction) {
  63. var range = this.timeSrv.timeRange();
  64. var timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  65. var to, from;
  66. if (direction === -1) {
  67. to = range.to.valueOf() - timespan;
  68. from = range.from.valueOf() - timespan;
  69. } else if (direction === 1) {
  70. to = range.to.valueOf() + timespan;
  71. from = range.from.valueOf() + timespan;
  72. if (to > Date.now() && range.to < Date.now()) {
  73. to = Date.now();
  74. from = range.from.valueOf();
  75. }
  76. } else {
  77. to = range.to.valueOf();
  78. from = range.from.valueOf();
  79. }
  80. this.timeSrv.setTime({ from: moment.utc(from), to: moment.utc(to) });
  81. }
  82. openDropdown() {
  83. if (this.isOpen) {
  84. this.closeDropdown();
  85. return;
  86. }
  87. this.onRefresh();
  88. this.editTimeRaw = this.timeRaw;
  89. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  90. this.refresh = {
  91. value: this.dashboard.refresh,
  92. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  93. return { text: interval, value: interval };
  94. }),
  95. };
  96. this.refresh.options.unshift({ text: 'off' });
  97. this.isOpen = true;
  98. this.$rootScope.appEvent('timepickerOpen');
  99. }
  100. closeDropdown() {
  101. this.isOpen = false;
  102. this.$rootScope.appEvent('timepickerClosed');
  103. }
  104. applyCustom() {
  105. if (this.refresh.value !== this.dashboard.refresh) {
  106. this.timeSrv.setAutoRefresh(this.refresh.value);
  107. }
  108. this.timeSrv.setTime(this.editTimeRaw);
  109. this.closeDropdown();
  110. }
  111. absoluteFromChanged() {
  112. this.editTimeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  113. }
  114. absoluteToChanged() {
  115. this.editTimeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  116. }
  117. getAbsoluteMomentForTimezone(jsDate) {
  118. return this.dashboard.isTimezoneUtc() ? moment(jsDate).utc() : moment(jsDate);
  119. }
  120. setRelativeFilter(timespan) {
  121. var range = { from: timespan.from, to: timespan.to };
  122. if (this.panel.nowDelay && range.to === 'now') {
  123. range.to = 'now-' + this.panel.nowDelay;
  124. }
  125. this.timeSrv.setTime(range);
  126. this.closeDropdown();
  127. }
  128. }
  129. export function settingsDirective() {
  130. return {
  131. restrict: 'E',
  132. templateUrl: 'public/app/features/dashboard/timepicker/settings.html',
  133. controller: TimePickerCtrl,
  134. bindToController: true,
  135. controllerAs: 'ctrl',
  136. scope: {
  137. dashboard: '=',
  138. },
  139. };
  140. }
  141. export function timePickerDirective() {
  142. return {
  143. restrict: 'E',
  144. templateUrl: 'public/app/features/dashboard/timepicker/timepicker.html',
  145. controller: TimePickerCtrl,
  146. bindToController: true,
  147. controllerAs: 'ctrl',
  148. scope: {
  149. dashboard: '=',
  150. },
  151. };
  152. }
  153. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  154. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  155. import { inputDateDirective } from './input_date';
  156. angular.module('grafana.directives').directive('inputDatetime', inputDateDirective);