metrics_panel_ctrl.ts 7.1 KB

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