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

120 lines
3.1 KiB
TypeScript
Raw Normal View History

2018-12-29 02:45:24 +08:00
import React from 'react';
import _ from 'lodash';
2019-01-11 20:53:04 +08:00
import { TemplateSrv } from 'app/features/templating/template_srv';
import StackdriverDatasource from '../datasource';
2018-12-29 02:45:24 +08:00
import { Metrics } from './Metrics';
import { Filter } from './Filter';
import { AnnotationTarget } from '../types';
2019-01-02 16:36:32 +08:00
import { AnnotationsHelp } from './AnnotationsHelp';
2018-12-29 02:45:24 +08:00
export interface Props {
onQueryChange: (target: AnnotationTarget) => void;
target: AnnotationTarget;
2019-01-11 20:53:04 +08:00
datasource: StackdriverDatasource;
templateSrv: TemplateSrv;
2018-12-29 02:45:24 +08:00
}
interface State extends AnnotationTarget {
[key: string]: any;
}
const DefaultTarget: State = {
defaultProject: 'loading project...',
metricType: '',
filters: [],
metricKind: '',
valueType: '',
refId: 'annotationQuery',
title: '',
text: '',
};
export class AnnotationQueryEditor extends React.Component<Props, State> {
state: State = DefaultTarget;
componentDidMount() {
this.setState({
...this.props.target,
});
}
onMetricTypeChange({ valueType, metricKind, type, unit }) {
2018-12-29 02:45:24 +08:00
const { onQueryChange } = this.props;
this.setState(
{
metricType: type,
unit,
valueType,
metricKind,
},
() => {
onQueryChange(this.state);
}
);
}
onChange(prop, value) {
2018-12-29 02:45:24 +08:00
this.setState({ [prop]: value }, () => {
this.props.onQueryChange(this.state);
});
}
render() {
2019-01-02 16:36:32 +08:00
const { defaultProject, metricType, filters, refId, title, text } = this.state;
2019-01-08 20:52:19 +08:00
const { datasource, templateSrv } = this.props;
2018-12-29 02:45:24 +08:00
return (
2019-01-08 20:00:31 +08:00
<>
2018-12-29 02:45:24 +08:00
<Metrics
defaultProject={defaultProject}
metricType={metricType}
2019-01-08 20:52:19 +08:00
templateSrv={templateSrv}
2018-12-29 02:45:24 +08:00
datasource={datasource}
onChange={value => this.onMetricTypeChange(value)}
2018-12-29 02:45:24 +08:00
>
{metric => (
2019-01-08 20:00:31 +08:00
<>
2018-12-29 02:45:24 +08:00
<Filter
filtersChanged={value => this.onChange('filters', value)}
2018-12-29 02:45:24 +08:00
filters={filters}
refId={refId}
hideGroupBys={true}
2019-01-08 20:52:19 +08:00
templateSrv={templateSrv}
2018-12-29 02:45:24 +08:00
datasource={datasource}
metricType={metric ? metric.type : ''}
/>
2019-01-08 20:00:31 +08:00
</>
2018-12-29 02:45:24 +08:00
)}
</Metrics>
<div className="gf-form gf-form-inline">
<div className="gf-form">
<span className="gf-form-label query-keyword width-9">Title</span>
<input
type="text"
className="gf-form-input width-20"
value={title}
onChange={e => this.onChange('title', e.target.value)}
2018-12-29 02:45:24 +08:00
/>
</div>
<div className="gf-form">
<span className="gf-form-label query-keyword width-9">Text</span>
2019-01-02 16:36:32 +08:00
<input
type="text"
className="gf-form-input width-20"
value={text}
onChange={e => this.onChange('text', e.target.value)}
2019-01-02 16:36:32 +08:00
/>
2018-12-29 02:45:24 +08:00
</div>
<div className="gf-form gf-form--grow">
<div className="gf-form-label gf-form-label--grow" />
</div>
</div>
2019-01-02 16:36:32 +08:00
<AnnotationsHelp />
2019-01-08 20:00:31 +08:00
</>
2018-12-29 02:45:24 +08:00
);
}
}