timepicker.ts 4.9 KB

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