metrics_panel_ctrl.ts 5.8 KB

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