StackdriverMetricFindQuery.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import isString from 'lodash/isString';
  2. import { alignmentPeriods } from './constants';
  3. import { MetricFindQueryTypes } from './types';
  4. import { getMetricTypesByService, getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from './functions';
  5. export default class StackdriverMetricFindQuery {
  6. constructor(private datasource) {}
  7. async execute(query: any) {
  8. try {
  9. switch (query.selectedQueryType) {
  10. case MetricFindQueryTypes.MetricTypes:
  11. return this.handleMetricTypesQuery(query);
  12. case MetricFindQueryTypes.LabelKeys:
  13. return this.handleLabelKeysQuery(query);
  14. case MetricFindQueryTypes.LabelValues:
  15. return this.handleLabelValuesQuery(query);
  16. case MetricFindQueryTypes.ResourceTypes:
  17. return this.handleResourceTypeQuery(query);
  18. case MetricFindQueryTypes.Aligners:
  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 handleLabelKeysQuery({ selectedQueryType, selectedMetricType, labelKey }) {
  44. if (!selectedMetricType) {
  45. return [];
  46. }
  47. const refId = 'handleLabelKeysQuery';
  48. const response = await this.datasource.getLabels(selectedMetricType, refId);
  49. const labelKeys = response.meta
  50. ? [...Object.keys(response.meta.resourceLabels), ...Object.keys(response.meta.metricLabels)]
  51. : [];
  52. return labelKeys.map(this.toFindQueryResult);
  53. }
  54. async handleLabelValuesQuery({ selectedQueryType, selectedMetricType, labelKey }) {
  55. if (!selectedMetricType) {
  56. return [];
  57. }
  58. const refId = 'handleLabelValuesQuery';
  59. const response = await this.datasource.getLabels(selectedMetricType, refId);
  60. let values = [];
  61. if (response.meta && response.meta.metricLabels && response.meta.metricLabels.hasOwnProperty(labelKey)) {
  62. values = response.meta.metricLabels[labelKey];
  63. } else if (response.meta && response.meta.resourceLabels && response.meta.resourceLabels.hasOwnProperty(labelKey)) {
  64. values = response.meta.resourceLabels[labelKey];
  65. }
  66. return values.map(this.toFindQueryResult);
  67. }
  68. async handleResourceTypeQuery({ selectedMetricType }) {
  69. if (!selectedMetricType) {
  70. return [];
  71. }
  72. const refId = 'handleResourceTypeQueryQueryType';
  73. const response = await this.datasource.getLabels(selectedMetricType, refId);
  74. return response.meta.resourceTypes ? response.meta.resourceTypes.map(this.toFindQueryResult) : [];
  75. }
  76. async handleAlignersQuery({ selectedMetricType }) {
  77. if (!selectedMetricType) {
  78. return [];
  79. }
  80. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  81. const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
  82. return getAlignmentOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  83. }
  84. async handleAggregationQuery({ selectedMetricType }) {
  85. if (!selectedMetricType) {
  86. return [];
  87. }
  88. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  89. const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
  90. return getAggregationOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  91. }
  92. handleAlignmentPeriodQuery() {
  93. return alignmentPeriods.map(this.toFindQueryResult);
  94. }
  95. toFindQueryResult(x) {
  96. return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true };
  97. }
  98. }