time_srv.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ///<reference path="../../headers/common.d.ts" />
  2. import moment from 'moment';
  3. import _ from 'lodash';
  4. import coreModule from 'app/core/core_module';
  5. import kbn from 'app/core/utils/kbn';
  6. import * as dateMath from 'app/core/utils/datemath';
  7. class TimeSrv {
  8. time: any;
  9. refreshTimer: any;
  10. refresh: boolean;
  11. oldRefresh: boolean;
  12. dashboard: any;
  13. timeAtLoad: any;
  14. private autoRefreshBlocked: boolean;
  15. /** @ngInject **/
  16. constructor(private $rootScope, private $timeout, private $location, private timer, private contextSrv) {
  17. // default time
  18. this.time = {from: '6h', to: 'now'};
  19. $rootScope.$on('zoom-out', this.zoomOut.bind(this));
  20. $rootScope.$on('$routeUpdate', this.routeUpdated.bind(this));
  21. document.addEventListener('visibilitychange', () => {
  22. if (this.autoRefreshBlocked && document.visibilityState === 'visible') {
  23. this.autoRefreshBlocked = false;
  24. this.refreshDashboard();
  25. }
  26. });
  27. }
  28. init(dashboard) {
  29. this.timer.cancelAll();
  30. this.dashboard = dashboard;
  31. this.time = dashboard.time;
  32. this.refresh = dashboard.refresh;
  33. this.initTimeFromUrl();
  34. this.parseTime();
  35. // remember time at load so we can go back to it
  36. this.timeAtLoad = _.cloneDeep(this.time);
  37. if (this.refresh) {
  38. this.setAutoRefresh(this.refresh);
  39. }
  40. }
  41. private parseTime() {
  42. // when absolute time is saved in json it is turned to a string
  43. if (_.isString(this.time.from) && this.time.from.indexOf('Z') >= 0) {
  44. this.time.from = moment(this.time.from).utc();
  45. }
  46. if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
  47. this.time.to = moment(this.time.to).utc();
  48. }
  49. }
  50. private parseUrlParam(value) {
  51. if (value.indexOf('now') !== -1) {
  52. return value;
  53. }
  54. if (value.length === 8) {
  55. return moment.utc(value, 'YYYYMMDD');
  56. }
  57. if (value.length === 15) {
  58. return moment.utc(value, 'YYYYMMDDTHHmmss');
  59. }
  60. if (!isNaN(value)) {
  61. var epoch = parseInt(value);
  62. return moment.utc(epoch);
  63. }
  64. return null;
  65. }
  66. private initTimeFromUrl() {
  67. var params = this.$location.search();
  68. if (params.from) {
  69. this.time.from = this.parseUrlParam(params.from) || this.time.from;
  70. }
  71. if (params.to) {
  72. this.time.to = this.parseUrlParam(params.to) || this.time.to;
  73. }
  74. if (params.refresh) {
  75. this.refresh = params.refresh || this.refresh;
  76. }
  77. }
  78. private routeUpdated() {
  79. var params = this.$location.search();
  80. var urlRange = this.timeRangeForUrl();
  81. // check if url has time range
  82. if (params.from && params.to) {
  83. // is it different from what our current time range?
  84. if (params.from !== urlRange.from || params.to !== urlRange.to) {
  85. // issue update
  86. this.initTimeFromUrl();
  87. this.setTime(this.time, true);
  88. }
  89. } else if (this.timeHasChangedSinceLoad()) {
  90. this.setTime(this.timeAtLoad, true);
  91. }
  92. }
  93. private timeHasChangedSinceLoad() {
  94. return this.timeAtLoad.from !== this.time.from || this.timeAtLoad.to !== this.time.to;
  95. }
  96. setAutoRefresh(interval) {
  97. this.dashboard.refresh = interval;
  98. this.cancelNextRefresh();
  99. if (interval) {
  100. var intervalMs = kbn.interval_to_ms(interval);
  101. this.refreshTimer = this.timer.register(this.$timeout(() => {
  102. this.startNextRefreshTimer(intervalMs);
  103. this.refreshDashboard();
  104. }, intervalMs));
  105. }
  106. // update url
  107. var params = this.$location.search();
  108. if (interval) {
  109. params.refresh = interval;
  110. this.$location.search(params);
  111. } else if (params.refresh) {
  112. delete params.refresh;
  113. this.$location.search(params);
  114. }
  115. }
  116. refreshDashboard() {
  117. this.$rootScope.$broadcast('refresh');
  118. }
  119. private startNextRefreshTimer(afterMs) {
  120. this.cancelNextRefresh();
  121. this.refreshTimer = this.timer.register(this.$timeout(() => {
  122. this.startNextRefreshTimer(afterMs);
  123. if (this.contextSrv.isGrafanaVisible()) {
  124. this.refreshDashboard();
  125. } else {
  126. this.autoRefreshBlocked = true;
  127. }
  128. }, afterMs));
  129. }
  130. private cancelNextRefresh() {
  131. this.timer.cancel(this.refreshTimer);
  132. }
  133. setTime(time, fromRouteUpdate?) {
  134. _.extend(this.time, time);
  135. // disable refresh if zoom in or zoom out
  136. if (moment.isMoment(time.to)) {
  137. this.oldRefresh = this.dashboard.refresh || this.oldRefresh;
  138. this.setAutoRefresh(false);
  139. } else if (this.oldRefresh && this.oldRefresh !== this.dashboard.refresh) {
  140. this.setAutoRefresh(this.oldRefresh);
  141. this.oldRefresh = null;
  142. }
  143. // update url
  144. if (fromRouteUpdate !== true) {
  145. var urlRange = this.timeRangeForUrl();
  146. var urlParams = this.$location.search();
  147. urlParams.from = urlRange.from;
  148. urlParams.to = urlRange.to;
  149. this.$location.search(urlParams);
  150. }
  151. this.$rootScope.appEvent('time-range-changed', this.time);
  152. this.$timeout(this.refreshDashboard.bind(this), 0);
  153. }
  154. timeRangeForUrl() {
  155. var range = this.timeRange().raw;
  156. if (moment.isMoment(range.from)) { range.from = range.from.valueOf().toString(); }
  157. if (moment.isMoment(range.to)) { range.to = range.to.valueOf().toString(); }
  158. return range;
  159. }
  160. timeRange() {
  161. // make copies if they are moment (do not want to return out internal moment, because they are mutable!)
  162. var raw = {
  163. from: moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from,
  164. to: moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to,
  165. };
  166. var timezone = this.dashboard && this.dashboard.getTimezone ? this.dashboard.getTimezone() : 'local';
  167. return {
  168. from: dateMath.parse(raw.from, false, timezone),
  169. to: dateMath.parse(raw.to, true, timezone),
  170. raw: raw
  171. };
  172. }
  173. zoomOut(e, factor) {
  174. var range = this.timeRange();
  175. var timespan = (range.to.valueOf() - range.from.valueOf());
  176. var center = range.to.valueOf() - timespan/2;
  177. var to = (center + (timespan*factor)/2);
  178. var from = (center - (timespan*factor)/2);
  179. if (to > Date.now() && range.to <= Date.now()) {
  180. var offset = to - Date.now();
  181. from = from - offset;
  182. to = Date.now();
  183. }
  184. this.setTime({from: moment.utc(from), to: moment.utc(to)});
  185. }
  186. }
  187. coreModule.service('timeSrv', TimeSrv);