query_ctrl.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import _ from 'lodash';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. import './query_aggregation_ctrl';
  4. import './query_filter_ctrl';
  5. import { OptionPicker } from './components/OptionPicker';
  6. import { OptionGroupPicker } from './components/OptionGroupPicker';
  7. import { react2AngularDirective } from 'app/core/utils/react2angular';
  8. export interface QueryMeta {
  9. alignmentPeriod: string;
  10. rawQuery: string;
  11. rawQueryString: string;
  12. metricLabels: { [key: string]: string[] };
  13. resourceLabels: { [key: string]: string[] };
  14. }
  15. export class StackdriverQueryCtrl extends QueryCtrl {
  16. static templateUrl = 'partials/query.editor.html';
  17. target: {
  18. defaultProject: string;
  19. unit: string;
  20. metricType: string;
  21. service: string;
  22. refId: string;
  23. aggregation: {
  24. crossSeriesReducer: string;
  25. alignmentPeriod: string;
  26. perSeriesAligner: string;
  27. groupBys: string[];
  28. };
  29. filters: string[];
  30. aliasBy: string;
  31. metricKind: any;
  32. valueType: any;
  33. };
  34. defaults = {
  35. defaultProject: 'loading project...',
  36. metricType: '',
  37. service: '',
  38. metric: '',
  39. unit: '',
  40. aggregation: {
  41. crossSeriesReducer: 'REDUCE_MEAN',
  42. alignmentPeriod: 'stackdriver-auto',
  43. perSeriesAligner: 'ALIGN_MEAN',
  44. groupBys: [],
  45. },
  46. filters: [],
  47. showAggregationOptions: false,
  48. aliasBy: '',
  49. metricKind: '',
  50. valueType: '',
  51. };
  52. showHelp: boolean;
  53. showLastQuery: boolean;
  54. lastQueryMeta: QueryMeta;
  55. lastQueryError?: string;
  56. /** @ngInject */
  57. constructor($scope, $injector) {
  58. super($scope, $injector);
  59. _.defaultsDeep(this.target, this.defaults);
  60. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  61. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  62. react2AngularDirective('optionPicker', OptionPicker, [
  63. 'options',
  64. 'onChange',
  65. 'selected',
  66. 'searchable',
  67. 'className',
  68. 'placeholder',
  69. ]);
  70. react2AngularDirective('optionGroupPicker', OptionGroupPicker, [
  71. 'groups',
  72. 'onChange',
  73. 'selected',
  74. 'searchable',
  75. 'className',
  76. 'placeholder',
  77. ]);
  78. }
  79. onDataReceived(dataList) {
  80. this.lastQueryError = null;
  81. this.lastQueryMeta = null;
  82. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  83. if (anySeriesFromQuery) {
  84. this.lastQueryMeta = anySeriesFromQuery.meta;
  85. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  86. }
  87. }
  88. onDataError(err) {
  89. if (err.data && err.data.results) {
  90. const queryRes = err.data.results[this.target.refId];
  91. if (queryRes && queryRes.error) {
  92. this.lastQueryMeta = queryRes.meta;
  93. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  94. let jsonBody;
  95. try {
  96. jsonBody = JSON.parse(queryRes.error);
  97. } catch {
  98. this.lastQueryError = queryRes.error;
  99. }
  100. this.lastQueryError = jsonBody.error.message;
  101. }
  102. }
  103. }
  104. }