StackdriverMetricFindQuery.ts 3.8 KB

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