mirror of
https://github.com/grafana/grafana.git
synced 2025-02-20 11:48:34 -06:00
* wip: add slo support * Export DataSourcePlugin * wip: break out metric query editor into its own component * wip: refactor frontend - keep SLO and Metric query in differnt objects * wip - load services and slos * Fix broken test * Add interactive slo expression builder * Change order of dropdowns * Refactoring backend model. slo unit testing in progress * Unit test migration and SLOs * Cleanup SLO editor * Simplify alias by component * Support alias by for slos * Support slos in variable queries * Fix broken last query error * Update Help section to include SLO aliases * streamline datasource resource cache * Break out api specific stuff in datasource to its own file * Move get projects call to frontend * Refactor api caching * Unit test api service * Fix lint go issue * Fix typescript strict errors * Fix test datasource * Use budget fraction selector instead of budget * Reset SLO when service is changed * Handle error in case resource call returned no data * Show real SLI display name * Use unsafe prefix on will mount hook * Store goal in query model since it will be used as soon as graph panel supports adding a threshold * Add comment to describe why componentWillMount is used * Interpolate sloid * Break out SLO aggregation into its own func * Also test group bys for metricquery test * Remove not used type fields * Remove annoying stackdriver prefix from error message * Default view param to FULL * Add part about SLO query builder in docs * Use new images * Fixes after feedback * Add one more group by test * Make stackdriver types internal * Update docs/sources/features/datasources/stackdriver.md Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update docs/sources/features/datasources/stackdriver.md Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Update docs/sources/features/datasources/stackdriver.md Co-Authored-By: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> * Updates after PR feedback * add test for when no alias by defined * fix infinite loop when newVariables feature flag is on onChange being called in componentDidUpdate produces an infinite loop when using the new React template variable implementation. Also fixes a spelling mistake * implements feedback for documentation changes * more doc changes Co-authored-by: Diana Payton <52059945+oddlittlebird@users.noreply.github.com> Co-authored-by: Daniel Lee <dan.limerick@gmail.com>
158 lines
5.1 KiB
TypeScript
158 lines
5.1 KiB
TypeScript
import React, { useEffect, useState } from 'react';
|
|
import _ from 'lodash';
|
|
|
|
import { TemplateSrv } from 'app/features/templating/template_srv';
|
|
import { SelectableValue } from '@grafana/data';
|
|
import StackdriverDatasource from '../datasource';
|
|
import { Segment } from '@grafana/ui';
|
|
import { MetricDescriptor } from '../types';
|
|
|
|
export interface Props {
|
|
onChange: (metricDescriptor: MetricDescriptor) => void;
|
|
templateSrv: TemplateSrv;
|
|
templateVariableOptions: Array<SelectableValue<string>>;
|
|
datasource: StackdriverDatasource;
|
|
projectName: string;
|
|
metricType: string;
|
|
children?: (renderProps: any) => JSX.Element;
|
|
}
|
|
|
|
interface State {
|
|
metricDescriptors: MetricDescriptor[];
|
|
metrics: any[];
|
|
services: any[];
|
|
service: string;
|
|
metric: string;
|
|
metricDescriptor: MetricDescriptor;
|
|
projectName: string;
|
|
}
|
|
|
|
export function Metrics(props: Props) {
|
|
const [state, setState] = useState<State>({
|
|
metricDescriptors: [],
|
|
metrics: [],
|
|
services: [],
|
|
service: '',
|
|
metric: '',
|
|
metricDescriptor: null,
|
|
projectName: null,
|
|
});
|
|
|
|
const { services, service, metrics } = state;
|
|
const { metricType, templateVariableOptions, projectName } = props;
|
|
|
|
const loadMetricDescriptors = async () => {
|
|
if (projectName) {
|
|
const metricDescriptors = await props.datasource.getMetricTypes(props.projectName);
|
|
const services = getServicesList(metricDescriptors);
|
|
const metrics = getMetricsList(metricDescriptors);
|
|
const service = metrics.length > 0 ? metrics[0].service : '';
|
|
const metricDescriptor = getSelectedMetricDescriptor(metricDescriptors, props.metricType);
|
|
setState({ ...state, metricDescriptors, services, metrics, service: service, metricDescriptor });
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
loadMetricDescriptors();
|
|
}, [projectName]);
|
|
|
|
const getSelectedMetricDescriptor = (metricDescriptors: MetricDescriptor[], metricType: string) => {
|
|
return metricDescriptors.find(md => md.type === props.templateSrv.replace(metricType));
|
|
};
|
|
|
|
const getMetricsList = (metricDescriptors: MetricDescriptor[]) => {
|
|
const selectedMetricDescriptor = getSelectedMetricDescriptor(metricDescriptors, props.metricType);
|
|
if (!selectedMetricDescriptor) {
|
|
return [];
|
|
}
|
|
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;
|
|
};
|
|
|
|
const onServiceChange = ({ value: service }: any) => {
|
|
const { metricDescriptors } = state;
|
|
const { metricType, templateSrv } = props;
|
|
|
|
const metrics = metricDescriptors
|
|
.filter((m: MetricDescriptor) => m.service === templateSrv.replace(service))
|
|
.map((m: MetricDescriptor) => ({
|
|
service: m.service,
|
|
value: m.type,
|
|
label: m.displayName,
|
|
description: m.description,
|
|
}));
|
|
|
|
if (metrics.length > 0 && !metrics.some(m => m.value === templateSrv.replace(metricType))) {
|
|
onMetricTypeChange(metrics[0], { service, metrics });
|
|
} else {
|
|
setState({ ...state, service, metrics });
|
|
}
|
|
};
|
|
|
|
const onMetricTypeChange = ({ value }: SelectableValue<string>, extra: any = {}) => {
|
|
const metricDescriptor = getSelectedMetricDescriptor(state.metricDescriptors, value);
|
|
setState({ ...state, metricDescriptor, ...extra });
|
|
props.onChange({ ...metricDescriptor, type: value });
|
|
};
|
|
|
|
const getServicesList = (metricDescriptors: MetricDescriptor[]) => {
|
|
const services = metricDescriptors.map(m => ({
|
|
value: m.service,
|
|
label: _.startCase(m.serviceShortName),
|
|
}));
|
|
|
|
return services.length > 0 ? _.uniqBy(services, s => s.value) : [];
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div className="gf-form-inline">
|
|
<span className="gf-form-label width-9 query-keyword">Service</span>
|
|
<Segment
|
|
onChange={onServiceChange}
|
|
value={[...services, ...templateVariableOptions].find(s => s.value === service)}
|
|
options={[
|
|
{
|
|
label: 'Template Variables',
|
|
options: templateVariableOptions,
|
|
},
|
|
...services,
|
|
]}
|
|
placeholder="Select Services"
|
|
></Segment>
|
|
<div className="gf-form gf-form--grow">
|
|
<div className="gf-form-label gf-form-label--grow" />
|
|
</div>
|
|
</div>
|
|
<div className="gf-form-inline">
|
|
<span className="gf-form-label width-9 query-keyword">Metric</span>
|
|
|
|
<Segment
|
|
className="query-part"
|
|
onChange={onMetricTypeChange}
|
|
value={[...metrics, ...templateVariableOptions].find(s => s.value === metricType)}
|
|
options={[
|
|
{
|
|
label: 'Template Variables',
|
|
options: templateVariableOptions,
|
|
},
|
|
...metrics,
|
|
]}
|
|
placeholder="Select Metric"
|
|
></Segment>
|
|
<div className="gf-form gf-form--grow">
|
|
<div className="gf-form-label gf-form-label--grow" />
|
|
</div>
|
|
</div>
|
|
{props.children(state.metricDescriptor)}
|
|
</>
|
|
);
|
|
}
|