metrics_panel_ctrl.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. datasourceName: any;
  15. $q: any;
  16. $timeout: any;
  17. datasourceSrv: any;
  18. timeSrv: any;
  19. templateSrv: any;
  20. timing: any;
  21. range: any;
  22. rangeRaw: any;
  23. interval: any;
  24. resolution: any;
  25. timeInfo: any;
  26. skipDataOnInit: boolean;
  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. }
  47. private onMetricsPanelRefresh() {
  48. // ignore fetching data if another panel is in fullscreen
  49. if (this.otherPanelInFullscreenMode()) { return; }
  50. // if we have snapshot data use that
  51. if (this.panel.snapshotData) {
  52. this.updateTimeRange();
  53. var data = this.panel.snapshotData;
  54. // backward compatability
  55. if (!_.isArray(data)) {
  56. data = data.data;
  57. }
  58. this.events.emit('data-snapshot-load', data);
  59. return;
  60. }
  61. // // ignore if we have data stream
  62. if (this.dataStream) {
  63. return;
  64. }
  65. // clear loading/error state
  66. delete this.error;
  67. this.loading = true;
  68. // load datasource service
  69. this.setTimeQueryStart();
  70. this.datasourceSrv.get(this.panel.datasource)
  71. .then(this.issueQueries.bind(this))
  72. .then(this.handleQueryResult.bind(this))
  73. .catch(err => {
  74. this.loading = false;
  75. this.error = err.message || "Timeseries data request error";
  76. this.inspector = {error: err};
  77. this.events.emit('data-error', err);
  78. console.log('Panel data error:', err);
  79. });
  80. }
  81. setTimeQueryStart() {
  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. metricsQuery.targets.forEach(function(target) {
  154. target.exprID = target.refId + metricsQuery.panelId;
  155. });
  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. if (!result || !result.data) {
  170. console.log('Data source query result invalid, missing data field:', result);
  171. result = {data: []};
  172. }
  173. return this.events.emit('data-received', result.data);
  174. }
  175. handleDataStream(stream) {
  176. // if we already have a connection
  177. if (this.dataStream) {
  178. console.log('two stream observables!');
  179. return;
  180. }
  181. this.dataStream = stream;
  182. this.dataSubscription = stream.subscribe({
  183. next: (data) => {
  184. console.log('dataSubject next!');
  185. if (data.range) {
  186. this.range = data.range;
  187. }
  188. this.events.emit('data-received', data.data);
  189. },
  190. error: (error) => {
  191. this.events.emit('data-error', error);
  192. console.log('panel: observer got error');
  193. },
  194. complete: () => {
  195. console.log('panel: observer got complete');
  196. }
  197. });
  198. }
  199. setDatasource(datasource) {
  200. // switching to mixed
  201. if (datasource.meta.mixed) {
  202. _.each(this.panel.targets, target => {
  203. target.datasource = this.panel.datasource;
  204. if (target.datasource === null) {
  205. target.datasource = config.defaultDatasource;
  206. }
  207. });
  208. } else if (this.datasource && this.datasource.meta.mixed) {
  209. _.each(this.panel.targets, target => {
  210. delete target.datasource;
  211. });
  212. }
  213. this.panel.datasource = datasource.value;
  214. this.datasourceName = datasource.name;
  215. this.datasource = null;
  216. this.refresh();
  217. }
  218. }
  219. export {MetricsPanelCtrl};