query_ctrl.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import _ from 'lodash';
  2. import { QueryCtrl } from 'app/plugins/sdk';
  3. import './query_aggregation_ctrl';
  4. import './query_filter_ctrl';
  5. export interface QueryMeta {
  6. alignmentPeriod: string;
  7. rawQuery: string;
  8. rawQueryString: string;
  9. metricLabels: { [key: string]: string[] };
  10. resourceLabels: { [key: string]: string[] };
  11. }
  12. export class StackdriverQueryCtrl extends QueryCtrl {
  13. static templateUrl = 'partials/query.editor.html';
  14. target: {
  15. project: {
  16. id: string;
  17. name: string;
  18. };
  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. project: {
  38. id: 'default',
  39. name: 'loading project...',
  40. },
  41. metricType: this.defaultDropdownValue,
  42. service: this.defaultServiceValue,
  43. metric: '',
  44. unit: '',
  45. aggregation: {
  46. crossSeriesReducer: 'REDUCE_MEAN',
  47. alignmentPeriod: 'auto',
  48. perSeriesAligner: 'ALIGN_MEAN',
  49. groupBys: [],
  50. },
  51. filters: [],
  52. showAggregationOptions: false,
  53. aliasBy: '',
  54. metricKind: '',
  55. valueType: '',
  56. };
  57. showHelp: boolean;
  58. showLastQuery: boolean;
  59. lastQueryMeta: QueryMeta;
  60. lastQueryError?: string;
  61. /** @ngInject */
  62. constructor($scope, $injector) {
  63. super($scope, $injector);
  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. }
  68. onDataReceived(dataList) {
  69. this.lastQueryError = null;
  70. this.lastQueryMeta = null;
  71. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  72. if (anySeriesFromQuery) {
  73. this.lastQueryMeta = anySeriesFromQuery.meta;
  74. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  75. }
  76. }
  77. onDataError(err) {
  78. if (err.data && err.data.results) {
  79. const queryRes = err.data.results[this.target.refId];
  80. if (queryRes && queryRes.error) {
  81. this.lastQueryMeta = queryRes.meta;
  82. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  83. let jsonBody;
  84. try {
  85. jsonBody = JSON.parse(queryRes.error);
  86. } catch {
  87. this.lastQueryError = queryRes.error;
  88. }
  89. this.lastQueryError = jsonBody.error.message;
  90. }
  91. }
  92. console.error(err);
  93. }
  94. }