TimeSeries: Fix crash when min >= max in config (#54069)

* TimeSeries: fix crash when min === max in config

* stat sparkline, too

* better types
This commit is contained in:
Leon Sorokin 2022-08-23 03:00:42 -05:00 committed by GitHub
parent c29a2c37c1
commit 530dd63ac6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 7 deletions

View File

@ -1694,8 +1694,7 @@ exports[`better eslint`] = {
[0, 0, 0, "Unexpected any. Specify a different type.", "20"]
],
"packages/grafana-ui/src/components/Sparkline/Sparkline.tsx:5381": [
[0, 0, 0, "Do not use any type assertions.", "0"],
[0, 0, 0, "Do not use any type assertions.", "1"]
[0, 0, 0, "Do not use any type assertions.", "0"]
],
"packages/grafana-ui/src/components/StatsPicker/StatsPicker.story.tsx:5381": [
[0, 0, 0, "Unexpected any. Specify a different type.", "0"],

View File

@ -93,7 +93,7 @@ export class Sparkline extends PureComponent<SparklineProps, State> {
}
}
getYRange(field: Field) {
getYRange(field: Field): Range.MinMax {
let { min, max } = this.state.alignedDataFrame.fields[1].state?.range!;
if (min === max) {
@ -103,12 +103,11 @@ export class Sparkline extends PureComponent<SparklineProps, State> {
min = 0;
max! *= 2;
}
return [min, max!];
}
return [
Math.max(min!, field.config.min ?? -Infinity),
Math.min(max!, field.config.max ?? Infinity),
] as Range.MinMax;
return [Math.max(min!, field.config.min ?? -Infinity), Math.min(max!, field.config.max ?? Infinity)];
}
prepareConfig(data: DataFrame) {

View File

@ -109,6 +109,12 @@ export class UPlotScaleBuilder extends PlotConfigBuilder<ScaleProps, Scale> {
minMax[1] = hardMax!;
}
// guard against invalid y ranges
if (minMax[0]! >= minMax[1]!) {
minMax[0] = 0;
minMax[1] = 100;
}
return minMax;
};