time_srv.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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(
  102. this.$timeout(() => {
  103. this.startNextRefreshTimer(intervalMs);
  104. this.refreshDashboard();
  105. }, intervalMs)
  106. );
  107. }
  108. // update url
  109. var params = this.$location.search();
  110. if (interval) {
  111. params.refresh = interval;
  112. this.$location.search(params);
  113. } else if (params.refresh) {
  114. delete params.refresh;
  115. this.$location.search(params);
  116. }
  117. }
  118. refreshDashboard() {
  119. this.$rootScope.$broadcast('refresh');
  120. }
  121. private startNextRefreshTimer(afterMs) {
  122. this.cancelNextRefresh();
  123. this.refreshTimer = this.timer.register(
  124. this.$timeout(() => {
  125. this.startNextRefreshTimer(afterMs);
  126. if (this.contextSrv.isGrafanaVisible()) {
  127. this.refreshDashboard();
  128. } else {
  129. this.autoRefreshBlocked = true;
  130. }
  131. }, afterMs)
  132. );
  133. }
  134. private cancelNextRefresh() {
  135. this.timer.cancel(this.refreshTimer);
  136. }
  137. setTime(time, fromRouteUpdate?) {
  138. _.extend(this.time, time);
  139. // disable refresh if zoom in or zoom out
  140. if (moment.isMoment(time.to)) {
  141. this.oldRefresh = this.dashboard.refresh || this.oldRefresh;
  142. this.setAutoRefresh(false);
  143. } else if (this.oldRefresh && this.oldRefresh !== this.dashboard.refresh) {
  144. this.setAutoRefresh(this.oldRefresh);
  145. this.oldRefresh = null;
  146. }
  147. // update url
  148. if (fromRouteUpdate !== true) {
  149. var urlRange = this.timeRangeForUrl();
  150. var urlParams = this.$location.search();
  151. urlParams.from = urlRange.from;
  152. urlParams.to = urlRange.to;
  153. this.$location.search(urlParams);
  154. }
  155. this.$rootScope.appEvent('time-range-changed', this.time);
  156. this.$timeout(this.refreshDashboard.bind(this), 0);
  157. }
  158. timeRangeForUrl() {
  159. var range = this.timeRange().raw;
  160. if (moment.isMoment(range.from)) {
  161. range.from = range.from.valueOf().toString();
  162. }
  163. if (moment.isMoment(range.to)) {
  164. range.to = range.to.valueOf().toString();
  165. }
  166. return range;
  167. }
  168. timeRange() {
  169. // make copies if they are moment (do not want to return out internal moment, because they are mutable!)
  170. var raw = {
  171. from: moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from,
  172. to: moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to,
  173. };
  174. var timezone = this.dashboard && this.dashboard.getTimezone();
  175. return {
  176. from: dateMath.parse(raw.from, false, timezone),
  177. to: dateMath.parse(raw.to, true, timezone),
  178. raw: raw,
  179. };
  180. }
  181. zoomOut(e, factor) {
  182. var range = this.timeRange();
  183. var timespan = range.to.valueOf() - range.from.valueOf();
  184. var center = range.to.valueOf() - timespan / 2;
  185. var to = center + timespan * factor / 2;
  186. var from = center - timespan * factor / 2;
  187. if (to > Date.now() && range.to <= Date.now()) {
  188. var offset = to - Date.now();
  189. from = from - offset;
  190. to = Date.now();
  191. }
  192. this.setTime({ from: moment.utc(from), to: moment.utc(to) });
  193. }
  194. }
  195. coreModule.service('timeSrv', TimeSrv);