timeSrv.js 4.7 KB

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