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

95 lines
2.5 KiB
TypeScript
Raw Normal View History

import React from 'react';
import _ from 'lodash';
2019-01-02 22:07:38 +08:00
import { MetricSelect } from 'app/core/components/Select/MetricSelect';
import { getAggregationOptionsByMetric } from '../functions';
import { TemplateSrv } from 'app/features/templating/template_srv';
export interface Props {
onChange: (metricDescriptor) => void;
templateSrv: TemplateSrv;
2018-12-19 22:54:45 +08:00
metricDescriptor: {
valueType: string;
metricKind: string;
};
2018-12-20 18:00:16 +08:00
crossSeriesReducer: string;
groupBys: string[];
2018-12-19 23:08:18 +08:00
children?: (renderProps: any) => JSX.Element;
}
2019-01-03 23:00:42 +08:00
export interface State {
aggOptions: any[];
2018-12-19 23:08:18 +08:00
displayAdvancedOptions: boolean;
}
2018-12-19 21:29:00 +08:00
export class Aggregations extends React.Component<Props, State> {
state: State = {
aggOptions: [],
2018-12-19 23:08:18 +08:00
displayAdvancedOptions: false,
};
componentDidMount() {
2018-12-20 20:27:47 +08:00
this.setAggOptions(this.props);
}
componentWillReceiveProps(nextProps: Props) {
2018-12-20 20:27:47 +08:00
this.setAggOptions(nextProps);
}
2018-12-20 20:27:47 +08:00
setAggOptions({ metricDescriptor }: Props) {
let aggOptions = [];
2018-12-22 06:21:55 +08:00
if (metricDescriptor) {
2019-01-02 22:07:38 +08:00
aggOptions = [
{
label: 'Aggregations',
expanded: true,
options: getAggregationOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(a => ({
...a,
label: a.text,
})),
},
];
}
2018-12-20 20:27:47 +08:00
this.setState({ aggOptions });
}
onToggleDisplayAdvanced = () => {
2018-12-19 23:08:18 +08:00
this.setState(state => ({
displayAdvancedOptions: !state.displayAdvancedOptions,
}));
};
2018-12-19 23:08:18 +08:00
render() {
2018-12-20 20:27:47 +08:00
const { displayAdvancedOptions, aggOptions } = this.state;
2018-12-20 18:00:16 +08:00
const { templateSrv, onChange, crossSeriesReducer } = this.props;
return (
2019-01-08 20:00:31 +08:00
<>
<div className="gf-form-inline">
<div className="gf-form">
<label className="gf-form-label query-keyword width-9">Aggregation</label>
2019-01-02 22:07:38 +08:00
<MetricSelect
onChange={onChange}
2019-01-02 22:07:38 +08:00
value={crossSeriesReducer}
variables={templateSrv.variables}
2018-12-19 21:19:27 +08:00
options={aggOptions}
placeholder="Select Aggregation"
className="width-15"
/>
</div>
<div className="gf-form gf-form--grow">
<label className="gf-form-label gf-form-label--grow">
<a onClick={this.onToggleDisplayAdvanced}>
2019-01-08 20:00:31 +08:00
<>
2019-01-03 23:00:42 +08:00
<i className={`fa fa-caret-${displayAdvancedOptions ? 'down' : 'right'}`} /> Advanced Options
2019-01-08 20:00:31 +08:00
</>
</a>
</label>
</div>
</div>
2018-12-19 23:08:18 +08:00
{this.props.children(this.state.displayAdvancedOptions)}
2019-01-08 20:00:31 +08:00
</>
);
}
}