diff --git a/tests/test_generate_popular_node_signatures.py b/tests/test_generate_popular_node_signatures.py index 4e5ac53..b27917a 100644 --- a/tests/test_generate_popular_node_signatures.py +++ b/tests/test_generate_popular_node_signatures.py @@ -16,6 +16,7 @@ from tools.generate_popular_node_signatures import ( main, normalise_input_spec, normalise_manager_entries, + rank_entries, rank_packs, repo_cache_path, write_artifact, @@ -5650,6 +5651,39 @@ class ManagerIngestionTests(unittest.TestCase): self.assertEqual(["many-downloads", "many-stars"], [pack["id"] for pack in ranked]) + def test_rank_entries_reads_top_level_popularity_fields(self): + entries = [ + { + "id": "third", + "title": "Third", + "repository": "https://github.com/example/third", + "downloads": 1, + "github_stars": 10, + "manager_order": 0, + }, + { + "id": "first", + "title": "First", + "repository": "https://github.com/example/first", + "downloads": 100, + "github_stars": 0, + "manager_order": 2, + }, + { + "id": "second", + "title": "Second", + "repository": "https://github.com/example/second", + "downloads": 100, + "github_stars": 50, + "manager_order": 1, + }, + ] + + ranked = rank_entries(entries) + + self.assertEqual(["second", "first", "third"], [entry["id"] for entry in ranked]) + self.assertEqual([1, 2, 3], [entry["rank"] for entry in ranked]) + class RepoCacheTests(unittest.TestCase): def test_default_cache_dir_is_under_system_temp_dir(self): diff --git a/tools/generate_popular_node_signatures.py b/tools/generate_popular_node_signatures.py index db63ecd..8c3b586 100644 --- a/tools/generate_popular_node_signatures.py +++ b/tools/generate_popular_node_signatures.py @@ -260,6 +260,15 @@ def _metric_min_float(metrics, names): return min(values) if values else None +def _metric_min_float_from_sources(sources, names): + values = [] + for source in sources: + value = _metric_min_float(source, names) + if value is not None: + values.append(value) + return min(values) if values else None + + def _pack_id_from_repository(repository): parsed = urlparse(repository) if parsed.netloc: @@ -300,10 +309,13 @@ def normalise_manager_entries(raw): def _rank_sort_key(pack): - metrics = pack.get("metrics", {}) - downloads = _metric_max(metrics, ("downloads", "download_count")) - stars = _metric_max(metrics, ("stars", "github_stars", "stargazers_count")) - search_ranking = _metric_min_float(metrics, ("search_ranking", "search_rank", "search_order")) + metric_sources = (pack.get("metrics", {}), pack) + 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) + search_ranking = _metric_min_float_from_sources( + metric_sources, + ("search_ranking", "search_rank", "search_order"), + ) manager_order = int(pack.get("manager_order", 0)) return ( -downloads,