timeSrv.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. 'kbn',
  6. 'moment'
  7. ], function (angular, _, config, kbn, moment) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.service('timeSrv', function($rootScope, $timeout, $routeParams, timer) {
  11. var self = this;
  12. this.init = function(dashboard) {
  13. timer.cancel_all();
  14. this.dashboard = dashboard;
  15. this.time = dashboard.time;
  16. this._initTimeFromUrl();
  17. this._parseTime();
  18. if(this.dashboard.refresh) {
  19. this.set_interval(this.dashboard.refresh);
  20. }
  21. };
  22. this._parseTime = function() {
  23. // when absolute time is saved in json it is turned to a string
  24. if (_.isString(this.time.from) && this.time.from.indexOf('Z') >= 0) {
  25. this.time.from = new Date(this.time.from);
  26. }
  27. if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
  28. this.time.to = new Date(this.time.to);
  29. }
  30. };
  31. this._parseUrlParam = function(value) {
  32. if (value.indexOf('now') !== -1) {
  33. return value;
  34. }
  35. if (value.length === 8) {
  36. return moment.utc(value, 'YYYYMMDD').toDate();
  37. }
  38. if (value.length === 15) {
  39. return moment.utc(value, 'YYYYMMDDTHHmmss').toDate();
  40. }
  41. var epoch = parseInt(value);
  42. if (!_.isNaN(epoch)) {
  43. return new Date(epoch);
  44. }
  45. return null;
  46. };
  47. this._initTimeFromUrl = function() {
  48. if ($routeParams.from) {
  49. this.time.from = this._parseUrlParam($routeParams.from) || this.time.from;
  50. }
  51. if ($routeParams.to) {
  52. this.time.to = this._parseUrlParam($routeParams.to) || this.time.to;
  53. }
  54. };
  55. this.set_interval = function (interval) {
  56. this.dashboard.refresh = interval;
  57. if (interval) {
  58. var _i = kbn.interval_to_ms(interval);
  59. this.start_scheduled_refresh(_i);
  60. } else {
  61. this.cancel_scheduled_refresh();
  62. }
  63. };
  64. this.refreshDashboard = function() {
  65. $rootScope.$broadcast('refresh');
  66. };
  67. this.start_scheduled_refresh = function (after_ms) {
  68. self.cancel_scheduled_refresh();
  69. self.refresh_timer = timer.register($timeout(function () {
  70. self.start_scheduled_refresh(after_ms);
  71. self.refreshDashboard();
  72. }, after_ms));
  73. };
  74. this.cancel_scheduled_refresh = function () {
  75. timer.cancel(this.refresh_timer);
  76. };
  77. this.setTime = function(time) {
  78. _.extend(this.time, time);
  79. // disable refresh if we have an absolute time
  80. if (_.isString(time.to) && time.to.indexOf('now') === -1) {
  81. this.old_refresh = this.dashboard.refresh || this.old_refresh;
  82. this.set_interval(false);
  83. }
  84. else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
  85. this.set_interval(this.old_refresh);
  86. this.old_refresh = null;
  87. }
  88. $rootScope.appEvent('time-range-changed', this.time);
  89. $timeout(this.refreshDashboard, 0);
  90. };
  91. this.timeRangeForUrl = function() {
  92. var range = this.timeRange(false);
  93. if (_.isString(range.to) && range.to.indexOf('now')) {
  94. range = this.timeRange();
  95. }
  96. if (_.isDate(range.from)) { range.from = range.from.getTime(); }
  97. if (_.isDate(range.to)) { range.to = range.to.getTime(); }
  98. return range;
  99. };
  100. this.timeRange = function(parse) {
  101. var _t = this.time;
  102. if(parse === false) {
  103. return {
  104. from: _t.from,
  105. to: _t.to
  106. };
  107. } else {
  108. var _from = _t.from;
  109. var _to = _t.to || new Date();
  110. return {
  111. from: kbn.parseDate(_from),
  112. to: kbn.parseDate(_to)
  113. };
  114. }
  115. };
  116. });
  117. });