time_srv.ts 5.4 KB

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