Skip to content

Commit 56e05ed

Browse files
authored
Handle 0 downloads in growth function (#8741)
1 parent 0f51317 commit 56e05ed

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

app/lib/service/download_counts/package_trends.dart

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ double computeRelativeGrowthRate(List<int> totalDownloads) {
3535
recentDownloads.reduce((prev, element) => prev + element) /
3636
recentDownloads.length;
3737

38+
if (averageRecentDownloads == 0) {
39+
return 0;
40+
}
41+
3842
// We reverse the recentDownloads list for regression, since the first entry
3943
// is the newest point in time. By reversing, we pass the data in
4044
// chronological order.

app/test/service/download_counts/package_trends_test.dart

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ void main() {
1111
expect(calculateLinearRegressionSlope([10.0, 20.0, 30.0]), 10.0);
1212
expect(calculateLinearRegressionSlope([30.0, 20.0, 10.0]), -10.0);
1313
expect(calculateLinearRegressionSlope([10.0, 10.0, 10.0]), 0);
14+
expect(calculateLinearRegressionSlope([0.0, 0.0, 0.0]), 0);
1415
});
1516

1617
test('return 0.0 if denominator is very small', () {
@@ -20,11 +21,16 @@ void main() {
2021
});
2122

2223
group('computeRelativeGrowthRate', () {
23-
test('returns 0.0 for stable downloads meeting threshold', () {
24+
test('returns 0.0 for stable downloads', () {
2425
final downloads = List<int>.generate(analysisWindowDays, (i) => 2000);
2526
expect(computeRelativeGrowthRate(downloads), 0.0);
2627
});
2728

29+
test('returns 0.0 for 0 downloads', () {
30+
final downloads = List<int>.generate(analysisWindowDays, (i) => 0);
31+
expect(computeRelativeGrowthRate(downloads), 0.0);
32+
});
33+
2834
test('calculates positive relative growth rate for positive trend', () {
2935
// Input list (newest first): [1645, 1635, ..., 1355] (30 values)
3036
// Average = 1500 for the first 30 values. Slope: 10.

0 commit comments

Comments
 (0)