fix(router): guard OperationalError si tables météo inexistantes
This commit is contained in:
@@ -4,6 +4,7 @@ from typing import Any, Optional
|
|||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, Query
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
||||||
from sqlalchemy import text
|
from sqlalchemy import text
|
||||||
|
from sqlalchemy.exc import OperationalError
|
||||||
from sqlmodel import Session
|
from sqlmodel import Session
|
||||||
|
|
||||||
from app.database import get_session
|
from app.database import get_session
|
||||||
@@ -13,13 +14,16 @@ router = APIRouter(tags=["météo"])
|
|||||||
|
|
||||||
def _station_daily_summary(session: Session, iso_date: str) -> Optional[dict]:
|
def _station_daily_summary(session: Session, iso_date: str) -> Optional[dict]:
|
||||||
"""Agrège les mesures horaires d'une journée en résumé."""
|
"""Agrège les mesures horaires d'une journée en résumé."""
|
||||||
rows = session.exec(
|
try:
|
||||||
text(
|
rows = session.exec(
|
||||||
"SELECT temp_ext, t_min, t_max, pluie_mm, vent_kmh, humidite "
|
text(
|
||||||
"FROM meteostation WHERE substr(date_heure, 1, 10) = :d"
|
"SELECT temp_ext, t_min, t_max, pluie_mm, vent_kmh, humidite "
|
||||||
),
|
"FROM meteostation WHERE substr(date_heure, 1, 10) = :d"
|
||||||
params={"d": iso_date},
|
),
|
||||||
).fetchall()
|
params={"d": iso_date},
|
||||||
|
).fetchall()
|
||||||
|
except OperationalError:
|
||||||
|
return None
|
||||||
|
|
||||||
if not rows:
|
if not rows:
|
||||||
return None
|
return None
|
||||||
@@ -46,12 +50,15 @@ def _station_daily_summary(session: Session, iso_date: str) -> Optional[dict]:
|
|||||||
|
|
||||||
def _station_current_row(session: Session) -> Optional[dict]:
|
def _station_current_row(session: Session) -> Optional[dict]:
|
||||||
"""Dernière mesure station (max 2h d'ancienneté)."""
|
"""Dernière mesure station (max 2h d'ancienneté)."""
|
||||||
row = session.exec(
|
try:
|
||||||
text(
|
row = session.exec(
|
||||||
"SELECT temp_ext, humidite, pression, pluie_mm, vent_kmh, vent_dir, uv, solaire, date_heure "
|
text(
|
||||||
"FROM meteostation WHERE type='current' ORDER BY date_heure DESC LIMIT 1"
|
"SELECT temp_ext, humidite, pression, pluie_mm, vent_kmh, vent_dir, uv, solaire, date_heure "
|
||||||
)
|
"FROM meteostation WHERE type='current' ORDER BY date_heure DESC LIMIT 1"
|
||||||
).fetchone()
|
)
|
||||||
|
).fetchone()
|
||||||
|
except OperationalError:
|
||||||
|
return None
|
||||||
|
|
||||||
if not row:
|
if not row:
|
||||||
return None
|
return None
|
||||||
@@ -64,13 +71,16 @@ def _station_current_row(session: Session) -> Optional[dict]:
|
|||||||
|
|
||||||
|
|
||||||
def _open_meteo_day(session: Session, iso_date: str) -> Optional[dict]:
|
def _open_meteo_day(session: Session, iso_date: str) -> Optional[dict]:
|
||||||
row = session.exec(
|
try:
|
||||||
text(
|
row = session.exec(
|
||||||
"SELECT t_min, t_max, pluie_mm, vent_kmh, wmo, label, humidite_moy, sol_0cm, etp_mm "
|
text(
|
||||||
"FROM meteoopenmeteo WHERE date = :d"
|
"SELECT t_min, t_max, pluie_mm, vent_kmh, wmo, label, humidite_moy, sol_0cm, etp_mm "
|
||||||
),
|
"FROM meteoopenmeteo WHERE date = :d"
|
||||||
params={"d": iso_date},
|
),
|
||||||
).fetchone()
|
params={"d": iso_date},
|
||||||
|
).fetchone()
|
||||||
|
except OperationalError:
|
||||||
|
return None
|
||||||
|
|
||||||
if not row:
|
if not row:
|
||||||
return None
|
return None
|
||||||
|
|||||||
Reference in New Issue
Block a user