feat(api): add FastAPI backend with JWT authentication

REST API serving forecast data with HS256 JWT auth (14-day tokens).
Public: /health, /auth/register, /auth/login.
Protected: /stations, /forecast, /forecast/history, /forecast/{id}, /status.
Includes APScheduler for periodic forecasts and Telegram bot subprocess.
This commit is contained in:
dasemu committed 2026-03-13 09:36:18 +01:00
1 parent c8188e3995
commit 9d97afe538
14 files changed
+1631 -10

No files matched your search

+276
View File
@@ -0,0 +1,276 @@
# SkiPredictor API Specification v1
Base URL: `/api/v1`
All responses use JSON. Dates use ISO 8601 format (`YYYY-MM-DD`). Timestamps use ISO 8601 with timezone (`YYYY-MM-DDTHH:MM:SS+HH:MM`).
---
## Stations
### `GET /api/v1/stations`
List all tracked ski stations with metadata.
**Response 200:**
```json
{
"stations": [
{
"station_id": "pajares",
"name": "Valgrande-Pajares",
"max_km": 22.0,
"latitude": 43.05,
"longitude": -5.77,
"altitude": 1505,
"wind_closure_threshold_kmh": 80,
"min_snow_depth_cm": 30
},
{
"station_id": "sanisidro",
"name": "San Isidro",
"max_km": 31.0,
"latitude": 43.07,
"longitude": -5.37,
"altitude": 1500,
"wind_closure_threshold_kmh": 80,
"min_snow_depth_cm": 30
},
{
"station_id": "fuentes",
"name": "Fuentes de Invierno",
"max_km": 8.6,
"latitude": 43.08,
"longitude": -5.93,
"altitude": 1500,
"wind_closure_threshold_kmh": 80,
"min_snow_depth_cm": 30
}
]
}
```
---
## Forecasts
### `GET /api/v1/forecast`
Get the latest forecast for **all stations**. Returns today + next 7 days per station.
**Response 200:**
```json
{
"generated_at": "2026-03-13T10:00:00+01:00",
"run_hour": 10,
"stations": {
"pajares": {
"station_id": "pajares",
"name": "Valgrande-Pajares",
"days": [
{
"date": "2026-03-13",
"open_probability": 0.85,
"open_label": "Abierta",
"km_open": 18.5,
"max_km": 22.0,
"crowd_level": "medium",
"weather_summary": "Nieve ligera, -2°C, viento 25 km/h",
"rule_notes": [],
"prediction_source": "hybrid"
},
{
"date": "2026-03-14",
"open_probability": 0.72,
"open_label": "Abierta",
"km_open": 15.0,
"max_km": 22.0,
"crowd_level": "high",
"weather_summary": "Soleado, 3°C, viento 15 km/h",
"rule_notes": ["Fin de semana + nieve reciente"],
"prediction_source": "hybrid"
}
]
},
"sanisidro": { "...same structure..." },
"fuentes": { "...same structure..." }
}
}
```
**Notes:**
- `open_label`: `"Abierta"` (>=0.70), `"Probable apertura"` (>=0.40), `"Cerrada"` (<0.40)
- At run hours 15 and 20, `days[0]` is tomorrow (today is omitted)
- At run hours 00, 05, and 10, `days[0]` is today
---
### `GET /api/v1/forecast/{station_id}`
Get the latest forecast for a **single station**. Same structure as above but only one station.
**Path parameters:**
- `station_id`: `pajares` | `sanisidro` | `fuentes`
**Response 200:**
```json
{
"generated_at": "2026-03-13T10:00:00+01:00",
"run_hour": 10,
"station_id": "pajares",
"name": "Valgrande-Pajares",
"days": [
{
"date": "2026-03-13",
"open_probability": 0.85,
"open_label": "Abierta",
"km_open": 18.5,
"max_km": 22.0,
"crowd_level": "medium",
"weather_summary": "Nieve ligera, -2°C, viento 25 km/h",
"rule_notes": [],
"prediction_source": "hybrid"
}
]
}
```
**Response 404:**
```json
{
"detail": "Station 'xyz' not found. Valid: pajares, sanisidro, fuentes"
}
```
---
### `GET /api/v1/forecast/history`
Get past forecast runs (archived when a new run replaces them).
**Query parameters:**
- `station_id` (optional): filter by station
- `date_from` (optional): earliest prediction date (`YYYY-MM-DD`)
- `date_to` (optional): latest prediction date (`YYYY-MM-DD`)
- `limit` (optional, default 50, max 200): number of runs to return
- `offset` (optional, default 0): pagination offset
**Response 200:**
```json
{
"total": 150,
"limit": 50,
"offset": 0,
"runs": [
{
"generated_at": "2026-03-13T05:00:00+01:00",
"run_hour": 5,
"prediction_date": "2026-03-13",
"station_id": "pajares",
"open_probability": 0.85,
"km_open": 18.5,
"crowd_level": "medium",
"prediction_source": "hybrid"
}
]
}
```
---
## System
### `GET /api/v1/health`
Health check endpoint.
**Response 200:**
```json
{
"status": "ok",
"timestamp": "2026-03-13T10:15:00+01:00"
}
```
---
### `GET /api/v1/status`
System status: last forecast run, next scheduled run, model info.
**Response 200:**
```json
{
"last_forecast_run": "2026-03-13T10:00:00+01:00",
"next_forecast_run": "2026-03-13T15:00:00+01:00",
"last_training_run": "2026-03-10T03:00:00+01:00",
"next_training_run": "2026-03-17T03:00:00+01:00",
"models": {
"station_open": "RandomForestClassifier",
"km_open": "GradientBoostingRegressor",
"crowd_level": "LGBMClassifier"
},
"telegram_bot_active": true,
"stations_count": 3,
"database_size_mb": 8.1
}
```
---
## Schedule (internal, not exposed as API)
The backend orchestrates these tasks internally:
| Hour (Europe/Madrid) | Task |
|---|---|
| 00:00 | Forecast run: today + 7 days |
| 05:00 | Forecast run: today + 7 days |
| 10:00 | Forecast run: today + 7 days |
| 15:00 | Forecast run: tomorrow + 6 days (skip today) |
| 20:00 | Forecast run: tomorrow + 6 days (skip today) + scrape today's actual |
| Weekly (Monday 03:00) | ML model retraining |
Each forecast run:
1. Archives current forecasts → `past_forecasts` table
2. Fetches fresh weather data from Open-Meteo
3. Generates new predictions via HybridPredictor
4. Stores in `current_forecasts` table
---
## Database Tables (new/modified)
### `current_forecasts`
Active predictions served by the API.
| Column | Type | Description |
|---|---|---|
| id | INTEGER PK | Auto-increment |
| generated_at | TEXT | When this forecast was created |
| run_hour | INTEGER | Hour of the run (0, 5, 10, 15, 20) |
| station_id | TEXT | pajares/sanisidro/fuentes |
| prediction_date | TEXT | The date being predicted |
| open_probability | REAL | 0.0 - 1.0 |
| km_open | REAL | Predicted km open |
| crowd_level | TEXT | low/medium/high |
| weather_summary | TEXT | Human-readable weather |
| rule_notes_json | TEXT | JSON array of rule notes |
| prediction_source | TEXT | rules/ml/hybrid |
### `past_forecasts`
Archived forecasts (same schema as `current_forecasts`).
### `historical_actuals`
Actual station data scraped from infonieve at 20:00.
| Column | Type | Description |
|---|---|---|
| id | INTEGER PK | Auto-increment |
| date | TEXT | The actual date |
| station_id | TEXT | pajares/sanisidro/fuentes |
| open_status | INTEGER | 0 or 1 |
| km_open | REAL | Actual km open |
| lifts_open | INTEGER | Number of lifts open |
| snow_depth | REAL | Snow depth cm |
| scraped_at | TEXT | When data was scraped |