metrics_panel_ctrl.ts 5.2 KB

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