timepicker.ts 5.5 KB

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