query_ctrl.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. projects: any;
  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. }
  63. onDataReceived(dataList) {
  64. this.lastQueryError = null;
  65. this.lastQueryMeta = null;
  66. const anySeriesFromQuery: any = _.find(dataList, { refId: this.target.refId });
  67. if (anySeriesFromQuery) {
  68. this.lastQueryMeta = anySeriesFromQuery.meta;
  69. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  70. }
  71. }
  72. onDataError(err) {
  73. if (err.data && err.data.results) {
  74. const queryRes = err.data.results[this.target.refId];
  75. if (queryRes && queryRes.error) {
  76. this.lastQueryMeta = queryRes.meta;
  77. this.lastQueryMeta.rawQueryString = decodeURIComponent(this.lastQueryMeta.rawQuery);
  78. let jsonBody;
  79. try {
  80. jsonBody = JSON.parse(queryRes.error);
  81. } catch {
  82. this.lastQueryError = queryRes.error;
  83. }
  84. this.lastQueryError = jsonBody.error.message;
  85. }
  86. }
  87. }
  88. }