StackdriverMetricFindQuery.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. try {
  59. const refId = 'handleResourceTypeQueryQueryType';
  60. const response = await this.datasource.getLabels(selectedMetricType, refId);
  61. return response.meta.resourceTypes.map(this.toFindQueryResult);
  62. } catch (error) {
  63. return [];
  64. }
  65. }
  66. async handleAlignersQuery({ selectedMetricType }) {
  67. if (!selectedMetricType) {
  68. return [];
  69. }
  70. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  71. const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
  72. return getAlignmentOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  73. }
  74. async handleAggregationQuery({ selectedMetricType }) {
  75. if (!selectedMetricType) {
  76. return [];
  77. }
  78. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  79. const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
  80. return getAggregationOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  81. }
  82. handleAlignmentPeriodQuery() {
  83. return alignmentPeriods.map(this.toFindQueryResult);
  84. }
  85. toFindQueryResult(x) {
  86. return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true };
  87. }
  88. }