timepicker.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. ///<reference path="../../../headers/common.d.ts" />
  2. ///<amd-dependency path="./input_date" name="inputDate" />
  3. import angular = require('angular');
  4. import _ = require('lodash');
  5. import moment = require('moment');
  6. import kbn = require('kbn');
  7. import dateMath = require('app/core/utils/datemath');
  8. import rangeUtil = require('app/core/utils/rangeutil');
  9. declare var inputDate: any;
  10. export class TimePickerCtrl {
  11. static tooltipFormat = 'MMM D, YYYY HH:mm:ss';
  12. static defaults = {
  13. time_options : ['5m','15m','1h','6h','12h','24h','2d','7d','30d'],
  14. refresh_intervals : ['5s','10s','30s','1m','5m','15m','30m','1h','2h','1d'],
  15. };
  16. dashboard: any;
  17. panel: any;
  18. absolute: any;
  19. timeRaw: any;
  20. tooltip: string;
  21. rangeString: string;
  22. timeOptions: any;
  23. refresh: any;
  24. isOpen: boolean;
  25. isUtc: boolean;
  26. constructor(private $scope, private $rootScope, private timeSrv) {
  27. $scope.ctrl = this;
  28. $rootScope.onAppEvent('refresh', () => this.init(), $scope);
  29. $rootScope.onAppEvent('zoom-out', () => this.zoom(2), $scope);
  30. $rootScope.onAppEvent('dash-editor-hidden', () => {
  31. this.isOpen = false;
  32. }, $scope);
  33. this.init();
  34. }
  35. init() {
  36. this.panel = this.dashboard.timepicker;
  37. _.defaults(this.panel, TimePickerCtrl.defaults);
  38. var time = angular.copy(this.timeSrv.timeRange());
  39. var timeRaw = angular.copy(this.timeSrv.timeRange(false));
  40. if (this.dashboard.timezone === 'browser') {
  41. time.from.local();
  42. time.to.local();
  43. if (moment.isMoment(timeRaw.from)) {
  44. timeRaw.from.local();
  45. }
  46. if (moment.isMoment(timeRaw.to)) {
  47. timeRaw.to.local();
  48. }
  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.timeRaw = timeRaw;
  55. this.tooltip = this.dashboard.formatDate(time.from) + ' <br>to<br>';
  56. this.tooltip += this.dashboard.formatDate(time.to);
  57. }
  58. zoom(factor) {
  59. var range = this.timeSrv.timeRange();
  60. var timespan = (range.to.valueOf() - range.from.valueOf());
  61. var center = range.to.valueOf() - timespan/2;
  62. var to = (center + (timespan*factor)/2);
  63. var from = (center - (timespan*factor)/2);
  64. if (to > Date.now() && range.to <= Date.now()) {
  65. var offset = to - Date.now();
  66. from = from - offset;
  67. to = Date.now();
  68. }
  69. this.timeSrv.setTime({from: moment.utc(from), to: moment.utc(to) });
  70. }
  71. openDropdown() {
  72. this.isOpen = true;
  73. this.timeOptions = rangeUtil.getRelativeTimesList(this.panel, this.rangeString);
  74. this.refresh = {
  75. value: this.dashboard.refresh,
  76. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  77. return {text: interval, value: interval};
  78. })
  79. };
  80. this.refresh.options.unshift({text: 'off'});
  81. this.$rootScope.appEvent('show-dash-editor', {
  82. src: 'app/features/dashboard/timepicker/dropdown.html',
  83. scope: this.$scope,
  84. cssClass: 'gf-timepicker-dropdown',
  85. });
  86. }
  87. applyCustom() {
  88. if (this.refresh.value !== this.dashboard.refresh) {
  89. this.timeSrv.setAutoRefresh(this.refresh.value);
  90. }
  91. this.timeSrv.setTime(this.timeRaw);
  92. this.$rootScope.appEvent('hide-dash-editor');
  93. }
  94. absoluteFromChanged() {
  95. this.timeRaw.from = this.getAbsoluteMomentForTimezone(this.absolute.fromJs);
  96. }
  97. absoluteToChanged() {
  98. this.timeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  99. }
  100. getAbsoluteMomentForTimezone(jsDate) {
  101. return this.dashboard.timezone === 'browser' ? moment(jsDate) : moment(jsDate).utc();
  102. }
  103. setRelativeFilter(timespan) {
  104. this.panel.now = true;
  105. var range = {from: timespan.from, to: timespan.to};
  106. if (this.panel.nowDelay && range.to === 'now') {
  107. range.to = 'now-' + this.panel.nowDelay;
  108. }
  109. this.timeSrv.setTime(range);
  110. this.$rootScope.appEvent('hide-dash-editor');
  111. }
  112. }
  113. export function settingsDirective() {
  114. 'use strict';
  115. return {
  116. restrict: 'E',
  117. templateUrl: 'app/features/dashboard/timepicker/settings.html',
  118. controller: TimePickerCtrl,
  119. bindToController: true,
  120. controllerAs: 'ctrl',
  121. scope: {
  122. dashboard: "="
  123. }
  124. };
  125. }
  126. export function timePickerDirective() {
  127. 'use strict';
  128. return {
  129. restrict: 'E',
  130. templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
  131. controller: TimePickerCtrl,
  132. bindToController: true,
  133. controllerAs: 'ctrl',
  134. scope: {
  135. dashboard: "="
  136. }
  137. };
  138. }
  139. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  140. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);