query_ctrl.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. defaultProject: string;
  16. unit: string;
  17. metricType: string;
  18. service: string;
  19. refId: string;
  20. aggregation: {
  21. crossSeriesReducer: string;
  22. alignmentPeriod: string;
  23. perSeriesAligner: string;
  24. groupBys: string[];
  25. };
  26. filters: string[];
  27. aliasBy: string;
  28. metricKind: any;
  29. valueType: any;
  30. };
  31. defaultDropdownValue = 'Select Metric';
  32. defaultServiceValue = 'All Services';
  33. defaults = {
  34. defaultProject: 'loading project...',
  35. metricType: this.defaultDropdownValue,
  36. service: this.defaultServiceValue,
  37. metric: '',
  38. unit: '',
  39. aggregation: {
  40. crossSeriesReducer: 'REDUCE_MEAN',
  41. alignmentPeriod: 'stackdriver-auto',
  42. perSeriesAligner: 'ALIGN_MEAN',
  43. groupBys: [],
  44. },
  45. filters: [],
  46. showAggregationOptions: false,
  47. aliasBy: '',
  48. metricKind: '',
  49. valueType: '',
  50. };
  51. showHelp: boolean;
  52. showLastQuery: boolean;
  53. lastQueryMeta: QueryMeta;
  54. lastQueryError?: string;
  55. /** @ngInject */
  56. constructor($scope, $injector) {
  57. super($scope, $injector);
  58. _.defaultsDeep(this.target, this.defaults);
  59. this.panelCtrl.events.on('data-received', this.onDataReceived.bind(this), $scope);
  60. this.panelCtrl.events.on('data-error', this.onDataError.bind(this), $scope);
  61. }
  62. onDataReceived(dataList) {
  63. this.lastQueryError = null;
  64. this.lastQueryMeta = null;
  65. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  66. if (anySeriesFromQuery) {
  67. this.lastQueryMeta = anySeriesFromQuery.meta;
  68. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  69. }
  70. }
  71. onDataError(err) {
  72. if (err.data && err.data.results) {
  73. const queryRes = err.data.results[this.target.refId];
  74. if (queryRes && queryRes.error) {
  75. this.lastQueryMeta = queryRes.meta;
  76. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  77. let jsonBody;
  78. try {
  79. jsonBody = JSON.parse(queryRes.error);
  80. } catch {
  81. this.lastQueryError = queryRes.error;
  82. }
  83. this.lastQueryError = jsonBody.error.message;
  84. }
  85. }
  86. console.error(err);
  87. }
  88. }