Files
grafana/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts

98 lines
3.4 KiB
TypeScript
Raw Normal View History

2018-10-31 13:55:40 +01:00
import has from 'lodash/has';
import isString from 'lodash/isString';
import { alignmentPeriods } from './constants';
import { MetricFindQueryTypes } from './types';
import { getMetricTypesByService, getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from './functions';
export default class StackdriverMetricFindQuery {
constructor(private datasource) {}
async query(query: any) {
try {
2018-10-31 13:36:41 +01:00
switch (query.selectedQueryType) {
case MetricFindQueryTypes.MetricTypes:
return this.handleMetricTypesQuery(query);
case MetricFindQueryTypes.MetricLabels:
case MetricFindQueryTypes.ResourceLabels:
return this.handleLabelQuery(query);
case MetricFindQueryTypes.ResourceTypes:
return this.handleResourceTypeQuery(query);
case MetricFindQueryTypes.Alignerns:
return this.handleAlignersQuery(query);
case MetricFindQueryTypes.AlignmentPeriods:
return this.handleAlignmentPeriodQuery();
case MetricFindQueryTypes.Aggregations:
return this.handleAggregationQuery(query);
default:
return [];
}
} catch (error) {
console.error(`Could not run StackdriverMetricFindQuery ${query}`, error);
return [];
}
}
2018-10-31 13:36:41 +01:00
async handleMetricTypesQuery({ selectedService }) {
if (!selectedService) {
return [];
}
const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
2018-10-31 13:36:41 +01:00
return getMetricTypesByService(metricDescriptors, selectedService).map(s => ({
text: s.displayName,
2018-11-01 13:31:01 +01:00
value: s.type,
expandable: true,
}));
}
2018-10-26 15:57:01 +02:00
2018-10-31 13:36:41 +01:00
async handleLabelQuery({ selectedQueryType, selectedMetricType, labelKey }) {
if (!selectedMetricType) {
2018-10-26 15:57:01 +02:00
return [];
}
const refId = 'handleLabelsQueryType';
2018-10-31 13:36:41 +01:00
const response = await this.datasource.getLabels(selectedMetricType, refId);
if (!has(response, `meta.${selectedQueryType}.${labelKey}`)) {
return [];
}
2018-10-31 13:36:41 +01:00
return response.meta[selectedQueryType][labelKey].map(this.toFindQueryResult);
2018-10-26 15:57:01 +02:00
}
2018-10-29 15:41:11 +01:00
2018-10-31 13:36:41 +01:00
async handleResourceTypeQuery({ selectedMetricType }) {
if (!selectedMetricType) {
2018-10-29 15:41:11 +01:00
return [];
}
try {
2018-10-29 17:38:43 +01:00
const refId = 'handleResourceTypeQueryQueryType';
2018-10-31 13:36:41 +01:00
const response = await this.datasource.getLabels(selectedMetricType, refId);
2018-10-29 17:38:43 +01:00
return response.meta.resourceTypes.map(this.toFindQueryResult);
2018-10-29 15:41:11 +01:00
} catch (error) {
return [];
}
}
2018-10-29 16:27:53 +01:00
2018-10-31 13:36:41 +01:00
async handleAlignersQuery({ selectedMetricType }) {
if (!selectedMetricType) {
2018-10-29 16:27:53 +01:00
return [];
}
const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
2018-10-31 13:36:41 +01:00
const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
2018-10-29 17:38:43 +01:00
return getAlignmentOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
2018-10-29 16:40:59 +01:00
}
2018-10-31 13:36:41 +01:00
async handleAggregationQuery({ selectedMetricType }) {
if (!selectedMetricType) {
2018-10-29 16:57:07 +01:00
return [];
}
const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
2018-10-31 13:36:41 +01:00
const { valueType, metricKind } = metricDescriptors.find(m => m.type === selectedMetricType);
2018-10-29 17:38:43 +01:00
return getAggregationOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult);
2018-10-29 16:57:07 +01:00
}
2018-10-29 17:38:43 +01:00
handleAlignmentPeriodQuery() {
return alignmentPeriods.map(this.toFindQueryResult);
}
toFindQueryResult(x) {
return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true };
}
}