metrics_panel_ctrl.ts 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. import _ from 'lodash';
  2. import kbn from 'app/core/utils/kbn';
  3. import { PanelCtrl } from 'app/features/panel/panel_ctrl';
  4. import { getExploreUrl } from 'app/core/utils/explore';
  5. import { applyPanelTimeOverrides, getResolution } from 'app/features/dashboard/utils/panel';
  6. import { ContextSrv } from 'app/core/services/context_srv';
  7. class MetricsPanelCtrl extends PanelCtrl {
  8. scope: any;
  9. datasource: any;
  10. $q: any;
  11. $timeout: any;
  12. contextSrv: ContextSrv;
  13. datasourceSrv: any;
  14. timeSrv: any;
  15. templateSrv: any;
  16. range: any;
  17. interval: any;
  18. intervalMs: any;
  19. resolution: any;
  20. timeInfo: any;
  21. skipDataOnInit: boolean;
  22. dataStream: any;
  23. dataSubscription: any;
  24. dataList: any;
  25. constructor($scope, $injector) {
  26. super($scope, $injector);
  27. this.$q = $injector.get('$q');
  28. this.contextSrv = $injector.get('contextSrv');
  29. this.datasourceSrv = $injector.get('datasourceSrv');
  30. this.timeSrv = $injector.get('timeSrv');
  31. this.templateSrv = $injector.get('templateSrv');
  32. this.scope = $scope;
  33. this.panel.datasource = this.panel.datasource || null;
  34. this.events.on('refresh', this.onMetricsPanelRefresh.bind(this));
  35. this.events.on('panel-teardown', this.onPanelTearDown.bind(this));
  36. }
  37. private onPanelTearDown() {
  38. if (this.dataSubscription) {
  39. this.dataSubscription.unsubscribe();
  40. this.dataSubscription = null;
  41. }
  42. }
  43. private onMetricsPanelRefresh() {
  44. // ignore fetching data if another panel is in fullscreen
  45. if (this.otherPanelInFullscreenMode()) {
  46. return;
  47. }
  48. // if we have snapshot data use that
  49. if (this.panel.snapshotData) {
  50. this.updateTimeRange();
  51. let data = this.panel.snapshotData;
  52. // backward compatibility
  53. if (!_.isArray(data)) {
  54. data = data.data;
  55. }
  56. // Defer panel rendering till the next digest cycle.
  57. // For some reason snapshot panels don't init at this time, so this helps to avoid rendering issues.
  58. return this.$timeout(() => {
  59. this.events.emit('data-snapshot-load', data);
  60. });
  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.datasourceSrv
  71. .get(this.panel.datasource, this.panel.scopedVars)
  72. .then(this.updateTimeRange.bind(this))
  73. .then(this.issueQueries.bind(this))
  74. .then(this.handleQueryResult.bind(this))
  75. .catch(err => {
  76. // if canceled keep loading set to true
  77. if (err.cancelled) {
  78. console.log('Panel request cancelled', err);
  79. return;
  80. }
  81. this.loading = false;
  82. this.error = err.message || 'Request Error';
  83. this.inspector = { error: err };
  84. if (err.data) {
  85. if (err.data.message) {
  86. this.error = err.data.message;
  87. }
  88. if (err.data.error) {
  89. this.error = err.data.error;
  90. }
  91. }
  92. this.events.emit('data-error', err);
  93. console.log('Panel data error:', err);
  94. });
  95. }
  96. updateTimeRange(datasource?) {
  97. this.datasource = datasource || this.datasource;
  98. this.range = this.timeSrv.timeRange();
  99. this.resolution = getResolution(this.panel);
  100. const newTimeData = applyPanelTimeOverrides(this.panel, this.range);
  101. this.timeInfo = newTimeData.timeInfo;
  102. this.range = newTimeData.timeRange;
  103. this.calculateInterval();
  104. return this.datasource;
  105. }
  106. calculateInterval() {
  107. let intervalOverride = this.panel.interval;
  108. // if no panel interval check datasource
  109. if (intervalOverride) {
  110. intervalOverride = this.templateSrv.replace(intervalOverride, this.panel.scopedVars);
  111. } else if (this.datasource && this.datasource.interval) {
  112. intervalOverride = this.datasource.interval;
  113. }
  114. const res = kbn.calculateInterval(this.range, this.resolution, intervalOverride);
  115. this.interval = res.interval;
  116. this.intervalMs = res.intervalMs;
  117. }
  118. issueQueries(datasource) {
  119. this.datasource = datasource;
  120. if (!this.panel.targets || this.panel.targets.length === 0) {
  121. return this.$q.when([]);
  122. }
  123. // make shallow copy of scoped vars,
  124. // and add built in variables interval and interval_ms
  125. const scopedVars = Object.assign({}, this.panel.scopedVars, {
  126. __interval: { text: this.interval, value: this.interval },
  127. __interval_ms: { text: this.intervalMs, value: this.intervalMs },
  128. });
  129. const metricsQuery = {
  130. timezone: this.dashboard.getTimezone(),
  131. panelId: this.panel.id,
  132. dashboardId: this.dashboard.id,
  133. range: this.range,
  134. rangeRaw: this.range.raw,
  135. interval: this.interval,
  136. intervalMs: this.intervalMs,
  137. targets: this.panel.targets,
  138. maxDataPoints: this.resolution,
  139. scopedVars: scopedVars,
  140. cacheTimeout: this.panel.cacheTimeout,
  141. };
  142. return datasource.query(metricsQuery);
  143. }
  144. handleQueryResult(result) {
  145. this.loading = false;
  146. // check for if data source returns subject
  147. if (result && result.subscribe) {
  148. this.handleDataStream(result);
  149. return;
  150. }
  151. if (this.dashboard.snapshot) {
  152. this.panel.snapshotData = result.data;
  153. }
  154. if (!result || !result.data) {
  155. console.log('Data source query result invalid, missing data field:', result);
  156. result = { data: [] };
  157. }
  158. this.events.emit('data-received', result.data);
  159. }
  160. handleDataStream(stream) {
  161. // if we already have a connection
  162. if (this.dataStream) {
  163. console.log('two stream observables!');
  164. return;
  165. }
  166. this.dataStream = stream;
  167. this.dataSubscription = stream.subscribe({
  168. next: data => {
  169. console.log('dataSubject next!');
  170. if (data.range) {
  171. this.range = data.range;
  172. }
  173. this.events.emit('data-received', data.data);
  174. },
  175. error: error => {
  176. this.events.emit('data-error', error);
  177. console.log('panel: observer got error');
  178. },
  179. complete: () => {
  180. console.log('panel: observer got complete');
  181. this.dataStream = null;
  182. },
  183. });
  184. }
  185. getAdditionalMenuItems() {
  186. const items = [];
  187. if (this.contextSrv.hasAccessToExplore() && this.datasource) {
  188. items.push({
  189. text: 'Explore',
  190. click: 'ctrl.explore();',
  191. icon: 'gicon gicon-explore',
  192. shortcut: 'x',
  193. });
  194. }
  195. return items;
  196. }
  197. async explore() {
  198. const url = await getExploreUrl(this.panel, this.panel.targets, this.datasource, this.datasourceSrv, this.timeSrv);
  199. if (url) {
  200. this.$timeout(() => this.$location.url(url));
  201. }
  202. }
  203. }
  204. export { MetricsPanelCtrl };