StackdriverMetricFindQuery.ts 3.9 KB

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