query_ctrl.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. defaultDropdownValue = 'Select Metric';
  35. defaultServiceValue = 'All Services';
  36. defaults = {
  37. defaultProject: 'loading project...',
  38. metricType: this.defaultDropdownValue,
  39. service: this.defaultServiceValue,
  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. /** @ngInject */
  59. constructor($scope, $injector) {
  60. super($scope, $injector);
  61. _.defaultsDeep(this.target, this.defaults);
  62. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  63. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  64. react2AngularDirective('optionPicker', OptionPicker, [
  65. 'options',
  66. 'onChange',
  67. 'selected',
  68. 'searchable',
  69. 'className',
  70. 'placeholder',
  71. ]);
  72. react2AngularDirective('optionGroupPicker', OptionGroupPicker, [
  73. 'groups',
  74. 'onChange',
  75. 'selected',
  76. 'searchable',
  77. 'className',
  78. 'placeholder',
  79. ]);
  80. }
  81. onDataReceived(dataList) {
  82. this.lastQueryError = null;
  83. this.lastQueryMeta = null;
  84. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  85. if (anySeriesFromQuery) {
  86. this.lastQueryMeta = anySeriesFromQuery.meta;
  87. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  88. }
  89. }
  90. onDataError(err) {
  91. if (err.data && err.data.results) {
  92. const queryRes = err.data.results[this.target.refId];
  93. if (queryRes && queryRes.error) {
  94. this.lastQueryMeta = queryRes.meta;
  95. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  96. let jsonBody;
  97. try {
  98. jsonBody = JSON.parse(queryRes.error);
  99. } catch {
  100. this.lastQueryError = queryRes.error;
  101. }
  102. this.lastQueryError = jsonBody.error.message;
  103. }
  104. }
  105. }
  106. }