Commit 493bf0c7 by Ryan McKinley Committed by GitHub

Refactor: rename statsCalculator to fieldReducer (#16867)

parent bb64a067
...@@ -8,7 +8,7 @@ import { ...@@ -8,7 +8,7 @@ import {
PanelOptionsGroup, PanelOptionsGroup,
StatsPicker, StatsPicker,
UnitPicker, UnitPicker,
StatID, ReducerID,
SelectOptionItem, SelectOptionItem,
} from '@grafana/ui'; } from '@grafana/ui';
...@@ -27,7 +27,7 @@ export class SingleStatValueEditor extends PureComponent<Props> { ...@@ -27,7 +27,7 @@ export class SingleStatValueEditor extends PureComponent<Props> {
onUnitChange = (unit: SelectOptionItem<string>) => this.props.onChange({ ...this.props.value, unit: unit.value }); onUnitChange = (unit: SelectOptionItem<string>) => this.props.onChange({ ...this.props.value, unit: unit.value });
onStatsChange = (stats: string[]) => { onStatsChange = (stats: string[]) => {
const stat = stats[0] || StatID.mean; const stat = stats[0] || ReducerID.mean;
this.props.onChange({ ...this.props.value, stat }); this.props.onChange({ ...this.props.value, stat });
}; };
...@@ -65,7 +65,7 @@ export class SingleStatValueEditor extends PureComponent<Props> { ...@@ -65,7 +65,7 @@ export class SingleStatValueEditor extends PureComponent<Props> {
<StatsPicker <StatsPicker
width={12} width={12}
placeholder="Choose Stat" placeholder="Choose Stat"
defaultStat={StatID.mean} defaultStat={ReducerID.mean}
allowMultiple={false} allowMultiple={false}
stats={[stat]} stats={[stat]}
onChange={this.onStatsChange} onChange={this.onStatsChange}
......
...@@ -11,7 +11,7 @@ import { ...@@ -11,7 +11,7 @@ import {
SeriesData, SeriesData,
InterpolateFunction, InterpolateFunction,
} from '../../types'; } from '../../types';
import { getStatsCalculators, calculateStats } from '../../utils/statsCalculator'; import { getFieldReducers, reduceField } from '../../utils/fieldReducer';
import { getDisplayProcessor } from '../../utils/displayValue'; import { getDisplayProcessor } from '../../utils/displayValue';
export { SingleStatValueEditor } from './SingleStatValueEditor'; export { SingleStatValueEditor } from './SingleStatValueEditor';
...@@ -66,10 +66,10 @@ export const getSingleStatDisplayValues = (options: GetSingleStatDisplayValueOpt ...@@ -66,10 +66,10 @@ export const getSingleStatDisplayValues = (options: GetSingleStatDisplayValueOpt
// Show all fields that are not 'time' // Show all fields that are not 'time'
if (column.type === FieldType.number) { if (column.type === FieldType.number) {
const stats = calculateStats({ const stats = reduceField({
series, series,
fieldIndex: i, fieldIndex: i,
stats: [stat], // The stats to calculate reducers: [stat], // The stats to calculate
nullValueMode: NullValueMode.Null, nullValueMode: NullValueMode.Null,
}); });
...@@ -124,7 +124,7 @@ export const sharedSingleStatMigrationCheck = (panel: PanelModel<SingleStatBaseO ...@@ -124,7 +124,7 @@ export const sharedSingleStatMigrationCheck = (panel: PanelModel<SingleStatBaseO
// avg -> mean, current -> last, total -> sum // avg -> mean, current -> last, total -> sum
const { valueOptions } = options; const { valueOptions } = options;
if (valueOptions && valueOptions.stat) { if (valueOptions && valueOptions.stat) {
valueOptions.stat = getStatsCalculators([valueOptions.stat]).map(s => s.id)[0]; valueOptions.stat = getFieldReducers([valueOptions.stat]).map(s => s.id)[0];
} }
} }
return options; return options;
......
...@@ -5,7 +5,7 @@ import difference from 'lodash/difference'; ...@@ -5,7 +5,7 @@ import difference from 'lodash/difference';
import { Select } from '../index'; import { Select } from '../index';
import { getStatsCalculators } from '../../utils/statsCalculator'; import { getFieldReducers } from '../../utils/fieldReducer';
import { SelectOptionItem } from '../Select/Select'; import { SelectOptionItem } from '../Select/Select';
interface Props { interface Props {
...@@ -34,7 +34,7 @@ export class StatsPicker extends PureComponent<Props> { ...@@ -34,7 +34,7 @@ export class StatsPicker extends PureComponent<Props> {
checkInput = () => { checkInput = () => {
const { stats, allowMultiple, defaultStat, onChange } = this.props; const { stats, allowMultiple, defaultStat, onChange } = this.props;
const current = getStatsCalculators(stats); const current = getFieldReducers(stats);
if (current.length !== stats.length) { if (current.length !== stats.length) {
const found = current.map(v => v.id); const found = current.map(v => v.id);
const notFound = difference(stats, found); const notFound = difference(stats, found);
...@@ -65,7 +65,7 @@ export class StatsPicker extends PureComponent<Props> { ...@@ -65,7 +65,7 @@ export class StatsPicker extends PureComponent<Props> {
render() { render() {
const { width, stats, allowMultiple, defaultStat, placeholder } = this.props; const { width, stats, allowMultiple, defaultStat, placeholder } = this.props;
const options = getStatsCalculators().map(s => { const options = getFieldReducers().map(s => {
return { return {
value: s.id, value: s.id,
label: s.name, label: s.name,
......
import { getStatsCalculators, StatID, calculateStats } from './statsCalculator'; import { getFieldReducers, ReducerID, reduceField } from './fieldReducer';
import _ from 'lodash'; import _ from 'lodash';
...@@ -10,28 +10,28 @@ describe('Stats Calculators', () => { ...@@ -10,28 +10,28 @@ describe('Stats Calculators', () => {
it('should load all standard stats', () => { it('should load all standard stats', () => {
const names = [ const names = [
StatID.sum, ReducerID.sum,
StatID.max, ReducerID.max,
StatID.min, ReducerID.min,
StatID.logmin, ReducerID.logmin,
StatID.mean, ReducerID.mean,
StatID.last, ReducerID.last,
StatID.first, ReducerID.first,
StatID.count, ReducerID.count,
StatID.range, ReducerID.range,
StatID.diff, ReducerID.diff,
StatID.step, ReducerID.step,
StatID.delta, ReducerID.delta,
// StatID.allIsZero, // ReducerID.allIsZero,
// StatID.allIsNull, // ReducerID.allIsNull,
]; ];
const stats = getStatsCalculators(names); const stats = getFieldReducers(names);
expect(stats.length).toBe(names.length); expect(stats.length).toBe(names.length);
}); });
it('should fail to load unknown stats', () => { it('should fail to load unknown stats', () => {
const names = ['not a stat', StatID.max, StatID.min, 'also not a stat']; const names = ['not a stat', ReducerID.max, ReducerID.min, 'also not a stat'];
const stats = getStatsCalculators(names); const stats = getFieldReducers(names);
expect(stats.length).toBe(2); expect(stats.length).toBe(2);
const found = stats.map(v => v.id); const found = stats.map(v => v.id);
...@@ -42,10 +42,10 @@ describe('Stats Calculators', () => { ...@@ -42,10 +42,10 @@ describe('Stats Calculators', () => {
}); });
it('should calculate basic stats', () => { it('should calculate basic stats', () => {
const stats = calculateStats({ const stats = reduceField({
series: basicTable, series: basicTable,
fieldIndex: 0, fieldIndex: 0,
stats: ['first', 'last', 'mean'], reducers: ['first', 'last', 'mean'],
}); });
// First // First
...@@ -59,10 +59,10 @@ describe('Stats Calculators', () => { ...@@ -59,10 +59,10 @@ describe('Stats Calculators', () => {
}); });
it('should support a single stat also', () => { it('should support a single stat also', () => {
const stats = calculateStats({ const stats = reduceField({
series: basicTable, series: basicTable,
fieldIndex: 0, fieldIndex: 0,
stats: ['first'], reducers: ['first'],
}); });
// Should do the simple version that just looks up value // Should do the simple version that just looks up value
...@@ -71,10 +71,10 @@ describe('Stats Calculators', () => { ...@@ -71,10 +71,10 @@ describe('Stats Calculators', () => {
}); });
it('should get non standard stats', () => { it('should get non standard stats', () => {
const stats = calculateStats({ const stats = reduceField({
series: basicTable, series: basicTable,
fieldIndex: 0, fieldIndex: 0,
stats: [StatID.distinctCount, StatID.changeCount], reducers: [ReducerID.distinctCount, ReducerID.changeCount],
}); });
expect(stats.distinctCount).toEqual(2); expect(stats.distinctCount).toEqual(2);
...@@ -82,10 +82,10 @@ describe('Stats Calculators', () => { ...@@ -82,10 +82,10 @@ describe('Stats Calculators', () => {
}); });
it('should calculate step', () => { it('should calculate step', () => {
const stats = calculateStats({ const stats = reduceField({
series: { fields: [{ name: 'A' }], rows: [[100], [200], [300], [400]] }, series: { fields: [{ name: 'A' }], rows: [[100], [200], [300], [400]] },
fieldIndex: 0, fieldIndex: 0,
stats: [StatID.step, StatID.delta], reducers: [ReducerID.step, ReducerID.delta],
}); });
expect(stats.step).toEqual(100); expect(stats.step).toEqual(100);
......
...@@ -5,7 +5,7 @@ export * from './namedColorsPalette'; ...@@ -5,7 +5,7 @@ export * from './namedColorsPalette';
export * from './thresholds'; export * from './thresholds';
export * from './string'; export * from './string';
export * from './csv'; export * from './csv';
export * from './statsCalculator'; export * from './fieldReducer';
export * from './displayValue'; export * from './displayValue';
export * from './deprecationWarning'; export * from './deprecationWarning';
export * from './logs'; export * from './logs';
......
import { VizOrientation, SelectOptionItem, StatID, SingleStatBaseOptions } from '@grafana/ui'; import { VizOrientation, SelectOptionItem, ReducerID, SingleStatBaseOptions } from '@grafana/ui';
export interface BarGaugeOptions extends SingleStatBaseOptions { export interface BarGaugeOptions extends SingleStatBaseOptions {
minValue: number; minValue: number;
...@@ -24,7 +24,7 @@ export const defaults: BarGaugeOptions = { ...@@ -24,7 +24,7 @@ export const defaults: BarGaugeOptions = {
orientation: VizOrientation.Horizontal, orientation: VizOrientation.Horizontal,
valueOptions: { valueOptions: {
unit: 'none', unit: 'none',
stat: StatID.mean, stat: ReducerID.mean,
prefix: '', prefix: '',
suffix: '', suffix: '',
decimals: null, decimals: null,
......
import { VizOrientation, StatID, SingleStatBaseOptions } from '@grafana/ui'; import { VizOrientation, ReducerID, SingleStatBaseOptions } from '@grafana/ui';
export interface GaugeOptions extends SingleStatBaseOptions { export interface GaugeOptions extends SingleStatBaseOptions {
maxValue: number; maxValue: number;
...@@ -16,7 +16,7 @@ export const defaults: GaugeOptions = { ...@@ -16,7 +16,7 @@ export const defaults: GaugeOptions = {
prefix: '', prefix: '',
suffix: '', suffix: '',
decimals: null, decimals: null,
stat: StatID.mean, stat: ReducerID.mean,
unit: 'none', unit: 'none',
}, },
valueMappings: [], valueMappings: [],
......
import { import {
GraphSeriesXY, GraphSeriesXY,
NullValueMode, NullValueMode,
calculateStats, reduceField,
colors, colors,
getFlotPairs, getFlotPairs,
getColorFromHexRgbOrName, getColorFromHexRgbOrName,
...@@ -45,8 +45,12 @@ export const getGraphSeriesModel = ( ...@@ -45,8 +45,12 @@ export const getGraphSeriesModel = (
}); });
if (points.length > 0) { if (points.length > 0) {
const seriesStats = calculateStats({ series, stats: legendOptions.stats, fieldIndex: field.index }); const seriesStats = reduceField({
let statsDisplayValues; series,
reducers: legendOptions.stats,
fieldIndex: field.index,
});
let statsDisplayValues: DisplayValue[];
if (legendOptions.stats) { if (legendOptions.stats) {
statsDisplayValues = legendOptions.stats.map<DisplayValue>(stat => { statsDisplayValues = legendOptions.stats.map<DisplayValue>(stat => {
......
import { PieChartType, StatID, VizOrientation, SingleStatBaseOptions } from '@grafana/ui'; import { PieChartType, ReducerID, VizOrientation, SingleStatBaseOptions } from '@grafana/ui';
export interface PieChartOptions extends SingleStatBaseOptions { export interface PieChartOptions extends SingleStatBaseOptions {
pieType: PieChartType; pieType: PieChartType;
...@@ -10,7 +10,7 @@ export const defaults: PieChartOptions = { ...@@ -10,7 +10,7 @@ export const defaults: PieChartOptions = {
strokeWidth: 1, strokeWidth: 1,
valueOptions: { valueOptions: {
unit: 'short', unit: 'short',
stat: StatID.last, stat: ReducerID.last,
suffix: '', suffix: '',
prefix: '', prefix: '',
}, },
......
...@@ -16,7 +16,7 @@ import { ...@@ -16,7 +16,7 @@ import {
PanelProps, PanelProps,
getDisplayProcessor, getDisplayProcessor,
NullValueMode, NullValueMode,
calculateStats, reduceField,
FieldCache, FieldCache,
FieldType, FieldType,
} from '@grafana/ui'; } from '@grafana/ui';
...@@ -57,10 +57,10 @@ export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions> ...@@ -57,10 +57,10 @@ export class SingleStatPanel extends PureComponent<PanelProps<SingleStatOptions>
for (let i = 0; i < numberFields.length; i++) { for (let i = 0; i < numberFields.length; i++) {
const field = numberFields[i]; const field = numberFields[i];
const stats = calculateStats({ const stats = reduceField({
series, series,
fieldIndex: field.index, fieldIndex: field.index,
stats: [stat], // The stats to calculate reducers: [stat], // The stats to calculate
nullValueMode: NullValueMode.Null, nullValueMode: NullValueMode.Null,
}); });
......
import { VizOrientation, StatID, SingleStatBaseOptions } from '@grafana/ui'; import { VizOrientation, ReducerID, SingleStatBaseOptions } from '@grafana/ui';
export interface SparklineOptions { export interface SparklineOptions {
show: boolean; show: boolean;
...@@ -33,7 +33,7 @@ export const defaults: SingleStatOptions = { ...@@ -33,7 +33,7 @@ export const defaults: SingleStatOptions = {
prefix: '', prefix: '',
suffix: '', suffix: '',
decimals: null, decimals: null,
stat: StatID.mean, stat: ReducerID.mean,
unit: 'none', unit: 'none',
}, },
valueMappings: [], valueMappings: [],
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment