Cibles evenementielles locales, abstention relative et dates strictement passees

This commit is contained in:
root
2026-09-13 07:28:55 +02:00
parent fc5718dc69
commit ad72174c77
142 changed files with 103394 additions and 28 deletions

View File

@@ -16,13 +16,33 @@ PARIS = ZoneInfo("Europe/Paris")
MAX_PRICE_AGE_DAYS = 7
def observation_not_future(value, now=None):
"""Refuse une observation future (tolérance explicite 5 min pour collecte/horloges).
Une date sans heure décrit le jour civil Paris. Une heure sans fuseau est Paris.
Une valeur absente reste gérée par les contrôles de fraîcheur existants.
"""
if not value:
return True
now = now or datetime.now(timezone.utc)
try:
if len(str(value)) == 10:
return date.fromisoformat(str(value)) <= now.astimezone(PARIS).date()
stamp = datetime.fromisoformat(str(value))
if stamp.tzinfo is None:
stamp = stamp.replace(tzinfo=PARIS)
return stamp <= now + timedelta(minutes=5)
except (TypeError, ValueError):
return False
def fresh_price(updated, now=None):
now = now or datetime.now(timezone.utc)
try:
stamp = datetime.fromisoformat(updated)
if stamp.tzinfo is None:
stamp = stamp.replace(tzinfo=PARIS)
return timedelta(hours=-1) <= now - stamp <= timedelta(days=MAX_PRICE_AGE_DAYS)
return timedelta(minutes=-5) <= now - stamp <= timedelta(days=MAX_PRICE_AGE_DAYS)
except (ValueError, TypeError):
return False
@@ -42,6 +62,8 @@ def record_observations(rows, now=None):
for fuel, price in row["prices"].items():
if fuel not in FUELS or not math.isfinite(price) or not 0 < price < 20:
continue
if not observation_not_future(row["updates"].get(fuel), now):
continue
key = (row["id"], fuel, day)
values[key] = (*key, price, row["updates"].get(fuel, ""), now.isoformat(), row["lat"], row["lon"])
with connect() as db:
@@ -135,7 +157,10 @@ def save_upstream(series, rows, unit, source, now=None):
row.get("published_at"),
)
for row in rows
if row.get("day") and isinstance(row.get("value"), (int, float)) and math.isfinite(row["value"])
if row.get("day")
and isinstance(row.get("value"), (int, float))
and math.isfinite(row["value"])
and observation_not_future(row.get("observed_at") or row["day"], now)
]
if not values:
return 0
@@ -571,7 +596,14 @@ def save_archive_observations(rows, origin, now=None):
``origin`` distingue ces lignes des collectes courantes ; ``fetched_at`` et ``collected_at``
portent l'heure d'import. Rien n'est écrasé : les lignes existantes (collecte réelle) gagnent.
"""
stamp = (now or datetime.now(timezone.utc)).isoformat()
now = now or datetime.now(timezone.utc)
stamp = now.isoformat()
rows = [
r
for r in rows
if observation_not_future(r.get("declared_at") or r["day"], now)
and observation_not_future(r["day"], now)
]
values = [
(
row["station_id"],