timepicker.ts 6.1 KB

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