2018-12-13 07:16:48 +08:00
|
|
|
import React from 'react';
|
|
|
|
|
import _ from 'lodash';
|
|
|
|
|
|
2019-01-02 22:07:38 +08:00
|
|
|
import { MetricSelect } from 'app/core/components/Select/MetricSelect';
|
2018-12-13 07:16:48 +08:00
|
|
|
|
|
|
|
|
export interface Props {
|
|
|
|
|
onChange: (metricDescriptor) => void;
|
|
|
|
|
templateSrv: any;
|
|
|
|
|
datasource: any;
|
|
|
|
|
defaultProject: string;
|
|
|
|
|
metricType: string;
|
2018-12-19 22:54:45 +08:00
|
|
|
children?: (renderProps: any) => JSX.Element;
|
2018-12-13 07:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
|
metricDescriptors: any[];
|
|
|
|
|
metrics: any[];
|
|
|
|
|
services: any[];
|
|
|
|
|
service: string;
|
|
|
|
|
metric: string;
|
2018-12-19 22:54:45 +08:00
|
|
|
metricDescriptor: any;
|
2018-12-29 02:45:24 +08:00
|
|
|
defaultProject: string;
|
2018-12-13 07:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
2018-12-19 21:29:00 +08:00
|
|
|
export class Metrics extends React.Component<Props, State> {
|
2018-12-13 07:16:48 +08:00
|
|
|
state: State = {
|
|
|
|
|
metricDescriptors: [],
|
|
|
|
|
metrics: [],
|
|
|
|
|
services: [],
|
|
|
|
|
service: '',
|
|
|
|
|
metric: '',
|
2018-12-19 22:54:45 +08:00
|
|
|
metricDescriptor: null,
|
2018-12-29 02:45:24 +08:00
|
|
|
defaultProject: '',
|
2018-12-13 07:16:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-12-29 02:45:24 +08:00
|
|
|
this.setState({ defaultProject: this.props.defaultProject }, () => {
|
|
|
|
|
this.getCurrentProject()
|
|
|
|
|
.then(this.loadMetricDescriptors.bind(this))
|
|
|
|
|
.then(this.initializeServiceAndMetrics.bind(this));
|
|
|
|
|
});
|
2018-12-13 07:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async getCurrentProject() {
|
|
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
|
try {
|
2018-12-29 02:45:24 +08:00
|
|
|
if (!this.state.defaultProject || this.state.defaultProject === 'loading project...') {
|
|
|
|
|
const defaultProject = await this.props.datasource.getDefaultProject();
|
|
|
|
|
this.setState({ defaultProject });
|
2018-12-13 07:16:48 +08:00
|
|
|
}
|
2018-12-29 02:45:24 +08:00
|
|
|
resolve(this.state.defaultProject);
|
2018-12-13 07:16:48 +08:00
|
|
|
} catch (error) {
|
|
|
|
|
// appEvents.emit('ds-request-error', error);
|
|
|
|
|
reject();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async loadMetricDescriptors() {
|
2018-12-29 02:45:24 +08:00
|
|
|
if (this.state.defaultProject !== 'loading project...') {
|
|
|
|
|
const metricDescriptors = await this.props.datasource.getMetricTypes(this.state.defaultProject);
|
2018-12-13 07:16:48 +08:00
|
|
|
this.setState({ metricDescriptors });
|
|
|
|
|
return metricDescriptors;
|
|
|
|
|
} else {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async initializeServiceAndMetrics() {
|
|
|
|
|
const { metricDescriptors } = this.state;
|
|
|
|
|
const services = this.getServicesList(metricDescriptors);
|
|
|
|
|
const metrics = this.getMetricsList(metricDescriptors);
|
|
|
|
|
const service = metrics.length > 0 ? metrics[0].service : '';
|
2018-12-19 22:54:45 +08:00
|
|
|
const metricDescriptor = this.getSelectedMetricDescriptor(this.props.metricType);
|
|
|
|
|
this.setState({ metricDescriptors, services, metrics, service: service, metricDescriptor });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getSelectedMetricDescriptor(metricType) {
|
|
|
|
|
return this.state.metricDescriptors.find(md => md.type === this.props.templateSrv.replace(metricType));
|
2018-12-13 07:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getMetricsList(metricDescriptors) {
|
2018-12-19 22:54:45 +08:00
|
|
|
const selectedMetricDescriptor = this.getSelectedMetricDescriptor(this.props.metricType);
|
2018-12-29 02:45:24 +08:00
|
|
|
if (!selectedMetricDescriptor) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
2018-12-13 07:16:48 +08:00
|
|
|
const metricsByService = metricDescriptors.filter(m => m.service === selectedMetricDescriptor.service).map(m => ({
|
|
|
|
|
service: m.service,
|
|
|
|
|
value: m.type,
|
|
|
|
|
label: m.displayName,
|
|
|
|
|
description: m.description,
|
|
|
|
|
}));
|
|
|
|
|
return metricsByService;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleServiceChange(service) {
|
|
|
|
|
const { metricDescriptors } = this.state;
|
|
|
|
|
const { templateSrv, metricType } = this.props;
|
|
|
|
|
|
|
|
|
|
const metrics = metricDescriptors.filter(m => m.service === templateSrv.replace(service)).map(m => ({
|
|
|
|
|
service: m.service,
|
|
|
|
|
value: m.type,
|
|
|
|
|
label: m.displayName,
|
|
|
|
|
description: m.description,
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
this.setState({ service, metrics });
|
|
|
|
|
|
|
|
|
|
if (metrics.length > 0 && !metrics.some(m => m.value === templateSrv.replace(metricType))) {
|
|
|
|
|
this.handleMetricTypeChange(metrics[0].value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
handleMetricTypeChange(value) {
|
2018-12-19 22:54:45 +08:00
|
|
|
const metricDescriptor = this.getSelectedMetricDescriptor(value);
|
|
|
|
|
this.setState({ metricDescriptor });
|
|
|
|
|
this.props.onChange(metricDescriptor);
|
2018-12-13 07:16:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getServicesList(metricDescriptors) {
|
|
|
|
|
const services = metricDescriptors.map(m => ({
|
|
|
|
|
value: m.service,
|
|
|
|
|
label: _.startCase(m.serviceShortName),
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
return services.length > 0 ? _.uniqBy(services, s => s.value) : [];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getTemplateVariablesGroup() {
|
|
|
|
|
return {
|
|
|
|
|
label: 'Template Variables',
|
|
|
|
|
options: this.props.templateSrv.variables.map(v => ({
|
|
|
|
|
label: `$${v.name}`,
|
|
|
|
|
value: `$${v.name}`,
|
|
|
|
|
})),
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
2018-12-19 21:29:00 +08:00
|
|
|
const { services, service, metrics } = this.state;
|
|
|
|
|
const { metricType, templateSrv } = this.props;
|
2018-12-13 07:16:48 +08:00
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<React.Fragment>
|
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
|
<div className="gf-form">
|
|
|
|
|
<span className="gf-form-label width-9 query-keyword">Service</span>
|
2019-01-02 22:07:38 +08:00
|
|
|
<MetricSelect
|
2018-12-13 07:16:48 +08:00
|
|
|
onChange={value => this.handleServiceChange(value)}
|
2019-01-02 22:07:38 +08:00
|
|
|
value={service}
|
2018-12-13 07:16:48 +08:00
|
|
|
options={services}
|
2019-01-02 22:07:38 +08:00
|
|
|
isSearchable={false}
|
2018-12-13 07:16:48 +08:00
|
|
|
placeholder="Select Services"
|
|
|
|
|
className="width-15"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="gf-form gf-form--grow">
|
|
|
|
|
<div className="gf-form-label gf-form-label--grow" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="gf-form-inline">
|
|
|
|
|
<div className="gf-form">
|
|
|
|
|
<span className="gf-form-label width-9 query-keyword">Metric</span>
|
2019-01-02 22:07:38 +08:00
|
|
|
<MetricSelect
|
2018-12-13 07:16:48 +08:00
|
|
|
onChange={value => this.handleMetricTypeChange(value)}
|
2019-01-02 22:07:38 +08:00
|
|
|
value={metricType}
|
|
|
|
|
variables={templateSrv.variables}
|
2018-12-19 21:29:00 +08:00
|
|
|
options={metrics}
|
2018-12-13 07:16:48 +08:00
|
|
|
placeholder="Select Metric"
|
|
|
|
|
className="width-15"
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="gf-form gf-form--grow">
|
|
|
|
|
<div className="gf-form-label gf-form-label--grow" />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
2018-12-19 22:54:45 +08:00
|
|
|
{this.props.children(this.state.metricDescriptor)}
|
2018-12-13 07:16:48 +08:00
|
|
|
</React.Fragment>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|