Add project-level prevented package stats for the dependency firewall dashboard

What does this MR do and why?

Part 4 of the Dependency Firewall dashboard stats series, work item https://gitlab.com/gitlab-org/gitlab/-/work_items/627538: the read path for the dashboard's prevented-package counts. Project dashboards only for now.

Merged before it: ledger table and model (!253941 (merged), !255353 (merged)), write path (!257625 (merged), !257627 (merged), !253944 (merged)). Part 5 (GraphQL, !253947 (merged)) exposes these counts; nothing calls them until it merges.

No migrations, no UI changes and no feature flag in this MR. The surface is gated by dependency_firewall_dashboard_v2 (made beta in !258702 (merged)), which Part 5 and the write path check. No changelog entry: nothing uses this until Part 5, and it sits behind a default-off flag.

What it adds

  • Security::DependencyFirewall::PreventedStats::DataCollector: the single entry point. For a project it returns counts from PostgresqlCollector; for a group it returns nil, meaning not available yet, which Part 5 exposes as null.
  • Security::DependencyFirewall::PreventedStats::PostgresqlCollector: counts for one project, from the ledger.
  • Three model methods on Security::DependencyFirewallPreventedPackage, built on the Part 1 scopes: distinct_blocked_count, distinct_warned_only_count and blocked_count_by_max_severity. They compare purls byte-wise (COLLATE "C"), as Sbom::Occurrence does; the default collation is deterministic, so counts cannot change.
  • The counts: distinct packages blocked in the window for the vulnerability family (vulnerability and risk_severity rule types), malicious and license; warned-only counts for malicious and license (warned in the window and not blocked in it); and a per-severity breakdown where each package counts once at its highest severity, so it sums to the vulnerabilities total.
  • Removes the Keela exclusions for for_rule_types, blocked_since and warned_only_since, which now have callers.

Why group reads come later

On seeded Database Lab clones, group-level PostgreSQL reads take about 0.8 to 1.3 s per dashboard load at gitlab-org scale, over the 100 ms guideline, since a group's rows span one index range per project; project reads are about 0.2 to 0.3 ms per statement. Evidence and options tried: !253946 (comment 3928362865)

