time_srv.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import angular from 'angular';
  4. import moment from 'moment';
  5. import _ from 'lodash';
  6. import coreModule from 'app/core/core_module';
  7. import kbn from 'app/core/utils/kbn';
  8. import * as dateMath from 'app/core/utils/datemath';
  9. class TimeSrv {
  10. time: any;
  11. refreshTimer: any;
  12. refresh: boolean;
  13. oldRefresh: boolean;
  14. dashboard: any;
  15. timeAtLoad: any;
  16. private autoRefreshBlocked: boolean;
  17. /** @ngInject **/
  18. constructor(private $rootScope, private $timeout, private $location, private timer, private contextSrv) {
  19. // default time
  20. this.time = {from: '6h', to: 'now'};
  21. $rootScope.$on('zoom-out', this.zoomOut.bind(this));
  22. $rootScope.$on('$routeUpdate', this.routeUpdated.bind(this));
  23. document.addEventListener('visibilitychange', () => {
  24. if (this.autoRefreshBlocked && document.visibilityState === 'visible') {
  25. this.autoRefreshBlocked = false;
  26. this.refreshDashboard();
  27. }
  28. });
  29. }
  30. init(dashboard) {
  31. this.timer.cancelAll();
  32. this.dashboard = dashboard;
  33. this.time = dashboard.time;
  34. this.refresh = dashboard.refresh;
  35. this.initTimeFromUrl();
  36. this.parseTime();
  37. // remember time at load so we can go back to it
  38. this.timeAtLoad = _.cloneDeep(this.time);
  39. if (this.refresh) {
  40. this.setAutoRefresh(this.refresh);
  41. }
  42. }
  43. private parseTime() {
  44. // when absolute time is saved in json it is turned to a string
  45. if (_.isString(this.time.from) && this.time.from.indexOf('Z') >= 0) {
  46. this.time.from = moment(this.time.from).utc();
  47. }
  48. if (_.isString(this.time.to) && this.time.to.indexOf('Z') >= 0) {
  49. this.time.to = moment(this.time.to).utc();
  50. }
  51. }
  52. private parseUrlParam(value) {
  53. if (value.indexOf('now') !== -1) {
  54. return value;
  55. }
  56. if (value.length === 8) {
  57. return moment.utc(value, 'YYYYMMDD');
  58. }
  59. if (value.length === 15) {
  60. return moment.utc(value, 'YYYYMMDDTHHmmss');
  61. }
  62. if (!isNaN(value)) {
  63. var epoch = parseInt(value);
  64. return moment.utc(epoch);
  65. }
  66. return null;
  67. }
  68. private initTimeFromUrl() {
  69. var params = this.$location.search();
  70. if (params.from) {
  71. this.time.from = this.parseUrlParam(params.from) || this.time.from;
  72. }
  73. if (params.to) {
  74. this.time.to = this.parseUrlParam(params.to) || this.time.to;
  75. }
  76. if (params.refresh) {
  77. this.refresh = params.refresh || this.refresh;
  78. }
  79. }
  80. private routeUpdated() {
  81. var params = this.$location.search();
  82. var urlRange = this.timeRangeForUrl();
  83. // check if url has time range
  84. if (params.from && params.to) {
  85. // is it different from what our current time range?
  86. if (params.from !== urlRange.from || params.to !== urlRange.to) {
  87. // issue update
  88. this.initTimeFromUrl();
  89. this.setTime(this.time, true);
  90. }
  91. } else if (this.timeHasChangedSinceLoad()) {
  92. this.setTime(this.timeAtLoad, true);
  93. }
  94. }
  95. private timeHasChangedSinceLoad() {
  96. return this.timeAtLoad.from !== this.time.from || this.timeAtLoad.to !== this.time.to;
  97. }
  98. setAutoRefresh(interval) {
  99. this.dashboard.refresh = interval;
  100. if (interval) {
  101. var intervalMs = kbn.interval_to_ms(interval);
  102. this.$timeout(() => {
  103. this.startNextRefreshTimer(intervalMs);
  104. this.refreshDashboard();
  105. }, intervalMs);
  106. } else {
  107. this.cancelNextRefresh();
  108. }
  109. // update url
  110. if (interval) {
  111. var params = this.$location.search();
  112. params.refresh = interval;
  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. return {
  167. from: dateMath.parse(raw.from, false),
  168. to: dateMath.parse(raw.to, true),
  169. raw: raw
  170. };
  171. }
  172. zoomOut(e, factor) {
  173. var range = this.timeRange();
  174. var timespan = (range.to.valueOf() - range.from.valueOf());
  175. var center = range.to.valueOf() - timespan/2;
  176. var to = (center + (timespan*factor)/2);
  177. var from = (center - (timespan*factor)/2);
  178. if (to > Date.now() && range.to <= Date.now()) {
  179. var offset = to - Date.now();
  180. from = from - offset;
  181. to = Date.now();
  182. }
  183. this.setTime({from: moment.utc(from), to: moment.utc(to)});
  184. }
  185. }
  186. coreModule.service('timeSrv', TimeSrv);