StatPanel: Fixes hanging issue when all values are zero (#29077)

* StatPanel: Fixes hanging issue when all values are zero

* Minor tweak
This commit is contained in:
Torkel Ödegaard 2020-11-13 10:28:03 +01:00 committed by GitHub
parent eba046d3cb
commit 01a4951da0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 16 deletions

View File

@ -88,6 +88,17 @@ describe('Global MinMax', () => {
expect(minmax.max).toEqual(1234);
});
it('find global min max when all values are zero', () => {
const f0 = new ArrayDataFrame<{ title: string; value: number; value2: number | null }>([
{ title: 'AAA', value: 0, value2: 0 },
{ title: 'CCC', value: 0, value2: 0 },
]);
const minmax = findNumericFieldMinMax([f0]);
expect(minmax.min).toEqual(0);
expect(minmax.max).toEqual(0);
});
describe('when value is null', () => {
it('then global min max should be null', () => {
const frame = toDataFrame({
@ -98,8 +109,8 @@ describe('Global MinMax', () => {
});
const { min, max } = findNumericFieldMinMax([frame]);
expect(min).toBe(Number.MIN_VALUE);
expect(max).toBe(Number.MAX_VALUE);
expect(min).toBe(null);
expect(max).toBe(null);
});
});

View File

@ -41,13 +41,13 @@ interface OverrideProps {
}
interface GlobalMinMax {
min: number;
max: number;
min?: number | null;
max?: number | null;
}
export function findNumericFieldMinMax(data: DataFrame[]): GlobalMinMax {
let min = Number.MAX_VALUE;
let max = Number.MIN_VALUE;
let min: number | null = null;
let max: number | null = null;
const reducers = [ReducerID.min, ReducerID.max];
@ -58,25 +58,17 @@ export function findNumericFieldMinMax(data: DataFrame[]): GlobalMinMax {
const statsMin = stats[ReducerID.min];
const statsMax = stats[ReducerID.max];
if (statsMin !== undefined && statsMin !== null && statsMin < min) {
if (min === null || statsMin < min) {
min = statsMin;
}
if (statsMax !== undefined && statsMax !== null && statsMax > max) {
if (max === null || statsMax > max) {
max = statsMax;
}
}
}
}
if (min === Number.MAX_VALUE) {
min = Number.MIN_VALUE;
}
if (max === Number.MIN_VALUE) {
max = Number.MAX_VALUE;
}
return { min, max };
}