metrics_panel_ctrl.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 '../../core/utils/rangeutil';
  8. import * as dateMath from '../../core/utils/datemath';
  9. class MetricsPanelCtrl extends PanelCtrl {
  10. error: boolean;
  11. loading: boolean;
  12. datasource: any;
  13. $q: any;
  14. $timeout: any;
  15. datasourceSrv: any;
  16. timeSrv: any;
  17. timing: any;
  18. range: any;
  19. rangeRaw: any;
  20. interval: any;
  21. resolution: any;
  22. timeInfo: any;
  23. skipDataOnInit: boolean;
  24. datasources: any[];
  25. constructor($scope, $injector) {
  26. super($scope, $injector);
  27. // make metrics tab the default
  28. this.editorTabIndex = 1;
  29. this.$q = $injector.get('$q');
  30. this.datasourceSrv = $injector.get('datasourceSrv');
  31. this.timeSrv = $injector.get('timeSrv');
  32. if (!this.panel.targets) {
  33. this.panel.targets = [{}];
  34. }
  35. }
  36. initEditMode() {
  37. this.addEditorTab('Metrics', 'public/app/partials/metrics.html');
  38. this.addEditorTab('Time range', 'public/app/features/panel/partials/panelTime.html');
  39. this.datasources = this.datasourceSrv.getMetricSources();
  40. }
  41. refresh() {
  42. this.getData();
  43. }
  44. refreshData(data) {
  45. // null op
  46. return this.$q.when(data);
  47. }
  48. loadSnapshot(data) {
  49. // null op
  50. return data;
  51. }
  52. getData() {
  53. // ignore fetching data if another panel is in fullscreen
  54. if (this.otherPanelInFullscreenMode()) { return; }
  55. // if we have snapshot data use that
  56. if (this.panel.snapshotData) {
  57. if (this.loadSnapshot) {
  58. this.loadSnapshot(this.panel.snapshotData);
  59. }
  60. return;
  61. }
  62. // clear loading/error state
  63. delete this.error;
  64. this.loading = true;
  65. // load datasource service
  66. this.datasourceSrv.get(this.panel.datasource).then(datasource => {
  67. this.datasource = datasource;
  68. return this.refreshData(this.datasource);
  69. }).then(() => {
  70. this.loading = false;
  71. }).catch(err => {
  72. console.log('Panel data error:', err);
  73. this.loading = false;
  74. this.error = err.message || "Timeseries data request error";
  75. this.inspector = {error: err};
  76. });
  77. }
  78. setTimeQueryStart() {
  79. this.timing = {};
  80. this.timing.queryStart = new Date().getTime();
  81. }
  82. setTimeQueryEnd() {
  83. this.timing.queryEnd = new Date().getTime();
  84. }
  85. updateTimeRange() {
  86. this.range = this.timeSrv.timeRange();
  87. this.rangeRaw = this.timeSrv.timeRange(false);
  88. this.applyPanelTimeOverrides();
  89. if (this.panel.maxDataPoints) {
  90. this.resolution = this.panel.maxDataPoints;
  91. } else {
  92. this.resolution = Math.ceil($(window).width() * (this.panel.span / 12));
  93. }
  94. var panelInterval = this.panel.interval;
  95. var datasourceInterval = (this.datasource || {}).interval;
  96. this.interval = kbn.calculateInterval(this.range, this.resolution, panelInterval || datasourceInterval);
  97. };
  98. applyPanelTimeOverrides() {
  99. this.timeInfo = '';
  100. // check panel time overrrides
  101. if (this.panel.timeFrom) {
  102. var timeFromInfo = rangeUtil.describeTextRange(this.panel.timeFrom);
  103. if (timeFromInfo.invalid) {
  104. this.timeInfo = 'invalid time override';
  105. return;
  106. }
  107. if (_.isString(this.rangeRaw.from)) {
  108. var timeFromDate = dateMath.parse(timeFromInfo.from);
  109. this.timeInfo = timeFromInfo.display;
  110. this.rangeRaw.from = timeFromInfo.from;
  111. this.rangeRaw.to = timeFromInfo.to;
  112. this.range.from = timeFromDate;
  113. }
  114. }
  115. if (this.panel.timeShift) {
  116. var timeShiftInfo = rangeUtil.describeTextRange(this.panel.timeShift);
  117. if (timeShiftInfo.invalid) {
  118. this.timeInfo = 'invalid timeshift';
  119. return;
  120. }
  121. var timeShift = '-' + this.panel.timeShift;
  122. this.timeInfo += ' timeshift ' + timeShift;
  123. this.range.from = dateMath.parseDateMath(timeShift, this.range.from, false);
  124. this.range.to = dateMath.parseDateMath(timeShift, this.range.to, true);
  125. this.rangeRaw = this.range;
  126. }
  127. if (this.panel.hideTimeOverride) {
  128. this.timeInfo = '';
  129. }
  130. };
  131. issueQueries(datasource) {
  132. this.updateTimeRange();
  133. if (!this.panel.targets || this.panel.targets.length === 0) {
  134. return this.$q.when([]);
  135. }
  136. var metricsQuery = {
  137. range: this.range,
  138. rangeRaw: this.rangeRaw,
  139. interval: this.interval,
  140. targets: this.panel.targets,
  141. format: this.panel.renderer === 'png' ? 'png' : 'json',
  142. maxDataPoints: this.resolution,
  143. scopedVars: this.panel.scopedVars,
  144. cacheTimeout: this.panel.cacheTimeout
  145. };
  146. this.setTimeQueryStart();
  147. try {
  148. return datasource.query(metricsQuery).then(results => {
  149. this.setTimeQueryEnd();
  150. if (this.dashboard.snapshot) {
  151. this.panel.snapshotData = results;
  152. }
  153. return results;
  154. });
  155. } catch (err) {
  156. return this.$q.reject(err);
  157. }
  158. }
  159. setDatasource(datasource) {
  160. // switching to mixed
  161. if (datasource.meta.mixed) {
  162. _.each(this.panel.targets, target => {
  163. target.datasource = this.panel.datasource;
  164. if (target.datasource === null) {
  165. target.datasource = config.defaultDatasource;
  166. }
  167. });
  168. } else if (this.datasource && this.datasource.meta.mixed) {
  169. _.each(this.panel.targets, target => {
  170. delete target.datasource;
  171. });
  172. }
  173. this.panel.datasource = datasource.value;
  174. this.datasource = null;
  175. this.refresh();
  176. }
  177. addDataQuery(datasource) {
  178. var target = {
  179. datasource: datasource ? datasource.name : undefined
  180. };
  181. this.panel.targets.push(target);
  182. }
  183. }
  184. export {MetricsPanelCtrl};