metrics_panel_ctrl.ts 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. ///<reference path="../../headers/common.d.ts" />
  2. import config from 'app/core/config';
  3. import $ from 'jquery';
  4. import _ from 'lodash';
  5. import kbn from 'app/core/utils/kbn';
  6. import {PanelCtrl} from './panel_ctrl';
  7. import * as rangeUtil from 'app/core/utils/rangeutil';
  8. import * as dateMath from 'app/core/utils/datemath';
  9. import {Subject} from 'vendor/npm/rxjs/Subject';
  10. class MetricsPanelCtrl extends PanelCtrl {
  11. error: boolean;
  12. loading: boolean;
  13. datasource: any;
  14. $q: any;
  15. $timeout: any;
  16. datasourceSrv: any;
  17. timeSrv: any;
  18. templateSrv: any;
  19. timing: any;
  20. range: any;
  21. rangeRaw: any;
  22. interval: any;
  23. resolution: any;
  24. timeInfo: any;
  25. skipDataOnInit: boolean;
  26. dataStream: any;
  27. dataSubscription: any;
  28. constructor($scope, $injector) {
  29. super($scope, $injector);
  30. // make metrics tab the default
  31. this.editorTabIndex = 1;
  32. this.$q = $injector.get('$q');
  33. this.datasourceSrv = $injector.get('datasourceSrv');
  34. this.timeSrv = $injector.get('timeSrv');
  35. this.templateSrv = $injector.get('templateSrv');
  36. if (!this.panel.targets) {
  37. this.panel.targets = [{}];
  38. }
  39. this.events.on('refresh', this.onMetricsPanelRefresh.bind(this));
  40. this.events.on('init-edit-mode', this.onInitMetricsPanelEditMode.bind(this));
  41. }
  42. private onInitMetricsPanelEditMode() {
  43. this.addEditorTab('Metrics', 'public/app/partials/metrics.html');
  44. this.addEditorTab('Time range', 'public/app/features/panel/partials/panelTime.html');
  45. }
  46. private onMetricsPanelRefresh() {
  47. // ignore fetching data if another panel is in fullscreen
  48. if (this.otherPanelInFullscreenMode()) { return; }
  49. // if we have snapshot data use that
  50. if (this.panel.snapshotData) {
  51. this.updateTimeRange();
  52. var data = this.panel.snapshotData;
  53. // backward compatability
  54. if (!_.isArray(data)) {
  55. data = data.data;
  56. }
  57. this.events.emit('data-snapshot-load', data);
  58. return;
  59. }
  60. // // ignore if we have data stream
  61. if (this.dataStream) {
  62. return;
  63. }
  64. // clear loading/error state
  65. delete this.error;
  66. this.loading = true;
  67. // load datasource service
  68. this.setTimeQueryStart();
  69. this.datasourceSrv.get(this.panel.datasource)
  70. .then(this.issueQueries.bind(this))
  71. .then(this.handleQueryResult.bind(this))
  72. .catch(err => {
  73. this.loading = false;
  74. this.error = err.message || "Timeseries data request error";
  75. this.inspector = {error: err};
  76. this.events.emit('data-error', err);
  77. console.log('Panel data error:', err);
  78. });
  79. }
  80. setTimeQueryStart() {
  81. this.timing = {};
  82. this.timing.queryStart = new Date().getTime();
  83. }
  84. setTimeQueryEnd() {
  85. this.timing.queryEnd = new Date().getTime();
  86. }
  87. updateTimeRange() {
  88. this.range = this.timeSrv.timeRange();
  89. this.rangeRaw = this.timeSrv.timeRange(false);
  90. this.applyPanelTimeOverrides();
  91. if (this.panel.maxDataPoints) {
  92. this.resolution = this.panel.maxDataPoints;
  93. } else {
  94. this.resolution = Math.ceil($(window).width() * (this.panel.span / 12));
  95. }
  96. var panelInterval = this.panel.interval;
  97. var datasourceInterval = (this.datasource || {}).interval;
  98. this.interval = kbn.calculateInterval(this.range, this.resolution, panelInterval || datasourceInterval);
  99. };
  100. applyPanelTimeOverrides() {
  101. this.timeInfo = '';
  102. // check panel time overrrides
  103. if (this.panel.timeFrom) {
  104. var timeFromInterpolated = this.templateSrv.replace(this.panel.timeFrom, this.panel.scopedVars);
  105. var timeFromInfo = rangeUtil.describeTextRange(timeFromInterpolated);
  106. if (timeFromInfo.invalid) {
  107. this.timeInfo = 'invalid time override';
  108. return;
  109. }
  110. if (_.isString(this.rangeRaw.from)) {
  111. var timeFromDate = dateMath.parse(timeFromInfo.from);
  112. this.timeInfo = timeFromInfo.display;
  113. this.rangeRaw.from = timeFromInfo.from;
  114. this.rangeRaw.to = timeFromInfo.to;
  115. this.range.from = timeFromDate;
  116. this.range.to = dateMath.parse(timeFromInfo.to);
  117. }
  118. }
  119. if (this.panel.timeShift) {
  120. var timeShiftInterpolated = this.templateSrv.replace(this.panel.timeShift, this.panel.scopedVars);
  121. var timeShiftInfo = rangeUtil.describeTextRange(timeShiftInterpolated);
  122. if (timeShiftInfo.invalid) {
  123. this.timeInfo = 'invalid timeshift';
  124. return;
  125. }
  126. var timeShift = '-' + timeShiftInterpolated;
  127. this.timeInfo += ' timeshift ' + timeShift;
  128. this.range.from = dateMath.parseDateMath(timeShift, this.range.from, false);
  129. this.range.to = dateMath.parseDateMath(timeShift, this.range.to, true);
  130. this.rangeRaw = this.range;
  131. }
  132. if (this.panel.hideTimeOverride) {
  133. this.timeInfo = '';
  134. }
  135. };
  136. issueQueries(datasource) {
  137. this.updateTimeRange();
  138. this.datasource = datasource;
  139. if (!this.panel.targets || this.panel.targets.length === 0) {
  140. return this.$q.when([]);
  141. }
  142. var metricsQuery = {
  143. panelId: this.panel.id,
  144. range: this.range,
  145. rangeRaw: this.rangeRaw,
  146. interval: this.interval,
  147. targets: this.panel.targets,
  148. format: this.panel.renderer === 'png' ? 'png' : 'json',
  149. maxDataPoints: this.resolution,
  150. scopedVars: this.panel.scopedVars,
  151. cacheTimeout: this.panel.cacheTimeout
  152. };
  153. return datasource.query(metricsQuery);
  154. }
  155. handleQueryResult(result) {
  156. this.setTimeQueryEnd();
  157. this.loading = false;
  158. // check for if data source returns subject
  159. if (result && result.subscribe) {
  160. this.handleDataStream(result);
  161. return;
  162. }
  163. if (this.dashboard.snapshot) {
  164. this.panel.snapshotData = result.data;
  165. }
  166. return this.events.emit('data-received', result.data);
  167. }
  168. handleDataStream(stream) {
  169. // if we already have a connection
  170. if (this.dataStream) {
  171. console.log('two stream observables!');
  172. return;
  173. }
  174. this.dataStream = stream;
  175. this.dataSubscription = stream.subscribe({
  176. next: (data) => {
  177. console.log('dataSubject next!');
  178. if (data.range) {
  179. this.range = data.range;
  180. }
  181. this.events.emit('data-received', data.data);
  182. },
  183. error: (error) => {
  184. this.events.emit('data-error', error);
  185. console.log('panel: observer got error');
  186. },
  187. complete: () => {
  188. console.log('panel: observer got complete');
  189. }
  190. });
  191. }
  192. setDatasource(datasource) {
  193. // switching to mixed
  194. if (datasource.meta.mixed) {
  195. _.each(this.panel.targets, target => {
  196. target.datasource = this.panel.datasource;
  197. if (target.datasource === null) {
  198. target.datasource = config.defaultDatasource;
  199. }
  200. });
  201. } else if (this.datasource && this.datasource.meta.mixed) {
  202. _.each(this.panel.targets, target => {
  203. delete target.datasource;
  204. });
  205. }
  206. this.panel.datasource = datasource.value;
  207. this.datasource = null;
  208. this.refresh();
  209. }
  210. }
  211. export {MetricsPanelCtrl};