2018-10-26 14:03:05 +02:00
|
|
|
import { extractServicesFromMetricDescriptors, getMetricTypesByService } from './functions';
|
2018-10-26 15:57:01 +02:00
|
|
|
import has from 'lodash/has';
|
2018-10-26 14:03:05 +02:00
|
|
|
|
|
|
|
|
export default class StackdriverMetricFindQuery {
|
|
|
|
|
constructor(private datasource) {}
|
|
|
|
|
|
|
|
|
|
async query(query: any) {
|
|
|
|
|
switch (query.type) {
|
|
|
|
|
case 'services':
|
|
|
|
|
return this.handleServiceQueryType();
|
|
|
|
|
case 'metricTypes':
|
|
|
|
|
return this.handleMetricTypesQueryType(query);
|
2018-10-26 15:57:01 +02:00
|
|
|
case 'metricLabels':
|
|
|
|
|
return this.handleMetricLabelsQueryType(query);
|
2018-10-26 14:03:05 +02:00
|
|
|
default:
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handleServiceQueryType() {
|
|
|
|
|
const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
|
|
|
|
|
const services = extractServicesFromMetricDescriptors(metricDescriptors);
|
|
|
|
|
return services.map(s => ({
|
2018-10-26 16:35:12 +02:00
|
|
|
text: s.serviceShortName,
|
|
|
|
|
value: s.name,
|
2018-10-26 14:03:05 +02:00
|
|
|
expandable: true,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async handleMetricTypesQueryType({ service }) {
|
|
|
|
|
if (!service) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName);
|
|
|
|
|
return getMetricTypesByService(metricDescriptors, service).map(s => ({
|
2018-10-26 16:35:12 +02:00
|
|
|
text: s.displayName,
|
|
|
|
|
value: s.name,
|
2018-10-26 14:03:05 +02:00
|
|
|
expandable: true,
|
|
|
|
|
}));
|
|
|
|
|
}
|
2018-10-26 15:57:01 +02:00
|
|
|
|
|
|
|
|
async handleMetricLabelsQueryType({ metricType, metricLabelKey }) {
|
|
|
|
|
if (!metricType || !metricLabelKey) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const refId = 'handleMetricLabelsQueryType';
|
|
|
|
|
const response = await this.datasource.getLabels(metricType, refId);
|
|
|
|
|
return has(response, `meta.metricLabels.${metricLabelKey}`)
|
|
|
|
|
? response.meta.metricLabels[metricLabelKey].map(s => ({
|
|
|
|
|
text: s,
|
|
|
|
|
expandable: true,
|
|
|
|
|
}))
|
|
|
|
|
: [];
|
|
|
|
|
}
|
2018-10-26 14:03:05 +02:00
|
|
|
}
|