timepicker.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. closeDropdown: any;
  23. isOpen: 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('refresh', this.onRefresh.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.isOpen = false;
  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. }
  99. applyCustom() {
  100. if (this.refresh.value !== this.dashboard.refresh) {
  101. this.timeSrv.setAutoRefresh(this.refresh.value);
  102. }
  103. this.timeSrv.setTime(this.editTimeRaw);
  104. this.isOpen = false;
  105. }
  106. absoluteFromChanged() {
  107. this.editTimeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  108. }
  109. absoluteToChanged() {
  110. this.editTimeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  111. }
  112. getAbsoluteMomentForTimezone(jsDate) {
  113. return this.dashboard.isTimezoneUtc() ? moment(jsDate).utc() : moment(jsDate);
  114. }
  115. setRelativeFilter(timespan) {
  116. var range = { from: timespan.from, to: timespan.to };
  117. if (this.panel.nowDelay && range.to === 'now') {
  118. range.to = 'now-' + this.panel.nowDelay;
  119. }
  120. this.timeSrv.setTime(range);
  121. this.isOpen = false;
  122. }
  123. }
  124. export function settingsDirective() {
  125. return {
  126. restrict: 'E',
  127. templateUrl: 'public/app/features/dashboard/timepicker/settings.html',
  128. controller: TimePickerCtrl,
  129. bindToController: true,
  130. controllerAs: 'ctrl',
  131. scope: {
  132. dashboard: '=',
  133. },
  134. };
  135. }
  136. export function timePickerDirective() {
  137. return {
  138. restrict: 'E',
  139. templateUrl: 'public/app/features/dashboard/timepicker/timepicker.html',
  140. controller: TimePickerCtrl,
  141. bindToController: true,
  142. controllerAs: 'ctrl',
  143. scope: {
  144. dashboard: '=',
  145. },
  146. };
  147. }
  148. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  149. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  150. import { inputDateDirective } from './input_date';
  151. angular.module('grafana.directives').directive('inputDatetime', inputDateDirective);