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

+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():