timeSrv.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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 interval_ms = kbn.interval_to_ms(interval);
  67. $timeout(function () {
  68. self.start_scheduled_refresh(interval_ms);
  69. self.refreshDashboard();
  70. }, interval_ms);
  71. } else {
  72. this.cancel_scheduled_refresh();
  73. }
  74. };
  75. this.refreshDashboard = function() {
  76. $rootScope.$broadcast('refresh');
  77. };
  78. this.start_scheduled_refresh = function (after_ms) {
  79. self.cancel_scheduled_refresh();
  80. self.refresh_timer = timer.register($timeout(function () {
  81. self.start_scheduled_refresh(after_ms);
  82. self.refreshDashboard();
  83. }, after_ms));
  84. };
  85. this.cancel_scheduled_refresh = function () {
  86. timer.cancel(this.refresh_timer);
  87. };
  88. this.setTime = function(time, enableRefresh) {
  89. _.extend(this.time, time);
  90. // disable refresh if zoom in or zoom out
  91. if (!enableRefresh && moment.isMoment(time.to)) {
  92. this.old_refresh = this.dashboard.refresh || this.old_refresh;
  93. this.setAutoRefresh(false);
  94. }
  95. else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
  96. this.setAutoRefresh(this.old_refresh);
  97. this.old_refresh = null;
  98. }
  99. $rootScope.appEvent('time-range-changed', this.time);
  100. $timeout(this.refreshDashboard, 0);
  101. };
  102. this.timeRangeForUrl = function() {
  103. var range = this.timeRange().raw;
  104. if (moment.isMoment(range.from)) { range.from = range.from.valueOf(); }
  105. if (moment.isMoment(range.to)) { range.to = range.to.valueOf(); }
  106. return range;
  107. };
  108. this.timeRange = function() {
  109. // make copies if they are moment (do not want to return out internal moment, because they are mutable!)
  110. var range = {
  111. from: moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from,
  112. to: moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to,
  113. };
  114. range = {
  115. from: dateMath.parse(range.from, false),
  116. to: dateMath.parse(range.to, true),
  117. raw: range
  118. };
  119. return range;
  120. };
  121. this.zoomOut = function(factor) {
  122. var range = this.timeRange();
  123. var timespan = (range.to.valueOf() - range.from.valueOf());
  124. var center = range.to.valueOf() - timespan/2;
  125. var to = (center + (timespan*factor)/2);
  126. var from = (center - (timespan*factor)/2);
  127. if (to > Date.now() && range.to <= Date.now()) {
  128. var offset = to - Date.now();
  129. from = from - offset;
  130. to = Date.now();
  131. }
  132. this.setTime({from: moment.utc(from), to: moment.utc(to) });
  133. };
  134. });
  135. });