@@ -33,16 +33,22 @@ class SearchSnapshot {
3333 documents! .remove (packageName);
3434 }
3535
36- /// Updates the PackageDocument.likeScore for each package in the snapshot.
37- /// The score is normalized into the range of [0.0 - 1.0] using the
38- /// ordered list of packages by like counts (same like count gets the same score).
39- void updateLikeScores () {
36+ /// Updates the [PackageDocument] instance's scores for each package in the snapshot.
37+ /// Sets `downloadScore` , `likeScore` and `popularityScore` fields, normalized into the
38+ /// range of [0.0 - 1.0] using the ordered list of their specific counts.
39+ void updateAllScores () {
40+ /// Updates the PackageDocument.downloadScore for each package in the snapshot.
41+ /// The score is normalized into the range of [0.0 - 1.0] using the
42+ /// ordered list of packages by download counts (same download count gets the same score).
43+ documents! .values.updateDownloadScores ();
44+
45+ /// Updates the PackageDocument.likeScore for each package in the snapshot.
46+ /// The score is normalized into the range of [0.0 - 1.0] using the
47+ /// ordered list of packages by like counts (same like count gets the same score).
4048 documents! .values.updateLikeScores ();
41- }
4249
43- /// Updates all popularity values to the currently cached one, otherwise
44- /// only updated package would have been on their new values.
45- void updatePopularityScores () {
50+ /// Updates all popularity values to the currently cached one, otherwise
51+ /// only updated package would have been on their new values.
4652 for (final d in documents! .values) {
4753 if (popularityStorage.isInvalid) {
4854 d.popularityScore = d.likeScore;
@@ -56,6 +62,20 @@ class SearchSnapshot {
5662}
5763
5864extension UpdateLikesExt on Iterable <PackageDocument > {
65+ /// Updates the PackageDocument.downloadScore for each package in the snapshot.
66+ /// The score is normalized into the range of [0.0 - 1.0] using the
67+ /// ordered list of packages by download counts (same download count gets the same score).
68+ void updateDownloadScores () {
69+ final list = sorted ((a, b) => a.downloadCount.compareTo (b.downloadCount));
70+ for (var i = 0 ; i < list.length; i++ ) {
71+ if (i > 0 && list[i - 1 ].downloadCount == list[i].downloadCount) {
72+ list[i].downloadScore = list[i - 1 ].downloadScore;
73+ } else {
74+ list[i].downloadScore = (i + 1 ) / list.length;
75+ }
76+ }
77+ }
78+
5979 /// Updates the PackageDocument.likeScore for each package in the snapshot.
6080 /// The score is normalized into the range of [0.0 - 1.0] using the
6181 /// ordered list of packages by like counts (same like count gets the same score).
0 commit comments