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
6 changes: 6 additions & 0 deletions app/lib/frontend/handlers/experimental.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ typedef PublicFlag = ({String name, String description});

const _publicFlags = <PublicFlag>{
(name: 'example', description: 'Short description'),
(
name: 'trending-search',
description: 'Show trending packages and search by trending scores'
),
};

final _allFlags = <String>{
Expand Down Expand Up @@ -88,6 +92,8 @@ class ExperimentalFlags {

bool get isDarkModeDefault => isEnabled('dark-as-default');

bool get showTrending => isEnabled('trending-search');

String encodedAsCookie() => _enabled.join(':');

@override
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.showTrending)
SortDict(
id: 'trending',
label: 'trending',
tooltip: 'Packages are sorted by trending.'),
];
}
1 change: 1 addition & 0 deletions app/lib/search/backend.dart
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ class SearchBackend {
updated: p.lastVersionPublished!,
readme: compactReadme(readmeAsset?.textContent),
downloadCount: downloadCountsBackend.lookup30DaysTotalCounts(pv.package),
trendScore: downloadCountsBackend.lookupTrendScore(pv.package),
likeCount: p.likes,
grantedPoints: scoreCard.grantedPubPoints,
maxPoints: scoreCard.maxPubPoints,
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 @@ -43,6 +43,7 @@ class InMemoryPackageIndex {
late final List<IndexedPackageHit> _downloadsOrderedHits;
late final List<IndexedPackageHit> _likesOrderedHits;
late final List<IndexedPackageHit> _pointsOrderedHits;
late final List<IndexedPackageHit> _trendingOrderedHits;

// Contains all of the topics the index had seen so far.
// TODO: consider moving this into a separate index
Expand Down Expand Up @@ -121,6 +122,8 @@ class InMemoryPackageIndex {
score: (doc) => doc.likeCount.toDouble());
_pointsOrderedHits = _rankWithComparator(_comparePoints,
score: (doc) => doc.grantedPoints.toDouble());
_trendingOrderedHits = _rankWithComparator(_compareTrending,
score: (doc) => doc.trendScore.toDouble());
}

IndexInfo indexInfo() {
Expand Down Expand Up @@ -289,6 +292,9 @@ class InMemoryPackageIndex {
case SearchOrder.points:
indexedHits = _pointsOrderedHits.whereInScores(packageScores);
break;
case SearchOrder.trending:
indexedHits = _trendingOrderedHits.whereInScores(packageScores);
break;
}

// bound by offset and limit (or randomize items)
Expand Down Expand Up @@ -532,6 +538,12 @@ class InMemoryPackageIndex {
if (x != 0) return x;
return _compareUpdated(a, b);
}

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

class _TextResults {
Expand Down
4 changes: 4 additions & 0 deletions app/lib/search/search_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ class PackageDocument {
/// The normalized score between [0.0-1.0] (1.0 being the most downloaded package).
double? downloadScore;

final int trendScore;

final int likeCount;

/// The normalized score between [0.0-1.0] (1.0 being the most liked package).
Expand Down Expand Up @@ -110,6 +112,7 @@ class PackageDocument {
List<String>? tags,
int? downloadCount,
this.downloadScore,
int? trendScore,
int? likeCount,
this.likeScore,
int? grantedPoints,
Expand All @@ -121,6 +124,7 @@ class PackageDocument {
}) : created = created ?? clock.now(),
updated = updated ?? clock.now(),
downloadCount = downloadCount ?? 0,
trendScore = trendScore ?? 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 @@ -64,6 +64,9 @@ enum SearchOrder {

/// Search order should be in decreasing pub points.
points,

/// Search order should be in decreasing trend score.
trending,
}

/// Returns null if [value] is not a recognized search order.
Expand Down