StackdriverMetricFindQuery.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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: any) {}
  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: any[] = extractServicesFromMetricDescriptors(metricDescriptors);
  43. return services.map(s => ({
  44. text: s.serviceShortName,
  45. value: s.service,
  46. expandable: true,
  47. }));
  48. }
  49. async handleMetricTypesQuery({ selectedService }: any) {
  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(
  55. (s: any) => ({
  56. text: s.displayName,
  57. value: s.type,
  58. expandable: true,
  59. })
  60. );
  61. }
  62. async handleLabelKeysQuery({ selectedMetricType }: any) {
  63. if (!selectedMetricType) {
  64. return [];
  65. }
  66. const labelKeys = await getLabelKeys(this.datasource, selectedMetricType);
  67. return labelKeys.map(this.toFindQueryResult);
  68. }
  69. async handleLabelValuesQuery({ selectedMetricType, labelKey }: any) {
  70. if (!selectedMetricType) {
  71. return [];
  72. }
  73. const refId = 'handleLabelValuesQuery';
  74. const response = await this.datasource.getLabels(selectedMetricType, refId);
  75. const interpolatedKey = this.datasource.templateSrv.replace(labelKey);
  76. const [name] = interpolatedKey.split('.').reverse();
  77. let values = [];
  78. if (response.meta && response.meta.metricLabels && response.meta.metricLabels.hasOwnProperty(name)) {
  79. values = response.meta.metricLabels[name];
  80. } else if (response.meta && response.meta.resourceLabels && response.meta.resourceLabels.hasOwnProperty(name)) {
  81. values = response.meta.resourceLabels[name];
  82. }
  83. return values.map(this.toFindQueryResult);
  84. }
  85. async handleResourceTypeQuery({ selectedMetricType }: any) {
  86. if (!selectedMetricType) {
  87. return [];
  88. }
  89. const refId = 'handleResourceTypeQueryQueryType';
  90. const response = await this.datasource.getLabels(selectedMetricType, refId);
  91. return response.meta.resourceTypes ? response.meta.resourceTypes.map(this.toFindQueryResult) : [];
  92. }
  93. async handleAlignersQuery({ selectedMetricType }: any) {
  94. if (!selectedMetricType) {
  95. return [];
  96. }
  97. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  98. const { valueType, metricKind } = metricDescriptors.find(
  99. (m: any) => m.type === this.datasource.templateSrv.replace(selectedMetricType)
  100. );
  101. return getAlignmentOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  102. }
  103. async handleAggregationQuery({ selectedMetricType }: any) {
  104. if (!selectedMetricType) {
  105. return [];
  106. }
  107. const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
  108. const { valueType, metricKind } = metricDescriptors.find(
  109. (m: any) => m.type === this.datasource.templateSrv.replace(selectedMetricType)
  110. );
  111. return getAggregationOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
  112. }
  113. handleAlignmentPeriodQuery() {
  114. return alignmentPeriods.map(this.toFindQueryResult);
  115. }
  116. toFindQueryResult(x: any) {
  117. return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true };
  118. }
  119. }