feat: unified architecture and webcam archive optimization

- Refactor backend into a single process (FastAPI + APScheduler + Telegram Bot)
- Implement 'current_forecasts' and 'past_forecasts' architecture in storage
- Optimize historical webcam collector with date range discovery for Pajares, San Isidro, and Fuentes
- Clean up legacy scripts and tables (prediction_runs, prediction_stations)
- Update tests and operations documentation
This commit is contained in:
dasemu committed 2026-03-13 11:56:59 +01:00
1 parent a70eff3e45
commit f6a65f41d9
14 files changed
+563 -660

No files matched your search

+43 -56
View File
@@ -5,70 +5,57 @@ from __future__ import annotations
from storage import SQLiteStore
def test_prediction_run_roundtrip(tmp_path):
"""Prediction payloads must round-trip through SQLite unchanged."""
def test_current_forecasts_roundtrip(tmp_path):
"""Forecasts must round-trip through current_forecasts table."""
store = SQLiteStore(tmp_path / "skipredictor.db")
payload = {
"generated_at": "2026-03-13T10:00:00+00:00",
"prediction_date": "2026-03-14",
"stations": {
"pajares": {
"station_name": "Valgrande-Pajares",
"open_probability": 0.82,
"km_open": 12.4,
"crowd_level": "medium",
"weather_summary": "Temp -2-5 C",
"rule_overrides": ["Wind below threshold."],
"prediction_source": "ml",
}
},
}
forecasts = [
{
"station_id": "pajares",
"prediction_date": "2026-03-14",
"open_probability": 0.82,
"km_open": 12.4,
"crowd_level": "medium",
"weather_summary": "Temp -2-5 C",
"rule_notes": ["Wind below threshold."],
"prediction_source": "ml",
}
]
store.save_prediction_run(payload)
store.save_current_forecasts(forecasts, run_hour=10)
loaded = store.load_current_forecasts()
loaded = store.load_latest_predictions()
assert loaded == payload
assert len(loaded) == 1
row = loaded[0]
assert row["station_id"] == "pajares"
assert row["prediction_date"] == "2026-03-14"
assert row["open_probability"] == 0.82
assert row["km_open"] == 12.4
def test_load_predictions_for_date_ignores_newer_other_dates(tmp_path):
"""Date-specific loads must not be displaced by a newer run for tomorrow."""
def test_archive_moves_to_past_forecasts(tmp_path):
"""Archiving must move rows from current to past_forecasts."""
store = SQLiteStore(tmp_path / "skipredictor.db")
today_payload = {
"generated_at": "2026-03-13T06:00:00+00:00",
"prediction_date": "2026-03-13",
"stations": {
"pajares": {
"station_name": "Valgrande-Pajares",
"open_probability": 0.79,
"km_open": 10.1,
"crowd_level": "low",
"weather_summary": "Temp 0-5 C",
"rule_overrides": [],
"prediction_source": "ml",
}
},
}
tomorrow_payload = {
"generated_at": "2026-03-13T07:00:00+00:00",
"prediction_date": "2026-03-14",
"stations": {
"pajares": {
"station_name": "Valgrande-Pajares",
"open_probability": 0.88,
"km_open": 7.4,
"crowd_level": "medium",
"weather_summary": "Temp 2-7 C",
"rule_overrides": ["Warm rain expected."],
"prediction_source": "hybrid",
}
},
}
forecasts = [
{
"station_id": "pajares",
"prediction_date": "2026-03-14",
"open_probability": 0.79,
"km_open": 10.1,
"crowd_level": "low",
"weather_summary": "Temp 0-5 C",
"rule_notes": [],
"prediction_source": "ml",
}
]
store.save_prediction_run(today_payload)
store.save_prediction_run(tomorrow_payload)
store.save_current_forecasts(forecasts, run_hour=5)
archived = store.archive_current_forecasts()
loaded = store.load_predictions_for_date(prediction_date="2026-03-13")
assert loaded == today_payload
assert archived == 1
assert store.load_current_forecasts() == []
past = store.load_past_forecasts()
assert len(past) == 1
assert past[0]["station_id"] == "pajares"
def test_feature_importances_replace_latest_values(tmp_path):
+54 -31
View File
@@ -70,31 +70,53 @@ from bot import telegram_bot
class FakeStore:
"""In-memory stand-in for SQLiteStore used in selection tests."""
"""In-memory stand-in for SQLiteStore used in selection tests.
def __init__(self, by_date: dict[str, dict], latest: dict | None = None) -> None:
self.by_date = by_date
self.latest = latest
Exposes ``load_current_forecasts()`` which returns flat forecast rows
matching the ``current_forecasts`` table schema.
"""
def __init__(
self,
rows: list[dict] | None = None,
) -> None:
self.rows = rows or []
self.db_path = "/tmp/fake.db"
self.requested_dates: list[str] = []
def load_predictions_for_date(self, *, prediction_date: str, run_kind: str):
self.requested_dates.append(prediction_date)
return self.by_date.get(prediction_date)
def load_current_forecasts(self, station_id=None):
if station_id is not None:
return [r for r in self.rows if r["station_id"] == station_id]
return list(self.rows)
def load_latest_predictions(self, *, run_kind: str):
return self.latest
def _make_forecast_row(
station_id: str,
prediction_date: str,
open_probability: float = 0.5,
) -> dict:
"""Build a minimal forecast row matching the current_forecasts schema."""
return {
"id": 1,
"generated_at": "2026-03-13T10:00:00+00:00",
"run_hour": 10,
"station_id": station_id,
"prediction_date": prediction_date,
"open_probability": open_probability,
"km_open": 10.0,
"crowd_level": "medium",
"weather_summary": None,
"rule_notes_json": "[]",
"prediction_source": "hybrid",
}
def test_load_predictions_prefers_today_before_ten():
"""Before 10:00 local time the bot should prefer today's run."""
today_payload = {"prediction_date": "2026-03-13", "stations": {}}
tomorrow_payload = {"prediction_date": "2026-03-14", "stations": {}}
store = FakeStore(
{
"2026-03-13": today_payload,
"2026-03-14": tomorrow_payload,
}
rows=[
_make_forecast_row("pajares", "2026-03-13"),
_make_forecast_row("pajares", "2026-03-14"),
]
)
loaded = telegram_bot.load_predictions(
@@ -102,19 +124,17 @@ def test_load_predictions_prefers_today_before_ten():
store=store,
)
assert loaded == today_payload
assert store.requested_dates == ["2026-03-13"]
assert loaded is not None
assert loaded["prediction_date"] == "2026-03-13"
def test_load_predictions_prefers_tomorrow_after_ten():
"""From 10:00 onwards the bot should prefer tomorrow's run."""
today_payload = {"prediction_date": "2026-03-13", "stations": {}}
tomorrow_payload = {"prediction_date": "2026-03-14", "stations": {}}
store = FakeStore(
{
"2026-03-13": today_payload,
"2026-03-14": tomorrow_payload,
}
rows=[
_make_forecast_row("pajares", "2026-03-13"),
_make_forecast_row("pajares", "2026-03-14"),
]
)
loaded = telegram_bot.load_predictions(
@@ -122,22 +142,25 @@ def test_load_predictions_prefers_tomorrow_after_ten():
store=store,
)
assert loaded == tomorrow_payload
assert store.requested_dates == ["2026-03-14"]
assert loaded is not None
assert loaded["prediction_date"] == "2026-03-14"
def test_load_predictions_falls_back_when_preferred_dates_are_missing():
"""If the preferred dates are absent, fall back to the latest published run."""
fallback_payload = {"prediction_date": "2026-03-16", "stations": {}}
store = FakeStore({}, latest=fallback_payload)
"""If the preferred dates are absent, fall back to whatever is available."""
store = FakeStore(
rows=[
_make_forecast_row("pajares", "2026-03-16"),
]
)
loaded = telegram_bot.load_predictions(
now=datetime(2026, 3, 13, 8, 0, tzinfo=ZoneInfo("Europe/Madrid")),
store=store,
)
assert loaded == fallback_payload
assert store.requested_dates == ["2026-03-13", "2026-03-14"]
assert loaded is not None
assert loaded["prediction_date"] == "2026-03-16"
def test_prediction_dates_window_returns_consecutive_dates():