Skip to content

Commit 4604ca5

Browse files
authored
feat: add rank packages function (CM-1310) (#4310)
Signed-off-by: Umberto Sgueglia <usgueglia@contractor.linuxfoundation.org>
1 parent cf9314d commit 4604ca5

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
-- Add the Sonatype popularity score as a 4th ranking signal in rank_packages().
2+
--
3+
-- Sonatype could not deliver raw Maven download counts, so they provide a
4+
-- popularity score (0-100, normalized per ecosystem) as the substitute for the
5+
-- missing downloads signal. Maven previously had only dependent_count and
6+
-- transitive_dependent_count feeding criticality.
7+
--
8+
-- The signal slots into the existing cumulative-coverage machinery unchanged:
9+
-- * cumulative-share is scale-invariant, so a 0-100 score sorts identically
10+
-- to a raw count — no normalization needed;
11+
-- * non-maven rows are all-NULL -> ecosystem total = 0 -> the WHERE clause
12+
-- drops the signal for them, so npm/pypi ranking is untouched.
13+
--
14+
-- No tier short-circuit here (measurement-first): is_critical is a BOOL_OR, so
15+
-- adding a signal can only make more packages critical, never fewer. Whether a
16+
-- hard "P0 always critical" guarantee is needed is decided after measuring the
17+
-- impact shift on real data.
18+
19+
CREATE OR REPLACE FUNCTION rank_packages(
20+
coverage_cutoff numeric DEFAULT 0.90,
21+
ecosystems text[] DEFAULT NULL
22+
)
23+
RETURNS TABLE(processed_rows int)
24+
LANGUAGE plpgsql AS $$
25+
DECLARE
26+
processed_count int;
27+
effective_ecosystems text[];
28+
BEGIN
29+
IF ecosystems IS NULL THEN
30+
SELECT ARRAY_AGG(DISTINCT ecosystem)
31+
INTO effective_ecosystems
32+
FROM packages;
33+
ELSE
34+
effective_ecosystems := ecosystems;
35+
END IF;
36+
37+
WITH base AS (
38+
SELECT
39+
id,
40+
ecosystem,
41+
COALESCE(downloads_last_30d, 0) AS downloads,
42+
COALESCE(dependent_count, 0) AS direct_dependents,
43+
COALESCE(transitive_dependent_count, 0) AS transitive_dependents,
44+
COALESCE(sonatype_popularity_score, 0) AS sonatype_popularity,
45+
SUM(COALESCE(downloads_last_30d, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_downloads,
46+
SUM(COALESCE(dependent_count, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_direct_dependents,
47+
SUM(COALESCE(transitive_dependent_count, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_transitive_dependents,
48+
SUM(COALESCE(sonatype_popularity_score, 0)) OVER (PARTITION BY ecosystem) AS ecosystem_total_sonatype
49+
FROM packages
50+
WHERE ecosystem = ANY(effective_ecosystems)
51+
),
52+
walked AS (
53+
-- One row per (package × signal). Signals with a zero ecosystem total are
54+
-- excluded so they don't factor into the average (e.g. downloads for maven,
55+
-- sonatype for npm/pypi).
56+
-- cumulative_share_exclusive for the top-ranked package equals 0 by arithmetic
57+
-- (sum of rows above it is 0), so the top package is always inside the critical set.
58+
SELECT
59+
id,
60+
ecosystem,
61+
SUM(signal_value) OVER coverage_window / ecosystem_signal_total::numeric AS cumulative_share_inclusive,
62+
(SUM(signal_value) OVER coverage_window - signal_value) / ecosystem_signal_total::numeric AS cumulative_share_exclusive
63+
FROM base
64+
CROSS JOIN LATERAL (VALUES
65+
('downloads', downloads, ecosystem_total_downloads),
66+
('direct_dependents', direct_dependents, ecosystem_total_direct_dependents),
67+
('transitive_dependents', transitive_dependents, ecosystem_total_transitive_dependents),
68+
('sonatype_popularity', sonatype_popularity, ecosystem_total_sonatype)
69+
) AS signal(signal_name, signal_value, ecosystem_signal_total)
70+
WHERE ecosystem_signal_total > 0
71+
WINDOW coverage_window AS (
72+
PARTITION BY ecosystem, signal_name
73+
ORDER BY signal_value DESC, id
74+
ROWS UNBOUNDED PRECEDING
75+
)
76+
),
77+
combined AS (
78+
SELECT
79+
id,
80+
ecosystem,
81+
AVG(1.0 - cumulative_share_inclusive)::numeric(10, 4) AS new_impact,
82+
BOOL_OR(cumulative_share_exclusive < coverage_cutoff) AS new_is_critical
83+
FROM walked
84+
GROUP BY id, ecosystem
85+
),
86+
final AS (
87+
SELECT
88+
combined.id,
89+
combined.new_impact,
90+
combined.new_is_critical OR (spotlight.package_id IS NOT NULL) AS new_is_critical,
91+
ROW_NUMBER() OVER (
92+
PARTITION BY combined.ecosystem
93+
ORDER BY combined.new_impact DESC NULLS LAST, combined.id
94+
) AS new_rank_in_ecosystem
95+
FROM combined
96+
LEFT JOIN package_criticality_spotlight spotlight ON spotlight.package_id = combined.id
97+
)
98+
UPDATE packages p
99+
SET impact = final.new_impact,
100+
is_critical = final.new_is_critical,
101+
rank_in_ecosystem = final.new_rank_in_ecosystem,
102+
last_rank_pass_at = NOW(),
103+
last_synced_at = NOW()
104+
FROM final
105+
WHERE p.id = final.id;
106+
107+
GET DIAGNOSTICS processed_count = ROW_COUNT;
108+
109+
RETURN QUERY SELECT processed_count;
110+
END;
111+
$$;

0 commit comments

Comments
 (0)