Resolve test file categories from test_results instead of stamping one on every coverage row
## Background
This issue first targeted `shared.test_file_mappings`, which stored a category on every test-to-source mapping. Its last row was written on 2026-09-22, https://gitlab.com/gitlab-org/ruby/gems/gitlab_quality-test_tooling/-/merge_requests/522+ removed the export, and its three-month TTL empties it around 2026-12-22. The dashboards in runbooks now read ownership from `code_coverage.test_coverage_per_file` instead.
That table repeats the design and is what this issue now targets. The per-test coverage export in `gitlab_quality-test_tooling` stamps one `feature_category` on every (test file, source file) row, taking the first category in that job's reports (`.first` in `per_test_coverage_data.rb`).
## Problem
Each example in a spec can declare its own category, but each coverage row stores only one. Which one it stores depends on the order of that job's reports. Of the 33,230 test files in `test_coverage_per_file` today:
- 690 have more than one category and every row keeps whichever one `.first` picked.
- 580 have a blank category on their newest row. The dashboards still find their group only by falling back to the newest older row that has one (`argMaxIf`). That fallback stops working once the 90-day TTL removes those rows.
`test_metrics.test_results` already holds the category of every example that observer exports, which gives the full set of categories for each file. Over 30 days it covers 23,732 of the 25,757 non-Jest test files and for only 3 files does its set leave out the category the coverage table stores. It is too large to read directly: grouping 30 days of it by file took 8 minutes 45 seconds.
## Proposed solution(s)
Keep the categories in their own small table, `test_metrics.test_file_categories`, with one row per `(ci_project_path, file_path, feature_category)` and the time that category was last seen. A materialized view fills it from `test_metrics.test_results` as rows arrive. A one-off `INSERT ... SELECT` backfills it. It needs no gem release and no grant change, since Grafana and the gem's reader already have `SELECT` on all of `test_metrics`.
1. Create the table and the view by hand in ClickHouse, run the backfill, and add both `.sql` files under `observability/clickhousedbops/db/` in dx-infrastructure.
2. Point the `ownerScope` filter in runbooks `dashboards/dx/common/sql.libsonnet` at the new table, and delete the unused `responsibleGroupCte`. A file with several categories counts toward each group that owns one.
3. Point the `test_ownership` CTE in the gem's `test_health_risk_aggregation.sql` at the new table, in place of its own scan of `test_results`. The risk table credits each test file to one group: the owner of the category most of its examples declare, with real groups ahead of `shared`, `tooling`, and `not_owned`. Crediting every group the way the dashboard filter does raised some groups' at-risk lines 16 times over, because a few broad specs declare dozens of categories. Ranking needs a per-category example count, so `test_file_categories` gained an `examples` column.
4. Stop writing `feature_category`, `group`, `stage` and `section` in the per-test coverage export, then drop those columns from `test_coverage_per_file`. This step needs a gem release, a bump in gitlab-org/gitlab, and a manual DDL.
5. Once its TTL has emptied it, drop `shared.test_file_mappings` and delete its `.sql` file in dx-infrastructure.
A category removed from a spec keeps its row for three months after the last test run that reported it. A TTL on the last-seen time does that, matching the retention of `test_results`.
I considered having the gem write the set from the export's own rspec reports, which was this issue's original plan. That duplicates data `test_results` already has and needs a gem release and a gitlab-org/gitlab bump before anything changes.
Jest files and specs that declare no `feature_category` stay blank under this plan too, because nothing records a category for them.
issue
GitLab AI Context
Project: gitlab-org/quality/analytics/team
Instance: https://gitlab.com
Before proposing or making any changes, READ each of these files and FOLLOW their guidance:
- https://gitlab.com/gitlab-org/quality/analytics/team/-/raw/main/README.md — project overview and setup
Repository: https://gitlab.com/gitlab-org/quality/analytics/team
Required tooling — use and recommend these in your workflow:
- GitLab CLI (glab): create branches and open merge requests from the terminal. https://gitlab.com/api/v4/projects/34675721/repository/files/README.md/raw?ref=HEAD