StackdriverMetricFindQuery.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import isString from 'lodash/isString';
  2. import { alignmentPeriods } from './constants';
  3. import { MetricFindQueryTypes } from './types';
  4. import {
  5. getMetricTypesByService,
  6. getAlignmentOptionsByMetric,
  7. getAggregationOptionsByMetric,
  8. extractServicesFromMetricDescriptors,
  9. getLabelKeys,
  10. } from './functions';
  11. export default class StackdriverMetricFindQuery {
  12. constructor(private datasource) {}
  13. async execute(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.LabelKeys:
  21. return this.handleLabelKeysQuery(query);
  22. case MetricFindQueryTypes.LabelValues:
  23. return this.handleLabelValuesQuery(query);
  24. case MetricFindQueryTypes.ResourceTypes:
  25. return this.handleResourceTypeQuery(query);
  26. case MetricFindQueryTypes.Aligners:
  27. return this.handleAlignersQuery(query);
  28. case MetricFindQueryTypes.AlignmentPeriods:
  29. return this.handleAlignmentPeriodQuery();
  30. case MetricFindQueryTypes.Aggregations:
  31. return this.handleAggregationQuery(query);
  32. default:
  33. return [];
  34. }
  35. } catch (error) {
  36. console.error(`Could not run StackdriverMetricFindQuery ${query}`, error);
  37. return [];
  38. }
  39. }
  40. async handleServiceQuery() {
  41. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  42. const services = extractServicesFromMetricDescriptors(metricDescriptors);
  43. return services.map(s => ({
  44. text: s.serviceShortName,
  45. value: s.service,
  46. expandable: true,
  47. }));
  48. }
  49. async handleMetricTypesQuery({ selectedService }) {
  50. if (!selectedService) {
  51. return [];
  52. }
  53. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  54. return getMetricTypesByService(metricDescriptors, this.datasource.templateSrv.replace(selectedService)).map(s => ({
  55. text: s.displayName,
  56. value: s.type,
  57. expandable: true,
  58. }));
  59. }
  60. async handleLabelKeysQuery({ selectedMetricType }) {
  61. if (!selectedMetricType) {
  62. return [];
  63. }
  64. const labelKeys = await getLabelKeys(this.datasource, selectedMetricType);
  65. return labelKeys.map(this.toFindQueryResult);
  66. }
  67. async handleLabelValuesQuery({ selectedMetricType, labelKey }) {
  68. if (!selectedMetricType) {
  69. return [];
  70. }
  71. const refId = 'handleLabelValuesQuery';
  72. const response = await this.datasource.getLabels(selectedMetricType, refId);
  73. const interpolatedKey = this.datasource.templateSrv.replace(labelKey);
  74. const [name] = interpolatedKey.split('.').reverse();
  75. let values = [];
  76. if (response.meta && response.meta.metricLabels && response.meta.metricLabels.hasOwnProperty(name)) {
  77. values = response.meta.metricLabels[name];
  78. } else if (response.meta && response.meta.resourceLabels && response.meta.resourceLabels.hasOwnProperty(name)) {
  79. values = response.meta.resourceLabels[name];
  80. }
  81. return values.map(this.toFindQueryResult);
  82. }
  83. async handleResourceTypeQuery({ selectedMetricType }) {
  84. if (!selectedMetricType) {
  85. return [];
  86. }
  87. const refId = 'handleResourceTypeQueryQueryType';
  88. const response = await this.datasource.getLabels(selectedMetricType, refId);
  89. return response.meta.resourceTypes ? response.meta.resourceTypes.map(this.toFindQueryResult) : [];
  90. }
  91. async handleAlignersQuery({ selectedMetricType }) {
  92. if (!selectedMetricType) {
  93. return [];
  94. }
  95. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  96. const { valueType, metricKind } = metricDescriptors.find(
  97. m => m.type === this.datasource.templateSrv.replace(selectedMetricType)
  98. );
  99. return getAlignmentOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  100. }
  101. async handleAggregationQuery({ selectedMetricType }) {
  102. if (!selectedMetricType) {
  103. return [];
  104. }
  105. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  106. const { valueType, metricKind } = metricDescriptors.find(
  107. m => m.type === this.datasource.templateSrv.replace(selectedMetricType)
  108. );
  109. return getAggregationOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  110. }
  111. handleAlignmentPeriodQuery() {
  112. return alignmentPeriods.map(this.toFindQueryResult);
  113. }
  114. toFindQueryResult(x) {
  115. return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true };
  116. }
  117. }