# CLAUDE.md This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. ## Project shortstats tracks YouTube Shorts statistics from 1 hour to 3 years after publication (full snapshot at 7 days) into PostgreSQL, and serves a private web site ("Shorts Stats") for exploring them with Vega-Lite charts. **Everything is in French**: code comments, docstrings, log/error messages, UI strings, docs, commit history. Keep new text in French. `docs/01-documentation-technique.md` is the authoritative design document (architecture, schema, roles, chart rules, and the rationale for each choice); read the relevant section before changing behavior. `docs/02-installation.md`, `docs/03-utilisation.md` (also summarized in the site's "Aide" page), `docs/04-sql-minimal.md` cover deployment, usage and the 4 bootstrap SQL commands. ## Commands ```sh pip install --constraint constraints.txt -e ".[collector,web,dev]" # Python >= 3.12 (images use 3.13) npm ci && npm run build # bundles Vega into src/shortstats/web/static/vendor/vega-bundle.js (needed to render charts locally) pytest -q # all tests pytest tests/test_charts.py -q # one file pytest tests/test_v132.py -k highlight # one test / pattern ``` Tests that touch PostgreSQL are **skipped** unless `TEST_OWNER_URL`, `TEST_COLLECTOR_URL`, `TEST_WEB_URL` are set, pointing at a database created with `sql/bootstrap.sql` (roles `shortstats_owner` / `shortstats_collector` / `shortstats_web`, passwords `remplacer-mdp-*`). The session fixture in `tests/conftest.py` runs the migrations itself; `clean_db` truncates all tables between tests. See `.forgejo/workflows/build.yaml` for the exact CI setup (private Postgres on a Unix socket, SCRAM auth). The YouTube API is faked with `tests/fake_youtube.py` (httpx `MockTransport`). CLI (`shortstats.cli:main`): `migrate`, `collect`, `watch`, `hash-password `, `web`. Configuration is environment-only (`config.py`); every variable also accepts a `NAME_FILE` variant (Docker secrets). Images: `docker build --target collector|web .` (one Dockerfile, multi-stage; the Vega bundle is built in a Node stage). CI (Forgejo) tests, then builds and pushes both images on `main` and `v*` tags. ## Architecture Two independent processes from one Python package, communicating **only through PostgreSQL**: - **Collector** (`collector.py`, `youtube.py`, extra `[collector]`): run periodically by supercronic (`docker/collector-entrypoint.sh`) plus a `watch` process that `LISTEN`s for "Lancer une collecte" requests inserted by the site (`collect_request` table). Discovers Shorts via each channel's `UUSH…` playlist, takes snapshots on a 151-step schedule (hourly on day 1 … monthly to 3 years, most urgent first), reads all comments within a quota cap, and classifies them into themes. - **Web** (`web/`, extra `[web]`): FastAPI + Jinja2 + uvicorn (`web.app:create_app` factory). `data.py` holds all SQL (`Repo`); `charts.py` is the chart catalog, the "auto" type selection, and Vega-Lite spec generation; `evolution.py`, `multi.py` (multiple Y / two axes), `highlight.py`, `legends.py`, `transpose.py`, `marks.py` each layer one feature onto the spec; `transfer.py` handles chart export/import formats. Accounts come from a `users.txt` file (Argon2id, `auth.py`), with per-user password overrides in the DB. - **Live updates**: collector and site emit `NOTIFY shortstats_events` (inside the transaction); `web/live.py` relays only the event *type* over the `/ws` WebSocket; browsers then re-fetch the page zone (`data-live-on`) or chart through normal authenticated routes. No data travels over the WebSocket. - **Themes**: users write PCRE2/regex101-style patterns; `regex_adapter.py` translates them to PostgreSQL ARE (`pattern_sources` keeps the input, `patterns` the translation; classification only reads `patterns`). Unsupported constructs are refused, not approximated. `comment_themes.py` is shared by collector and site (adding a theme recomputes the whole history). ### Database and migrations - Schema `shortstats`. Migrations are numbered SQL files in `src/shortstats/migrations/NNN_name.sql`, applied once each in its own transaction, tracked in `schema_migration`. `{{TIMEZONE}}`, `{{DATABASE}}`, `{{COLLECTOR_ROLE}}`, `{{WEB_ROLE}}` placeholders are substituted by `migrate.py`. Never edit an applied migration; add a new file. - `grants.sql` is reapplied on every `migrate` and defines least-privilege rights per role. **Any new table/view/column the collector or site needs must be granted there**; tests verify each role can do what it should and nothing else. The site never runs migrations (it doesn't hold owner rights). - The 151-step snapshot schedule exists both in Python and in the DB (migration 008); tests check they stay identical. ### Invariants enforced by tests - `deploy/compose.yaml` and `deploy/.env.example` must be copied **verbatim** into fenced blocks of `docs/02-installation.md` (`tests/test_deploy_docs.py`). The compose must not contain `build:` and may only mount `./users.txt`. - Saved/exported/shared chart configs from older versions must keep working: old keys are remapped (e.g. `remap_legacy_config` in `charts.py`, step → age in 1.3.2, transfer format 1 → 2). ## Releases Version lives in `pyproject.toml` and `src/shortstats/__init__.py`; the README quick-start, `CHANGELOG.md` (French release notes, newest first) and the docs also mention the current version. Tests for each release's features go in `tests/test_vXYZ.py`, and the docs' test table (section 12 of the technical doc) lists them. Releases are tagged `vX.Y.Z`.