CloudMonitoringMetricFindQuery.ts 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. import { isString } from 'lodash';
  2. import { SelectableValue } from '@grafana/data';
  3. import { ALIGNMENT_PERIODS, SELECTORS } from './constants';
  4. import CloudMonitoringDatasource from './datasource';
  5. import {
  6. extractServicesFromMetricDescriptors,
  7. getAggregationOptionsByMetric,
  8. getAlignmentOptionsByMetric,
  9. getLabelKeys,
  10. getMetricTypesByService,
  11. } from './functions';
  12. import { CloudMonitoringVariableQuery, MetricDescriptor, MetricFindQueryTypes, MetricKind, ValueTypes } from './types';
  13. export default class CloudMonitoringMetricFindQuery {
  14. constructor(private datasource: CloudMonitoringDatasource) {}
  15. async execute(query: CloudMonitoringVariableQuery) {
  16. try {
  17. if (!query.projectName) {
  18. query.projectName = this.datasource.getDefaultProject();
  19. }
  20. switch (query.selectedQueryType) {
  21. case MetricFindQueryTypes.Projects:
  22. return this.handleProjectsQuery();
  23. case MetricFindQueryTypes.Services:
  24. return this.handleServiceQuery(query);
  25. case MetricFindQueryTypes.MetricTypes:
  26. return this.handleMetricTypesQuery(query);
  27. case MetricFindQueryTypes.LabelKeys:
  28. return this.handleLabelKeysQuery(query);
  29. case MetricFindQueryTypes.LabelValues:
  30. return this.handleLabelValuesQuery(query);
  31. case MetricFindQueryTypes.ResourceTypes:
  32. return this.handleResourceTypeQuery(query);
  33. case MetricFindQueryTypes.Aligners:
  34. return this.handleAlignersQuery(query);
  35. case MetricFindQueryTypes.AlignmentPeriods:
  36. return this.handleAlignmentPeriodQuery();
  37. case MetricFindQueryTypes.Aggregations:
  38. return this.handleAggregationQuery(query);
  39. case MetricFindQueryTypes.SLOServices:
  40. return this.handleSLOServicesQuery(query);
  41. case MetricFindQueryTypes.SLO:
  42. return this.handleSLOQuery(query);
  43. case MetricFindQueryTypes.Selectors:
  44. return this.handleSelectorQuery();
  45. default:
  46. return [];
  47. }
  48. } catch (error) {
  49. console.error(`Could not run CloudMonitoringMetricFindQuery ${query}`, error);
  50. return [];
  51. }
  52. }
  53. async handleProjectsQuery() {
  54. const projects = await this.datasource.getProjects();
  55. return (projects as SelectableValue<string>).map((s: { label: string; value: string }) => ({
  56. text: s.label,
  57. value: s.value,
  58. expandable: true,
  59. }));
  60. }
  61. async handleServiceQuery({ projectName }: CloudMonitoringVariableQuery) {
  62. const metricDescriptors = await this.datasource.getMetricTypes(projectName);
  63. const services: MetricDescriptor[] = extractServicesFromMetricDescriptors(metricDescriptors);
  64. return services.map((s) => ({
  65. text: s.serviceShortName,
  66. value: s.service,
  67. expandable: true,
  68. }));
  69. }
  70. async handleMetricTypesQuery({ selectedService, projectName }: CloudMonitoringVariableQuery) {
  71. if (!selectedService) {
  72. return [];
  73. }
  74. const metricDescriptors = await this.datasource.getMetricTypes(projectName);
  75. return getMetricTypesByService(metricDescriptors, this.datasource.templateSrv.replace(selectedService)).map(
  76. (s) => ({
  77. text: s.displayName,
  78. value: s.type,
  79. expandable: true,
  80. })
  81. );
  82. }
  83. async handleLabelKeysQuery({ selectedMetricType, projectName }: CloudMonitoringVariableQuery) {
  84. if (!selectedMetricType) {
  85. return [];
  86. }
  87. const labelKeys = await getLabelKeys(this.datasource, selectedMetricType, projectName);
  88. return labelKeys.map(this.toFindQueryResult);
  89. }
  90. async handleLabelValuesQuery({ selectedMetricType, labelKey, projectName }: CloudMonitoringVariableQuery) {
  91. if (!selectedMetricType) {
  92. return [];
  93. }
  94. const refId = 'handleLabelValuesQuery';
  95. // REDUCE_MEAN is needed so the groupBy is not ignored
  96. const labels = await this.datasource.getLabels(selectedMetricType, refId, projectName, {
  97. groupBys: [labelKey],
  98. crossSeriesReducer: 'REDUCE_MEAN',
  99. });
  100. const interpolatedKey = this.datasource.templateSrv.replace(labelKey);
  101. const values = labels.hasOwnProperty(interpolatedKey) ? labels[interpolatedKey] : [];
  102. return values.map(this.toFindQueryResult);
  103. }
  104. async handleResourceTypeQuery({ selectedMetricType, projectName }: CloudMonitoringVariableQuery) {
  105. if (!selectedMetricType) {
  106. return [];
  107. }
  108. const refId = 'handleResourceTypeQueryQueryType';
  109. const labels = await this.datasource.getLabels(selectedMetricType, refId, projectName);
  110. return labels['resource.type']?.map(this.toFindQueryResult) ?? [];
  111. }
  112. async handleAlignersQuery({ selectedMetricType, projectName }: CloudMonitoringVariableQuery) {
  113. if (!selectedMetricType) {
  114. return [];
  115. }
  116. const metricDescriptors = await this.datasource.getMetricTypes(projectName);
  117. const descriptor = metricDescriptors.find(
  118. (m) => m.type === this.datasource.templateSrv.replace(selectedMetricType)
  119. );
  120. if (!descriptor) {
  121. return [];
  122. }
  123. return getAlignmentOptionsByMetric(descriptor.valueType, descriptor.metricKind).map(this.toFindQueryResult);
  124. }
  125. async handleAggregationQuery({ selectedMetricType, projectName }: CloudMonitoringVariableQuery) {
  126. if (!selectedMetricType) {
  127. return [];
  128. }
  129. const metricDescriptors = await this.datasource.getMetricTypes(projectName);
  130. const descriptor = metricDescriptors.find(
  131. (m) => m.type === this.datasource.templateSrv.replace(selectedMetricType)
  132. );
  133. if (!descriptor) {
  134. return [];
  135. }
  136. return getAggregationOptionsByMetric(descriptor.valueType as ValueTypes, descriptor.metricKind as MetricKind).map(
  137. this.toFindQueryResult
  138. );
  139. }
  140. async handleSLOServicesQuery({ projectName }: CloudMonitoringVariableQuery) {
  141. const services = await this.datasource.getSLOServices(projectName);
  142. return services.map(this.toFindQueryResult);
  143. }
  144. async handleSLOQuery({ selectedSLOService, projectName }: CloudMonitoringVariableQuery) {
  145. const slos = await this.datasource.getServiceLevelObjectives(projectName, selectedSLOService);
  146. return slos.map(this.toFindQueryResult);
  147. }
  148. async handleSelectorQuery() {
  149. return SELECTORS.map(this.toFindQueryResult);
  150. }
  151. handleAlignmentPeriodQuery() {
  152. return ALIGNMENT_PERIODS.map(this.toFindQueryResult);
  153. }
  154. toFindQueryResult(x: any) {
  155. return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true };
  156. }
  157. }