diff --git a/app/lib/service/download_counts/package_trends.dart b/app/lib/service/download_counts/package_trends.dart index 94ae8660f9..2a34af3691 100644 --- a/app/lib/service/download_counts/package_trends.dart +++ b/app/lib/service/download_counts/package_trends.dart @@ -35,6 +35,10 @@ double computeRelativeGrowthRate(List totalDownloads) { recentDownloads.reduce((prev, element) => prev + element) / recentDownloads.length; + if (averageRecentDownloads == 0) { + return 0; + } + // We reverse the recentDownloads list for regression, since the first entry // is the newest point in time. By reversing, we pass the data in // chronological order. diff --git a/app/test/service/download_counts/package_trends_test.dart b/app/test/service/download_counts/package_trends_test.dart index 85694c2781..461a520a27 100644 --- a/app/test/service/download_counts/package_trends_test.dart +++ b/app/test/service/download_counts/package_trends_test.dart @@ -11,6 +11,7 @@ void main() { expect(calculateLinearRegressionSlope([10.0, 20.0, 30.0]), 10.0); expect(calculateLinearRegressionSlope([30.0, 20.0, 10.0]), -10.0); expect(calculateLinearRegressionSlope([10.0, 10.0, 10.0]), 0); + expect(calculateLinearRegressionSlope([0.0, 0.0, 0.0]), 0); }); test('return 0.0 if denominator is very small', () { @@ -20,11 +21,16 @@ void main() { }); group('computeRelativeGrowthRate', () { - test('returns 0.0 for stable downloads meeting threshold', () { + test('returns 0.0 for stable downloads', () { final downloads = List.generate(analysisWindowDays, (i) => 2000); expect(computeRelativeGrowthRate(downloads), 0.0); }); + test('returns 0.0 for 0 downloads', () { + final downloads = List.generate(analysisWindowDays, (i) => 0); + expect(computeRelativeGrowthRate(downloads), 0.0); + }); + test('calculates positive relative growth rate for positive trend', () { // Input list (newest first): [1645, 1635, ..., 1355] (30 values) // Average = 1500 for the first 30 values. Slope: 10.