mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Reducers: Update percentile logic - move percentiles to after standard reducers (#86004)
* baldm0mma/pth_fix2/ move percentiles to after standard reducers * baldm0mma/pth_fix2/ update ordinal suffix --------- Co-authored-by: nmarrs <nathanielmarrs@gmail.com>
This commit is contained in:
parent
0a12ad0084
commit
f9af4675ad
@ -252,17 +252,19 @@ describe('Stats Calculators', () => {
|
|||||||
expect(reduce(someNulls, ReducerID.count)).toEqual(4);
|
expect(reduce(someNulls, ReducerID.count)).toEqual(4);
|
||||||
});
|
});
|
||||||
|
|
||||||
for (let i = 1; i < 100; i++) {
|
it('can reduce to percentiles', () => {
|
||||||
it(`can reduce the ${i}th percentile`, () => {
|
// This `Array.from` will build an array of elements from 1 to 99
|
||||||
|
const percentiles = [...Array.from({ length: 99 }, (_, i) => i + 1)];
|
||||||
|
percentiles.forEach((percentile) => {
|
||||||
const preciseStats = reduceField({
|
const preciseStats = reduceField({
|
||||||
field: createField(
|
field: createField(
|
||||||
'x',
|
'x',
|
||||||
Array.from({ length: 101 }, (_, index) => index)
|
Array.from({ length: 101 }, (_, index) => index)
|
||||||
),
|
),
|
||||||
reducers: [(ReducerID as Record<string, ReducerID>)[`p${i}`]],
|
reducers: [(ReducerID as Record<string, ReducerID>)[`p${percentile}`]],
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(preciseStats[`p${i}`]).toEqual(i);
|
expect(preciseStats[`p${percentile}`]).toEqual(percentile);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
@ -402,17 +402,22 @@ export const fieldReducers = new Registry<FieldReducerInfo>(() => [
|
|||||||
}),
|
}),
|
||||||
preservesUnits: false,
|
preservesUnits: false,
|
||||||
},
|
},
|
||||||
|
...buildPercentileReducers(),
|
||||||
]);
|
]);
|
||||||
|
|
||||||
for (let i = 1; i < 100; i++) {
|
// This `Array.from` will build an array of elements from 1 to 99
|
||||||
const percentile = i / 100;
|
const buildPercentileReducers = (percentiles = [...Array.from({ length: 99 }, (_, i) => i + 1)]) => {
|
||||||
const id = `p${i}` as ReducerID;
|
const percentileReducers: FieldReducerInfo[] = [];
|
||||||
const nth = (n: number) =>
|
const nth = (n: number) =>
|
||||||
n > 3 && n < 21 ? 'th' : n % 10 === 1 ? 'st' : n % 10 === 2 ? 'nd' : n % 10 === 3 ? 'rd' : 'th';
|
n > 3 && n < 21 ? 'th' : n % 10 === 1 ? 'st' : n % 10 === 2 ? 'nd' : n % 10 === 3 ? 'rd' : 'th';
|
||||||
const name = `${i}${nth(i)} percentile`;
|
|
||||||
const description = `${i}${nth(i)} percentile value`;
|
|
||||||
|
|
||||||
fieldReducers.register({
|
percentiles.forEach((p) => {
|
||||||
|
const percentile = p / 100;
|
||||||
|
const id = `p${p}` as ReducerID;
|
||||||
|
const name = `${p}${nth(p)} %`;
|
||||||
|
const description = `${p}${nth(p)} percentile value`;
|
||||||
|
|
||||||
|
percentileReducers.push({
|
||||||
id: id,
|
id: id,
|
||||||
name: name,
|
name: name,
|
||||||
description: description,
|
description: description,
|
||||||
@ -422,7 +427,9 @@ for (let i = 1; i < 100; i++) {
|
|||||||
},
|
},
|
||||||
preservesUnits: true,
|
preservesUnits: true,
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
return percentileReducers;
|
||||||
|
};
|
||||||
|
|
||||||
// Used for test cases
|
// Used for test cases
|
||||||
export const defaultCalcs: FieldCalcs = {
|
export const defaultCalcs: FieldCalcs = {
|
||||||
|
Loading…
Reference in New Issue
Block a user