From 138ef7729c2eca07031b732b4b2694fe42f36c97 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Muller?= Date: Mon, 12 Aug 2024 10:58:42 +0200 Subject: [PATCH] Make sure to only store at most `CHART_MAX_POINTS` points for each chart --- .../demo/shared/ui/components/Charts.kt | 26 +++++++------------ .../player/metrics/StatsForNerdsViewModel.kt | 22 ++++++++++------ 2 files changed, 23 insertions(+), 25 deletions(-) diff --git a/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/components/Charts.kt b/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/components/Charts.kt index fa85084dd..9d5212801 100644 --- a/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/components/Charts.kt +++ b/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/components/Charts.kt @@ -78,21 +78,20 @@ fun LineChart( Chart( data = data, modifier = modifier, - stretchChartToPointsCount = stretchChartToPointsCount, scaleItemsCount = scaleItemsCount, scaleTextFormatter = scaleTextFormatter, scaleTextStyle = scaleTextStyle, scaleTextHorizontalPadding = scaleTextHorizontalPadding, scaleLineColor = scaleLineColor, - drawChart = { points, maxValue, bounds -> + drawChart = { maxValue, bounds -> drawLineChart( - points = points, + points = data, bounds = bounds, maxValue = maxValue, lineColor = lineColor, lineWidth = lineWidth, lineCornerRadius = lineCornerRadius, - maxPoints = stretchChartToPointsCount ?: points.size, + maxPoints = stretchChartToPointsCount ?: data.size, ) }, ) @@ -132,20 +131,19 @@ fun BarChart( Chart( data = data, modifier = modifier, - stretchChartToPointsCount = stretchChartToPointsCount, scaleItemsCount = scaleItemsCount, scaleTextFormatter = scaleTextFormatter, scaleTextStyle = scaleTextStyle, scaleTextHorizontalPadding = scaleTextHorizontalPadding, scaleLineColor = scaleLineColor, - drawChart = { points, maxValue, bounds -> + drawChart = { maxValue, bounds -> drawBarChart( - points = points, + points = data, bounds = bounds, maxValue = maxValue, barColor = barColor, barSpacing = barSpacing, - maxPoints = stretchChartToPointsCount ?: points.size, + maxPoints = stretchChartToPointsCount ?: data.size, ) }, ) @@ -155,22 +153,16 @@ fun BarChart( private fun Chart( data: List, modifier: Modifier, - stretchChartToPointsCount: Int?, scaleItemsCount: Int, scaleTextFormatter: NumberFormat, scaleTextStyle: TextStyle, scaleTextHorizontalPadding: Dp, scaleLineColor: Color, - drawChart: DrawScope.(points: List, maxValue: Int, bounds: Rect) -> Unit, + drawChart: DrawScope.(maxValue: Int, bounds: Rect) -> Unit, ) { - val trimmedData = if (stretchChartToPointsCount != null) data.takeLast(stretchChartToPointsCount) else data - if (trimmedData.isEmpty()) { - return - } - val textMeasurer = rememberTextMeasurer() - val maxValue = trimmedData.max() + val maxValue = data.max() val numberOfDigitsInMaxValue = log10(abs(maxValue.toDouble())).toInt() val increment = 10.0.pow(numberOfDigitsInMaxValue).toInt() @@ -189,7 +181,7 @@ private fun Chart( ), ) - drawChart(trimmedData, nextMaxMultipleOfScales, chartBounds) + drawChart(nextMaxMultipleOfScales, chartBounds) drawScale( textMeasurer = textMeasurer, diff --git a/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/player/metrics/StatsForNerdsViewModel.kt b/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/player/metrics/StatsForNerdsViewModel.kt index e66641d16..ba082aaa2 100644 --- a/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/player/metrics/StatsForNerdsViewModel.kt +++ b/pillarbox-demo-shared/src/main/java/ch/srgssr/pillarbox/demo/shared/ui/player/metrics/StatsForNerdsViewModel.kt @@ -79,9 +79,10 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic } _indicatedBitRates.update { - BitRates( - data = it.data + (value.indicatedBitrate / TO_MEGA), - ) + val newData = (it.data + (value.indicatedBitrate / TO_MEGA)) + .takeLast(CHART_MAX_POINTS) + + BitRates(data = newData) } _information.update { @@ -103,9 +104,10 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic } _observedBitRates.update { - BitRates( - data = it.data + (value.bandwidth / TO_MEGA), - ) + val newData = (it.data + (value.bandwidth / TO_MEGA)) + .takeLast(CHART_MAX_POINTS) + + BitRates(data = newData) } _stalls.update { @@ -115,9 +117,11 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic } else { stallCount - it.data.sum() } + val newData = (it.data + stall.coerceAtLeast(0f)) + .takeLast(CHART_MAX_POINTS) Stalls( - data = it.data + stall.coerceAtLeast(0f), + data = newData, total = value.stallCount.toFloat().toFormattedBytes(includeUnit = false) ) } @@ -139,9 +143,11 @@ class StatsForNerdsViewModel(application: Application) : AndroidViewModel(applic } else { totalBytesLoaded / TO_MEGA - it.data.sum() } + val newData = (it.data + volume.coerceAtLeast(0f)) + .takeLast(CHART_MAX_POINTS) DataVolumes( - data = it.data + volume.coerceAtLeast(0f), + data = newData, total = totalBytesLoaded.toFormattedBytes(includeUnit = true), ) }