timepicker.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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('zoom-out', () => this.zoom(2), $scope);
  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.init(), $scope);
  30. $rootScope.onAppEvent('dash-editor-hidden', () => this.isOpen = false, $scope);
  31. this.init();
  32. }
  33. init() {
  34. this.panel = this.dashboard.timepicker;
  35. _.defaults(this.panel, TimePickerCtrl.defaults);
  36. this.firstDayOfWeek = moment.localeData().firstDayOfWeek();
  37. var time = angular.copy(this.timeSrv.timeRange());
  38. var timeRaw = angular.copy(this.timeSrv.timeRange(false));
  39. if (!this.dashboard.isTimezoneUtc()) {
  40. time.from.local();
  41. time.to.local();
  42. if (moment.isMoment(timeRaw.from)) {
  43. timeRaw.from.local();
  44. }
  45. if (moment.isMoment(timeRaw.to)) {
  46. timeRaw.to.local();
  47. }
  48. } else {
  49. this.isUtc = true;
  50. }
  51. this.rangeString = rangeUtil.describeTimeRange(timeRaw);
  52. this.absolute = {fromJs: time.from.toDate(), toJs: time.to.toDate()};
  53. this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
  54. this.tooltip += this.dashboard.formatDate(time.to);
  55. // do not update time raw when dropdown is open
  56. // as auto refresh will reset the from/to input fields
  57. if (!this.isOpen) {
  58. this.timeRaw = timeRaw;
  59. }
  60. }
  61. zoom(factor) {
  62. var range = this.timeSrv.timeRange();
  63. var timespan = (range.to.valueOf() - range.from.valueOf());
  64. var center = range.to.valueOf() - timespan/2;
  65. var to = (center + (timespan*factor)/2);
  66. var from = (center - (timespan*factor)/2);
  67. if (to > Date.now() && range.to <= Date.now()) {
  68. var offset = to - Date.now();
  69. from = from - offset;
  70. to = Date.now();
  71. }
  72. this.timeSrv.setTime({from: moment.utc(from), to: moment.utc(to) });
  73. }
  74. move(direction) {
  75. var range = this.timeSrv.timeRange();
  76. var timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  77. var to, from;
  78. if (direction === -1) {
  79. to = range.to.valueOf() - timespan;
  80. from = range.from.valueOf() - timespan;
  81. } else if (direction === 1) {
  82. to = range.to.valueOf() + timespan;
  83. from = range.from.valueOf() + timespan;
  84. if (to > Date.now() && range.to < Date.now()) {
  85. to = Date.now();
  86. from = range.from.valueOf();
  87. }
  88. } else {
  89. to = range.to.valueOf();
  90. from = range.from.valueOf();
  91. }
  92. this.timeSrv.setTime({from: moment.utc(from), to: moment.utc(to) });
  93. }
  94. openDropdown() {
  95. this.init();
  96. this.isOpen = true;
  97. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  98. this.refresh = {
  99. value: this.dashboard.refresh,
  100. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  101. return {text: interval, value: interval};
  102. })
  103. };
  104. this.refresh.options.unshift({text: 'off'});
  105. this.$rootScope.appEvent('show-dash-editor', {
  106. src: 'public/app/features/dashboard/timepicker/dropdown.html',
  107. scope: this.$scope,
  108. cssClass: 'gf-timepicker-dropdown',
  109. });
  110. }
  111. applyCustom() {
  112. if (this.refresh.value !== this.dashboard.refresh) {
  113. this.timeSrv.setAutoRefresh(this.refresh.value);
  114. }
  115. this.timeSrv.setTime(this.timeRaw, true);
  116. this.$rootScope.appEvent('hide-dash-editor');
  117. }
  118. absoluteFromChanged() {
  119. this.timeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  120. }
  121. absoluteToChanged() {
  122. this.timeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  123. }
  124. getAbsoluteMomentForTimezone(jsDate) {
  125. return this.dashboard.isTimezoneUtc() ? moment(jsDate).utc() : moment(jsDate);
  126. }
  127. setRelativeFilter(timespan) {
  128. var range = {from: timespan.from, to: timespan.to};
  129. if (this.panel.nowDelay && range.to === 'now') {
  130. range.to = 'now-' + this.panel.nowDelay;
  131. }
  132. this.timeSrv.setTime(range);
  133. this.$rootScope.appEvent('hide-dash-editor');
  134. }
  135. }
  136. export function settingsDirective() {
  137. return {
  138. restrict: 'E',
  139. templateUrl: 'public/app/features/dashboard/timepicker/settings.html',
  140. controller: TimePickerCtrl,
  141. bindToController: true,
  142. controllerAs: 'ctrl',
  143. scope: {
  144. dashboard: "="
  145. }
  146. };
  147. }
  148. export function timePickerDirective() {
  149. return {
  150. restrict: 'E',
  151. templateUrl: 'public/app/features/dashboard/timepicker/timepicker.html',
  152. controller: TimePickerCtrl,
  153. bindToController: true,
  154. controllerAs: 'ctrl',
  155. scope: {
  156. dashboard: "="
  157. }
  158. };
  159. }
  160. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  161. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  162. import {inputDateDirective} from './input_date';
  163. angular.module("grafana.directives").directive('inputDatetime', inputDateDirective);