time_srv.ts 5.5 KB

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