timeSrv.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. $rootScope.$on('zoom-out', function(e, factor) { self.zoomOut(factor); });
  14. this.init = function(dashboard) {
  15. timer.cancel_all();
  16. this.dashboard = dashboard;
  17. this.time = dashboard.time;
  18. this.refresh = dashboard.refresh;
  19. this._initTimeFromUrl();
  20. this._parseTime();
  21. if(this.refresh) {
  22. this.setAutoRefresh(this.refresh);
  23. }
  24. };
  25. this._parseTime = function() {
  26. // when absolute time is saved in json it is turned to a string
  27. if (_.isString(this.time.from) && this.time.from.indexOf('Z') >= 0) {
  28. this.time.from = moment(this.time.from).utc();
  29. }
  30. if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
  31. this.time.to = moment(this.time.to).utc();
  32. }
  33. };
  34. this._parseUrlParam = function(value) {
  35. if (value.indexOf('now') !== -1) {
  36. return value;
  37. }
  38. if (value.length === 8) {
  39. return moment.utc(value, 'YYYYMMDD');
  40. }
  41. if (value.length === 15) {
  42. return moment.utc(value, 'YYYYMMDDTHHmmss');
  43. }
  44. if (!isNaN(value)) {
  45. var epoch = parseInt(value);
  46. return moment.utc(epoch);
  47. }
  48. return null;
  49. };
  50. this._initTimeFromUrl = function() {
  51. if ($routeParams.from) {
  52. this.time.from = this._parseUrlParam($routeParams.from) || this.time.from;
  53. }
  54. if ($routeParams.to) {
  55. this.time.to = this._parseUrlParam($routeParams.to) || this.time.to;
  56. }
  57. if ($routeParams.refresh) {
  58. this.refresh = $routeParams.refresh || this.refresh;
  59. }
  60. };
  61. this.setAutoRefresh = function (interval) {
  62. this.dashboard.refresh = interval;
  63. if (interval) {
  64. var _i = kbn.interval_to_ms(interval);
  65. var wait_ms = _i - (Date.now() % _i);
  66. $timeout(function () {
  67. self.start_scheduled_refresh(_i);
  68. self.refreshDashboard();
  69. }, wait_ms);
  70. } else {
  71. this.cancel_scheduled_refresh();
  72. }
  73. };
  74. this.refreshDashboard = function() {
  75. $rootScope.$broadcast('refresh');
  76. };
  77. this.start_scheduled_refresh = function (after_ms) {
  78. self.cancel_scheduled_refresh();
  79. self.refresh_timer = timer.register($timeout(function () {
  80. self.start_scheduled_refresh(after_ms);
  81. self.refreshDashboard();
  82. }, after_ms));
  83. };
  84. this.cancel_scheduled_refresh = function () {
  85. timer.cancel(this.refresh_timer);
  86. };
  87. this.setTime = function(time, enableRefresh) {
  88. _.extend(this.time, time);
  89. // disable refresh if zoom in or zoom out
  90. if (!enableRefresh && moment.isMoment(time.to)) {
  91. this.old_refresh = this.dashboard.refresh || this.old_refresh;
  92. this.setAutoRefresh(false);
  93. }
  94. else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
  95. this.setAutoRefresh(this.old_refresh);
  96. this.old_refresh = null;
  97. }
  98. $rootScope.appEvent('time-range-changed', this.time);
  99. $timeout(this.refreshDashboard, 0);
  100. };
  101. this.timeRangeForUrl = function() {
  102. var range = this.timeRange(false);
  103. if (_.isString(range.to) && range.to.indexOf('now')) {
  104. range = this.timeRange();
  105. }
  106. if (moment.isMoment(range.from)) { range.from = range.from.valueOf(); }
  107. if (moment.isMoment(range.to)) { range.to = range.to.valueOf(); }
  108. return range;
  109. };
  110. this.timeRange = function(parse) {
  111. // make copies if they are moment (do not want to return out internal moment, because they are mutable!)
  112. var from = moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from ;
  113. var to = moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to ;
  114. if (parse !== false) {
  115. from = dateMath.parse(from, false);
  116. to = dateMath.parse(to, true);
  117. }
  118. return {from: from, to: to};
  119. };
  120. this.zoomOut = function(factor) {
  121. var range = this.timeRange();
  122. var timespan = (range.to.valueOf() - range.from.valueOf());
  123. var center = range.to.valueOf() - timespan/2;
  124. var to = (center + (timespan*factor)/2);
  125. var from = (center - (timespan*factor)/2);
  126. if (to > Date.now() && range.to <= Date.now()) {
  127. var offset = to - Date.now();
  128. from = from - offset;
  129. to = Date.now();
  130. }
  131. this.setTime({from: moment.utc(from), to: moment.utc(to) });
  132. };
  133. });
  134. });