timepicker.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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(this.timeSrv.timeRange(false));
  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. } else {
  48. this.isUtc = true;
  49. }
  50. this.rangeString = rangeUtil.describeTimeRange(timeRaw);
  51. this.absolute = {fromJs: time.from.toDate(), toJs: time.to.toDate()};
  52. this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
  53. this.tooltip += this.dashboard.formatDate(time.to);
  54. // do not update time raw when dropdown is open
  55. // as auto refresh will reset the from/to input fields
  56. if (!this.isOpen) {
  57. this.timeRaw = timeRaw;
  58. }
  59. }
  60. zoom(factor) {
  61. this.$rootScope.appEvent('zoom-out', 2);
  62. }
  63. move(direction) {
  64. var range = this.timeSrv.timeRange();
  65. var timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  66. var 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: moment.utc(from), to: moment.utc(to) });
  82. }
  83. openDropdown() {
  84. this.init();
  85. this.isOpen = true;
  86. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  87. this.refresh = {
  88. value: this.dashboard.refresh,
  89. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  90. return {text: interval, value: interval};
  91. })
  92. };
  93. this.refresh.options.unshift({text: 'off'});
  94. this.$rootScope.appEvent('show-dash-editor', {
  95. src: 'public/app/features/dashboard/timepicker/dropdown.html',
  96. scope: this.$scope,
  97. cssClass: 'gf-timepicker-dropdown',
  98. });
  99. }
  100. applyCustom() {
  101. if (this.refresh.value !== this.dashboard.refresh) {
  102. this.timeSrv.setAutoRefresh(this.refresh.value);
  103. }
  104. this.timeSrv.setTime(this.timeRaw, true);
  105. this.$rootScope.appEvent('hide-dash-editor');
  106. }
  107. absoluteFromChanged() {
  108. this.timeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  109. }
  110. absoluteToChanged() {
  111. this.timeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  112. }
  113. getAbsoluteMomentForTimezone(jsDate) {
  114. return this.dashboard.isTimezoneUtc() ? moment(jsDate).utc() : moment(jsDate);
  115. }
  116. setRelativeFilter(timespan) {
  117. var range = {from: timespan.from, to: timespan.to};
  118. if (this.panel.nowDelay && range.to === 'now') {
  119. range.to = 'now-' + this.panel.nowDelay;
  120. }
  121. this.timeSrv.setTime(range);
  122. this.$rootScope.appEvent('hide-dash-editor');
  123. }
  124. }
  125. export function settingsDirective() {
  126. return {
  127. restrict: 'E',
  128. templateUrl: 'public/app/features/dashboard/timepicker/settings.html',
  129. controller: TimePickerCtrl,
  130. bindToController: true,
  131. controllerAs: 'ctrl',
  132. scope: {
  133. dashboard: "="
  134. }
  135. };
  136. }
  137. export function timePickerDirective() {
  138. return {
  139. restrict: 'E',
  140. templateUrl: 'public/app/features/dashboard/timepicker/timepicker.html',
  141. controller: TimePickerCtrl,
  142. bindToController: true,
  143. controllerAs: 'ctrl',
  144. scope: {
  145. dashboard: "="
  146. }
  147. };
  148. }
  149. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  150. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  151. import {inputDateDirective} from './input_date';
  152. angular.module("grafana.directives").directive('inputDatetime', inputDateDirective);