timeSrv.js 4.0 KB

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