2018-10-24 14:11:10 +02:00
|
|
|
import React, { PureComponent } from 'react';
|
2018-10-31 11:16:49 +01:00
|
|
|
import SimpleDropdown from './SimpleDropdown';
|
2018-10-26 14:03:05 +02:00
|
|
|
import { TemplateQueryProps } from 'app/types/plugins';
|
2018-10-31 13:07:57 +01:00
|
|
|
import { getMetricTypes, extractServicesFromMetricDescriptors } from '../functions';
|
2018-10-26 14:03:05 +02:00
|
|
|
import defaultsDeep from 'lodash/defaultsDeep';
|
2018-10-31 13:44:17 +01:00
|
|
|
import { MetricFindQueryTypes, TemplateQueryComponentData } from '../types';
|
2018-10-24 14:11:10 +02:00
|
|
|
|
2018-10-31 13:44:17 +01:00
|
|
|
export class StackdriverTemplateQueryComponent extends PureComponent<TemplateQueryProps, TemplateQueryComponentData> {
|
2018-10-25 17:00:32 +02:00
|
|
|
queryTypes: Array<{ value: string; name: string }> = [
|
2018-10-29 17:38:43 +01:00
|
|
|
{ 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' },
|
2018-10-25 17:00:32 +02:00
|
|
|
];
|
2018-10-29 14:56:55 +01:00
|
|
|
|
2018-10-31 13:44:17 +01:00
|
|
|
defaults: TemplateQueryComponentData = {
|
2018-10-31 13:36:41 +01:00
|
|
|
selectedQueryType: '',
|
2018-10-26 15:57:01 +02:00
|
|
|
metricDescriptors: [],
|
2018-10-31 13:36:41 +01:00
|
|
|
selectedService: '',
|
|
|
|
|
selectedMetricType: '',
|
2018-10-31 10:48:24 +01:00
|
|
|
labels: [],
|
|
|
|
|
labelKey: '',
|
|
|
|
|
metricTypes: [],
|
|
|
|
|
services: [],
|
2018-10-26 15:57:01 +02:00
|
|
|
};
|
2018-10-25 17:00:32 +02:00
|
|
|
|
2018-10-26 14:03:05 +02:00
|
|
|
constructor(props: TemplateQueryProps) {
|
2018-10-24 14:11:10 +02:00
|
|
|
super(props);
|
2018-10-26 09:44:36 +02:00
|
|
|
this.handleQueryTypeChange = this.handleQueryTypeChange.bind(this);
|
2018-10-25 17:00:32 +02:00
|
|
|
this.onServiceChange = this.onServiceChange.bind(this);
|
2018-10-26 09:44:36 +02:00
|
|
|
this.onMetricTypeChange = this.onMetricTypeChange.bind(this);
|
2018-10-29 17:59:10 +01:00
|
|
|
this.onLabelKeyChange = this.onLabelKeyChange.bind(this);
|
2018-10-26 14:03:05 +02:00
|
|
|
this.state = defaultsDeep(this.props.query, this.defaults);
|
2018-10-24 14:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-25 13:53:02 +02:00
|
|
|
async componentDidMount() {
|
|
|
|
|
const metricDescriptors = await this.props.datasource.getMetricTypes(this.props.datasource.projectName);
|
2018-10-31 10:48:24 +01:00
|
|
|
const services = extractServicesFromMetricDescriptors(metricDescriptors).map(m => ({
|
|
|
|
|
value: m.service,
|
|
|
|
|
name: m.serviceShortName,
|
|
|
|
|
}));
|
2018-10-31 13:36:41 +01:00
|
|
|
const selectedService = services.some(s => s.value === this.state.selectedService)
|
|
|
|
|
? this.state.selectedService
|
|
|
|
|
: services[0].value;
|
|
|
|
|
const { metricTypes, selectedMetricType } = getMetricTypes(
|
|
|
|
|
metricDescriptors,
|
|
|
|
|
this.state.selectedMetricType,
|
|
|
|
|
selectedService
|
|
|
|
|
);
|
2018-10-31 11:50:38 +01:00
|
|
|
const state: any = {
|
|
|
|
|
services,
|
2018-10-31 13:36:41 +01:00
|
|
|
selectedService,
|
2018-10-31 11:50:38 +01:00
|
|
|
metricTypes,
|
2018-10-31 13:36:41 +01:00
|
|
|
selectedMetricType,
|
2018-10-31 11:50:38 +01:00
|
|
|
metricDescriptors,
|
2018-10-31 13:36:41 +01:00
|
|
|
...await this.getLabels(this.state.selectedMetricType),
|
2018-10-31 11:50:38 +01:00
|
|
|
};
|
2018-10-31 11:26:56 +01:00
|
|
|
this.setState(state);
|
2018-10-25 17:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 10:48:24 +01:00
|
|
|
async handleQueryTypeChange(event) {
|
2018-10-31 13:36:41 +01:00
|
|
|
const state: any = {
|
|
|
|
|
selectedQueryType: event.target.value,
|
|
|
|
|
...await this.getLabels(this.state.selectedMetricType, event.target.value),
|
|
|
|
|
};
|
2018-10-31 10:48:24 +01:00
|
|
|
this.setState(state);
|
2018-10-26 15:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 10:48:24 +01:00
|
|
|
async onServiceChange(event) {
|
2018-10-31 13:36:41 +01:00
|
|
|
const { metricTypes, selectedMetricType } = getMetricTypes(
|
2018-10-31 13:07:57 +01:00
|
|
|
this.state.metricDescriptors,
|
2018-10-31 13:36:41 +01:00
|
|
|
this.state.selectedMetricType,
|
2018-10-31 13:07:57 +01:00
|
|
|
event.target.value
|
|
|
|
|
);
|
|
|
|
|
const state: any = {
|
2018-10-31 13:36:41 +01:00
|
|
|
selectedService: event.target.value,
|
2018-10-31 13:07:57 +01:00
|
|
|
metricTypes,
|
2018-10-31 13:36:41 +01:00
|
|
|
selectedMetricType,
|
|
|
|
|
...await this.getLabels(selectedMetricType),
|
2018-10-31 13:07:57 +01:00
|
|
|
};
|
2018-10-31 10:48:24 +01:00
|
|
|
this.setState(state);
|
2018-10-25 17:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 10:48:24 +01:00
|
|
|
async onMetricTypeChange(event) {
|
2018-10-31 13:36:41 +01:00
|
|
|
const state: any = { selectedMetricType: event.target.value, ...await this.getLabels(event.target.value) };
|
2018-10-31 10:48:24 +01:00
|
|
|
this.setState(state);
|
2018-10-25 17:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 13:07:57 +01:00
|
|
|
onLabelKeyChange(event) {
|
|
|
|
|
this.setState({ labelKey: event.target.value });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidUpdate() {
|
2018-10-31 13:43:07 +01:00
|
|
|
const { metricDescriptors, labels, ...queryModel } = this.state;
|
2018-10-31 13:07:57 +01:00
|
|
|
this.props.onChange(queryModel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isLabelQuery(queryType) {
|
|
|
|
|
return [MetricFindQueryTypes.MetricLabels, MetricFindQueryTypes.ResourceLabels].indexOf(queryType) !== -1;
|
2018-10-26 15:57:01 +02:00
|
|
|
}
|
|
|
|
|
|
2018-10-31 13:36:41 +01:00
|
|
|
async getLabels(selectedMetricType, selectedQueryType = this.state.selectedQueryType) {
|
2018-10-31 11:50:38 +01:00
|
|
|
let result = { labels: this.state.labels, labelKey: this.state.labelKey };
|
2018-10-31 13:36:41 +01:00
|
|
|
if (this.isLabelQuery(selectedQueryType)) {
|
2018-10-31 11:50:38 +01:00
|
|
|
const refId = 'StackdriverTemplateQueryComponent';
|
2018-10-31 13:36:41 +01:00
|
|
|
const response = await this.props.datasource.getLabels(selectedMetricType, refId);
|
|
|
|
|
const labels = Object.keys(response.meta[selectedQueryType]);
|
2018-10-31 11:50:38 +01:00
|
|
|
const labelKey = labels.indexOf(this.state.labelKey) !== -1 ? this.state.labelKey : labels[0];
|
|
|
|
|
result = { labels, labelKey };
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-29 17:42:15 +01:00
|
|
|
renderQueryTypeSwitch(queryType) {
|
2018-10-25 17:00:32 +02:00
|
|
|
switch (queryType) {
|
2018-10-29 17:38:43 +01:00
|
|
|
case MetricFindQueryTypes.MetricTypes:
|
2018-10-26 09:44:36 +02:00
|
|
|
return (
|
2018-10-31 11:16:49 +01:00
|
|
|
<SimpleDropdown
|
2018-10-31 13:36:41 +01:00
|
|
|
value={this.state.selectedService}
|
2018-10-31 10:48:24 +01:00
|
|
|
options={this.state.services}
|
|
|
|
|
onValueChange={this.onServiceChange}
|
|
|
|
|
label="Services"
|
2018-10-30 14:48:07 +01:00
|
|
|
/>
|
2018-10-26 09:44:36 +02:00
|
|
|
);
|
2018-10-29 17:38:43 +01:00
|
|
|
case MetricFindQueryTypes.MetricLabels:
|
|
|
|
|
case MetricFindQueryTypes.ResourceLabels:
|
|
|
|
|
case MetricFindQueryTypes.ResourceTypes:
|
2018-10-25 17:00:32 +02:00
|
|
|
return (
|
|
|
|
|
<React.Fragment>
|
2018-10-31 11:16:49 +01:00
|
|
|
<SimpleDropdown
|
2018-10-31 13:36:41 +01:00
|
|
|
value={this.state.selectedService}
|
2018-10-31 10:48:24 +01:00
|
|
|
options={this.state.services}
|
|
|
|
|
onValueChange={this.onServiceChange}
|
|
|
|
|
label="Services"
|
2018-10-30 14:48:07 +01:00
|
|
|
/>
|
2018-10-31 11:16:49 +01:00
|
|
|
<SimpleDropdown
|
2018-10-31 13:36:41 +01:00
|
|
|
value={this.state.selectedMetricType}
|
2018-10-31 10:48:24 +01:00
|
|
|
options={this.state.metricTypes}
|
|
|
|
|
onValueChange={this.onMetricTypeChange}
|
|
|
|
|
label="Metric Types"
|
2018-10-25 17:00:32 +02:00
|
|
|
/>
|
2018-10-31 11:26:56 +01:00
|
|
|
<SimpleDropdown
|
|
|
|
|
value={this.state.labelKey}
|
|
|
|
|
options={this.state.labels.map(l => ({ value: l, name: l }))}
|
|
|
|
|
onValueChange={this.onLabelKeyChange}
|
|
|
|
|
label={
|
2018-10-31 13:36:41 +01:00
|
|
|
this.state.selectedQueryType === MetricFindQueryTypes.ResourceLabels
|
|
|
|
|
? 'Resource Label Key'
|
|
|
|
|
: 'Metric Label Key'
|
2018-10-31 11:26:56 +01:00
|
|
|
}
|
|
|
|
|
/>
|
2018-10-25 17:00:32 +02:00
|
|
|
</React.Fragment>
|
|
|
|
|
);
|
2018-10-29 17:38:43 +01:00
|
|
|
case MetricFindQueryTypes.Alignerns:
|
|
|
|
|
case MetricFindQueryTypes.Aggregations:
|
2018-10-29 16:27:53 +01:00
|
|
|
return (
|
|
|
|
|
<React.Fragment>
|
2018-10-31 11:16:49 +01:00
|
|
|
<SimpleDropdown
|
2018-10-31 13:36:41 +01:00
|
|
|
value={this.state.selectedService}
|
2018-10-31 10:48:24 +01:00
|
|
|
options={this.state.services}
|
|
|
|
|
onValueChange={this.onServiceChange}
|
|
|
|
|
label="Services"
|
2018-10-30 14:48:07 +01:00
|
|
|
/>
|
2018-10-31 11:16:49 +01:00
|
|
|
<SimpleDropdown
|
2018-10-31 13:36:41 +01:00
|
|
|
value={this.state.selectedMetricType}
|
2018-10-31 10:48:24 +01:00
|
|
|
options={this.state.metricTypes}
|
|
|
|
|
onValueChange={this.onMetricTypeChange}
|
|
|
|
|
label="Metric Types"
|
2018-10-29 16:27:53 +01:00
|
|
|
/>
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
);
|
2018-10-25 17:00:32 +02:00
|
|
|
default:
|
|
|
|
|
return '';
|
|
|
|
|
}
|
2018-10-24 14:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-10-25 13:53:02 +02:00
|
|
|
return (
|
2018-10-25 17:00:32 +02:00
|
|
|
<React.Fragment>
|
2018-10-31 11:16:49 +01:00
|
|
|
<SimpleDropdown
|
2018-10-31 13:36:41 +01:00
|
|
|
value={this.state.selectedQueryType}
|
2018-10-31 10:57:24 +01:00
|
|
|
options={this.queryTypes}
|
|
|
|
|
onValueChange={this.handleQueryTypeChange}
|
|
|
|
|
label="Query Types"
|
|
|
|
|
/>
|
2018-10-31 13:36:41 +01:00
|
|
|
{this.renderQueryTypeSwitch(this.state.selectedQueryType)}
|
2018-10-25 17:00:32 +02:00
|
|
|
</React.Fragment>
|
2018-10-25 13:53:02 +02:00
|
|
|
);
|
2018-10-24 14:11:10 +02:00
|
|
|
}
|
|
|
|
|
}
|