Skip to content

Commit 2d1024b

Browse files
authored
Use donwload counts in search index and sort control. (#8179)
1 parent c5682bb commit 2d1024b

File tree

7 files changed

+30
-0
lines changed

7 files changed

+30
-0
lines changed

app/lib/fake/backend/fake_popularity.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ import '../../shared/popularity_storage.dart';
1010

1111
/// Scans the datastore for packages and generates popularity values with a
1212
/// deterministic random seed.
13+
///
14+
/// TODO: generate similar values for download counts.
1315
Future<void> generateFakePopularityValues() async {
1416
final values = <String, double>{};
1517
final query = dbService.query<Package>();

app/lib/frontend/templates/_consts.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:_pub_shared/search/tags.dart';
6+
import 'package:pub_dev/frontend/request_context.dart';
67

78
import '../dom/dom.dart' as d;
89

@@ -124,5 +125,10 @@ List<SortDict> getSortDicts(bool isSearch) {
124125
final removeId = isSearch ? 'listing_relevance' : 'search_relevance';
125126
return <SortDict>[
126127
..._sortDicts.where((d) => d.id != removeId),
128+
if (requestContext.experimentalFlags.showDownloadCounts)
129+
SortDict(
130+
id: 'downloads',
131+
label: 'downloads',
132+
tooltip: 'Packages are sorted by their download counts.'),
127133
];
128134
}

app/lib/search/backend.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import 'package:pool/pool.dart';
2222
import 'package:pub_dev/publisher/backend.dart';
2323

2424
import 'package:pub_dev/search/search_client.dart';
25+
import 'package:pub_dev/service/download_counts/backend.dart';
2526
import 'package:pub_dev/shared/popularity_storage.dart';
2627
import 'package:pub_dev/shared/redis_cache.dart';
2728
import 'package:pub_dev/shared/utils.dart';
@@ -356,6 +357,7 @@ class SearchBackend {
356357
created: p.created!,
357358
updated: p.lastVersionPublished!,
358359
readme: compactReadme(readmeAsset?.textContent),
360+
downloadCount: downloadCountsBackend.lookup30DaysTotalCounts(pv.package),
359361
likeCount: p.likes,
360362
popularityScore: popularityStorage.lookup(packageName),
361363
grantedPoints: scoreCard.grantedPubPoints,

app/lib/search/mem_index.dart

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class InMemoryPackageIndex {
3333
late final List<PackageHit> _createdOrderedHits;
3434
late final List<PackageHit> _updatedOrderedHits;
3535
late final List<PackageHit> _popularityOrderedHits;
36+
late final List<PackageHit> _downloadsOrderedHits;
3637
late final List<PackageHit> _likesOrderedHits;
3738
late final List<PackageHit> _pointsOrderedHits;
3839

@@ -81,6 +82,8 @@ class InMemoryPackageIndex {
8182
_updatedOrderedHits = _rankWithComparator(_compareUpdated);
8283
_popularityOrderedHits = _rankWithComparator(_comparePopularity,
8384
score: (doc) => doc.popularityScore ?? 0);
85+
_downloadsOrderedHits = _rankWithComparator(_compareDownloads,
86+
score: (doc) => doc.downloadCount.toDouble());
8487
_likesOrderedHits = _rankWithComparator(_compareLikes,
8588
score: (doc) => doc.likeCount.toDouble());
8689
_pointsOrderedHits = _rankWithComparator(_comparePoints,
@@ -195,6 +198,9 @@ class InMemoryPackageIndex {
195198
case SearchOrder.popularity:
196199
packageHits = _popularityOrderedHits.whereInSet(packages);
197200
break;
201+
case SearchOrder.downloads:
202+
packageHits = _downloadsOrderedHits.whereInSet(packages);
203+
break;
198204
case SearchOrder.like:
199205
packageHits = _likesOrderedHits.whereInSet(packages);
200206
break;
@@ -414,6 +420,12 @@ class InMemoryPackageIndex {
414420
return _compareUpdated(a, b);
415421
}
416422

423+
int _compareDownloads(PackageDocument a, PackageDocument b) {
424+
final x = -a.downloadCount.compareTo(b.downloadCount);
425+
if (x != 0) return x;
426+
return _compareUpdated(a, b);
427+
}
428+
417429
int _compareLikes(PackageDocument a, PackageDocument b) {
418430
final x = -a.likeCount.compareTo(b.likeCount);
419431
if (x != 0) return x;

app/lib/search/search_service.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ class PackageDocument {
7373

7474
final List<String> tags;
7575

76+
final int downloadCount;
7677
final int likeCount;
7778

7879
/// The normalized score between [0.0-1.0] (1.0 being the most liked package).
@@ -105,6 +106,7 @@ class PackageDocument {
105106
DateTime? updated,
106107
this.readme = '',
107108
List<String>? tags,
109+
int? downloadCount,
108110
int? likeCount,
109111
this.likeScore,
110112
this.popularityScore,
@@ -116,6 +118,7 @@ class PackageDocument {
116118
this.sourceUpdated,
117119
}) : created = created ?? clock.now(),
118120
updated = updated ?? clock.now(),
121+
downloadCount = downloadCount ?? 0,
119122
likeCount = likeCount ?? 0,
120123
grantedPoints = grantedPoints ?? 0,
121124
maxPoints = maxPoints ?? 0,

app/lib/search/search_service.g.dart

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/_pub_shared/lib/search/search_form.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ enum SearchOrder {
5252
/// Search order should be in decreasing popularity score.
5353
popularity,
5454

55+
/// Search order should be in decreasing download counts.
56+
downloads,
57+
5558
/// Search order should be in decreasing like count.
5659
like,
5760

0 commit comments

Comments
 (0)