timeSrv.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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(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. this.start_scheduled_refresh(_i);
  61. } else {
  62. this.cancel_scheduled_refresh();
  63. }
  64. };
  65. this.refreshDashboard = function() {
  66. $rootScope.$broadcast('refresh');
  67. };
  68. this.start_scheduled_refresh = function (after_ms) {
  69. self.cancel_scheduled_refresh();
  70. self.refresh_timer = timer.register($timeout(function () {
  71. self.start_scheduled_refresh(after_ms);
  72. self.refreshDashboard();
  73. }, after_ms));
  74. };
  75. this.cancel_scheduled_refresh = function () {
  76. timer.cancel(this.refresh_timer);
  77. };
  78. this.setTime = function(time, enableRefresh) {
  79. _.extend(this.time, time);
  80. // disable refresh if zoom in or zoom out
  81. if (!enableRefresh && moment.isMoment(time.to)) {
  82. this.old_refresh = this.dashboard.refresh || this.old_refresh;
  83. this.setAutoRefresh(false);
  84. }
  85. else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
  86. this.setAutoRefresh(this.old_refresh);
  87. this.old_refresh = null;
  88. }
  89. $rootScope.appEvent('time-range-changed', this.time);
  90. $timeout(this.refreshDashboard, 0);
  91. };
  92. this.timeRangeForUrl = function() {
  93. var range = this.timeRange(false);
  94. if (_.isString(range.to) && range.to.indexOf('now')) {
  95. range = this.timeRange();
  96. }
  97. if (moment.isMoment(range.from)) { range.from = range.from.valueOf(); }
  98. if (moment.isMoment(range.to)) { range.to = range.to.valueOf(); }
  99. return range;
  100. };
  101. this.timeRange = function(parse) {
  102. // make copies if they are moment (do not want to return out internal moment, because they are mutable!)
  103. var from = moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from ;
  104. var to = moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to ;
  105. if (parse !== false) {
  106. from = dateMath.parse(from, false);
  107. to = dateMath.parse(to, true);
  108. }
  109. return {from: from, to: to};
  110. };
  111. });
  112. });