Commit 493bf0c7 by Ryan McKinley Committed by GitHub

Refactor: rename statsCalculator to fieldReducer (#16867)

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