Files
aimsight-app/app/serveur/check.py
T
maximeandClaude Opus 5.5 8c82368dec Aligné sur le dépôt detectors finalisé (3886196) : scripts/, events, /detectors:wf-improve avec sa requête complète
La page Data lit detectors/scripts/categories.py (events) ; « Optimiser » envoie à /detectors:wf-improve la plage
de l'analyse, ses étiquettes, la vérité de référence, les familles, rapport.md et le dossier de sortie
<analyse>/detectors/<heure>/, dont le summary.md est le bilan affiché. Run app ne monte detectors qu'une fois,
sur main, pour ne pas masquer les commits d'une optimisation. Docs et commandes (--from/--to) à jour.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-13 13:10:33 +01:00

178 lines
8.8 KiB
Python

"""Vérifications de la web app, sans appel à claude : .venv/bin/python app/serveur/check.py [nom]"""
import json
import shutil
import socket
import sys
import tempfile
import threading
import time
import urllib.request
from pathlib import Path
from urllib.error import HTTPError
ICI = Path(__file__).resolve().parent
sys.path.insert(0, str(ICI))
import uvicorn # noqa: E402
import serveur # noqa: E402
import station # noqa: E402
def seance_temporaire():
return Path(tempfile.mkdtemp())
def check_serveur():
# le vrai serveur sur un port libre : la garde (Host, JSON déclaré), une lecture
with socket.socket() as so:
so.bind(("127.0.0.1", 0))
port = so.getsockname()[1]
srv = uvicorn.Server(uvicorn.Config(serveur.app, host="127.0.0.1", port=port, log_level="error"))
threading.Thread(target=srv.run, daemon=True).start()
while not srv.started:
time.sleep(0.05)
base = f"http://127.0.0.1:{port}"
def appel(chemin, corps=None, entetes=None, methode=None):
r = urllib.request.Request(base + chemin, json.dumps(corps).encode() if corps is not None else None,
{"Content-Type": "application/json", **(entetes or {})}, method=methode)
try:
with urllib.request.urlopen(r) as rep:
return rep.status
except HTTPError as e:
return e.code
try:
assert appel("/api/stations", entetes={"Host": "evil.example:80"}) == 403
assert appel("/api/etiquettes", {"station": "x"}, {"Content-Type": "text/plain"}) == 403
assert appel("/api/stations") == 200
assert appel("/api/analyses") == 200
assert appel("/api/analyses/INCONNUE/optimiser", {}) == 400
finally:
srv.should_exit = True
def leve(f, *a):
try:
f(*a)
except station.Refus as e:
return str(e)
raise AssertionError(f"{f.__name__}{a} n'a pas refusé")
def check_station():
# les données de la page Data, sur les vraies données ; les étiquettes dans un dossier temporaire
assert "NIEDERHORN" in station.stations()
im = station.images("NIEDERHORN", "2026-09-25", "2026-09-26")
assert im["images"] and im["positions"] and set(im["corr"]) == set(im["positions"]), list(im)
assert all(len(c) == len(im["images"]) for c in im["corr"].values())
assert station.image("NIEDERHORN", im["images"][0]["nom"]).is_file()
leve(station.image, "NIEDERHORN", "../../../.env")
leve(station.images, "../API", "", "")
assert station.images("LENZSPITZE", "2026-10-26", "2026-10-27")["images"]
c = station.courbes("LENZSPITZE", "2026-10-26", "2026-10-27")
titres = [p["titre"] for p in c["plots"]]
assert "Relative Corrected Vertical Shift" in titres and any(t.startswith("System: ") for t in titres), titres
ev = station.detecteurs("LENZSPITZE", "2026-10-26", "2026-10-27") # les détecteurs du journal, ~10 s
assert ev and {"debut", "fin", "categorie", "description", "capteurs"} <= set(ev[0]), ev[:1]
assert all(e["message"] and e["action"] and set(e["cibles"] + e["references"]) == set(e["capteurs"]) for e in ev), ev[:1]
assert station.message("A moved; nothing to do. The jump of 2 targets in this period has the same cause.") \
== "A moved. The jump of 2 targets in this period has the same cause."
assert all(e["fin"] >= "2026-10-26T00:00Z" and e["debut"] < "2026-10-27T00:00Z" for e in ev), [(e["debut"], e["fin"]) for e in ev]
assert not any("SIM_CCID" in t or t.startswith("System: ip") for t in titres)
assert all(len(c["T"][tr["t"]]) == len(tr["y"]) for p in c["plots"] for tr in p["traces"])
avant, station.ETIQUETTES = station.ETIQUETTES, seance_temporaire()
try:
e = station.ajouter({"station": "NIEDERHORN", "dossier": "auto", "debut": "2026-09-25T01:00Z",
"fin": "2026-09-25T02:00Z", "categorie": "Visibility", "description": "brouillard"})
assert e["id"] == "NIEDERHORN-1" and next(iter(e)) == "id", e
e2 = station.modifier({"station": "NIEDERHORN", "dossier": "auto", "avant": e,
"debut": e["debut"], "fin": e["fin"], "categorie": "", "description": "pluie"})
assert e2 == {"id": "NIEDERHORN-1", "debut": e["debut"], "fin": e["fin"], "description": "pluie"}, e2
assert station.lire("NIEDERHORN", "auto") == [e2]
assert "plus dans le fichier" in leve(station.supprimer, {"station": "NIEDERHORN", "dossier": "auto", "avant": e})
station.supprimer({"station": "NIEDERHORN", "dossier": "auto", "avant": e2})
assert station.lire("NIEDERHORN", "auto") == []
leve(station.ajouter, {"station": "NIEDERHORN", "dossier": "../x", "debut": "a", "fin": "b", "description": "x"})
leve(station.ajouter, {"station": "NIEDERHORN", "debut": "2026-09-25T02:00Z", "fin": "2026-09-25T01:00Z", "description": "x"})
finally:
shutil.rmtree(station.ETIQUETTES)
station.ETIQUETTES = avant
def check_optimisation():
# le bouton Optimiser (analyse.optimiser), sans claude : un faux processus qui ne finit que sur signal, une analyse finie
# copiée dans un dossier temporaire ; le verrou (une optimisation à la fois, ni analyse ni message pendant), l'état
# relu après un redémarrage
import analyse
fin = threading.Event()
lances = []
class FauxProcessus:
returncode = 0
stdout = iter(lambda: fin.wait() and "", "") # rien, jusqu'au signal
def __init__(self, args, *a, **k):
lances.append(args)
def wait(self):
return 0
avant = analyse.DOSSIER, analyse.subprocess.Popen
analyse.DOSSIER = seance_temporaire()
analyse.subprocess.Popen = FauxProcessus
try:
for nom in ("NORDEND_a", "NORDEND_b"):
(analyse.DOSSIER / nom / "travail").mkdir(parents=True)
(analyse.DOSSIER / nom / "demande.json").write_text(json.dumps(
{"station": "NORDEND", "debut": "2026-11-04T21:12Z", "fin": "2026-11-07T14:12Z", "indices": ""}))
(analyse.DOSSIER / nom / "rapport.md").write_text("# rapport")
assert analyse.etat("NORDEND_a")["optimisation"] is None
assert analyse.optimiser("NORDEND_a") == {}
assert analyse.etat("NORDEND_a")["optimisation"]["etat"] == "en cours"
leve(analyse.optimiser, "NORDEND_a")
leve(analyse.optimiser, "NORDEND_b")
leve(analyse.lancer, {"station": "NORDEND", "debut": "2026-11-04T00:00Z", "fin": "2026-11-05T00:00Z"})
leve(analyse.message, "NORDEND_b", {"texte": "continue"})
fin.set()
while analyse.etat("NORDEND_a")["optimisation"]["etat"] == "en cours":
time.sleep(0.05)
assert analyse.etat("NORDEND_a")["optimisation"]["etat"] == "fini"
requete = lances[0][2] # la plage de l'analyse, TO exclu
assert requete.startswith("/detectors:wf-improve ") and "/NORDEND. FROM: 2026-11-04. TO: 2026-11-08. LABELS: " \
in requete and "/NORDEND_a/detectors/" in requete, requete
analyse.analyses["NORDEND_b"]["etat"] = "en cours" # une analyse qui tourne bloque l'optimisation
leve(analyse.optimiser, "NORDEND_a")
# après un redémarrage : le flux finit par {"type": "fin"} -> fini ; sans cette ligne -> interrompue ; le bilan des
# détecteurs et celui des outils en une page
flux = analyse.DOSSIER / "NORDEND_a" / "travail" / "optimisation.jsonl"
assert flux.read_text().endswith('{"type": "fin", "etat": "fini"}\n'), flux.read_text()
analyse.analyses.clear()
analyse.optimisations.clear()
assert analyse.etat("NORDEND_a")["optimisation"]["etat"] == "fini"
flux.write_text('{"type": "assistant", "message": {"content": []}}\n')
analyse.optimisations.clear()
assert analyse.etat("NORDEND_a")["optimisation"]["etat"] == "interrompue"
(analyse.DOSSIER / "NORDEND_a" / "detectors" / "20261113T1000Z").mkdir(parents=True)
(analyse.DOSSIER / "NORDEND_a" / "detectors" / "20261113T1000Z" / "summary.md").write_text("# wf-improve\n\n**Result**: none")
(analyse.DOSSIER / "NORDEND_a" / "outils.md").write_text("# Outils\n\n**Outils modifiés** : aucun")
assert analyse.etat("NORDEND_a")["optimisation"]["bilan"]
page = analyse.bilan("NORDEND_a", False)
assert "<strong>Result</strong>" in page and "<strong>Outils modifiés</strong>" in page, page
assert analyse.etat("NORDEND_b")["optimisation"] is None
finally:
fin.set()
shutil.rmtree(analyse.DOSSIER)
analyse.DOSSIER, analyse.subprocess.Popen = avant
analyse.analyses.clear()
analyse.optimisations.clear()
CHECKS = [check_serveur, check_station, check_optimisation]
if __name__ == "__main__":
nom = sys.argv[1] if len(sys.argv) > 1 else None
for f in CHECKS:
if nom in (None, f.__name__):
f()
print("ok", f.__name__)