From 945b0aeb8640195c5e2c3dcc8d77ab630cb5d6dc Mon Sep 17 00:00:00 2001 From: Erik Sundell Date: Wed, 19 Dec 2018 17:12:50 +0100 Subject: [PATCH] add alignment component --- .../stackdriver/components/Aggregations.tsx | 16 +--- .../stackdriver/components/Alignments.tsx | 83 +++++++++++++++++++ .../stackdriver/components/QueryEditor.tsx | 33 +++++++- 3 files changed, 116 insertions(+), 16 deletions(-) create mode 100644 public/app/plugins/datasource/stackdriver/components/Alignments.tsx diff --git a/public/app/plugins/datasource/stackdriver/components/Aggregations.tsx b/public/app/plugins/datasource/stackdriver/components/Aggregations.tsx index b025229a95b..7dae4017991 100644 --- a/public/app/plugins/datasource/stackdriver/components/Aggregations.tsx +++ b/public/app/plugins/datasource/stackdriver/components/Aggregations.tsx @@ -13,24 +13,18 @@ export interface Props { }; aggregation: { crossSeriesReducer: string; - alignmentPeriod: string; - perSeriesAligner: string; groupBys: string[]; }; children?: (renderProps: any) => JSX.Element; } interface State { - alignmentPeriods: any[]; - alignOptions: any[]; aggOptions: any[]; displayAdvancedOptions: boolean; } export class Aggregations extends React.Component { state: State = { - alignmentPeriods: [], - alignOptions: [], aggOptions: [], displayAdvancedOptions: false, }; @@ -80,11 +74,7 @@ export class Aggregations extends React.Component { this.props.metricDescriptor.metricKind ); const newValue = aggregations.find(o => o.value !== notValidOptionValue); - this.handleAggregationChange(newValue ? newValue.value : ''); - } - - handleAggregationChange(value) { - this.props.onChange(value); + this.props.onChange(newValue ? newValue.value : ''); } handleToggleDisplayAdvanced() { @@ -95,7 +85,7 @@ export class Aggregations extends React.Component { render() { const { aggOptions, displayAdvancedOptions } = this.state; - const { aggregation, templateSrv } = this.props; + const { aggregation, templateSrv, onChange } = this.props; return ( @@ -103,7 +93,7 @@ export class Aggregations extends React.Component {
this.handleAggregationChange(value)} + onChange={value => onChange(value)} selected={aggregation.crossSeriesReducer} templateVariables={templateSrv.variables} options={aggOptions} diff --git a/public/app/plugins/datasource/stackdriver/components/Alignments.tsx b/public/app/plugins/datasource/stackdriver/components/Alignments.tsx new file mode 100644 index 00000000000..b871984a1b3 --- /dev/null +++ b/public/app/plugins/datasource/stackdriver/components/Alignments.tsx @@ -0,0 +1,83 @@ +import React from 'react'; +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 kbn from 'app/core/utils/kbn'; + +export interface Props { + onChange: (metricDescriptor) => void; + templateSrv: any; + metricDescriptor: { + valueType: string; + metricKind: string; + }; + perSeriesAligner: string; +} + +interface State { + alignOptions: any[]; +} + +export class Alignments extends React.Component { + state: State = { + alignOptions: [], + }; + + constructor(props) { + super(props); + } + + componentDidMount() { + if (this.props.metricDescriptor !== null) { + 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 ( + +
+
+ + onChange(value)} + selected={perSeriesAligner} + templateVariables={templateSrv.variables} + options={alignOptions} + searchable={true} + placeholder="Select Alignment" + className="width-15" + groupName="Alignment Options" + /> +
+
+
+ ); + } +} diff --git a/public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx b/public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx index 474e80e3394..ec5ed594280 100644 --- a/public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx +++ b/public/app/plugins/datasource/stackdriver/components/QueryEditor.tsx @@ -4,6 +4,7 @@ import _ from 'lodash'; import { Metrics } from './Metrics'; import { Filter } from './Filter'; import { Aggregations } from './Aggregations'; +import { Alignments } from './Alignments'; import { Target } from '../types'; export interface Props { @@ -58,7 +59,7 @@ export class QueryEditor extends React.Component { }, }, () => { - this.props.onQueryChange(this.state.target); + // this.props.onQueryChange(this.state.target); this.props.onExecuteQuery(); } ); @@ -84,7 +85,10 @@ export class QueryEditor extends React.Component { { target: { ...this.state.target, - groupBys: value, + aggregation: { + ...this.state.target.aggregation, + groupBys: value, + }, }, }, () => { @@ -108,6 +112,20 @@ export class QueryEditor extends React.Component { }); } + handleAlignmentChange(value) { + const target = { + ...this.state.target, + aggregation: { + ...this.state.target.aggregation, + perSeriesAligner: value, + }, + }; + this.setState({ target }, () => { + this.props.onQueryChange(target); + this.props.onExecuteQuery(); + }); + } + render() { const { target } = this.state; const { defaultProject, metricType, aggregation } = target; @@ -139,7 +157,16 @@ export class QueryEditor extends React.Component { aggregation={aggregation} onChange={value => this.handleAggregationChange(value)} > - {displayAdvancedOptions => displayAdvancedOptions &&

RÖV

} + {displayAdvancedOptions => + displayAdvancedOptions && ( + this.handleAlignmentChange(value)} + /> + ) + } )}