Group reads will come from the ClickHouse Siphon replica in a follow-up, bringing back the ClickHouse collector and dependency_firewall_clickhouse_reads flag (rollout #631122). Only DataCollector changes then; callers and GraphQL fields stay the same.

Database review

Request database review. No schema changes. New queries read dependency_firewall_prevented_packages, filtered by project_id. A project dashboard runs 6 statements: 5 COUNT(DISTINCT identifier) counts (vulnerability family blocked; malicious blocked and warned-only; license blocked and warned-only) and one severity breakdown, each one index range scan on i_dep_fw_prevented_packages_blocked or i_dep_fw_prevented_packages_warned.

Measured on a Database Lab thin clone of GitLab.com main (PostgreSQL 17.5, 2026-09-29), seeded with about 5.35 million rows since the GitLab.com ledger is empty until the write path is deployed and the flag is on; project 278964 (gitlab-org/gitlab), 7-day window. Warm is the median of 3 runs; cold is the first run on a fresh clone:

Query Warm Cold
Blocked 0.23 ms 0.22 ms
Warned-only 0.27 ms 0.20 ms
Severity breakdown 0.33 ms 0.29 ms

The seed's 300 packages per project is an unvalidated assumption. Cost grows with a project's own rows in the window; a project with unusually many prevented packages was not measured. The dashboard frontend defaults to a 30-day window.

The other counts differ only in rule_type.

Raw SQL and plans

Project, blocked (vulnerability family)

SELECT COUNT(DISTINCT "dependency_firewall_prevented_packages"."identifier" COLLATE "C") FROM "dependency_firewall_prevented_packages" WHERE "dependency_firewall_prevented_packages"."project_id" = 278964 AND "dependency_firewall_prevented_packages"."rule_type" IN (1, 4) AND "dependency_firewall_prevented_packages"."last_blocked_at" >= '2026-09-22 06:44:00';
Plan, warm, median of 3
Aggregate  (cost=5.42..5.43 rows=1 width=8) (actual time=0.157..0.158 rows=1 loops=1)
  Buffers: shared hit=24
  ->  Sort  (cost=5.41..5.41 rows=1 width=24) (actual time=0.149..0.150 rows=13 loops=1)
        Sort Key: identifier COLLATE "C"
        Sort Method: quicksort  Memory: 25kB
        Buffers: shared hit=24
        ->  Index Scan using i_dep_fw_prevented_packages_blocked on dependency_firewall_prevented_packages  (cost=0.43..5.40 rows=1 width=24) (actual time=0.086..0.117 rows=13 loops=1)
              Index Cond: ((project_id = 278964) AND (rule_type = ANY ('{1,4}'::integer[])) AND (last_blocked_at >= '2026-09-22 06:44:00+00'::timestamp with time zone))
              Buffers: shared hit=21
Settings: random_page_cost = '1.5', seq_page_cost = '4', effective_cache_size = '472585MB', work_mem = '230MB', jit = 'off'
Planning:
  Buffers: shared hit=322
Planning Time: 1.050 ms
Execution Time: 0.229 ms

Project, warned-only (license)

SELECT COUNT(DISTINCT "dependency_firewall_prevented_packages"."identifier" COLLATE "C") FROM "dependency_firewall_prevented_packages" WHERE "dependency_firewall_prevented_packages"."project_id" = 278964 AND "dependency_firewall_prevented_packages"."rule_type" = 0 AND "dependency_firewall_prevented_packages"."last_warned_at" >= '2026-09-22 06:44:00' AND ("dependency_firewall_prevented_packages"."last_blocked_at" IS NULL OR "dependency_firewall_prevented_packages"."last_blocked_at" < '2026-09-22 06:44:00');
Plan, warm, median of 3
Aggregate  (cost=3.48..3.48 rows=1 width=8) (actual time=0.131..0.132 rows=1 loops=1)
  Buffers: shared hit=14
  ->  Sort  (cost=3.47..3.47 rows=1 width=24) (actual time=0.124..0.125 rows=3 loops=1)
        Sort Key: identifier COLLATE "C"
        Sort Method: quicksort  Memory: 25kB
        Buffers: shared hit=14
        ->  Index Scan using i_dep_fw_prevented_packages_warned on dependency_firewall_prevented_packages  (cost=0.43..3.46 rows=1 width=24) (actual time=0.076..0.083 rows=3 loops=1)
              Index Cond: ((project_id = 278964) AND (rule_type = 0) AND (last_warned_at >= '2026-09-22 06:44:00+00'::timestamp with time zone))
              Filter: ((last_blocked_at IS NULL) OR (last_blocked_at < '2026-09-22 06:44:00+00'::timestamp with time zone))
              Buffers: shared hit=11
Settings: random_page_cost = '1.5', seq_page_cost = '4', effective_cache_size = '472585MB', work_mem = '230MB', jit = 'off'
Planning:
  Buffers: shared hit=305
Planning Time: 1.135 ms
Execution Time: 0.267 ms

Project, severity breakdown

SELECT COUNT(*) AS "count_all", max_severity AS "max_severity" FROM (SELECT MAX(severity) AS max_severity FROM "dependency_firewall_prevented_packages" WHERE "dependency_firewall_prevented_packages"."project_id" = 278964 AND "dependency_firewall_prevented_packages"."rule_type" IN (1, 4) AND "dependency_firewall_prevented_packages"."last_blocked_at" >= '2026-09-22 06:44:00' GROUP BY "dependency_firewall_prevented_packages"."identifier" COLLATE "C") max_severities GROUP BY max_severity;
Plan, warm, median of 3
GroupAggregate  (cost=5.45..5.47 rows=1 width=10) (actual time=0.184..0.189 rows=6 loops=1)
  Group Key: max_severities.max_severity
  Buffers: shared hit=27
  ->  Sort  (cost=5.45..5.45 rows=1 width=2) (actual time=0.181..0.183 rows=13 loops=1)
        Sort Key: max_severities.max_severity
        Sort Method: quicksort  Memory: 25kB
        Buffers: shared hit=27
        ->  Subquery Scan on max_severities  (cost=5.41..5.44 rows=1 width=2) (actual time=0.150..0.159 rows=13 loops=1)
              Buffers: shared hit=24
              ->  GroupAggregate  (cost=5.41..5.43 rows=1 width=34) (actual time=0.149..0.156 rows=13 loops=1)
                    Group Key: ((dependency_firewall_prevented_packages.identifier)::text)
                    Buffers: shared hit=24
                    ->  Sort  (cost=5.41..5.41 rows=1 width=34) (actual time=0.143..0.144 rows=13 loops=1)
                          Sort Key: ((dependency_firewall_prevented_packages.identifier)::text) COLLATE "C"
                          Sort Method: quicksort  Memory: 25kB
                          Buffers: shared hit=24
                          ->  Index Scan using i_dep_fw_prevented_packages_blocked on dependency_firewall_prevented_packages  (cost=0.43..5.40 rows=1 width=34) (actual time=0.077..0.107 rows=13 loops=1)
                                Index Cond: ((project_id = 278964) AND (rule_type = ANY ('{1,4}'::integer[])) AND (last_blocked_at >= '2026-09-22 06:44:00+00'::timestamp with time zone))
                                Buffers: shared hit=21
Settings: random_page_cost = '1.5', seq_page_cost = '4', effective_cache_size = '472585MB', work_mem = '230MB', jit = 'off'
Planning:
  Buffers: shared hit=329
Planning Time: 1.103 ms
Execution Time: 0.332 ms

Testing

49 examples pass locally across the data collector (2), PostgreSQL collector (3) and model (44, 5 of them for the count methods) specs. RuboCop and Keela clean.

References

Screenshots or screen recordings

Not applicable, no UI changes.

How to set up and validate locally

  1. Create ledger rows for a project, for example with Security::DependencyFirewallPreventedPackage.create!(project: project, rule_type: :license, identifier: 'pkg:npm/left-pad@1.3.0', first_seen_at: 1.day.ago, last_blocked_at: 1.day.ago) in bin/rails console, or through the write path.
  2. Run Security::DependencyFirewall::PreventedStats::DataCollector.new(container: project, from: 7.days.ago).counts and check the counts; for a group it returns nil.
  3. Run the specs: bin/rspec ee/spec/lib/security/dependency_firewall/prevented_stats/ ee/spec/models/security/dependency_firewall_prevented_package_spec.rb

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Arpit Gogia

Merge request reports

Loading
Loading