TimeSrv.ts 6.6 KB

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