From 530dd63ac62fac5d5f5a4ece3081db0f9ceb6709 Mon Sep 17 00:00:00 2001 From: Leon Sorokin Date: Tue, 23 Aug 2022 03:00:42 -0500 Subject: [PATCH] TimeSeries: Fix crash when min >= max in config (#54069) * TimeSeries: fix crash when min === max in config * stat sparkline, too * better types --- .betterer.results | 3 +-- .../grafana-ui/src/components/Sparkline/Sparkline.tsx | 9 ++++----- .../src/components/uPlot/config/UPlotScaleBuilder.ts | 6 ++++++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/.betterer.results b/.betterer.results index d3cf00326dd..ce962e677c8 100644 --- a/.betterer.results +++ b/.betterer.results @@ -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"], diff --git a/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx b/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx index 270b103aa9f..ba02f8db302 100644 --- a/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx +++ b/packages/grafana-ui/src/components/Sparkline/Sparkline.tsx @@ -93,7 +93,7 @@ export class Sparkline extends PureComponent { } } - 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 { 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) { diff --git a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts index 24c443f0807..89a1c036881 100644 --- a/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts +++ b/packages/grafana-ui/src/components/uPlot/config/UPlotScaleBuilder.ts @@ -109,6 +109,12 @@ export class UPlotScaleBuilder extends PlotConfigBuilder { minMax[1] = hardMax!; } + // guard against invalid y ranges + if (minMax[0]! >= minMax[1]!) { + minMax[0] = 0; + minMax[1] = 100; + } + return minMax; };