timepicker.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import _ from "lodash";
  2. import angular from "angular";
  3. import moment from "moment";
  4. import * as rangeUtil from "app/core/utils/rangeutil";
  5. export class TimePickerCtrl {
  6. static tooltipFormat = "MMM D, YYYY HH:mm:ss";
  7. static defaults = {
  8. time_options: ["5m", "15m", "1h", "6h", "12h", "24h", "2d", "7d", "30d"],
  9. refresh_intervals: [
  10. "5s",
  11. "10s",
  12. "30s",
  13. "1m",
  14. "5m",
  15. "15m",
  16. "30m",
  17. "1h",
  18. "2h",
  19. "1d"
  20. ]
  21. };
  22. dashboard: any;
  23. panel: any;
  24. absolute: any;
  25. timeRaw: any;
  26. editTimeRaw: any;
  27. tooltip: string;
  28. rangeString: string;
  29. timeOptions: any;
  30. refresh: any;
  31. isUtc: boolean;
  32. firstDayOfWeek: number;
  33. closeDropdown: any;
  34. isOpen: boolean;
  35. /** @ngInject */
  36. constructor(private $scope, private $rootScope, private timeSrv) {
  37. this.$scope.ctrl = this;
  38. $rootScope.onAppEvent("shift-time-forward", () => this.move(1), $scope);
  39. $rootScope.onAppEvent("shift-time-backward", () => this.move(-1), $scope);
  40. $rootScope.onAppEvent("refresh", this.onRefresh.bind(this), $scope);
  41. // init options
  42. this.panel = this.dashboard.timepicker;
  43. _.defaults(this.panel, TimePickerCtrl.defaults);
  44. this.firstDayOfWeek = moment.localeData().firstDayOfWeek();
  45. // init time stuff
  46. this.onRefresh();
  47. }
  48. onRefresh() {
  49. var time = angular.copy(this.timeSrv.timeRange());
  50. var timeRaw = angular.copy(time.raw);
  51. if (!this.dashboard.isTimezoneUtc()) {
  52. time.from.local();
  53. time.to.local();
  54. if (moment.isMoment(timeRaw.from)) {
  55. timeRaw.from.local();
  56. }
  57. if (moment.isMoment(timeRaw.to)) {
  58. timeRaw.to.local();
  59. }
  60. this.isUtc = false;
  61. } else {
  62. this.isUtc = true;
  63. }
  64. this.rangeString = rangeUtil.describeTimeRange(timeRaw);
  65. this.absolute = { fromJs: time.from.toDate(), toJs: time.to.toDate() };
  66. this.tooltip = this.dashboard.formatDate(time.from) + " <br>to<br>";
  67. this.tooltip += this.dashboard.formatDate(time.to);
  68. this.timeRaw = timeRaw;
  69. }
  70. zoom(factor) {
  71. this.$rootScope.appEvent("zoom-out", 2);
  72. }
  73. move(direction) {
  74. var range = this.timeSrv.timeRange();
  75. var timespan = (range.to.valueOf() - range.from.valueOf()) / 2;
  76. var to, from;
  77. if (direction === -1) {
  78. to = range.to.valueOf() - timespan;
  79. from = range.from.valueOf() - timespan;
  80. } else if (direction === 1) {
  81. to = range.to.valueOf() + timespan;
  82. from = range.from.valueOf() + timespan;
  83. if (to > Date.now() && range.to < Date.now()) {
  84. to = Date.now();
  85. from = range.from.valueOf();
  86. }
  87. } else {
  88. to = range.to.valueOf();
  89. from = range.from.valueOf();
  90. }
  91. this.timeSrv.setTime({ from: moment.utc(from), to: moment.utc(to) });
  92. }
  93. openDropdown() {
  94. if (this.isOpen) {
  95. this.isOpen = false;
  96. return;
  97. }
  98. this.onRefresh();
  99. this.editTimeRaw = this.timeRaw;
  100. this.timeOptions = rangeUtil.getRelativeTimesList(
  101. this.panel,
  102. this.rangeString
  103. );
  104. this.refresh = {
  105. value: this.dashboard.refresh,
  106. options: _.map(this.panel.refresh_intervals, (interval: any) => {
  107. return { text: interval, value: interval };
  108. })
  109. };
  110. this.refresh.options.unshift({ text: "off" });
  111. this.isOpen = true;
  112. }
  113. applyCustom() {
  114. if (this.refresh.value !== this.dashboard.refresh) {
  115. this.timeSrv.setAutoRefresh(this.refresh.value);
  116. }
  117. this.timeSrv.setTime(this.editTimeRaw);
  118. this.isOpen = false;
  119. }
  120. absoluteFromChanged() {
  121. this.editTimeRaw.from = this.getAbsoluteMomentForTimezone(
  122. this.absolute.fromJs
  123. );
  124. }
  125. absoluteToChanged() {
  126. this.editTimeRaw.to = this.getAbsoluteMomentForTimezone(this.absolute.toJs);
  127. }
  128. getAbsoluteMomentForTimezone(jsDate) {
  129. return this.dashboard.isTimezoneUtc()
  130. ? moment(jsDate).utc()
  131. : moment(jsDate);
  132. }
  133. setRelativeFilter(timespan) {
  134. var range = { from: timespan.from, to: timespan.to };
  135. if (this.panel.nowDelay && range.to === "now") {
  136. range.to = "now-" + this.panel.nowDelay;
  137. }
  138. this.timeSrv.setTime(range);
  139. this.isOpen = false;
  140. }
  141. }
  142. export function settingsDirective() {
  143. return {
  144. restrict: "E",
  145. templateUrl: "public/app/features/dashboard/timepicker/settings.html",
  146. controller: TimePickerCtrl,
  147. bindToController: true,
  148. controllerAs: "ctrl",
  149. scope: {
  150. dashboard: "="
  151. }
  152. };
  153. }
  154. export function timePickerDirective() {
  155. return {
  156. restrict: "E",
  157. templateUrl: "public/app/features/dashboard/timepicker/timepicker.html",
  158. controller: TimePickerCtrl,
  159. bindToController: true,
  160. controllerAs: "ctrl",
  161. scope: {
  162. dashboard: "="
  163. }
  164. };
  165. }
  166. angular
  167. .module("grafana.directives")
  168. .directive("gfTimePickerSettings", settingsDirective);
  169. angular
  170. .module("grafana.directives")
  171. .directive("gfTimePicker", timePickerDirective);
  172. import { inputDateDirective } from "./input_date";
  173. angular
  174. .module("grafana.directives")
  175. .directive("inputDatetime", inputDateDirective);