timepicker.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import _ from 'lodash';
  3. import kbn from 'app/core/utils/kbn';
  4. import angular from 'angular';
  5. import moment from 'moment';
  6. import * as dateMath from 'app/core/utils/datemath';
  7. import * as rangeUtil from 'app/core/utils/rangeutil';
  8. export class TimePickerCtrl {
  9. static tooltipFormat = 'MMM D, YYYY HH:mm:ss';
  10. static defaults = {
  11. time_options : ['5m','15m','1h','6h','12h','24h','2d','7d','30d'],
  12. refresh_intervals : ['5s','10s','30s','1m','5m','15m','30m','1h','2h','1d'],
  13. };
  14. dashboard: any;
  15. panel: any;
  16. absolute: any;
  17. timeRaw: any;
  18. tooltip: string;
  19. rangeString: string;
  20. timeOptions: any;
  21. refresh: any;
  22. isOpen: boolean;
  23. isUtc: boolean;
  24. /** @ngInject */
  25. constructor(private $scope, private $rootScope, private timeSrv) {
  26. $scope.ctrl = this;
  27. $rootScope.onAppEvent('zoom-out', () => this.zoom(2), $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.timezone === 'browser') {
  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. openDropdown() {
  73. this.init();
  74. this.isOpen = true;
  75. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  76. this.refresh = {
  77. value: this.dashboard.refresh,
  78. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  79. return {text: interval, value: interval};
  80. })
  81. };
  82. this.refresh.options.unshift({text: 'off'});
  83. this.$rootScope.appEvent('show-dash-editor', {
  84. src: 'app/features/dashboard/timepicker/dropdown.html',
  85. scope: this.$scope,
  86. cssClass: 'gf-timepicker-dropdown',
  87. });
  88. }
  89. applyCustom() {
  90. if (this.refresh.value !== this.dashboard.refresh) {
  91. this.timeSrv.setAutoRefresh(this.refresh.value);
  92. }
  93. this.timeSrv.setTime(this.timeRaw, true);
  94. this.$rootScope.appEvent('hide-dash-editor');
  95. }
  96. absoluteFromChanged() {
  97. this.timeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  98. }
  99. absoluteToChanged() {
  100. this.timeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  101. }
  102. getAbsoluteMomentForTimezone(jsDate) {
  103. return this.dashboard.timezone === 'browser' ? moment(jsDate) : moment(jsDate).utc();
  104. }
  105. setRelativeFilter(timespan) {
  106. this.panel.now = true;
  107. var range = {from: timespan.from, to: timespan.to};
  108. if (this.panel.nowDelay && range.to === 'now') {
  109. range.to = 'now-' + this.panel.nowDelay;
  110. }
  111. this.timeSrv.setTime(range);
  112. this.$rootScope.appEvent('hide-dash-editor');
  113. }
  114. }
  115. export function settingsDirective() {
  116. 'use strict';
  117. return {
  118. restrict: 'E',
  119. templateUrl: 'app/features/dashboard/timepicker/settings.html',
  120. controller: TimePickerCtrl,
  121. bindToController: true,
  122. controllerAs: 'ctrl',
  123. scope: {
  124. dashboard: "="
  125. }
  126. };
  127. }
  128. export function timePickerDirective() {
  129. 'use strict';
  130. return {
  131. restrict: 'E',
  132. templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
  133. controller: TimePickerCtrl,
  134. bindToController: true,
  135. controllerAs: 'ctrl',
  136. scope: {
  137. dashboard: "="
  138. }
  139. };
  140. }
  141. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  142. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);
  143. import {inputDateDirective} from './input_date';
  144. angular.module("grafana.directives").directive('inputDatetime', inputDateDirective);