StackdriverMetricFindQuery.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import has from 'lodash/has';
  2. import isString from 'lodash/isString';
  3. import { alignmentPeriods } from './constants';
  4. import { MetricFindQueryTypes } from './types';
  5. import { getMetricTypesByService, getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from './functions';
  6. export default class StackdriverMetricFindQuery {
  7. constructor(private datasource) {}
  8. async execute(query: any) {
  9. try {
  10. switch (query.selectedQueryType) {
  11. case MetricFindQueryTypes.MetricTypes:
  12. return this.handleMetricTypesQuery(query);
  13. case MetricFindQueryTypes.MetricLabels:
  14. case MetricFindQueryTypes.ResourceLabels:
  15. return this.handleLabelQuery(query);
  16. case MetricFindQueryTypes.ResourceTypes:
  17. return this.handleResourceTypeQuery(query);
  18. case MetricFindQueryTypes.Alignerns:
  19. return this.handleAlignersQuery(query);
  20. case MetricFindQueryTypes.AlignmentPeriods:
  21. return this.handleAlignmentPeriodQuery();
  22. case MetricFindQueryTypes.Aggregations:
  23. return this.handleAggregationQuery(query);
  24. default:
  25. return [];
  26. }
  27. } catch (error) {
  28. console.error(`Could not run StackdriverMetricFindQuery ${query}`, error);
  29. return [];
  30. }
  31. }
  32. async handleMetricTypesQuery({ selectedService }) {
  33. if (!selectedService) {
  34. return [];
  35. }
  36. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  37. return getMetricTypesByService(metricDescriptors, selectedService).map(s => ({
  38. text: s.displayName,
  39. value: s.type,
  40. expandable: true,
  41. }));
  42. }
  43. async handleLabelQuery({ selectedQueryType, selectedMetricType, labelKey }) {
  44. if (!selectedMetricType) {
  45. return [];
  46. }
  47. const refId = 'handleLabelsQueryType';
  48. const response = await this.datasource.getLabels(selectedMetricType, refId);
  49. if (!has(response, `meta.${selectedQueryType}.${labelKey}`)) {
  50. return [];
  51. }
  52. return response.meta[selectedQueryType][labelKey].map(this.toFindQueryResult);
  53. }
  54. async handleResourceTypeQuery({ selectedMetricType }) {
  55. if (!selectedMetricType) {
  56. return [];
  57. }
  58. const refId = 'handleResourceTypeQueryQueryType';
  59. const response = await this.datasource.getLabels(selectedMetricType, refId);
  60. return response.meta.resourceTypes.map(this.toFindQueryResult);
  61. }
  62. async handleAlignersQuery({ selectedMetricType }) {
  63. if (!selectedMetricType) {
  64. return [];
  65. }
  66. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  67. const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
  68. return getAlignmentOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  69. }
  70. async handleAggregationQuery({ selectedMetricType }) {
  71. if (!selectedMetricType) {
  72. return [];
  73. }
  74. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  75. const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
  76. return getAggregationOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  77. }
  78. handleAlignmentPeriodQuery() {
  79. return alignmentPeriods.map(this.toFindQueryResult);
  80. }
  81. toFindQueryResult(x) {
  82. return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true };
  83. }
  84. }