query_ctrl.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import _ from 'lodash';
  2. import appEvents from 'app/core/app_events';
  3. import { QueryCtrl } from 'app/plugins/sdk';
  4. import './query_aggregation_ctrl';
  5. import './query_filter_ctrl';
  6. import { MetricPicker } from './components/MetricPicker';
  7. import { OptionPicker } from './components/OptionPicker';
  8. import { OptionGroupPicker } from './components/OptionGroupPicker';
  9. import { react2AngularDirective } from 'app/core/utils/react2angular';
  10. export interface QueryMeta {
  11. alignmentPeriod: string;
  12. rawQuery: string;
  13. rawQueryString: string;
  14. metricLabels: { [key: string]: string[] };
  15. resourceLabels: { [key: string]: string[] };
  16. }
  17. export class StackdriverQueryCtrl extends QueryCtrl {
  18. static templateUrl = 'partials/query.editor.html';
  19. target: {
  20. defaultProject: string;
  21. unit: string;
  22. metricType: string;
  23. service: string;
  24. refId: string;
  25. aggregation: {
  26. crossSeriesReducer: string;
  27. alignmentPeriod: string;
  28. perSeriesAligner: string;
  29. groupBys: string[];
  30. };
  31. filters: string[];
  32. aliasBy: string;
  33. metricKind: any;
  34. valueType: any;
  35. };
  36. defaults = {
  37. defaultProject: 'loading project...',
  38. metricType: '',
  39. service: '',
  40. metric: '',
  41. unit: '',
  42. aggregation: {
  43. crossSeriesReducer: 'REDUCE_MEAN',
  44. alignmentPeriod: 'stackdriver-auto',
  45. perSeriesAligner: 'ALIGN_MEAN',
  46. groupBys: [],
  47. },
  48. filters: [],
  49. showAggregationOptions: false,
  50. aliasBy: '',
  51. metricKind: '',
  52. valueType: '',
  53. };
  54. showHelp: boolean;
  55. showLastQuery: boolean;
  56. lastQueryMeta: QueryMeta;
  57. lastQueryError?: string;
  58. labelData: QueryMeta;
  59. loadLabelsPromise: Promise<any>;
  60. templateSrv: any;
  61. /** @ngInject */
  62. constructor($scope, $injector, templateSrv, private $rootScope) {
  63. super($scope, $injector);
  64. this.templateSrv = templateSrv;
  65. _.defaultsDeep(this.target, this.defaults);
  66. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  67. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  68. this.handleMetricTypeChange = this.handleMetricTypeChange.bind(this);
  69. react2AngularDirective('optionPicker', OptionPicker, [
  70. 'options',
  71. 'onChange',
  72. 'selected',
  73. 'searchable',
  74. 'className',
  75. 'placeholder',
  76. ]);
  77. react2AngularDirective('optionGroupPicker', OptionGroupPicker, [
  78. 'groups',
  79. 'onChange',
  80. 'selected',
  81. 'searchable',
  82. 'className',
  83. 'placeholder',
  84. ]);
  85. react2AngularDirective('metricPicker', MetricPicker, [
  86. 'target',
  87. ['onChange', { watchDepth: 'reference' }],
  88. 'defaultProject',
  89. 'metricType',
  90. ['templateSrv', { watchDepth: 'reference' }],
  91. ['datasource', { watchDepth: 'reference' }],
  92. ]);
  93. this.getLabels();
  94. }
  95. handleMetricTypeChange({ valueType, metricKind, type, unit }) {
  96. this.target.metricType = type;
  97. this.target.unit = unit;
  98. this.target.valueType = valueType;
  99. this.target.metricKind = metricKind;
  100. this.$rootScope.$broadcast('metricTypeChanged');
  101. this.getLabels();
  102. this.refresh();
  103. }
  104. async getLabels() {
  105. this.loadLabelsPromise = new Promise(async resolve => {
  106. try {
  107. const { meta } = await this.datasource.getLabels(this.target.metricType, this.target.refId);
  108. this.labelData = meta;
  109. resolve();
  110. } catch (error) {
  111. if (error.data && error.data.message) {
  112. console.log(error.data.message);
  113. } else {
  114. console.log(error);
  115. }
  116. appEvents.emit('alert-error', ['Error', 'Error loading metric labels for ' + this.target.metricType]);
  117. resolve();
  118. }
  119. });
  120. }
  121. onDataReceived(dataList) {
  122. this.lastQueryError = null;
  123. this.lastQueryMeta = null;
  124. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  125. if (anySeriesFromQuery) {
  126. this.lastQueryMeta = anySeriesFromQuery.meta;
  127. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  128. }
  129. }
  130. onDataError(err) {
  131. if (err.data && err.data.results) {
  132. const queryRes = err.data.results[this.target.refId];
  133. if (queryRes && queryRes.error) {
  134. this.lastQueryMeta = queryRes.meta;
  135. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  136. let jsonBody;
  137. try {
  138. jsonBody = JSON.parse(queryRes.error);
  139. } catch {
  140. this.lastQueryError = queryRes.error;
  141. }
  142. this.lastQueryError = jsonBody.error.message;
  143. }
  144. }
  145. }
  146. }