Skip to content

Normalize trend by overall average #8767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
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
13 changes: 11 additions & 2 deletions app/lib/service/download_counts/package_trends.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'dart:math';

const analysisWindowDays = 30;
const totalTrendWindowDays = 330;
const minThirtyDaysDownloadThreshold = 30000;

/// Calculates the relative daily growth rate of a package's downloads.
Expand All @@ -22,6 +23,9 @@ const minThirtyDaysDownloadThreshold = 30000;
/// downloads (10% relative growth) than for a package with 10000 average daily
/// downloads (0.1% relative growth).
double computeRelativeGrowthRate(List<int> totalDownloads) {
if (totalDownloads.isEmpty) {
return 0;
}
final List<int> data;
if (totalDownloads.length < analysisWindowDays) {
data = [
Expand All @@ -38,7 +42,12 @@ double computeRelativeGrowthRate(List<int> totalDownloads) {
recentDownloads.reduce((prev, element) => prev + element) /
recentDownloads.length;

if (averageRecentDownloads == 0) {
final m = min(totalDownloads.length, totalTrendWindowDays);
final averageTotalDownloads =
totalDownloads.sublist(0, m).reduce((prev, element) => prev + element) /
m;

if (averageRecentDownloads == 0 || averageTotalDownloads == 0) {
return 0;
}

Expand All @@ -51,7 +60,7 @@ double computeRelativeGrowthRate(List<int> totalDownloads) {
// Normalize slope by average downloads to represent relative growth.
// This measures how much the download count is growing relative to its
// current volume.
return growthRate / averageRecentDownloads;
return growthRate / averageTotalDownloads;
}

/// Computes the slope of the best-fit line for a given list of data points
Expand Down
14 changes: 10 additions & 4 deletions app/test/service/download_counts/computations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,16 @@ void main() {
'fake_download_counts_data_for_trend2.jsonl'));
await processDownloadCounts(d);
}
final neonTrend = computeTrendScore(
[...List.filled(15, 2000), ...List.filled(15, 1000)]);
final oxygenTrend = computeTrendScore(
[...List.filled(15, 5000), ...List.filled(15, 3000)]);
final neonTrend = computeTrendScore([
...List.filled(15, 2000),
...List.filled(15, 1000),
...List.filled(701, -1)
]);
final oxygenTrend = computeTrendScore([
...List.filled(15, 5000),
...List.filled(15, 3000),
...List.filled(701, -1)
]);

expect(await computeTrend(),
{'flutter_titanium': 0.0, 'neon': neonTrend, 'oxygen': oxygenTrend});
Expand Down
15 changes: 9 additions & 6 deletions app/test/service/download_counts/package_trends_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,12 @@ void main() {
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.
final downloads =
List<int>.generate(analysisWindowDays * 2, (i) => 1645 - (i * 10));
final expectedRate = 10.0 / 1500.0;
final downloads = <int>[
...List<int>.generate(analysisWindowDays * 2, (i) => 1645 - (i * 10)),
...List.filled(300, 0)
];
final avg = downloads.reduce((prev, element) => prev + element) / 330;
final expectedRate = 10.0 / avg;
expect(computeRelativeGrowthRate(downloads), expectedRate);
});

Expand Down Expand Up @@ -107,10 +110,10 @@ void main() {
final downloads = [100, 50];
// For relativeGrowth:
// Padded data: [100, 50, 0...0] (28 zeros)
// avg = 150/30 = 5
// avg = (100 + 50) / 2 = 75.
// growthRate = 63750 / 67425
final expectedDampening = min(1.0, 150 / 30000);
final expectedRelativeGrowth = 63750 / 67425 / 5;
final expectedRelativeGrowth = (63750 / 67425) / 75;
final expectedScore =
expectedRelativeGrowth * expectedDampening * expectedDampening;
expect(computeTrendScore(downloads), expectedScore);
Expand Down Expand Up @@ -178,7 +181,7 @@ void main() {
test('Short history, high sum meets threshold -> no dampening', () {
final downloads = List<int>.filled(15, 2000);
final expectedDampening = min(1.0, 30000 / 30000);
final expectedRelativeGrowth = 6750000 / 67425 / 1000;
final expectedRelativeGrowth = (6750000 / 67425) / 2000;
final expectedScore =
expectedRelativeGrowth * expectedDampening * expectedDampening;

Expand Down