timeSrv.js 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. define([
  2. 'angular',
  3. 'lodash',
  4. 'config',
  5. 'kbn',
  6. 'moment'
  7. ], function (angular, _, config, kbn, moment) {
  8. 'use strict';
  9. var module = angular.module('grafana.services');
  10. module.service('timeSrv', function($rootScope, $timeout, $routeParams, timer) {
  11. var self = this;
  12. this.init = function(dashboard) {
  13. timer.cancel_all();
  14. this.dashboard = dashboard;
  15. this.time = dashboard.time;
  16. this._initTimeFromUrl();
  17. if(this.dashboard.refresh) {
  18. this.set_interval(this.dashboard.refresh);
  19. }
  20. };
  21. this._parseUrlParam = function(value) {
  22. if (value.indexOf('now') !== -1) {
  23. return value;
  24. }
  25. if (value.length === 8) {
  26. return moment.utc(value, 'YYYYMMDD').toDate();
  27. }
  28. if (value.length === 15) {
  29. return moment.utc(value, 'YYYYMMDDTHHmmss').toDate();
  30. }
  31. var epoch = parseInt(value);
  32. if (!_.isNaN(epoch)) {
  33. return new Date(epoch);
  34. }
  35. return null;
  36. };
  37. this._initTimeFromUrl = function() {
  38. if ($routeParams.from) {
  39. this.time.from = this._parseUrlParam($routeParams.from) || this.time.from;
  40. }
  41. if ($routeParams.to) {
  42. this.time.to = this._parseUrlParam($routeParams.to) || this.time.to;
  43. }
  44. };
  45. this.set_interval = function (interval) {
  46. this.dashboard.refresh = interval;
  47. if (interval) {
  48. var _i = kbn.interval_to_ms(interval);
  49. this.start_scheduled_refresh(_i);
  50. } else {
  51. this.cancel_scheduled_refresh();
  52. }
  53. };
  54. this.refreshDashboard = function() {
  55. $rootScope.$broadcast('refresh');
  56. };
  57. this.start_scheduled_refresh = function (after_ms) {
  58. self.cancel_scheduled_refresh();
  59. self.refresh_timer = timer.register($timeout(function () {
  60. self.start_scheduled_refresh(after_ms);
  61. self.refreshDashboard();
  62. }, after_ms));
  63. };
  64. this.cancel_scheduled_refresh = function () {
  65. timer.cancel(this.refresh_timer);
  66. };
  67. this.setTime = function(time) {
  68. _.extend(this.time, time);
  69. // disable refresh if we have an absolute time
  70. if (time.to !== 'now') {
  71. this.old_refresh = this.dashboard.refresh;
  72. this.set_interval(false);
  73. }
  74. else if (this.old_refresh && this.old_refresh !== this.dashboard.refresh) {
  75. this.set_interval(this.old_refresh);
  76. this.old_refresh = null;
  77. }
  78. $rootScope.appEvent('time-range-changed', this.time);
  79. $timeout(this.refreshDashboard, 0);
  80. };
  81. this.timeRange = function(parse) {
  82. var _t = this.time;
  83. if(_.isUndefined(_t) || _.isUndefined(_t.from)) {
  84. return false;
  85. }
  86. if(parse === false) {
  87. return {
  88. from: _t.from,
  89. to: _t.to
  90. };
  91. } else {
  92. var _from = _t.from;
  93. var _to = _t.to || new Date();
  94. return {
  95. from: kbn.parseDate(_from),
  96. to: kbn.parseDate(_to)
  97. };
  98. }
  99. };
  100. });
  101. });