diff --git a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts index a4865bf7074..130e9e0c78b 100644 --- a/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts +++ b/public/app/plugins/datasource/stackdriver/StackdriverMetricFindQuery.ts @@ -6,33 +6,35 @@ import { } from './functions'; import { alignmentPeriods } from './constants'; import has from 'lodash/has'; +import isString from 'lodash/isString'; +import { MetricFindQueryTypes } from './types'; 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); - case 'metricLabels': - case 'resourceLabels': - return this.handleLabelQueryType(query); - case 'resourceTypes': - return this.handleResourceType(query); - case 'alignerns': - return this.handleAlignersType(query); - case 'alignmentPeriods': - return this.handleAlignmentPeriodType(); - case 'aggregations': - return this.handleAggregationType(query); + case MetricFindQueryTypes.Services: + return this.handleServiceQuery(); + 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 []; } } - async handleServiceQueryType() { + async handleServiceQuery() { const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName); const services = extractServicesFromMetricDescriptors(metricDescriptors); return services.map(s => ({ @@ -42,7 +44,7 @@ export default class StackdriverMetricFindQuery { })); } - async handleMetricTypesQueryType({ service }) { + async handleMetricTypesQuery({ service }) { if (!service) { return []; } @@ -54,19 +56,7 @@ export default class StackdriverMetricFindQuery { })); } - getLabelKey({ type, metricLabelKey, resourceLabelKey }) { - switch (type) { - case 'metricLabels': - return metricLabelKey; - break; - case 'resourceLabels': - return resourceLabelKey; - default: - return ''; - } - } - - async handleLabelQueryType({ type, metricType, metricLabelKey, resourceLabelKey, resourceTypeKey }) { + async handleLabelQuery({ type, metricType, metricLabelKey, resourceLabelKey, resourceTypeKey }) { if (!metricType) { return []; } @@ -76,56 +66,57 @@ export default class StackdriverMetricFindQuery { if (!has(response, `meta.${type}.${key}`)) { return []; } - return response.meta[type][key].map(s => ({ - text: s, - expandable: true, - })); + return response.meta[type][key].map(this.toFindQueryResult); } - async handleResourceType({ metricType }) { + async handleResourceTypeQuery({ metricType }) { if (!metricType) { return []; } try { - const refId = 'handleResourceTypeQueryType'; + const refId = 'handleResourceTypeQueryQueryType'; const response = await this.datasource.getLabels(metricType, refId); - return response.meta.resourceTypes.map(s => ({ - text: s, - expandable: true, - })); + return response.meta.resourceTypes.map(this.toFindQueryResult); } catch (error) { return []; } } - async handleAlignersType({ metricType }) { + async handleAlignersQuery({ metricType }) { if (!metricType) { return []; } const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName); const { valueType, metricKind } = metricDescriptors.find(m => m.type === metricType); - return getAlignmentOptionsByMetric(valueType, metricKind).map(o => ({ - ...o, - expandable: true, - })); + return getAlignmentOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult); } - async handleAggregationType({ metricType }) { + async handleAggregationQuery({ metricType }) { if (!metricType) { return []; } const metricDescriptors = await this.datasource.getMetricTypes(this.datasource.projectName); const { valueType, metricKind } = metricDescriptors.find(m => m.type === metricType); - return getAggregationOptionsByMetric(valueType, metricKind).map(o => ({ - ...o, - expandable: true, - })); + return getAggregationOptionsByMetric(valueType, metricKind).map(this.toFindQueryResult); } - handleAlignmentPeriodType() { - return alignmentPeriods.map(s => ({ - ...s, - expandable: true, - })); + handleAlignmentPeriodQuery() { + return alignmentPeriods.map(this.toFindQueryResult); + } + + toFindQueryResult(x) { + return isString(x) ? { text: x, expandable: true } : { ...x, expandable: true }; + } + + getLabelKey({ type, metricLabelKey, resourceLabelKey }) { + switch (type) { + case MetricFindQueryTypes.MetricLabels: + return metricLabelKey; + break; + case MetricFindQueryTypes.ResourceLabels: + return resourceLabelKey; + default: + return ''; + } } } diff --git a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx index 651e4ea633f..2ddc1b1fae2 100644 --- a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx +++ b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx @@ -5,17 +5,18 @@ import SimpleDropdown from './SimpleDropdown'; import { TemplateQueryProps } from 'app/types/plugins'; import defaultsDeep from 'lodash/defaultsDeep'; import has from 'lodash/has'; +import { MetricFindQueryTypes } from '../types'; export class StackdriverTemplateQueryComponent extends PureComponent { queryTypes: Array<{ value: string; name: string }> = [ - { value: 'services', name: 'Services' }, - { value: 'metricTypes', name: 'Metric Types' }, - { value: 'metricLabels', name: 'Metric Labels' }, - { value: 'resourceLabels', name: 'Resource Labels' }, - { value: 'resourceTypes', name: 'Resource Types' }, - { value: 'aggregations', name: 'Aggregations' }, - { value: 'alignerns', name: 'Aligners' }, - { value: 'alignmentPeriods', name: 'Alignment Periods' }, + { value: MetricFindQueryTypes.Services, name: 'Services' }, + { value: MetricFindQueryTypes.MetricTypes, name: 'Metric Types' }, + { value: MetricFindQueryTypes.MetricLabels, name: 'Metric Labels' }, + { value: MetricFindQueryTypes.ResourceLabels, name: 'Resource Labels' }, + { value: MetricFindQueryTypes.ResourceTypes, name: 'Resource Types' }, + { value: MetricFindQueryTypes.Aggregations, name: 'Aggregations' }, + { value: MetricFindQueryTypes.Alignerns, name: 'Aligners' }, + { value: MetricFindQueryTypes.AlignmentPeriods, name: 'Alignment Periods' }, ]; defaults = { @@ -45,7 +46,7 @@ export class StackdriverTemplateQueryComponent extends PureComponent ); - case 'metricLabels': + case MetricFindQueryTypes.MetricLabels: return ( ); - case 'metricLabels': - case 'resourceLabels': - case 'resourceTypes': + case MetricFindQueryTypes.MetricLabels: + case MetricFindQueryTypes.ResourceLabels: + case MetricFindQueryTypes.ResourceTypes: const dropdown = this.switchMetaType(queryType); return ( @@ -133,8 +134,8 @@ export class StackdriverTemplateQueryComponent extends PureComponent ); - case 'alignerns': - case 'aggregations': + case MetricFindQueryTypes.Alignerns: + case MetricFindQueryTypes.Aggregations: return ( diff --git a/public/app/plugins/datasource/stackdriver/types.ts b/public/app/plugins/datasource/stackdriver/types.ts new file mode 100644 index 00000000000..43605623c78 --- /dev/null +++ b/public/app/plugins/datasource/stackdriver/types.ts @@ -0,0 +1,10 @@ +export enum MetricFindQueryTypes { + Services = 'services', + MetricTypes = 'metricTypes', + MetricLabels = 'metricLabels', + ResourceLabels = 'resourceLabels', + ResourceTypes = 'resourceTypes', + Aggregations = 'aggregations', + Alignerns = 'alignerns', + AlignmentPeriods = 'alignmentPeriods', +}