query_ctrl.ts 4.3 KB

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