metrics_panel_ctrl.ts 6.0 KB

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