diff --git a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx index 3c3b0ea8c44..9e9eefc9758 100644 --- a/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx +++ b/public/app/plugins/datasource/stackdriver/components/TemplateQueryComponent.tsx @@ -1,7 +1,7 @@ import React, { PureComponent } from 'react'; import SimpleDropdown from './SimpleDropdown'; import { TemplateQueryProps } from 'app/types/plugins'; -import { getMetricTypesByService, extractServicesFromMetricDescriptors } from '../functions'; +import { getMetricTypes, extractServicesFromMetricDescriptors } from '../functions'; import defaultsDeep from 'lodash/defaultsDeep'; import { MetricFindQueryTypes } from '../types'; @@ -43,12 +43,8 @@ export class StackdriverTemplateQueryComponent extends PureComponent ({ - value: m.type, - name: m.displayName, - })); - const metricType = this.state.metricType || metricTypes[0].value; + const service = services.some(s => s.value === this.state.service) ? this.state.service : services[0].value; + const { metricTypes, metricType } = getMetricTypes(metricDescriptors, this.state.metricType, service); const state: any = { services, service, @@ -66,13 +62,17 @@ export class StackdriverTemplateQueryComponent extends PureComponent ({ - value: m.type, - name: m.displayName, - })); - const metricTypeExistInArray = metricTypes.find(m => m.value === this.state.metricType); - const metricType = metricTypeExistInArray ? metricTypeExistInArray.value : metricTypes[0].value; - const state: any = { service: event.target.value, metricTypes, metricType, ...await this.getLabels(metricType) }; + const { metricTypes, metricType } = getMetricTypes( + this.state.metricDescriptors, + this.state.metricType, + event.target.value + ); + const state: any = { + service: event.target.value, + metricTypes, + metricType, + ...await this.getLabels(metricType), + }; this.setState(state); } @@ -81,24 +81,6 @@ export class StackdriverTemplateQueryComponent extends PureComponent uniqBy( export const getMetricTypesByService = (metricDescriptors, service) => metricDescriptors.filter(m => m.service === service); +export const getMetricTypes = (metricDescriptors, selectedMetricType, service) => { + const metricTypes = getMetricTypesByService(metricDescriptors, service).map(m => ({ + value: m.type, + name: m.displayName, + })); + const metricTypeExistInArray = metricTypes.some(m => m.value === selectedMetricType); + const metricType = metricTypeExistInArray ? metricTypeExistInArray.value : metricTypes[0].value; + return { + metricTypes, + metricType, + }; +}; + export const getAlignmentOptionsByMetric = (metricValueType, metricKind) => { return !metricValueType ? []