grafana/public/app/plugins/datasource/stackdriver/components/VariableQueryEditor.tsx

197 lines
6.9 KiB
TypeScript
Raw Normal View History

import React, { PureComponent } from 'react';
import { VariableQueryProps } from 'app/types/plugins';
2018-10-31 20:55:40 +08:00
import SimpleSelect from './SimpleSelect';
import { getMetricTypes, getLabelKeys, extractServicesFromMetricDescriptors } from '../functions';
import { MetricFindQueryTypes, VariableQueryData } from '../types';
export class StackdriverVariableQueryEditor extends PureComponent<VariableQueryProps, VariableQueryData> {
queryTypes: Array<{ value: string; name: string }> = [
{ value: MetricFindQueryTypes.Services, name: 'Services' },
2018-10-30 00:38:43 +08:00
{ value: MetricFindQueryTypes.MetricTypes, name: 'Metric Types' },
{ value: MetricFindQueryTypes.LabelKeys, name: 'Label Keys' },
{ value: MetricFindQueryTypes.LabelValues, name: 'Label Values' },
2018-10-30 00:38:43 +08:00
{ value: MetricFindQueryTypes.ResourceTypes, name: 'Resource Types' },
{ value: MetricFindQueryTypes.Aggregations, name: 'Aggregations' },
2018-11-09 21:36:04 +08:00
{ value: MetricFindQueryTypes.Aligners, name: 'Aligners' },
2018-10-30 00:38:43 +08:00
{ value: MetricFindQueryTypes.AlignmentPeriods, name: 'Alignment Periods' },
];
defaults: VariableQueryData = {
selectedQueryType: this.queryTypes[0].value,
2018-10-26 21:57:01 +08:00
metricDescriptors: [],
2018-10-31 20:36:41 +08:00
selectedService: '',
selectedMetricType: '',
labels: [],
labelKey: '',
metricTypes: [],
services: [],
2018-10-26 21:57:01 +08:00
};
constructor(props: VariableQueryProps) {
super(props);
this.state = Object.assign(this.defaults, this.props.query);
}
async componentDidMount() {
const metricDescriptors = await this.props.datasource.getMetricTypes(this.props.datasource.projectName);
const services = extractServicesFromMetricDescriptors(metricDescriptors).map(m => ({
value: m.service,
name: m.serviceShortName,
}));
let selectedService = '';
if (services.some(s => s.value === this.props.templateSrv.replace(this.state.selectedService))) {
selectedService = this.state.selectedService;
} else if (services && services.length > 0) {
selectedService = services[0].value;
}
2018-10-31 20:36:41 +08:00
const { metricTypes, selectedMetricType } = getMetricTypes(
metricDescriptors,
this.state.selectedMetricType,
this.props.templateSrv.replace(this.state.selectedMetricType),
this.props.templateSrv.replace(selectedService)
2018-10-31 20:36:41 +08:00
);
const state: any = {
services,
2018-10-31 20:36:41 +08:00
selectedService,
metricTypes,
2018-10-31 20:36:41 +08:00
selectedMetricType,
metricDescriptors,
...(await this.getLabels(selectedMetricType)),
};
2018-10-31 18:26:56 +08:00
this.setState(state);
}
async onQueryTypeChange(event) {
2018-10-31 20:36:41 +08:00
const state: any = {
selectedQueryType: event.target.value,
...(await this.getLabels(this.state.selectedMetricType, event.target.value)),
2018-10-31 20:36:41 +08:00
};
this.setState(state);
2018-10-26 21:57:01 +08:00
}
async onServiceChange(event) {
2018-10-31 20:36:41 +08:00
const { metricTypes, selectedMetricType } = getMetricTypes(
2018-10-31 20:07:57 +08:00
this.state.metricDescriptors,
2018-10-31 20:36:41 +08:00
this.state.selectedMetricType,
this.props.templateSrv.replace(this.state.selectedMetricType),
this.props.templateSrv.replace(event.target.value)
2018-10-31 20:07:57 +08:00
);
const state: any = {
2018-10-31 20:36:41 +08:00
selectedService: event.target.value,
2018-10-31 20:07:57 +08:00
metricTypes,
2018-10-31 20:36:41 +08:00
selectedMetricType,
...(await this.getLabels(selectedMetricType)),
2018-10-31 20:07:57 +08:00
};
this.setState(state);
}
async onMetricTypeChange(event) {
const state: any = { selectedMetricType: event.target.value, ...(await this.getLabels(event.target.value)) };
this.setState(state);
}
2018-10-31 20:07:57 +08:00
onLabelKeyChange(event) {
this.setState({ labelKey: event.target.value });
}
componentDidUpdate() {
2018-11-01 17:14:28 +08:00
const { metricDescriptors, labels, metricTypes, services, ...queryModel } = this.state;
2018-11-09 20:44:58 +08:00
const query = this.queryTypes.find(q => q.value === this.state.selectedQueryType);
this.props.onChange(queryModel, `Stackdriver - ${query.name}`);
2018-10-31 20:07:57 +08:00
}
async getLabels(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
let result = { labels: this.state.labels, labelKey: this.state.labelKey };
if (selectedMetricType && selectedQueryType === MetricFindQueryTypes.LabelValues) {
const labels = await getLabelKeys(this.props.datasource, selectedMetricType);
const labelKey = labels.some(l => l === this.props.templateSrv.replace(this.state.labelKey))
? this.state.labelKey
: labels[0];
result = { labels, labelKey };
}
return result;
}
insertTemplateVariables(options) {
const templateVariables = this.props.templateSrv.variables.map(v => ({ name: `$${v.name}`, value: `$${v.name}` }));
return [...templateVariables, ...options];
}
renderQueryTypeSwitch(queryType) {
switch (queryType) {
2018-10-30 00:38:43 +08:00
case MetricFindQueryTypes.MetricTypes:
2018-11-01 16:38:51 +08:00
return (
<SimpleSelect
value={this.state.selectedService}
options={this.insertTemplateVariables(this.state.services)}
2018-11-13 18:06:08 +08:00
onValueChange={e => this.onServiceChange(e)}
label="Service"
2018-11-01 16:38:51 +08:00
/>
);
case MetricFindQueryTypes.LabelKeys:
case MetricFindQueryTypes.LabelValues:
2018-10-30 00:38:43 +08:00
case MetricFindQueryTypes.ResourceTypes:
return (
2019-01-08 20:00:31 +08:00
<>
2018-11-01 16:38:51 +08:00
<SimpleSelect
value={this.state.selectedService}
options={this.insertTemplateVariables(this.state.services)}
2018-11-13 18:06:08 +08:00
onValueChange={e => this.onServiceChange(e)}
label="Service"
2018-11-01 16:38:51 +08:00
/>
<SimpleSelect
value={this.state.selectedMetricType}
options={this.insertTemplateVariables(this.state.metricTypes)}
2018-11-13 18:06:08 +08:00
onValueChange={e => this.onMetricTypeChange(e)}
label="Metric Type"
2018-11-01 16:38:51 +08:00
/>
{queryType === MetricFindQueryTypes.LabelValues && (
2018-11-01 16:38:51 +08:00
<SimpleSelect
value={this.state.labelKey}
options={this.insertTemplateVariables(this.state.labels.map(l => ({ value: l, name: l })))}
2018-11-13 18:06:08 +08:00
onValueChange={e => this.onLabelKeyChange(e)}
label="Label Key"
2018-11-01 16:38:51 +08:00
/>
)}
2019-01-08 20:00:31 +08:00
</>
);
2018-11-09 21:36:04 +08:00
case MetricFindQueryTypes.Aligners:
2018-10-30 00:38:43 +08:00
case MetricFindQueryTypes.Aggregations:
2018-10-29 23:27:53 +08:00
return (
2019-01-08 20:00:31 +08:00
<>
2018-11-01 16:38:51 +08:00
<SimpleSelect
value={this.state.selectedService}
options={this.insertTemplateVariables(this.state.services)}
2018-11-13 18:06:08 +08:00
onValueChange={e => this.onServiceChange(e)}
label="Service"
2018-11-01 16:38:51 +08:00
/>
<SimpleSelect
value={this.state.selectedMetricType}
options={this.insertTemplateVariables(this.state.metricTypes)}
2018-11-13 18:06:08 +08:00
onValueChange={e => this.onMetricTypeChange(e)}
label="Metric Type"
2018-11-01 16:38:51 +08:00
/>
2019-01-08 20:00:31 +08:00
</>
2018-10-29 23:27:53 +08:00
);
default:
return '';
}
}
render() {
return (
2019-01-08 20:00:31 +08:00
<>
2018-10-31 20:55:40 +08:00
<SimpleSelect
2018-10-31 20:36:41 +08:00
value={this.state.selectedQueryType}
options={this.queryTypes}
onValueChange={e => this.onQueryTypeChange(e)}
label="Query Type"
/>
2018-10-31 20:36:41 +08:00
{this.renderQueryTypeSwitch(this.state.selectedQueryType)}
2019-01-08 20:00:31 +08:00
</>
);
}
}