query_ctrl.ts 3.6 KB

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