move alignment population code to parent component. make alignment a stateless component instead.

This commit is contained in:
Erik Sundell
2018-12-20 11:26:05 +01:00
parent ec68c65660
commit 1452bc2e8a
3 changed files with 66 additions and 82 deletions
@@ -1,16 +1,12 @@
import React from 'react'; import React, { SFC } from 'react';
import _ from 'lodash'; import _ from 'lodash';
// import { OptionPicker } from './OptionPicker';
// import { alignmentPeriods } from '../constants';
// import { getAlignmentOptionsByMetric, getAggregationOptionsByMetric } from '../functions';
import { getAlignmentOptionsByMetric } from '../functions';
import { StackdriverPicker } from './StackdriverPicker'; import { StackdriverPicker } from './StackdriverPicker';
// import kbn from 'app/core/utils/kbn';
export interface Props { export interface Props {
onChange: (metricDescriptor) => void; onChange: (metricDescriptor) => void;
templateSrv: any; templateSrv: any;
alignOptions: any[];
metricDescriptor: { metricDescriptor: {
valueType: string; valueType: string;
metricKind: string; metricKind: string;
@@ -18,66 +14,24 @@ export interface Props {
perSeriesAligner: string; perSeriesAligner: string;
} }
interface State { export const Alignments: SFC<Props> = ({ perSeriesAligner, templateSrv, onChange, alignOptions }) => {
alignOptions: any[]; return (
} <React.Fragment>
<div className="gf-form-group">
export class Alignments extends React.Component<Props, State> { <div className="gf-form offset-width-9">
state: State = { <label className="gf-form-label query-keyword width-15">Aligner</label>
alignOptions: [], <StackdriverPicker
}; onChange={value => onChange(value)}
selected={perSeriesAligner}
constructor(props) { templateVariables={templateSrv.variables}
super(props); options={alignOptions}
} searchable={true}
placeholder="Select Alignment"
componentDidMount() { className="width-15"
if (this.props.metricDescriptor !== null) { groupName="Alignment Options"
this.setAlignOptions(this.props); />
}
}
componentWillReceiveProps(nextProps: Props) {
if (nextProps.metricDescriptor !== null) {
this.setAlignOptions(nextProps);
}
}
setAlignOptions({ metricDescriptor, perSeriesAligner, templateSrv, onChange }) {
const alignOptions = getAlignmentOptionsByMetric(metricDescriptor.valueType, metricDescriptor.metricKind).map(
option => ({
...option,
label: option.text,
})
);
if (!alignOptions.some(o => o.value === templateSrv.replace(perSeriesAligner))) {
onChange(alignOptions.length > 0 ? alignOptions[0].value : '');
}
this.setState({ alignOptions });
}
render() {
const { alignOptions } = this.state;
const { perSeriesAligner, templateSrv, onChange } = this.props;
return (
<React.Fragment>
<div className="gf-form-group">
<div className="gf-form offset-width-9">
<label className="gf-form-label query-keyword width-15">Aligner</label>
<StackdriverPicker
onChange={value => onChange(value)}
selected={perSeriesAligner}
templateVariables={templateSrv.variables}
options={alignOptions}
searchable={true}
placeholder="Select Alignment"
className="width-15"
groupName="Alignment Options"
/>
</div>
</div> </div>
</React.Fragment> </div>
); </React.Fragment>
} );
} };
@@ -6,6 +6,7 @@ import { Filter } from './Filter';
import { Aggregations } from './Aggregations'; import { Aggregations } from './Aggregations';
import { Alignments } from './Alignments'; import { Alignments } from './Alignments';
import { Target } from '../types'; import { Target } from '../types';
import { getAlignmentPickerData } from '../functions';
export interface Props { export interface Props {
onQueryChange: (target: Target) => void; onQueryChange: (target: Target) => void;
@@ -16,7 +17,11 @@ export interface Props {
uiSegmentSrv: any; uiSegmentSrv: any;
} }
const DefaultTarget: Target = { interface State extends Target {
alignOptions: any[];
}
const DefaultTarget: State = {
defaultProject: 'loading project...', defaultProject: 'loading project...',
metricType: '', metricType: '',
metricKind: '', metricKind: '',
@@ -30,18 +35,30 @@ const DefaultTarget: Target = {
groupBys: [], groupBys: [],
filters: [], filters: [],
aliasBy: '', aliasBy: '',
alignOptions: [],
}; };
export class QueryEditor extends React.Component<Props, Target> { export class QueryEditor extends React.Component<Props, State> {
state: Target = DefaultTarget; state: State = DefaultTarget;
componentDidMount() { componentDidMount() {
this.setState(this.props.target); const { perSeriesAligner, alignOptions } = getAlignmentPickerData(this.props.target, this.props.templateSrv);
this.setState({
...this.props.target,
alignOptions,
perSeriesAligner,
});
} }
handleMetricTypeChange({ valueType, metricKind, type, unit }) { handleMetricTypeChange({ valueType, metricKind, type, unit }) {
const { perSeriesAligner, alignOptions } = getAlignmentPickerData(
{ valueType, metricKind, perSeriesAligner: this.state.perSeriesAligner },
this.props.templateSrv
);
this.setState( this.setState(
{ {
alignOptions,
perSeriesAligner,
metricType: type, metricType: type,
unit, unit,
valueType, valueType,
@@ -97,7 +114,7 @@ export class QueryEditor extends React.Component<Props, Target> {
} }
render() { render() {
const { defaultProject, metricType, crossSeriesReducer, groupBys, perSeriesAligner } = this.state; const { defaultProject, metricType, crossSeriesReducer, groupBys, perSeriesAligner, alignOptions } = this.state;
const { templateSrv, datasource, uiSegmentSrv } = this.props; const { templateSrv, datasource, uiSegmentSrv } = this.props;
return ( return (
@@ -127,15 +144,17 @@ export class QueryEditor extends React.Component<Props, Target> {
groupBys={groupBys} groupBys={groupBys}
onChange={value => this.handleAggregationChange(value)} onChange={value => this.handleAggregationChange(value)}
> >
{displayAdvancedOptions => ( {displayAdvancedOptions =>
<Alignments displayAdvancedOptions && (
display={displayAdvancedOptions} <Alignments
metricDescriptor={metric} alignOptions={alignOptions}
templateSrv={templateSrv} metricDescriptor={metric}
perSeriesAligner={perSeriesAligner} templateSrv={templateSrv}
onChange={value => this.handleAlignmentChange(value)} perSeriesAligner={perSeriesAligner}
/> onChange={value => this.handleAlignmentChange(value)}
)} />
)
}
</Aggregations> </Aggregations>
</React.Fragment> </React.Fragment>
)} )}
@@ -46,3 +46,14 @@ export const getLabelKeys = async (datasource, selectedMetricType) => {
: []; : [];
return labelKeys; return labelKeys;
}; };
export const getAlignmentPickerData = ({ valueType, metricKind, perSeriesAligner }, templateSrv) => {
const alignOptions = getAlignmentOptionsByMetric(valueType, metricKind).map(option => ({
...option,
label: option.text,
}));
if (!alignOptions.some(o => o.value === templateSrv.replace(perSeriesAligner))) {
perSeriesAligner = alignOptions.length > 0 ? alignOptions[0].value : '';
}
return { alignOptions, perSeriesAligner };
};