mirror of
https://github.com/Dasemu/skipredictor.git
synced 2026-11-26 15:26:15 +00:00
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:
1 parent
a70eff3e45
commit
f6a65f41d9
14 files changed
+563
-660
No files matched your search
@@ -48,8 +48,6 @@ CURRENT_FORECASTS_TABLE = "current_forecasts"
|
||||
PAST_FORECASTS_TABLE = "past_forecasts"
|
||||
HISTORICAL_ACTUALS_TABLE = "historical_actuals"
|
||||
|
||||
PUBLISHED_RUN_KIND = "published"
|
||||
AD_HOC_RUN_KIND = "adhoc"
|
||||
|
||||
_WEATHER_COLUMNS: list[str] = [
|
||||
"date",
|
||||
@@ -174,115 +172,6 @@ class SQLiteStore:
|
||||
"""Return the historical station raw dataset."""
|
||||
return self.read_dataframe(HISTORICAL_STATION_TABLE, parse_dates=("date",))
|
||||
|
||||
def save_prediction_run(
|
||||
self,
|
||||
payload: dict[str, Any],
|
||||
*,
|
||||
run_kind: str = PUBLISHED_RUN_KIND,
|
||||
) -> int:
|
||||
"""Persist a prediction payload and return the inserted run id."""
|
||||
generated_at = payload.get("generated_at") or self._now_iso()
|
||||
prediction_date = str(payload["prediction_date"])
|
||||
stations = payload.get("stations", {})
|
||||
|
||||
with self.connect() as conn:
|
||||
cursor = conn.execute(
|
||||
"""
|
||||
INSERT INTO prediction_runs (generated_at, prediction_date, run_kind)
|
||||
VALUES (?, ?, ?)
|
||||
""",
|
||||
(generated_at, prediction_date, run_kind),
|
||||
)
|
||||
run_id = int(cursor.lastrowid)
|
||||
|
||||
rows = []
|
||||
for station_id, prediction in stations.items():
|
||||
rows.append(
|
||||
(
|
||||
run_id,
|
||||
station_id,
|
||||
prediction.get("station_name"),
|
||||
self._coerce_float(prediction.get("open_probability")),
|
||||
self._coerce_float(prediction.get("km_open")),
|
||||
prediction.get("crowd_level"),
|
||||
prediction.get("weather_summary"),
|
||||
json.dumps(
|
||||
prediction.get("rule_overrides") or [],
|
||||
ensure_ascii=False,
|
||||
),
|
||||
prediction.get("prediction_source"),
|
||||
)
|
||||
)
|
||||
|
||||
conn.executemany(
|
||||
"""
|
||||
INSERT INTO prediction_stations (
|
||||
run_id,
|
||||
station_id,
|
||||
station_name,
|
||||
open_probability,
|
||||
km_open,
|
||||
crowd_level,
|
||||
weather_summary,
|
||||
rule_overrides_json,
|
||||
prediction_source
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
rows,
|
||||
)
|
||||
|
||||
logger.info(
|
||||
"Saved prediction run %d (%s, %s) with %d stations.",
|
||||
run_id,
|
||||
run_kind,
|
||||
prediction_date,
|
||||
len(stations),
|
||||
)
|
||||
return run_id
|
||||
|
||||
def load_latest_predictions(
|
||||
self,
|
||||
*,
|
||||
run_kind: str = PUBLISHED_RUN_KIND,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Return the latest stored prediction payload for *run_kind*."""
|
||||
with self.connect() as conn:
|
||||
return self._load_prediction_payload(
|
||||
conn,
|
||||
"""
|
||||
SELECT id, generated_at, prediction_date, run_kind
|
||||
FROM prediction_runs
|
||||
WHERE run_kind = ?
|
||||
ORDER BY generated_at DESC, id DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
(run_kind,),
|
||||
)
|
||||
|
||||
def load_predictions_for_date(
|
||||
self,
|
||||
*,
|
||||
prediction_date: date | str,
|
||||
run_kind: str = PUBLISHED_RUN_KIND,
|
||||
) -> dict[str, Any] | None:
|
||||
"""Return the latest stored prediction payload for a specific date."""
|
||||
if isinstance(prediction_date, date):
|
||||
prediction_date = prediction_date.isoformat()
|
||||
|
||||
with self.connect() as conn:
|
||||
return self._load_prediction_payload(
|
||||
conn,
|
||||
"""
|
||||
SELECT id, generated_at, prediction_date, run_kind
|
||||
FROM prediction_runs
|
||||
WHERE run_kind = ? AND prediction_date = ?
|
||||
ORDER BY generated_at DESC, id DESC
|
||||
LIMIT 1
|
||||
""",
|
||||
(run_kind, str(prediction_date)),
|
||||
)
|
||||
|
||||
def save_training_run(
|
||||
self,
|
||||
*,
|
||||
@@ -724,27 +613,6 @@ class SQLiteStore:
|
||||
with self.connect() as conn:
|
||||
conn.executescript(
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS prediction_runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
generated_at TEXT NOT NULL,
|
||||
prediction_date TEXT NOT NULL,
|
||||
run_kind TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS prediction_stations (
|
||||
run_id INTEGER NOT NULL,
|
||||
station_id TEXT NOT NULL,
|
||||
station_name TEXT,
|
||||
open_probability REAL,
|
||||
km_open REAL,
|
||||
crowd_level TEXT,
|
||||
weather_summary TEXT,
|
||||
rule_overrides_json TEXT,
|
||||
prediction_source TEXT,
|
||||
PRIMARY KEY (run_id, station_id),
|
||||
FOREIGN KEY (run_id) REFERENCES prediction_runs(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS training_runs (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
started_at TEXT NOT NULL,
|
||||
@@ -842,46 +710,6 @@ class SQLiteStore:
|
||||
return Path(db_path)
|
||||
return Path(os.environ.get("SKIPREDICTOR_DB_PATH", DEFAULT_DB_PATH))
|
||||
|
||||
def _load_prediction_payload(
|
||||
self,
|
||||
conn: sqlite3.Connection,
|
||||
run_query: str,
|
||||
params: Sequence[Any],
|
||||
) -> dict[str, Any] | None:
|
||||
"""Load a prediction payload from a prediction-runs query."""
|
||||
run = conn.execute(run_query, tuple(params)).fetchone()
|
||||
if run is None:
|
||||
return None
|
||||
|
||||
station_rows = conn.execute(
|
||||
"""
|
||||
SELECT station_id, station_name, open_probability, km_open,
|
||||
crowd_level, weather_summary, rule_overrides_json,
|
||||
prediction_source
|
||||
FROM prediction_stations
|
||||
WHERE run_id = ?
|
||||
ORDER BY station_id
|
||||
""",
|
||||
(run["id"],),
|
||||
).fetchall()
|
||||
|
||||
payload = {
|
||||
"generated_at": run["generated_at"],
|
||||
"prediction_date": run["prediction_date"],
|
||||
"stations": {},
|
||||
}
|
||||
for row in station_rows:
|
||||
payload["stations"][row["station_id"]] = {
|
||||
"station_name": row["station_name"],
|
||||
"open_probability": row["open_probability"],
|
||||
"km_open": row["km_open"],
|
||||
"crowd_level": row["crowd_level"],
|
||||
"weather_summary": row["weather_summary"],
|
||||
"rule_overrides": json.loads(row["rule_overrides_json"] or "[]"),
|
||||
"prediction_source": row["prediction_source"],
|
||||
}
|
||||
return payload
|
||||
|
||||
@staticmethod
|
||||
def _normalise_dataframe(df: pd.DataFrame) -> pd.DataFrame:
|
||||
"""Convert unsupported pandas cell types to SQLite-safe values."""
|
||||
|
||||
Reference in new issue
Block a user