Fix search ranking priority order
This commit is contained in:
@@ -5684,6 +5684,32 @@ class ManagerIngestionTests(unittest.TestCase):
|
|||||||
self.assertEqual(["second", "first", "third"], [entry["id"] for entry in ranked])
|
self.assertEqual(["second", "first", "third"], [entry["id"] for entry in ranked])
|
||||||
self.assertEqual([1, 2, 3], [entry["rank"] for entry in ranked])
|
self.assertEqual([1, 2, 3], [entry["rank"] for entry in ranked])
|
||||||
|
|
||||||
|
def test_rank_packs_prefers_higher_search_ranking_when_downloads_and_stars_tie(self):
|
||||||
|
packs = [
|
||||||
|
{
|
||||||
|
"id": "low-search",
|
||||||
|
"title": "Low Search",
|
||||||
|
"repository": "https://github.com/example/low-search",
|
||||||
|
"downloads": 100,
|
||||||
|
"github_stars": 5,
|
||||||
|
"metrics": {"search_ranking": 1},
|
||||||
|
"manager_order": 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "high-search",
|
||||||
|
"title": "High Search",
|
||||||
|
"repository": "https://github.com/example/high-search",
|
||||||
|
"downloads": 100,
|
||||||
|
"github_stars": 5,
|
||||||
|
"search_ranking": 10,
|
||||||
|
"manager_order": 1,
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
ranked = rank_packs(packs)
|
||||||
|
|
||||||
|
self.assertEqual(["high-search", "low-search"], [pack["id"] for pack in ranked])
|
||||||
|
|
||||||
|
|
||||||
class RepoCacheTests(unittest.TestCase):
|
class RepoCacheTests(unittest.TestCase):
|
||||||
def test_default_cache_dir_is_under_system_temp_dir(self):
|
def test_default_cache_dir_is_under_system_temp_dir(self):
|
||||||
|
|||||||
@@ -251,22 +251,22 @@ def _metric_max(metrics, names):
|
|||||||
return max(values, default=0)
|
return max(values, default=0)
|
||||||
|
|
||||||
|
|
||||||
def _metric_min_float(metrics, names):
|
def _metric_max_float(metrics, names):
|
||||||
values = []
|
values = []
|
||||||
for name in names:
|
for name in names:
|
||||||
value = _coerce_float(metrics.get(name))
|
value = _coerce_float(metrics.get(name))
|
||||||
if value is not None:
|
if value is not None:
|
||||||
values.append(value)
|
values.append(value)
|
||||||
return min(values) if values else None
|
return max(values) if values else None
|
||||||
|
|
||||||
|
|
||||||
def _metric_min_float_from_sources(sources, names):
|
def _metric_max_float_from_sources(sources, names):
|
||||||
values = []
|
values = []
|
||||||
for source in sources:
|
for source in sources:
|
||||||
value = _metric_min_float(source, names)
|
value = _metric_max_float(source, names)
|
||||||
if value is not None:
|
if value is not None:
|
||||||
values.append(value)
|
values.append(value)
|
||||||
return min(values) if values else None
|
return max(values) if values else None
|
||||||
|
|
||||||
|
|
||||||
def _pack_id_from_repository(repository):
|
def _pack_id_from_repository(repository):
|
||||||
@@ -312,7 +312,7 @@ def _rank_sort_key(pack):
|
|||||||
metric_sources = (pack.get("metrics", {}), pack)
|
metric_sources = (pack.get("metrics", {}), pack)
|
||||||
downloads = max(_metric_max(source, ("downloads", "download_count")) for source in metric_sources)
|
downloads = max(_metric_max(source, ("downloads", "download_count")) for source in metric_sources)
|
||||||
stars = max(_metric_max(source, ("stars", "github_stars", "stargazers_count")) for source in metric_sources)
|
stars = max(_metric_max(source, ("stars", "github_stars", "stargazers_count")) for source in metric_sources)
|
||||||
search_ranking = _metric_min_float_from_sources(
|
search_ranking = _metric_max_float_from_sources(
|
||||||
metric_sources,
|
metric_sources,
|
||||||
("search_ranking", "search_rank", "search_order"),
|
("search_ranking", "search_rank", "search_order"),
|
||||||
)
|
)
|
||||||
@@ -321,7 +321,7 @@ def _rank_sort_key(pack):
|
|||||||
-downloads,
|
-downloads,
|
||||||
-stars,
|
-stars,
|
||||||
1 if search_ranking is None else 0,
|
1 if search_ranking is None else 0,
|
||||||
search_ranking if search_ranking is not None else 0.0,
|
-search_ranking if search_ranking is not None else 0.0,
|
||||||
manager_order,
|
manager_order,
|
||||||
str(pack.get("title", "")).lower(),
|
str(pack.get("title", "")).lower(),
|
||||||
str(pack.get("id", "")),
|
str(pack.get("id", "")),
|
||||||
|
|||||||
Reference in New Issue
Block a user