Commit 9872ea7d by Erik Sundell

stackdriver: extract common function

parent c993a4d2
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import SimpleDropdown from './SimpleDropdown'; import SimpleDropdown from './SimpleDropdown';
import { TemplateQueryProps } from 'app/types/plugins'; import { TemplateQueryProps } from 'app/types/plugins';
import { getMetricTypesByService, extractServicesFromMetricDescriptors } from '../functions'; import { getMetricTypes, extractServicesFromMetricDescriptors } from '../functions';
import defaultsDeep from 'lodash/defaultsDeep'; import defaultsDeep from 'lodash/defaultsDeep';
import { MetricFindQueryTypes } from '../types'; import { MetricFindQueryTypes } from '../types';
...@@ -43,12 +43,8 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue ...@@ -43,12 +43,8 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue
value: m.service, value: m.service,
name: m.serviceShortName, name: m.serviceShortName,
})); }));
const service = this.state.service || services[0].value; const service = services.some(s => s.value === this.state.service) ? this.state.service : services[0].value;
const metricTypes = getMetricTypesByService(metricDescriptors, service).map(m => ({ const { metricTypes, metricType } = getMetricTypes(metricDescriptors, this.state.metricType, service);
value: m.type,
name: m.displayName,
}));
const metricType = this.state.metricType || metricTypes[0].value;
const state: any = { const state: any = {
services, services,
service, service,
...@@ -66,13 +62,17 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue ...@@ -66,13 +62,17 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue
} }
async onServiceChange(event) { async onServiceChange(event) {
const metricTypes = getMetricTypesByService(this.state.metricDescriptors, event.target.value).map(m => ({ const { metricTypes, metricType } = getMetricTypes(
value: m.type, this.state.metricDescriptors,
name: m.displayName, this.state.metricType,
})); event.target.value
const metricTypeExistInArray = metricTypes.find(m => m.value === this.state.metricType); );
const metricType = metricTypeExistInArray ? metricTypeExistInArray.value : metricTypes[0].value; const state: any = {
const state: any = { service: event.target.value, metricTypes, metricType, ...await this.getLabels(metricType) }; service: event.target.value,
metricTypes,
metricType,
...await this.getLabels(metricType),
};
this.setState(state); this.setState(state);
} }
...@@ -81,10 +81,17 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue ...@@ -81,10 +81,17 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue
this.setState(state); this.setState(state);
} }
async getLabelKeys(metricType, type = this.state.type) { onLabelKeyChange(event) {
const refId = 'StackdriverTemplateQueryComponent'; this.setState({ labelKey: event.target.value });
const response = await this.props.datasource.getLabels(metricType, refId); }
return Object.keys(response.meta[type]);
componentDidUpdate() {
const { metricDescriptors, metricLabels, resourceLabels, ...queryModel } = this.state;
this.props.onChange(queryModel);
}
isLabelQuery(queryType) {
return [MetricFindQueryTypes.MetricLabels, MetricFindQueryTypes.ResourceLabels].indexOf(queryType) !== -1;
} }
async getLabels(metricType, type = this.state.type) { async getLabels(metricType, type = this.state.type) {
...@@ -99,19 +106,6 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue ...@@ -99,19 +106,6 @@ export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQue
return result; return result;
} }
onLabelKeyChange(event) {
this.setState({ labelKey: event.target.value });
}
componentDidUpdate() {
const { metricDescriptors, metricLabels, resourceLabels, ...queryModel } = this.state;
this.props.onChange(queryModel);
}
isLabelQuery(queryType) {
return [MetricFindQueryTypes.MetricLabels, MetricFindQueryTypes.ResourceLabels].indexOf(queryType) !== -1;
}
renderQueryTypeSwitch(queryType) { renderQueryTypeSwitch(queryType) {
switch (queryType) { switch (queryType) {
case MetricFindQueryTypes.MetricTypes: case MetricFindQueryTypes.MetricTypes:
......
...@@ -6,6 +6,19 @@ export const extractServicesFromMetricDescriptors = metricDescriptors => uniqBy( ...@@ -6,6 +6,19 @@ export const extractServicesFromMetricDescriptors = metricDescriptors => uniqBy(
export const getMetricTypesByService = (metricDescriptors, service) => export const getMetricTypesByService = (metricDescriptors, service) =>
metricDescriptors.filter(m => m.service === 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) => { export const getAlignmentOptionsByMetric = (metricValueType, metricKind) => {
return !metricValueType return !metricValueType
? [] ? []
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment