Skip to content

Commit 6426628

Browse files
committed
Small refactor + doc
1 parent c437c5b commit 6426628

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

app/lib/service/download_counts/computations.dart

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,7 @@ Future<Map<String, double>> computeTrend() async {
3232
?.totalCounts ??
3333
[0];
3434

35-
final lastNonZeroIndex = downloads.lastIndexWhere((e) => e != 0);
36-
res[name] = computeTrendScore(
37-
lastNonZeroIndex >= 0 ? downloads.sublist(0, lastNonZeroIndex) : []);
35+
res[name] = computeTrendScore(downloads);
3836
}
3937
return res;
4038
}

app/lib/service/download_counts/package_trends.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,22 @@ double calculateLinearRegressionSlope(List<num> yValues) {
8282
/// assessed by comparing the sum of its downloads over the available history
8383
/// (up to [analysisWindowDays]) against a [minThirtyDaysDownloadThreshold].
8484
double computeTrendScore(List<int> totalDownloads) {
85-
final n = min(analysisWindowDays, totalDownloads.length);
86-
final thirtydaySum = totalDownloads.isEmpty
85+
final lastNonZeroIndex = totalDownloads.lastIndexWhere((e) => e != 0);
86+
87+
// We trim trailing zeros to ensure an accurate calculation of the trend. The
88+
// zeros represent the time before the package was published. Leaving them in
89+
// would artificially flatten the calculated growth rate.
90+
final downloads = lastNonZeroIndex >= 0
91+
? totalDownloads.sublist(0, lastNonZeroIndex + 1)
92+
: <int>[];
93+
94+
final n = min(analysisWindowDays, downloads.length);
95+
final thirtydaySum = downloads.isEmpty
8796
? 0
88-
: totalDownloads.sublist(0, n).reduce((prev, element) => prev + element);
97+
: downloads.sublist(0, n).reduce((prev, element) => prev + element);
8998
final sigmoid = calculateSigmoidScaleScore(total30Downloads: thirtydaySum);
9099

91-
return computeRelativeGrowthRate(totalDownloads) * sigmoid;
100+
return computeRelativeGrowthRate(downloads) * sigmoid;
92101
}
93102

94103
/// Transforms a list of numbers to their natural logarithm.

0 commit comments

Comments
 (0)