TimeSrv.ts 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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
  118. const params = this.$location.search();
  119. if (interval) {
  120. params.refresh = interval;
  121. this.$location.search(params);
  122. } else if (params.refresh) {
  123. delete params.refresh;
  124. this.$location.search(params);
  125. }
  126. }
  127. refreshDashboard() {
  128. this.dashboard.timeRangeUpdated(this.timeRange());
  129. }
  130. private startNextRefreshTimer(afterMs) {
  131. this.cancelNextRefresh();
  132. this.refreshTimer = this.timer.register(
  133. this.$timeout(() => {
  134. this.startNextRefreshTimer(afterMs);
  135. if (this.contextSrv.isGrafanaVisible()) {
  136. this.refreshDashboard();
  137. } else {
  138. this.autoRefreshBlocked = true;
  139. }
  140. }, afterMs)
  141. );
  142. }
  143. private cancelNextRefresh() {
  144. this.timer.cancel(this.refreshTimer);
  145. }
  146. setTime(time, fromRouteUpdate?) {
  147. _.extend(this.time, time);
  148. // disable refresh if zoom in or zoom out
  149. if (moment.isMoment(time.to)) {
  150. this.oldRefresh = this.dashboard.refresh || this.oldRefresh;
  151. this.setAutoRefresh(false);
  152. } else if (this.oldRefresh && this.oldRefresh !== this.dashboard.refresh) {
  153. this.setAutoRefresh(this.oldRefresh);
  154. this.oldRefresh = null;
  155. }
  156. // update url
  157. if (fromRouteUpdate !== true) {
  158. const urlRange = this.timeRangeForUrl();
  159. const urlParams = this.$location.search();
  160. urlParams.from = urlRange.from;
  161. urlParams.to = urlRange.to;
  162. this.$location.search(urlParams);
  163. }
  164. this.$timeout(this.refreshDashboard.bind(this), 0);
  165. }
  166. timeRangeForUrl() {
  167. const range = this.timeRange().raw;
  168. if (moment.isMoment(range.from)) {
  169. range.from = range.from.valueOf().toString();
  170. }
  171. if (moment.isMoment(range.to)) {
  172. range.to = range.to.valueOf().toString();
  173. }
  174. return range;
  175. }
  176. timeRange(): TimeRange {
  177. // make copies if they are moment (do not want to return out internal moment, because they are mutable!)
  178. const raw = {
  179. from: moment.isMoment(this.time.from) ? moment(this.time.from) : this.time.from,
  180. to: moment.isMoment(this.time.to) ? moment(this.time.to) : this.time.to,
  181. };
  182. const timezone = this.dashboard && this.dashboard.getTimezone();
  183. return {
  184. from: dateMath.parse(raw.from, false, timezone),
  185. to: dateMath.parse(raw.to, true, timezone),
  186. raw: raw,
  187. };
  188. }
  189. zoomOut(e, factor) {
  190. const range = this.timeRange();
  191. const timespan = range.to.valueOf() - range.from.valueOf();
  192. const center = range.to.valueOf() - timespan / 2;
  193. const to = center + (timespan * factor) / 2;
  194. const from = center - (timespan * factor) / 2;
  195. this.setTime({ from: moment.utc(from), to: moment.utc(to) });
  196. }
  197. }
  198. let singleton;
  199. export function setTimeSrv(srv: TimeSrv) {
  200. singleton = srv;
  201. }
  202. export function getTimeSrv(): TimeSrv {
  203. return singleton;
  204. }
  205. coreModule.service('timeSrv', TimeSrv);