timepicker.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. ///<reference path="../../../headers/common.d.ts" />
  2. import angular = require('angular');
  3. import _ = require('lodash');
  4. import moment = require('moment');
  5. import kbn = require('kbn');
  6. import {TimeRange} from './timerange';
  7. export class TimePickerCtrl {
  8. static defaults = {
  9. status : "Stable",
  10. time_options : ['5m','15m','1h','6h','12h','24h','today', '2d','7d','30d'],
  11. refresh_intervals : ['5s','10s','30s','1m','5m','15m','30m','1h','2h','1d'],
  12. };
  13. static patterns = {
  14. date: /^[0-9]{2}\/[0-9]{2}\/[0-9]{4}$/,
  15. hour: /^([01]?[0-9]|2[0-3])$/,
  16. minute: /^[0-5][0-9]$/,
  17. second: /^[0-5][0-9]$/,
  18. millisecond: /^[0-9]*$/
  19. };
  20. constructor(private $scope : any, private $rootScope, private timeSrv) {
  21. $scope.patterns = TimePickerCtrl.patterns;
  22. $scope.timeSrv = timeSrv;
  23. $scope.ctrl = this;
  24. $scope.$on('refresh', () => this.init());
  25. this.init();
  26. }
  27. init() {
  28. this.$scope.panel = this.$scope.dashboard.timepicker;
  29. _.defaults(this.$scope.panel, TimePickerCtrl.defaults);
  30. var time = this.timeSrv.timeRange(true);
  31. this.$scope.panel.now = false;
  32. var unparsed = this.timeSrv.timeRange(false);
  33. if (_.isString(unparsed.to) && unparsed.to.indexOf('now') === 0) {
  34. this.$scope.panel.now = true;
  35. }
  36. this.$scope.time = this.getScopeTimeObj(time.from, time.to);
  37. this.$scope.onAppEvent('zoom-out', function() {
  38. this.$scope.zoom(2);
  39. });
  40. }
  41. pad(n: number, width: number, z = 0): string {
  42. var str = n.toString();
  43. return str.length >= width ? str : new Array(width - str.length + 1).join(z.toString()) + str;
  44. }
  45. getTimeObj(date): any {
  46. return {
  47. date: new Date(date),
  48. hour: this.pad(date.getHours(), 2),
  49. minute: this.pad(date.getMinutes(), 2),
  50. second: this.pad(date.getSeconds(), 2),
  51. millisecond: this.pad(date.getMilliseconds(), 3)
  52. };
  53. };
  54. getScopeTimeObj(from, to) {
  55. var model : any = {from: this.getTimeObj(from), to: this.getTimeObj(to)};
  56. if (model.from.date) {
  57. model.tooltip = this.$scope.dashboard.formatDate(model.from.date) + ' <br>to<br>';
  58. model.tooltip += this.$scope.dashboard.formatDate(model.to.date);
  59. }
  60. else {
  61. model.tooltip = 'Click to set time filter';
  62. }
  63. if (this.timeSrv.time) {
  64. if (this.$scope.panel.now) {
  65. model.rangeString = TimeRange.describeRelativeTime(this.timeSrv.time);
  66. }
  67. else {
  68. model.rangeString = this.$scope.dashboard.formatDate(model.from.date, 'MMM D, YYYY HH:mm:ss') + ' to ' +
  69. this.$scope.dashboard.formatDate(model.to.date, 'MMM D, YYYY HH:mm:ss');
  70. }
  71. }
  72. return model;
  73. }
  74. loadTimeOptions() {
  75. this.$scope.timeOptions = TimeRange.getRelativeTimesList(this.$scope.panel);
  76. this.$scope.refreshMenuLeftSide = this.$scope.time.rangeString.length < 10;
  77. }
  78. cloneTime(time) {
  79. var _n = { from: _.clone(time.from), to: _.clone(time.to) };
  80. // Create new dates as _.clone is shallow.
  81. _n.from.date = new Date(_n.from.date);
  82. _n.to.date = new Date(_n.to.date);
  83. return _n;
  84. }
  85. customTime() {
  86. // Assume the form is valid since we're setting it to something valid
  87. this.$scope.input.$setValidity("dummy", true);
  88. this.$scope.temptime = this.cloneTime(this.$scope.time);
  89. this.$scope.temptime.now = this.$scope.panel.now;
  90. this.$scope.temptime.from.date.setHours(0, 0, 0, 0);
  91. this.$scope.temptime.to.date.setHours(0, 0, 0, 0);
  92. // Date picker needs the date to be at the start of the day
  93. if (new Date().getTimezoneOffset() < 0) {
  94. this.$scope.temptime.from.date = moment(this.$scope.temptime.from.date).add(1, 'days').toDate();
  95. this.$scope.temptime.to.date = moment(this.$scope.temptime.to.date).add(1, 'days').toDate();
  96. }
  97. this.$scope.appEvent('show-dash-editor', {
  98. src: 'app/features/dashboard/timepicker/custom.html',
  99. scope: this.$scope
  100. });
  101. }
  102. setNow() {
  103. this.$scope.time.to = this.getTimeObj(new Date());
  104. }
  105. setAbsoluteTimeFilter(time) {
  106. // Create filter object
  107. var _filter = _.clone(time);
  108. if (time.now) {
  109. _filter.to = "now";
  110. }
  111. // Update our representation
  112. this.$scope.time = this.getScopeTimeObj(time.from, time.to);
  113. this.timeSrv.setTime(_filter);
  114. }
  115. setRelativeFilter(timespan) {
  116. this.$scope.panel.now = true;
  117. var range = {from: timespan.from, to: timespan.to};
  118. if (this.$scope.panel.nowDelay) {
  119. range.to = 'now-' + this.$scope.panel.nowDelay;
  120. }
  121. this.timeSrv.setTime(range);
  122. this.$scope.time = this.getScopeTimeObj(kbn.parseDate(range.from), new Date());
  123. }
  124. validate(time): any {
  125. // Assume the form is valid. There is a hidden dummy input for invalidating it programatically.
  126. this.$scope.input.$setValidity("dummy", true);
  127. var _from = this.datepickerToLocal(time.from.date);
  128. var _to = this.datepickerToLocal(time.to.date);
  129. var _t = time;
  130. if (this.$scope.input.$valid) {
  131. _from.setHours(_t.from.hour, _t.from.minute, _t.from.second, _t.from.millisecond);
  132. _to.setHours(_t.to.hour, _t.to.minute, _t.to.second, _t.to.millisecond);
  133. // Check that the objects are valid and to is after from
  134. if (isNaN(_from.getTime()) || isNaN(_to.getTime()) || _from.getTime() >= _to.getTime()) {
  135. this.$scope.input.$setValidity("dummy", false);
  136. return false;
  137. }
  138. } else {
  139. return false;
  140. }
  141. return { from: _from, to: _to, now: time.now };
  142. }
  143. datepickerToLocal(date) {
  144. date = moment(date).clone().toDate();
  145. return moment(new Date(date.getTime() + date.getTimezoneOffset() * 60000)).toDate();
  146. }
  147. }
  148. export function settingsDirective() {
  149. 'use strict';
  150. return {
  151. restrict: 'E',
  152. templateUrl: 'app/features/dashboard/timepicker/settings.html',
  153. controller: TimePickerCtrl,
  154. scope: true,
  155. };
  156. }
  157. export function timePickerDirective() {
  158. 'use strict';
  159. return {
  160. restrict: 'E',
  161. templateUrl: 'app/features/dashboard/timepicker/timepicker.html',
  162. controller: TimePickerCtrl,
  163. scope: true
  164. };
  165. }
  166. angular.module('grafana.directives').directive('gfTimePickerSettings', settingsDirective);
  167. angular.module('grafana.directives').directive('gfTimePicker', timePickerDirective);