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
2 changes: 2 additions & 0 deletions app/lib/fake/backend/fake_popularity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import '../../shared/popularity_storage.dart';

/// Scans the datastore for packages and generates popularity values with a
/// deterministic random seed.
///
/// TODO: generate similar values for download counts.
Future<void> generateFakePopularityValues() async {
final values = <String, double>{};
final query = dbService.query<Package>();
Expand Down
6 changes: 6 additions & 0 deletions app/lib/frontend/templates/_consts.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file.

import 'package:_pub_shared/search/tags.dart';
import 'package:pub_dev/frontend/request_context.dart';

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

Expand Down Expand Up @@ -124,5 +125,10 @@ List<SortDict> getSortDicts(bool isSearch) {
final removeId = isSearch ? 'listing_relevance' : 'search_relevance';
return <SortDict>[
..._sortDicts.where((d) => d.id != removeId),
if (requestContext.experimentalFlags.showDownloadCounts)
SortDict(
id: 'downloads',
label: 'downloads',
tooltip: 'Packages are sorted by their download counts.'),
];
}
2 changes: 2 additions & 0 deletions app/lib/search/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'package:pool/pool.dart';
import 'package:pub_dev/publisher/backend.dart';

import 'package:pub_dev/search/search_client.dart';
import 'package:pub_dev/service/download_counts/backend.dart';
import 'package:pub_dev/shared/popularity_storage.dart';
import 'package:pub_dev/shared/redis_cache.dart';
import 'package:pub_dev/shared/utils.dart';
Expand Down Expand Up @@ -356,6 +357,7 @@ class SearchBackend {
created: p.created!,
updated: p.lastVersionPublished!,
readme: compactReadme(readmeAsset?.textContent),
downloadCount: downloadCountsBackend.lookup30DaysTotalCounts(pv.package),
likeCount: p.likes,
popularityScore: popularityStorage.lookup(packageName),
grantedPoints: scoreCard.grantedPubPoints,
Expand Down
12 changes: 12 additions & 0 deletions app/lib/search/mem_index.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class InMemoryPackageIndex {
late final List<PackageHit> _createdOrderedHits;
late final List<PackageHit> _updatedOrderedHits;
late final List<PackageHit> _popularityOrderedHits;
late final List<PackageHit> _downloadsOrderedHits;
late final List<PackageHit> _likesOrderedHits;
late final List<PackageHit> _pointsOrderedHits;

Expand Down Expand Up @@ -81,6 +82,8 @@ class InMemoryPackageIndex {
_updatedOrderedHits = _rankWithComparator(_compareUpdated);
_popularityOrderedHits = _rankWithComparator(_comparePopularity,
score: (doc) => doc.popularityScore ?? 0);
_downloadsOrderedHits = _rankWithComparator(_compareDownloads,
score: (doc) => doc.downloadCount.toDouble());
_likesOrderedHits = _rankWithComparator(_compareLikes,
score: (doc) => doc.likeCount.toDouble());
_pointsOrderedHits = _rankWithComparator(_comparePoints,
Expand Down Expand Up @@ -195,6 +198,9 @@ class InMemoryPackageIndex {
case SearchOrder.popularity:
packageHits = _popularityOrderedHits.whereInSet(packages);
break;
case SearchOrder.downloads:
packageHits = _downloadsOrderedHits.whereInSet(packages);
break;
case SearchOrder.like:
packageHits = _likesOrderedHits.whereInSet(packages);
break;
Expand Down Expand Up @@ -414,6 +420,12 @@ class InMemoryPackageIndex {
return _compareUpdated(a, b);
}

int _compareDownloads(PackageDocument a, PackageDocument b) {
final x = -a.downloadCount.compareTo(b.downloadCount);
if (x != 0) return x;
return _compareUpdated(a, b);
}

int _compareLikes(PackageDocument a, PackageDocument b) {
final x = -a.likeCount.compareTo(b.likeCount);
if (x != 0) return x;
Expand Down
3 changes: 3 additions & 0 deletions app/lib/search/search_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class PackageDocument {

final List<String> tags;

final int downloadCount;
final int likeCount;

/// The normalized score between [0.0-1.0] (1.0 being the most liked package).
Expand Down Expand Up @@ -105,6 +106,7 @@ class PackageDocument {
DateTime? updated,
this.readme = '',
List<String>? tags,
int? downloadCount,
int? likeCount,
this.likeScore,
this.popularityScore,
Expand All @@ -116,6 +118,7 @@ class PackageDocument {
this.sourceUpdated,
}) : created = created ?? clock.now(),
updated = updated ?? clock.now(),
downloadCount = downloadCount ?? 0,
likeCount = likeCount ?? 0,
grantedPoints = grantedPoints ?? 0,
maxPoints = maxPoints ?? 0,
Expand Down
2 changes: 2 additions & 0 deletions app/lib/search/search_service.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/_pub_shared/lib/search/search_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ enum SearchOrder {
/// Search order should be in decreasing popularity score.
popularity,

/// Search order should be in decreasing download counts.
downloads,

/// Search order should be in decreasing like count.
like,

Expand Down
Loading