query_ctrl.ts 2.9 KB

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