combine mode with avg value

This commit is contained in:
Peter Holmberg 2019-02-06 15:38:51 +01:00
parent 9d3d1bc669
commit 71cfcd58ba
5 changed files with 52 additions and 16 deletions

View File

@ -34,7 +34,7 @@ export class Switch extends PureComponent<Props, State> {
const switchClassName = `gf-form-switch ${switchClass} ${transparent ? 'gf-form-switch--transparent' : ''}`; const switchClassName = `gf-form-switch ${switchClass} ${transparent ? 'gf-form-switch--transparent' : ''}`;
return ( return (
<label htmlFor={labelId} className={`gf-form gf-form-switch-container ${className}`}> <label htmlFor={labelId} className={`gf-form gf-form-switch-container ${className ? className : ''}`}>
{label && <div className={labelClassName}>{label}</div>} {label && <div className={labelClassName}>{label}</div>}
<div className={switchClassName}> <div className={switchClassName}>
<input id={labelId} type="checkbox" checked={checked} onChange={this.internalOnChange} /> <input id={labelId} type="checkbox" checked={checked} onChange={this.internalOnChange} />

View File

@ -1,9 +1,24 @@
import React, { PureComponent } from 'react'; import React, { PureComponent } from 'react';
import { FormField, PanelOptionsProps, PanelOptionsGroup, Switch } from '@grafana/ui'; import {
FormField,
FormLabel,
PanelOptionsProps,
PanelOptionsGroup,
Select,
SelectOptionItem,
Switch,
} from '@grafana/ui';
import { GaugeOptions } from './types'; import { GaugeOptions } from './types';
export default class GaugeOptionsEditor extends PureComponent<PanelOptionsProps<GaugeOptions>> { export default class GaugeOptionsEditor extends PureComponent<PanelOptionsProps<GaugeOptions>> {
multiSeriesOptions: SelectOptionItem[] = [
{ value: 'repeat', label: 'Repeat' },
{ value: 'combine', label: 'Combine' },
];
labelWidth = 9;
onToggleThresholdLabels = () => onToggleThresholdLabels = () =>
this.props.onChange({ ...this.props.options, showThresholdLabels: !this.props.options.showThresholdLabels }); this.props.onChange({ ...this.props.options, showThresholdLabels: !this.props.options.showThresholdLabels });
@ -14,26 +29,38 @@ export default class GaugeOptionsEditor extends PureComponent<PanelOptionsProps<
onMaxValueChange = ({ target }) => this.props.onChange({ ...this.props.options, maxValue: target.value }); onMaxValueChange = ({ target }) => this.props.onChange({ ...this.props.options, maxValue: target.value });
onMultiSeriesModeChange = ({ value }) => this.props.onChange({ ...this.props.options, multiSeriesMode: value });
render() { render() {
const { options } = this.props; const { options } = this.props;
const { maxValue, minValue, showThresholdLabels, showThresholdMarkers } = options; const { maxValue, minValue, multiSeriesMode, showThresholdLabels, showThresholdMarkers } = options;
return ( return (
<PanelOptionsGroup title="Gauge"> <PanelOptionsGroup title="Gauge">
<FormField label="Min value" labelWidth={8} onChange={this.onMinValueChange} value={minValue} /> <FormField label="Min value" labelWidth={this.labelWidth} onChange={this.onMinValueChange} value={minValue} />
<FormField label="Max value" labelWidth={8} onChange={this.onMaxValueChange} value={maxValue} /> <FormField label="Max value" labelWidth={this.labelWidth} onChange={this.onMaxValueChange} value={maxValue} />
<Switch <Switch
label="Show labels" label="Show labels"
labelClass="width-8" labelClass={`width-${this.labelWidth}`}
checked={showThresholdLabels} checked={showThresholdLabels}
onChange={this.onToggleThresholdLabels} onChange={this.onToggleThresholdLabels}
/> />
<Switch <Switch
label="Show markers" label="Show markers"
labelClass="width-8" labelClass={`width-${this.labelWidth}`}
checked={showThresholdMarkers} checked={showThresholdMarkers}
onChange={this.onToggleThresholdMarkers} onChange={this.onToggleThresholdMarkers}
/> />
<div className="gf-form">
<FormLabel width={this.labelWidth}>Multi series mode</FormLabel>
<Select
defaultValue={this.multiSeriesOptions[0]}
onChange={this.onMultiSeriesModeChange}
options={this.multiSeriesOptions}
value={this.multiSeriesOptions.find(option => option.value === multiSeriesMode)}
width={12}
/>
</div>
</PanelOptionsGroup> </PanelOptionsGroup>
); );
} }

View File

@ -50,7 +50,15 @@ export class GaugePanel extends PureComponent<Props> {
renderSingleGauge(timeSeries, theme) { renderSingleGauge(timeSeries, theme) {
const { options, width } = this.props; const { options, width } = this.props;
const timeSeriesValue = timeSeries[0].stats[options.stat];
let timeSeriesValue = timeSeries.reduce((accumulator, currentValue) => {
return { stats: { [options.stat]: accumulator.stats[options.stat] + currentValue.stats[options.stat] } };
});
if (options.stat === 'avg') {
timeSeriesValue = timeSeriesValue.stats[options.stat] / timeSeries.length;
}
return <div className="singlestat-panel">{this.renderGauge(timeSeriesValue, width, theme)}</div>; return <div className="singlestat-panel">{this.renderGauge(timeSeriesValue, width, theme)}</div>;
} }
@ -62,7 +70,7 @@ export class GaugePanel extends PureComponent<Props> {
} }
renderPanel(theme) { renderPanel(theme) {
const { panelData } = this.props; const { panelData, options } = this.props;
if (panelData.timeSeries) { if (panelData.timeSeries) {
const timeSeries = processTimeSeries({ const timeSeries = processTimeSeries({
@ -71,7 +79,10 @@ export class GaugePanel extends PureComponent<Props> {
}); });
if (timeSeries.length > 1) { if (timeSeries.length > 1) {
return this.renderMultipleGauge(timeSeries, theme); if (options.multiSeriesMode === 'repeat') {
return this.renderMultipleGauge(timeSeries, theme);
}
return this.renderSingleGauge(timeSeries, theme);
} else if (timeSeries.length > 0) { } else if (timeSeries.length > 0) {
return this.renderSingleGauge(timeSeries, theme); return this.renderSingleGauge(timeSeries, theme);
} else { } else {

View File

@ -26,6 +26,7 @@ export const defaultProps = {
unit: 'none', unit: 'none',
valueMappings: [], valueMappings: [],
thresholds: [], thresholds: [],
multiSeriesMode: 'repeat',
}, },
}; };
@ -48,16 +49,12 @@ export default class GaugePanelOptions extends PureComponent<PanelOptionsProps<G
const { onChange, options } = this.props; const { onChange, options } = this.props;
return ( return (
<ThemeProvider> <ThemeProvider>
{(theme) => ( {theme => (
<> <>
<PanelOptionsGrid> <PanelOptionsGrid>
<ValueOptions onChange={onChange} options={options} /> <ValueOptions onChange={onChange} options={options} />
<GaugeOptionsEditor onChange={onChange} options={options} /> <GaugeOptionsEditor onChange={onChange} options={options} />
<ThresholdsEditor <ThresholdsEditor onChange={this.onThresholdsChanged} thresholds={options.thresholds} theme={theme} />
onChange={this.onThresholdsChanged}
thresholds={options.thresholds}
theme={theme}
/>
</PanelOptionsGrid> </PanelOptionsGrid>
<ValueMappingsEditor onChange={this.onValueMappingsChanged} valueMappings={options.valueMappings} /> <ValueMappingsEditor onChange={this.onValueMappingsChanged} valueMappings={options.valueMappings} />

View File

@ -12,4 +12,5 @@ export interface GaugeOptions {
suffix: string; suffix: string;
thresholds: Threshold[]; thresholds: Threshold[];
unit: string; unit: string;
multiSeriesMode: string;
} }