Module Infoclimat economie de requetes
Requetes groupees par lots de 25, rythme mesure par station, passage principal /10 min seulement si une station est due, rattrapage cible des stations ponctuelles, fenetre courte, quota commun + plafond site 1000/jour et 24 h glissantes, alerte 80 % sans envoi reel, pause 15 min. Tests hors ligne sur reponses fabriquees. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
34c1bf6736
révision
017dfd9728
6 fichiers modifiés
+699
Aucun fichier ne correspond à votre recherche
@@ -0,0 +1,101 @@
|
|||||||
|
{
|
||||||
|
"_note": "Stations Infoclimat suivies (phase 1, 24 stations). ROMMA : toutes les stations ouvertes sur la clé (global=1).",
|
||||||
|
"infoclimat": {
|
||||||
|
"000T0": {
|
||||||
|
"nom": "Lans-en-Vercors – Les Bruyères",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"000GS": {
|
||||||
|
"nom": "Lans-en-Vercors – Les Allières",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"000HT": {
|
||||||
|
"nom": "Engins",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"000UF": {
|
||||||
|
"nom": "La Chapelle-en-Vercors – Combe-de-l'Oscence",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"000QU": {
|
||||||
|
"nom": "Brion",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"000PD": {
|
||||||
|
"nom": "Die",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"000OS": {
|
||||||
|
"nom": "Saint-Julien-en-Quint",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"STATIC0479": {
|
||||||
|
"nom": "Crête de la Moucherolle",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"STATIC0213": {
|
||||||
|
"nom": "Combe de Darbounouse",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"000IA": {
|
||||||
|
"nom": "Méaudre",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"000IL": {
|
||||||
|
"nom": "Autrans (Gêve)",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"00014": {
|
||||||
|
"nom": "Saint-Martin-d'Hères",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"ME102": {
|
||||||
|
"nom": "Vizille – Collège Les Mattons",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"000SD": {
|
||||||
|
"nom": "Theys (850 m)",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"STATIC0342": {
|
||||||
|
"nom": "Lumbin",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"STATIC0216": {
|
||||||
|
"nom": "Saint-Pancrasse",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"000BN": {
|
||||||
|
"nom": "Voiron – Centre-ville",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"STATIC0247": {
|
||||||
|
"nom": "Saint-Christophe-sur-Guiers",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"00094": {
|
||||||
|
"nom": "Miribel-les-Échelles",
|
||||||
|
"licence": "CC BY"
|
||||||
|
},
|
||||||
|
"STATIC0238": {
|
||||||
|
"nom": "Rochechinard",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"000O9": {
|
||||||
|
"nom": "Recoubeau-Jansac",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"000ZK": {
|
||||||
|
"nom": "Châtillon-en-Diois – Cirque d'Archiane",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"STATIC0203": {
|
||||||
|
"nom": "Marignac-en-Diois",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
},
|
||||||
|
"STATIC0323": {
|
||||||
|
"nom": "Solaure-en-Diois",
|
||||||
|
"licence": "CC BY-NC"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,291 @@
|
|||||||
|
"""Source Infoclimat (open data v2), d'après ~/meteo-direct/direct.py et les règles d'économie du §2.2.
|
||||||
|
|
||||||
|
1. Requêtes groupées : toutes les stations dues dans une seule requête (`stations[]=` répété), lots de 25.
|
||||||
|
2. Rythme par station *mesuré* dans la base (pas réel 10/15/20 min) ; due à dernière + pas + délai de publication.
|
||||||
|
3. Un passage principal toutes les 10 min (hh:x4), seulement si au moins une station est due.
|
||||||
|
4. Rattrapage ciblé (hh:x9) : seulement les stations ponctuelles manquées au passage principal.
|
||||||
|
5. Fenêtre courte : start=end=aujourd'hui UTC ; la veille seulement au premier passage après minuit UTC.
|
||||||
|
6. Cache serveur : les visiteurs lisent les fichiers publiés, jamais l'API.
|
||||||
|
|
||||||
|
Garde-fous : verrou (ordonnanceur), statut vérifié même en HTTP 200, pause 15 min sur toute erreur,
|
||||||
|
quota commun ~/gws-collecte/quota.json (plafond 4999, jour de Paris) + plafond propre au site (1000/jour,
|
||||||
|
compté sur le jour de Paris ET sur 24 h glissantes), alerte à 80 % (envoi réel désactivé par défaut).
|
||||||
|
Le jeton est lu au moment de l'appel, n'est jamais écrit, et est masqué dans tout message.
|
||||||
|
"""
|
||||||
|
import datetime as dt
|
||||||
|
import gzip
|
||||||
|
import json
|
||||||
|
import statistics
|
||||||
|
import urllib.parse
|
||||||
|
import urllib.request
|
||||||
|
|
||||||
|
from .commun import UTC, Pause, ecrire_json, fmt_utc, jour_paris, lire_json, lire_secret, lire_utc, scrub
|
||||||
|
|
||||||
|
SOURCE = "infoclimat"
|
||||||
|
URL_BASE = "https://www.infoclimat.fr/opendata/"
|
||||||
|
UA = "weather.gastinel.me/0.1 (meteo-collecte)"
|
||||||
|
DELAI_PUBLICATION_S = 120 # mesuré le 02/10 : ~2 min entre l'instant de mesure et la mise en ligne
|
||||||
|
PAS_DEFAUT_S = 600
|
||||||
|
PAS_MIN_S, PAS_MAX_S = 600, 3600
|
||||||
|
RETARD_PONCTUEL_S = 9 * 60 # [À VÉRIFIER] seuil « habituellement ponctuelle » (médiane réception - mesure)
|
||||||
|
ECART_MIN_PRINCIPAL_S = 8 * 60
|
||||||
|
|
||||||
|
# Champs Infoclimat -> colonnes du stockage (pluie_cumul_0h : cumul depuis 0 h, [À VÉRIFIER] UTC ou Paris)
|
||||||
|
CORRESPONDANCE = {"temperature": "temperature", "humidite": "humidite", "point_de_rosee": "point_de_rosee",
|
||||||
|
"pression": "pression", "vent_moyen": "vent_moyen", "vent_rafales": "vent_rafales",
|
||||||
|
"vent_direction": "vent_direction", "pluie_1h": "pluie_1h", "pluie_cumul_0h": "pluie_cumul"}
|
||||||
|
|
||||||
|
|
||||||
|
class ErreurInfoclimat(Exception):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Quota
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
class Quota:
|
||||||
|
"""Deux compteurs : le commun (même format que direct.py et le collecteur GWS) et celui du site."""
|
||||||
|
|
||||||
|
def __init__(self, cfg, journal, envoyer_alerte=None):
|
||||||
|
self.cfg = cfg
|
||||||
|
self.journal = journal
|
||||||
|
self.fichier_site = cfg.base / "var" / "quota_site.json"
|
||||||
|
self.envoyer_alerte = envoyer_alerte or (lambda texte: envoyer_ntfy(cfg, texte))
|
||||||
|
|
||||||
|
def _commun(self, maintenant):
|
||||||
|
j = jour_paris(maintenant)
|
||||||
|
q = lire_json(self.cfg.quota_commun, {})
|
||||||
|
if q.get("date") != j:
|
||||||
|
q = {"date": j, "requetes": 0}
|
||||||
|
return q
|
||||||
|
|
||||||
|
def _site(self, maintenant):
|
||||||
|
j = jour_paris(maintenant)
|
||||||
|
s = lire_json(self.fichier_site, {})
|
||||||
|
if s.get("date") != j:
|
||||||
|
s = {"date": j, "requetes": 0, "alerte_le": s.get("alerte_le"), "horodatages": s.get("horodatages", [])}
|
||||||
|
limite = maintenant.timestamp() - 86400
|
||||||
|
s["horodatages"] = [t for t in s.get("horodatages", []) if t > limite]
|
||||||
|
return s
|
||||||
|
|
||||||
|
def etat(self, maintenant):
|
||||||
|
c, s = self._commun(maintenant), self._site(maintenant)
|
||||||
|
return {"commun_jour": c["requetes"], "plafond_commun": self.cfg.plafond_commun,
|
||||||
|
"site_jour": s["requetes"], "site_24h": len(s["horodatages"]), "plafond_site": self.cfg.plafond_site}
|
||||||
|
|
||||||
|
def peut(self, n, maintenant):
|
||||||
|
e = self.etat(maintenant)
|
||||||
|
return (e["commun_jour"] + n <= self.cfg.plafond_commun and e["site_jour"] + n <= self.cfg.plafond_site
|
||||||
|
and e["site_24h"] + n <= self.cfg.plafond_site)
|
||||||
|
|
||||||
|
def noter(self, n, maintenant):
|
||||||
|
c = self._commun(maintenant)
|
||||||
|
c["requetes"] += n
|
||||||
|
ecrire_json(self.cfg.quota_commun, c, compact=False)
|
||||||
|
s = self._site(maintenant)
|
||||||
|
s["requetes"] += n
|
||||||
|
s["horodatages"].extend([maintenant.timestamp()] * n)
|
||||||
|
usage = max(s["requetes"], len(s["horodatages"]))
|
||||||
|
if usage >= self.cfg.seuil_alerte * self.cfg.plafond_site and s.get("alerte_le") != s["date"]:
|
||||||
|
s["alerte_le"] = s["date"]
|
||||||
|
texte = "Infoclimat : %d requêtes du site (jour %d, 24 h %d) sur un plafond de %d" % (
|
||||||
|
usage, s["requetes"], len(s["horodatages"]), self.cfg.plafond_site)
|
||||||
|
self.journal("ALERTE quota " + texte, maintenant)
|
||||||
|
try:
|
||||||
|
self.envoyer_alerte(texte)
|
||||||
|
except Exception as e: # une alerte ratée ne bloque jamais la collecte
|
||||||
|
self.journal("alerte non envoyée : %s" % str(e)[:200], maintenant)
|
||||||
|
ecrire_json(self.fichier_site, s)
|
||||||
|
return c["requetes"]
|
||||||
|
|
||||||
|
|
||||||
|
def envoyer_ntfy(cfg, texte):
|
||||||
|
"""Prévu mais inactif : rien n'est envoyé tant que MC_ALERTE_ACTIVE n'est pas à 1 et MC_NTFY_URL vide."""
|
||||||
|
if not (cfg.alerte_active and cfg.ntfy_url):
|
||||||
|
return False
|
||||||
|
req = urllib.request.Request(cfg.ntfy_url, data=texte.encode("utf-8"), headers={"Title": "meteo-collecte"})
|
||||||
|
urllib.request.urlopen(req, timeout=20).read()
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Rythme des stations
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def mesurer_rythme(instants):
|
||||||
|
"""[(dh_utc, recu_utc)...] -> (pas_s, retard_s). Pas = médiane des écarts, arrondi à 5 min, borné."""
|
||||||
|
dhs = [lire_utc(d) for d, _ in instants]
|
||||||
|
ecarts = [(b - a).total_seconds() for a, b in zip(dhs, dhs[1:]) if b > a]
|
||||||
|
if len(ecarts) >= 2:
|
||||||
|
pas = round(statistics.median(ecarts) / 300) * 300
|
||||||
|
pas = int(min(max(pas, PAS_MIN_S), PAS_MAX_S))
|
||||||
|
else:
|
||||||
|
pas = PAS_DEFAUT_S
|
||||||
|
retards = [(lire_utc(r) - lire_utc(d)).total_seconds() for d, r in instants]
|
||||||
|
retard = statistics.median(retards) if retards else None
|
||||||
|
return pas, retard
|
||||||
|
|
||||||
|
|
||||||
|
def rythmes(stockage, ids):
|
||||||
|
r = {}
|
||||||
|
for sid in ids:
|
||||||
|
inst = stockage.instants(SOURCE, sid, 30)
|
||||||
|
pas, retard = mesurer_rythme(inst)
|
||||||
|
r[sid] = {"derniere": lire_utc(inst[-1][0]) if inst else None, "pas_s": pas, "retard_s": retard}
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def est_due(r, maintenant):
|
||||||
|
if r["derniere"] is None:
|
||||||
|
return True
|
||||||
|
return maintenant >= r["derniere"] + dt.timedelta(seconds=r["pas_s"] + DELAI_PUBLICATION_S)
|
||||||
|
|
||||||
|
|
||||||
|
def est_ponctuelle(r):
|
||||||
|
return r["retard_s"] is not None and r["retard_s"] <= RETARD_PONCTUEL_S
|
||||||
|
|
||||||
|
|
||||||
|
def type_passage(maintenant):
|
||||||
|
"""hh:x4..x8 -> passage principal ; hh:x9..x3 -> rattrapage (le minuteur tire à x4 et x9)."""
|
||||||
|
return "principal" if (maintenant.minute - 4) % 10 < 5 else "rattrapage"
|
||||||
|
|
||||||
|
|
||||||
|
def planifier(etat, rythmes_st, maintenant):
|
||||||
|
"""Renvoie (type, [stations à demander]). Liste vide = aucun appel."""
|
||||||
|
genre = type_passage(maintenant)
|
||||||
|
dues = [sid for sid, r in rythmes_st.items() if est_due(r, maintenant)]
|
||||||
|
if genre == "principal":
|
||||||
|
dernier = etat.get("dernier_principal_utc")
|
||||||
|
if dernier and (maintenant - lire_utc(dernier)).total_seconds() < ECART_MIN_PRINCIPAL_S:
|
||||||
|
return genre, []
|
||||||
|
return genre, dues
|
||||||
|
cibles = [sid for sid in etat.get("manquees", [])
|
||||||
|
if sid in rythmes_st and sid in dues and est_ponctuelle(rythmes_st[sid])]
|
||||||
|
return genre, cibles
|
||||||
|
|
||||||
|
|
||||||
|
def fenetre(etat, maintenant, stations, rythmes_st):
|
||||||
|
"""start=end=aujourd'hui UTC ; veille ajoutée au premier passage après minuit UTC ou pour une station
|
||||||
|
sans donnée récente."""
|
||||||
|
auj = maintenant.date()
|
||||||
|
veille = etat.get("dernier_jour_utc") != str(auj) or any(
|
||||||
|
rythmes_st[s]["derniere"] is None or rythmes_st[s]["derniere"].date() < auj for s in stations)
|
||||||
|
return (auj - dt.timedelta(days=1) if veille else auj), auj
|
||||||
|
|
||||||
|
|
||||||
|
def construire_url(stations, debut, fin, jeton):
|
||||||
|
return (URL_BASE + "?version=2&method=get&format=json&"
|
||||||
|
+ "&".join("stations[]=" + urllib.parse.quote(s) for s in stations)
|
||||||
|
+ "&start=%s&end=%s&token=%s" % (debut, fin, urllib.parse.quote(jeton, safe="")))
|
||||||
|
|
||||||
|
|
||||||
|
def telecharger(url):
|
||||||
|
r = urllib.request.urlopen(urllib.request.Request(
|
||||||
|
url, headers={"User-Agent": UA, "Accept-Encoding": "gzip"}), timeout=120)
|
||||||
|
brut = r.read()
|
||||||
|
if r.headers.get("Content-Encoding") == "gzip":
|
||||||
|
brut = gzip.decompress(brut)
|
||||||
|
return brut
|
||||||
|
|
||||||
|
|
||||||
|
def lire_reponse(brut, demandees):
|
||||||
|
"""-> ({station: [mesures]}, {station: métadonnées}). Lève ErreurInfoclimat si le statut n'est pas OK."""
|
||||||
|
try:
|
||||||
|
d = json.loads(brut)
|
||||||
|
except ValueError:
|
||||||
|
raise ErreurInfoclimat("réponse non JSON")
|
||||||
|
if not isinstance(d, dict) or str(d.get("status", "")).upper() != "OK":
|
||||||
|
err = d.get("errors") if isinstance(d, dict) else d
|
||||||
|
raise ErreurInfoclimat("statut non OK : %s" % str(err)[:200])
|
||||||
|
mesures = {}
|
||||||
|
horaire = d.get("hourly") or {}
|
||||||
|
for sid in demandees:
|
||||||
|
lignes = horaire.get(sid) if isinstance(horaire, dict) else None
|
||||||
|
mesures[sid] = [dict({"dh_utc": x["dh_utc"]}, **{c: x.get(k) for k, c in CORRESPONDANCE.items()})
|
||||||
|
for x in (lignes or []) if isinstance(x, dict) and "dh_utc" in x]
|
||||||
|
meta = {}
|
||||||
|
for m in d.get("stations") or []:
|
||||||
|
if isinstance(m, dict) and m.get("id") in demandees:
|
||||||
|
meta[m["id"]] = {"altitude": m.get("elevation"), "lat": m.get("latitude"), "lon": m.get("longitude"),
|
||||||
|
"licence": (m.get("license") or {}).get("license", ""), "nom_ic": m.get("name")}
|
||||||
|
return mesures, meta
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Passage
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def passer(cfg, stockage, journal, maintenant, stations_cfg, fetch=telecharger, quota=None):
|
||||||
|
"""Un tick Infoclimat. Renvoie un petit résumé (dict) pour etat.json."""
|
||||||
|
var = cfg.base / "var"
|
||||||
|
fichier_etat = var / "etat_infoclimat.json"
|
||||||
|
etat = lire_json(fichier_etat, {})
|
||||||
|
pause = Pause(var / "pause_infoclimat.txt")
|
||||||
|
quota = quota or Quota(cfg, journal)
|
||||||
|
resume = {"source": SOURCE, "action": "rien", "requetes": 0}
|
||||||
|
if pause.active(maintenant):
|
||||||
|
resume.update(action="pause", jusqua=fmt_utc(pause.jusqua()))
|
||||||
|
return resume
|
||||||
|
|
||||||
|
for sid, info in stations_cfg.items():
|
||||||
|
stockage.maj_station(SOURCE, sid, nom=info.get("nom"), licence=info.get("licence"), lien=info.get("lien"))
|
||||||
|
r = rythmes(stockage, list(stations_cfg))
|
||||||
|
genre, cibles = planifier(etat, r, maintenant)
|
||||||
|
resume["type"] = genre
|
||||||
|
if not cibles:
|
||||||
|
return resume
|
||||||
|
|
||||||
|
try:
|
||||||
|
jeton = lire_secret(cfg.jeton_infoclimat)
|
||||||
|
except OSError:
|
||||||
|
journal("jeton Infoclimat illisible, aucun appel", maintenant)
|
||||||
|
resume["action"] = "sans_jeton"
|
||||||
|
return resume
|
||||||
|
journal.masquer(jeton)
|
||||||
|
|
||||||
|
nouvelles_total, recues, ok = 0, set(), False
|
||||||
|
for i in range(0, len(cibles), cfg.lot_max):
|
||||||
|
lot = cibles[i:i + cfg.lot_max]
|
||||||
|
cout = len(lot) if cfg.compte_par_station else 1
|
||||||
|
if not quota.peut(cout, maintenant):
|
||||||
|
journal("plafond atteint (%s), aucun appel" % quota.etat(maintenant), maintenant)
|
||||||
|
resume["action"] = "plafond"
|
||||||
|
break
|
||||||
|
debut, fin = fenetre(etat, maintenant, lot, r)
|
||||||
|
n = quota.noter(cout, maintenant)
|
||||||
|
resume["requetes"] += cout
|
||||||
|
try:
|
||||||
|
mesures, meta = lire_reponse(fetch(construire_url(lot, debut, fin, jeton)), lot)
|
||||||
|
except Exception as e: # toute erreur = arrêt avec pause de 15 min (décision de Théo)
|
||||||
|
pause.poser(maintenant)
|
||||||
|
journal("ERREUR Infoclimat (requête %d du jour) : %s ; pause 15 min"
|
||||||
|
% (n, scrub(str(e), jeton)[:300]), maintenant)
|
||||||
|
resume["action"] = "erreur"
|
||||||
|
resume["erreur"] = scrub(str(e), jeton)[:200]
|
||||||
|
break
|
||||||
|
for sid in lot:
|
||||||
|
nv = stockage.ajouter(SOURCE, sid, mesures.get(sid, []), maintenant)
|
||||||
|
if nv:
|
||||||
|
recues.add(sid)
|
||||||
|
nouvelles_total += nv
|
||||||
|
if sid in meta:
|
||||||
|
m = meta[sid]
|
||||||
|
stockage.maj_station(SOURCE, sid, lat=m["lat"], lon=m["lon"], altitude=m["altitude"],
|
||||||
|
licence=m["licence"])
|
||||||
|
ok = True
|
||||||
|
resume["action"] = "appel"
|
||||||
|
|
||||||
|
if ok:
|
||||||
|
etat["dernier_jour_utc"] = str(maintenant.date())
|
||||||
|
etat["dernier_succes_utc"] = fmt_utc(maintenant)
|
||||||
|
if genre == "principal":
|
||||||
|
etat["dernier_principal_utc"] = fmt_utc(maintenant)
|
||||||
|
etat["manquees"] = [s for s in cibles if s not in recues]
|
||||||
|
else:
|
||||||
|
etat["manquees"] = [s for s in etat.get("manquees", []) if s not in recues]
|
||||||
|
ecrire_json(fichier_etat, etat, compact=False)
|
||||||
|
journal("ok %s : %d station(s) demandée(s), %d avec du neuf, %d mesure(s) nouvelle(s) ; quota %s"
|
||||||
|
% (genre, len(cibles), len(recues), nouvelles_total, quota.etat(maintenant)), maintenant)
|
||||||
|
resume.update(demandees=len(cibles), nouvelles=nouvelles_total)
|
||||||
|
return resume
|
||||||
@@ -0,0 +1,46 @@
|
|||||||
|
"""Aides de test : configuration dans un dossier temporaire, faux réseau. Aucun appel réel, aucun vrai secret."""
|
||||||
|
import datetime as dt
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from meteo_collecte.commun import UTC, Config, Journal
|
||||||
|
|
||||||
|
FIXTURES = Path(__file__).resolve().parent / "fixtures"
|
||||||
|
JETON_FICTIF = "JETON+FICTIF/123=" # fabriqué pour les tests
|
||||||
|
CLE_FICTIVE = "CLE+FICTIVE/456="
|
||||||
|
|
||||||
|
|
||||||
|
def fixture(nom):
|
||||||
|
return (FIXTURES / nom).read_bytes()
|
||||||
|
|
||||||
|
|
||||||
|
def config(dossier, **kw):
|
||||||
|
d = Path(dossier)
|
||||||
|
(d / "secrets").mkdir(exist_ok=True)
|
||||||
|
(d / "secrets" / "infoclimat.token").write_text(JETON_FICTIF)
|
||||||
|
(d / "secrets" / "romma.key").write_text(CLE_FICTIVE)
|
||||||
|
env = {"MC_BASE": str(d), "MC_QUOTA_COMMUN": str(d / "quota.json"),
|
||||||
|
"MC_JETON_INFOCLIMAT": str(d / "secrets" / "infoclimat.token"),
|
||||||
|
"MC_CLE_ROMMA": str(d / "secrets" / "romma.key")}
|
||||||
|
cfg = Config.depuis_env(env)
|
||||||
|
for k, v in kw.items():
|
||||||
|
setattr(cfg, k, v)
|
||||||
|
return cfg, Journal(d / "var" / "journal.log")
|
||||||
|
|
||||||
|
|
||||||
|
class FauxReseau:
|
||||||
|
"""Remplace urllib : renvoie des réponses enregistrées et garde les URL demandées."""
|
||||||
|
|
||||||
|
def __init__(self, *reponses):
|
||||||
|
self.reponses = list(reponses)
|
||||||
|
self.urls = []
|
||||||
|
|
||||||
|
def __call__(self, url):
|
||||||
|
self.urls.append(url)
|
||||||
|
r = self.reponses.pop(0) if len(self.reponses) > 1 else self.reponses[0]
|
||||||
|
if isinstance(r, Exception):
|
||||||
|
raise r
|
||||||
|
return r
|
||||||
|
|
||||||
|
|
||||||
|
def t(h, m, jour=16, mois=10):
|
||||||
|
return dt.datetime(2026, mois, jour, h, m, tzinfo=UTC)
|
||||||
@@ -0,0 +1 @@
|
|||||||
|
{"status": "KO", "errors": ["invalid token"]}
|
||||||
+68
@@ -0,0 +1,68 @@
|
|||||||
|
{
|
||||||
|
"status": "OK",
|
||||||
|
"errors": [],
|
||||||
|
"stations": [
|
||||||
|
{
|
||||||
|
"id": "000T0",
|
||||||
|
"name": "Lans",
|
||||||
|
"latitude": 45.1,
|
||||||
|
"longitude": 5.58,
|
||||||
|
"elevation": 990,
|
||||||
|
"license": {
|
||||||
|
"license": "CC BY"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id": "000IL",
|
||||||
|
"name": "Autrans",
|
||||||
|
"latitude": 45.17,
|
||||||
|
"longitude": 5.54,
|
||||||
|
"elevation": 1353,
|
||||||
|
"license": {
|
||||||
|
"license": "CC BY"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"hourly": {
|
||||||
|
"_params": [
|
||||||
|
"dh_utc",
|
||||||
|
"temperature"
|
||||||
|
],
|
||||||
|
"000T0": [
|
||||||
|
{
|
||||||
|
"id_station": "000T0",
|
||||||
|
"dh_utc": "2026-10-16 11:40:00",
|
||||||
|
"temperature": "7.2",
|
||||||
|
"humidite": "81",
|
||||||
|
"pression": "1021.3",
|
||||||
|
"vent_moyen": "3.1",
|
||||||
|
"vent_rafales": "9.4",
|
||||||
|
"vent_direction": "200",
|
||||||
|
"pluie_1h": "0",
|
||||||
|
"pluie_cumul_0h": "0.2",
|
||||||
|
"point_de_rosee": "4.1"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"id_station": "000T0",
|
||||||
|
"dh_utc": "2026-10-16 11:50:00",
|
||||||
|
"temperature": "7.5",
|
||||||
|
"humidite": "80",
|
||||||
|
"pression": "1021.2",
|
||||||
|
"vent_moyen": "2.8",
|
||||||
|
"vent_rafales": "8.0",
|
||||||
|
"vent_direction": "210",
|
||||||
|
"pluie_1h": "0",
|
||||||
|
"pluie_cumul_0h": "0.2",
|
||||||
|
"point_de_rosee": "4.2"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"000IL": [
|
||||||
|
{
|
||||||
|
"id_station": "000IL",
|
||||||
|
"dh_utc": "2026-10-16 11:40:00",
|
||||||
|
"temperature": "4.0",
|
||||||
|
"humidite": null
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,192 @@
|
|||||||
|
import json
|
||||||
|
import tempfile
|
||||||
|
import unittest
|
||||||
|
import urllib.parse
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from meteo_collecte import infoclimat as ic
|
||||||
|
from meteo_collecte.commun import lire_json
|
||||||
|
from meteo_collecte.stockage import Stockage
|
||||||
|
from tests.aide import JETON_FICTIF, FauxReseau, config, fixture, t
|
||||||
|
|
||||||
|
STATIONS = {"000T0": {"nom": "Lans", "licence": "CC BY"}, "000IL": {"nom": "Autrans", "licence": "CC BY"}}
|
||||||
|
|
||||||
|
|
||||||
|
def instants(debut_min, pas_min, n, retard_min=2):
|
||||||
|
"""Fabrique [(dh_utc, recu_utc)] à partir de 10:00 UTC le 16/10."""
|
||||||
|
out = []
|
||||||
|
for i in range(n):
|
||||||
|
m = debut_min + i * pas_min
|
||||||
|
out.append(("2026-10-16 %02d:%02d:00" % (10 + m // 60, m % 60),
|
||||||
|
"2026-10-16 %02d:%02d:00" % (10 + (m + retard_min) // 60, (m + retard_min) % 60)))
|
||||||
|
return out
|
||||||
|
|
||||||
|
|
||||||
|
class TestRythme(unittest.TestCase):
|
||||||
|
def test_pas_mesure(self):
|
||||||
|
self.assertEqual(ic.mesurer_rythme(instants(0, 10, 6))[0], 600)
|
||||||
|
self.assertEqual(ic.mesurer_rythme(instants(0, 15, 6))[0], 900)
|
||||||
|
self.assertEqual(ic.mesurer_rythme(instants(0, 20, 5))[0], 1200)
|
||||||
|
self.assertEqual(ic.mesurer_rythme([])[0], 600)
|
||||||
|
|
||||||
|
def test_retard_et_ponctualite(self):
|
||||||
|
pas, retard = ic.mesurer_rythme(instants(0, 10, 6, retard_min=14))
|
||||||
|
self.assertEqual(retard, 14 * 60)
|
||||||
|
self.assertFalse(ic.est_ponctuelle({"retard_s": retard}))
|
||||||
|
self.assertTrue(ic.est_ponctuelle({"retard_s": 240}))
|
||||||
|
|
||||||
|
def test_due(self):
|
||||||
|
r = {"derniere": t(11, 50), "pas_s": 600, "retard_s": 120}
|
||||||
|
self.assertFalse(ic.est_due(r, t(12, 1)))
|
||||||
|
self.assertTrue(ic.est_due(r, t(12, 2)))
|
||||||
|
r15 = {"derniere": t(11, 45), "pas_s": 900, "retard_s": 120}
|
||||||
|
self.assertFalse(ic.est_due(r15, t(11, 54))) # une station à 15 min n'est pas prise à chaque passage
|
||||||
|
self.assertTrue(ic.est_due(r15, t(12, 4)))
|
||||||
|
|
||||||
|
def test_type_passage(self):
|
||||||
|
self.assertEqual(ic.type_passage(t(12, 4)), "principal")
|
||||||
|
self.assertEqual(ic.type_passage(t(12, 9)), "rattrapage")
|
||||||
|
self.assertEqual(ic.type_passage(t(12, 14)), "principal")
|
||||||
|
|
||||||
|
def test_planifier_rien_si_aucune_due(self):
|
||||||
|
r = {"A": {"derniere": t(12, 0), "pas_s": 600, "retard_s": 60}}
|
||||||
|
self.assertEqual(ic.planifier({}, r, t(12, 4)), ("principal", []))
|
||||||
|
|
||||||
|
def test_rattrapage_cible_seulement_les_ponctuelles_manquees(self):
|
||||||
|
r = {"P": {"derniere": t(11, 50), "pas_s": 600, "retard_s": 120}, # ponctuelle, manquée
|
||||||
|
"L": {"derniere": t(11, 50), "pas_s": 600, "retard_s": 840}, # toujours en retard
|
||||||
|
"X": {"derniere": t(11, 50), "pas_s": 600, "retard_s": 120}} # due mais pas manquée
|
||||||
|
genre, cibles = ic.planifier({"manquees": ["P", "L"]}, r, t(12, 9))
|
||||||
|
self.assertEqual((genre, cibles), ("rattrapage", ["P"]))
|
||||||
|
|
||||||
|
def test_pas_deux_principaux_rapproches(self):
|
||||||
|
r = {"A": {"derniere": None, "pas_s": 600, "retard_s": None}}
|
||||||
|
self.assertEqual(ic.planifier({"dernier_principal_utc": "2026-10-16 12:04:00"}, r, t(12, 8))[1], [])
|
||||||
|
|
||||||
|
def test_fenetre_courte(self):
|
||||||
|
r = {"A": {"derniere": t(11, 50)}}
|
||||||
|
self.assertEqual(ic.fenetre({"dernier_jour_utc": "2026-10-16"}, t(12, 4), ["A"], r),
|
||||||
|
(t(0, 0).date(), t(0, 0).date()))
|
||||||
|
# premier passage après minuit UTC : la veille est ajoutée
|
||||||
|
self.assertEqual(ic.fenetre({"dernier_jour_utc": "2026-10-15"}, t(0, 4), ["A"], r)[0].isoformat(),
|
||||||
|
"2026-10-15")
|
||||||
|
|
||||||
|
|
||||||
|
class TestReponse(unittest.TestCase):
|
||||||
|
def test_lecture_fixture(self):
|
||||||
|
m, meta = ic.lire_reponse(fixture("infoclimat_ok.json"), ["000T0", "000IL", "000GS"])
|
||||||
|
self.assertEqual(len(m["000T0"]), 2)
|
||||||
|
self.assertEqual(m["000T0"][1]["pluie_cumul"], "0.2")
|
||||||
|
self.assertEqual(m["000GS"], [])
|
||||||
|
self.assertEqual(meta["000T0"]["altitude"], 990)
|
||||||
|
|
||||||
|
def test_statut_non_ok_meme_en_200(self):
|
||||||
|
with self.assertRaises(ic.ErreurInfoclimat):
|
||||||
|
ic.lire_reponse(fixture("infoclimat_erreur.json"), ["000T0"])
|
||||||
|
with self.assertRaises(ic.ErreurInfoclimat):
|
||||||
|
ic.lire_reponse(b"<html>", ["000T0"])
|
||||||
|
|
||||||
|
def test_url_groupee(self):
|
||||||
|
u = ic.construire_url(["000T0", "STATIC0479"], "2026-10-16", "2026-10-16", JETON_FICTIF)
|
||||||
|
self.assertEqual(u.count("stations[]="), 2)
|
||||||
|
self.assertIn("start=2026-10-16&end=2026-10-16", u)
|
||||||
|
self.assertIn("token=" + urllib.parse.quote(JETON_FICTIF, safe=""), u)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPassage(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.tmp = tempfile.TemporaryDirectory()
|
||||||
|
self.cfg, self.journal = config(self.tmp.name)
|
||||||
|
self.st = Stockage(Path(self.tmp.name) / "m.sqlite")
|
||||||
|
self.alertes = []
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.st.fermer()
|
||||||
|
self.tmp.cleanup()
|
||||||
|
|
||||||
|
def quota(self):
|
||||||
|
return ic.Quota(self.cfg, self.journal, envoyer_alerte=self.alertes.append)
|
||||||
|
|
||||||
|
def passer(self, quand, reseau):
|
||||||
|
return ic.passer(self.cfg, self.st, self.journal, quand, STATIONS, fetch=reseau, quota=self.quota())
|
||||||
|
|
||||||
|
def test_une_seule_requete_pour_toutes_les_stations(self):
|
||||||
|
r = FauxReseau(fixture("infoclimat_ok.json"))
|
||||||
|
res = self.passer(t(12, 4), r)
|
||||||
|
self.assertEqual(len(r.urls), 1)
|
||||||
|
self.assertEqual(res["requetes"], 1)
|
||||||
|
self.assertEqual(self.st.compter("infoclimat"), 3)
|
||||||
|
self.assertEqual(lire_json(self.cfg.quota_commun, {})["requetes"], 1)
|
||||||
|
# 000IL n'a rien de neuf après 11:40 ? Elle a reçu une mesure : pas manquée.
|
||||||
|
self.assertEqual(lire_json(self.cfg.base / "var" / "etat_infoclimat.json", {})["manquees"], [])
|
||||||
|
|
||||||
|
def test_aucun_appel_si_rien_n_est_du(self):
|
||||||
|
r = FauxReseau(fixture("infoclimat_ok.json"))
|
||||||
|
self.passer(t(12, 4), r)
|
||||||
|
r2 = FauxReseau(fixture("infoclimat_ok.json"))
|
||||||
|
res = self.passer(t(12, 4).replace(second=30), r2) # même passage : bloqué
|
||||||
|
self.assertEqual(r2.urls, [])
|
||||||
|
self.assertEqual(res["requetes"], 0)
|
||||||
|
|
||||||
|
def test_erreur_pause_15_min_et_jeton_masque(self):
|
||||||
|
r = FauxReseau(OSError("échec pour url https://x/?token=%s" % urllib.parse.quote(JETON_FICTIF, safe="")))
|
||||||
|
res = self.passer(t(12, 4), r)
|
||||||
|
self.assertEqual(res["action"], "erreur")
|
||||||
|
journal = (self.cfg.base / "var" / "journal.log").read_text(encoding="utf-8")
|
||||||
|
self.assertIn("pause 15 min", journal)
|
||||||
|
self.assertNotIn("FICTIF", journal)
|
||||||
|
self.assertNotIn("FICTIF", json.dumps(res))
|
||||||
|
r2 = FauxReseau(fixture("infoclimat_ok.json"))
|
||||||
|
self.assertEqual(self.passer(t(12, 14), r2)["action"], "pause")
|
||||||
|
self.assertEqual(r2.urls, [])
|
||||||
|
self.assertEqual(self.passer(t(12, 24), r2)["action"], "appel")
|
||||||
|
|
||||||
|
def test_plafond_site(self):
|
||||||
|
self.cfg.plafond_site = 1
|
||||||
|
self.passer(t(12, 4), FauxReseau(fixture("infoclimat_ok.json")))
|
||||||
|
r2 = FauxReseau(fixture("infoclimat_ok.json"))
|
||||||
|
res = self.passer(t(13, 4), r2)
|
||||||
|
self.assertEqual(res["action"], "plafond")
|
||||||
|
self.assertEqual(r2.urls, [])
|
||||||
|
|
||||||
|
def test_plafond_commun(self):
|
||||||
|
self.cfg.quota_commun.write_text(json.dumps({"date": "2026-10-16", "requetes": 4999}))
|
||||||
|
r = FauxReseau(fixture("infoclimat_ok.json"))
|
||||||
|
self.assertEqual(self.passer(t(12, 4), r)["action"], "plafond")
|
||||||
|
self.assertEqual(r.urls, [])
|
||||||
|
|
||||||
|
def test_compteur_24h_glissantes(self):
|
||||||
|
q = self.quota()
|
||||||
|
self.cfg.plafond_site = 3
|
||||||
|
for h in (19, 20, 21):
|
||||||
|
q.noter(1, t(h, 0, jour=15))
|
||||||
|
# nouveau jour de Paris (16/10) mais 3 requêtes dans les 24 h : refus
|
||||||
|
self.assertEqual(q.etat(t(12, 0))["site_jour"], 0)
|
||||||
|
self.assertFalse(q.peut(1, t(12, 0)))
|
||||||
|
self.assertTrue(q.peut(1, t(20, 30)))
|
||||||
|
|
||||||
|
def test_alerte_80_pour_cent_une_fois_par_jour(self):
|
||||||
|
self.cfg.plafond_site = 10
|
||||||
|
q = self.quota()
|
||||||
|
for i in range(10):
|
||||||
|
q.noter(1, t(12, i))
|
||||||
|
self.assertEqual(len(self.alertes), 1)
|
||||||
|
self.assertIn("8 requêtes", self.alertes[0])
|
||||||
|
|
||||||
|
def test_envoi_ntfy_desactive_par_defaut(self):
|
||||||
|
self.assertFalse(ic.envoyer_ntfy(self.cfg, "test"))
|
||||||
|
|
||||||
|
def test_compte_par_station(self):
|
||||||
|
self.cfg.compte_par_station = True
|
||||||
|
res = self.passer(t(12, 4), FauxReseau(fixture("infoclimat_ok.json")))
|
||||||
|
self.assertEqual(res["requetes"], 2)
|
||||||
|
|
||||||
|
def test_lots_de_25(self):
|
||||||
|
self.cfg.lot_max = 1
|
||||||
|
r = FauxReseau(fixture("infoclimat_ok.json"))
|
||||||
|
self.passer(t(12, 4), r)
|
||||||
|
self.assertEqual(len(r.urls), 2)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
unittest.main()
|
||||||
Référencer dans un nouveau ticket
Bloquer un utilisateur