StackdriverMetricFindQuery.ts 4.1 KB

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