Skip to content

feat(avalanche-bulletins): harvest and store the bulletins, history by day - #2733

Open
eddy-geek wants to merge 11 commits into
masterfrom
feat/avalanche-harvester
Open

eddy-geek wants to merge 11 commits into
masterfrom
feat/avalanche-harvester

Conversation

@eddy-geek

Copy link
Copy Markdown
Contributor

The avalanche bulletins are harvested in the background and stored, instead of being fetched when a user asks. A small, generic harvester framework does the fetching, for the weather stations, partner sites and webcams to come. Closes #2682, closes #2686; the outing part of #2685 is UI-only for now (see below). Tracking: c2corg/c2c_ui#5174.

The design was chosen from alternatives (scheduling: a cron per provider, this framework, or a task queue; data model: as in #2682, shared harvest tables, or whole files). Details are in docs/harvest.md.

Harvesting (c2corg_api/harvest/)

  • The scheduler container runs one harvest_tick every minute. It claims the due sources (harvest_sources.next_run_at) with FOR UPDATE SKIP LOCKED and fetches them in 4 threads, in lanes by host (at most 2 requests at once per service).
  • Conditional GET (ETag, Last-Modified) plus a SHA-256 of the content: an unchanged file costs a 304 and writes nothing. SLF sends 2.4 MB per language.
  • Each harvester has a Policy:
    • its season;
    • its publication windows (Europe/Paris);
    • hourly polling after a publication, until the window ends;
    • a backoff after errors (15 min to 4 h), and jitter.
  • State and health per source are kept in Postgres and survive restarts. /health gets a harvest section, which never fails the check.
  • Sources that disappear are disabled, then deleted after 30 days. Raw payloads are gzipped and kept 30 days, with the retention set per harvester.
  • Settings: harvest.enabled, and harvest.<name>.enabled per harvester. Everything is off by default.
  • requests_per_minute: Météo-France is held at 40 a minute, leaving room under its quota of 50 for the PDFs fetched on demand.

Avalanche storage

Table Content Kept
avalanche_bulletins the CAAML of each bulletin × language (c2c languages) × publication; region_keys (a region and the regions holding it) with a GIN index forever. Superseded same-day publications are pruned after 7 days; after 18 months the bulky customData is removed and the texts kept (store.COMPACTED)
avalanche_bulletin_regions the danger of each region and day: max rating, low/high, elevation limit, all bands, problems, aspects forever
region_weather_hours / _periods the SLF weather series (temperature, snowfall limit, wind, new snow), moved out of the CAAML: the same in every language and repeated over 3 days forever (~400 kB a day)
harvest_sources, harvest_payloads shared by every harvester payloads: 30 days

Measured on the dev DB: about 2.3 KB per stored SLF bulletin (4.6 KB with its weather), 3.3 KB per French one; about 0.4 GB a season.

Read path

  • GET /avalanche-bulletins/regions/{region_id}/latest reads Postgres, with Redis in front, while the stored day is still valid. Otherwise it fetches on demand as before (new region, harvest off, a service late). The answer has the same shape either way, and the existing snapshots pass unchanged.
  • New: GET /avalanche-bulletins/regions/{region_id}/{YYYY-MM-DD} returns the bulletins of a day. A day that is over is cached for good, with immutable headers; its cache key includes updated_at, so a later correction still shows.
  • New: GET /avalanche-bulletins/regions/{region_id}/ratings?from=&to= returns the danger per day (7 days by default, 366 at most), including sub-regions (CH-4).
  • The PDF stays on demand.

Not in this PR

  • Outings (Avalanche bulletins: danger of the day on routes and outings #2685): the API cannot find an outing's EAWS regions yet, since it has no EAWS polygons. The outing box is UI-only for now (c2c_ui PR linked below) and calls the date endpoint. /outings/{id}/avalanche-bulletins will come with the EAWS micro-regions (Avalanche bulletins: EAWS micro-regions and danger summary for the map #2684).
  • Architecture diagram: docs/architecture/src/backend.json is updated, but backend.html and backend.png are not re-rendered, because Archify isn't available in the session. Someone with Archify needs to run the finalize command from docs/README.md before merging. Its meta.repository.revision should also be bumped after merge, so the source links to the new files work.
  • Deployment: the METEOFRANCE_API_KEY must reach the scheduler container. Then turn on harvest_enabled (c2corg/v6-docker-compose).
  • Backfill of past seasons from the EAWS feed (Avalanche bulletins: history per region #2686 mentions it): not done.

Tests

  • 2797 passed, 1 xfailed (make test in the dev container); ruff is clean.
  • The migration goes up, down and up again on the dev DB, with no autogenerate difference.
  • New tests:
    • the policy (fixed dates, DST changes, season edges, backoff);
    • the runner (claims and leases, 304, errors, sync and disable);
    • the store (the danger summary with 1, 2 and 3 elevation bands, upserts, corrections at the same publication time, pruning, compaction keeping the French texts);
    • the weather tables;
    • contract tests for the new endpoints and the stored /latest.
  • Live check: the 35 Météo-France massifs, SLF in 4 languages, Valle d'Aosta and Slovenia were harvested into the dev DB. A second run wrote nothing.
  • Two independent code reviews were run on the branch; their findings are fixed.

Generated by Claude Code

The harvester (#2682) sends the same requests, with the
headers of a conditional GET, and parses what it got later.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
A harvester lists its sources (synced into harvest_sources), fetches one
with a conditional GET and stores what changed. harvest_tick, every
minute in the scheduler once harvest.enabled, claims the due sources
(FOR UPDATE SKIP LOCKED, a lease) and runs them in 4 threads, 2 by host.
Each policy says when to come back: a season, publication windows,
after a new publication the next window, a backoff after errors. The
payloads are kept 30 days; /health tells the state of each harvester.

The tables of the avalanche bulletins are created by the same migration
(#2682).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
The BRA of each massif of Météo-France and the latest file of each EAWS
service, in the languages of c2c it publishes, every 15 minutes in the
publication windows, hourly else, from November to June. Each new
publication is stored as CAAML, and the danger of each region of the
day (ratings by elevation, problems, aspects) is kept forever. The
publications superseded the same day are pruned after a week.

store.py reads them back for the next endpoints (#2682).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
- the danger by region keeps all its bands; rating_low/high and the
  bound only with one elevation bound
- region_keys (a region and the regions holding it, SLF by digit) with
  GIN indexes, for the bulletins and the danger of a region; the latest
  bulletins are of the last 3 days
- ratings: by day, the highest rating of the region and its
  sub-regions, and each of them
- a publication is superseded when all its regions are published again
  later the same day (SLF changes its ids and groups)
- a publication changed at the same time is updated
- indexes on the foreign keys; a payload is unique by source, a content
  seen again refreshes it
- after a publication, an hourly poll until the window ends
- store returns Stored(published, state), the state is kept
- the sources are synced every 10 minutes, disabled when gone and
  deleted after 30 days
- no worker waits for a busy host: 2 lanes by host
- harvest.<name>.enabled, keep_payloads_days and requests_per_minute by
  harvester (Météo-France: 40, its key allows 50 a minute)
- the health check counts the enabled sources of the known harvesters
- no Météo-France sources without its key

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
…the danger

/latest reads the database first (Redis in front, its key changing with
each publication), else fetches the bulletins on demand as before: a
region not harvested, or the database failing, answers as today.
/regions/{region_id}/{YYYY-MM-DD} answers the bulletins of a day
(#2686), cached for good once the day is over, and
/regions/{region_id}/ratings the danger of the region and its
sub-regions day by day, the last 7 days by default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
SLF gives the weather of each micro-region with its bulletins
(customData.CH.weather: 13 of the 17 kB of a bulletin, the same in each
language): temperature and snowfall limit by hour, wind and new snow by
day, for the 2 days before and the forecast day. It goes to
region_weather_hours and region_weather_periods, written once by
publication, the latest winning, and is removed from the stored CAAML
(the bulletin endpoints did not answer it from the database).
weather.weather() reads it back for an endpoint to come.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
A monthly job removes the customData of the bulletins (all the
services, at all levels) whose day is more than 18 months ago: the
texts, ratings, problems, regions, times and ids are kept, and the
danger by region is untouched. compacted_at marks the rows done, a
partial index finds the others.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
- backend.json: the harvest tick of the scheduler, the avalanche services
  (Météo-France DPBRA, EAWS CAAML), the bulletin tables and the read path
  (Redis, then PostgreSQL, else on demand); a card on the harvest.
  backend.html and backend.png are not rendered again yet.
- docs/harvest.md: scheduling and policy, state and health, payloads,
  storage, read path and caching, adding a harvester, operations.
- Linked from docs/README.md and the code layout of the README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
… storage

- compaction keeps the texts (those of Météo-France are in its
  customData): only the paths of store.COMPACTED go (Météo-France
  weather, snow cover, new snow, history; SLF aggregation, danger
  evolution, weather), set-based, committed by batch
- updated_at on the bulletins and the danger by region, in the keys of
  the cache: a correction at the same time shows on a day over too
- the danger of a period is cached as the bulletins of a day
- a weather correction at the same time is stored, the same values of
  the other languages are not rewritten
- the on-demand answer has no SLF weather either, and a region holds
  its sub-regions as in the database (CH-4 holds CH-4231)
- /latest answers the stored day while valid, else fetches on demand,
  with the database session ended first
- an index on the days of the danger by region
- a bad day or date is a 400 in the shape of the API, under its name
- harvest.<name>.enabled in the .ini templates (empty: as
  harvest.enabled)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
… switches

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ct7g9RdZP8G8Nbo8LKKFBH
@codacy-production

Copy link
Copy Markdown

Not up to standards ⛔

🔴 Issues 2 medium

Alerts:
⚠ 2 issues (≤ 0 issues of at least minor severity)

Results:
2 new issues

Category Results
Security 2 medium

View in Codacy

🟢 Metrics 442 complexity · 2 duplication

Metric Results
Complexity 442
Duplication 2

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Avalanche bulletins: history per region Avalanche bulletins: harvest job and storage

2 participants