timeSrv.js 4.6 KB

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