Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/lib/service/download_counts/package_trends.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ double computeRelativeGrowthRate(List<int> totalDownloads) {
recentDownloads.reduce((prev, element) => prev + element) /
recentDownloads.length;

if (averageRecentDownloads == 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

< 1 might also be worth considering...

Packages with average downloads less than one shouldn't have their growthrate increased..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll keep it in mind but I'll keep it as it is for now. This is the mathematically correct calculation, and hence keeps the abstraction of the function clear. On the caller site we will handle packages with few downloads.

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.
Expand Down
8 changes: 7 additions & 1 deletion app/test/service/download_counts/package_trends_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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', () {
Expand All @@ -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<int>.generate(analysisWindowDays, (i) => 2000);
expect(computeRelativeGrowthRate(downloads), 0.0);
});

test('returns 0.0 for 0 downloads', () {
final downloads = List<int>.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.
Expand Down