English: the app (server/, analysis.py, routes and JSON fields), instructions/analyst.md, docs; the table of shared renames
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
1d80a77cb1
commit
a3e6ade845
24 files changed
+1959
-1835
No files matched your search
+29
-29
@@ -1,45 +1,45 @@
|
||||
# La web app AIMsight
|
||||
# The AIMsight web app
|
||||
|
||||
L'outil du diagnostic manuel : une seule page, **Data**.
|
||||
The tool of the manual diagnostic: a single page, **Data**.
|
||||
|
||||
## La page Data
|
||||
## The Data page
|
||||
|
||||
- **Les plots** : un au départ (déplacements, caméra, qualité d'image, métriques système), « Ajouter un plot » pour en voir plusieurs, alignés sur le temps, la croix pour en enlever un ; un clic sur une courbe cale les images sur l'image la plus proche de cet instant. Les zones des étiquettes y sont à peine visibles (opacité 0,05, 0,07 en sombre).
|
||||
- **Les étiquettes**, sous les plots, et leur barre, selon la source choisie :
|
||||
- **Detectors** (par défaut) : les événements des détecteurs (`detectors/scripts/categories.py`, `events`), en lecture seule. La barre ne porte que leur famille, la bulle leurs cibles ; au clic : leurs dates, leurs capteurs (cibles et références), le message (la première phrase du texte de l'événement, `categories.sentence`) et l'action (la colonne « Pour le client » de `labeling/familles.md`, précisée par sorte, `station.ACTION_SORTE`) ;
|
||||
- **LLM** : `labeling/auto/<STATION>.jsonl`, en lecture seule ;
|
||||
- **Analysis** : les mêmes étiquettes, à modifier ou supprimer ; écrit dans `labeling/auto/<STATION>.jsonl`.
|
||||
- **The plots**: one at the start (displacements, camera, image quality, system metrics), "Add a plot" to see several, aligned in time, the cross to remove one; a click on a curve sets the images to the image nearest to that moment. The zones of the labels are barely visible there (opacity 0.05, 0.07 in dark mode).
|
||||
- **The labels**, under the plots, and their bar, according to the chosen source:
|
||||
- **Detectors** (default): the detectors' events (`detectors/scripts/categories.py`, `events`), read-only. The bar carries only their family, the tooltip their targets; on click: their dates, their sensors (targets and references), the message (the first sentence of the text of the event, `categories.sentence`) and the action (the "For the client" column of `labeling/families.md`, refined per kind, `station.ACTION_KIND`);
|
||||
- **LLM**: `labeling/auto/<STATION>.jsonl`, read-only;
|
||||
- **Analysis**: the same labels, to edit or delete; writes in `labeling/auto/<STATION>.jsonl`.
|
||||
|
||||
Avec Detectors et LLM, glisser sur la barre zoome dans le temps ; avec Analysis, cela sélectionne une plage à analyser. Un clic met une étiquette en évidence sur les plots et la barre, cale les images sur son début et l'ouvre (avec Analysis : bornes, famille, texte, Enregistrer, Supprimer) ; un clic ailleurs sur la barre la referme. La page ne montre pas `labeling/manual/`.
|
||||
- **Les images de la caméra**, à droite, sur la même station et la même plage : glisseur, ◀ ▶ et flèches du clavier ; molette pour zoomer, glisser pour se déplacer, double clic pour l'image entière ; case Noms ; une plage sur la barre sous le glisseur dessine la flèche de déplacement de chaque capteur. Une flèche en haut à gauche agrandit l'image et pousse les plots vers la gauche ; la flèche inverse la ramène.
|
||||
- **Les paramètres du diagnostic**, sous l'image : « Focus on event curves » (un événement ouvert, les plots ne montrent que ses capteurs : ceux des détecteurs, ou ceux que nomme le texte d'une étiquette), les familles montrées et, pour Detectors seulement, les cibles considérées, les autres écartées avant les détecteurs (« Relancer les détecteurs », paramètre `cibles` de `/api/detecteurs`).
|
||||
With Detectors and LLM, dragging on the bar zooms in time; with Analysis, it selects a range to analyze. A click highlights a label on the plots and the bar, sets the images to its start and opens it (with Analysis: bounds, family, text, Save, Delete); a click elsewhere on the bar closes it. The page does not show `labeling/manual/`.
|
||||
- **The camera images**, on the right, on the same station and the same range: slider, ◀ ▶ and keyboard arrows; wheel to zoom, drag to move, double click for the whole image; Names box; a range on the bar under the slider draws the displacement arrow of each sensor. An arrow at the top left enlarges the image and pushes the plots to the left; the reverse arrow brings it back.
|
||||
- **The diagnostic settings**, under the image: "Focus on event curves" (with an event open, the plots show only its sensors: those of the detectors, or those the text of a label names), the families shown and, for Detectors only, the targets considered, the others removed before the detectors ("Rerun the detectors", parameter `targets` of `/api/detectors`).
|
||||
|
||||
L'adresse garde la station et la plage (UTC, `au` exclu) : `#/data?station=NIEDERHORN&du=2026-09-25&au=2026-10-08` ; sans dates, les 5 derniers jours UTC, aujourd'hui compris.
|
||||
The address keeps the station and the range (UTC, `end` excluded): `#/data?station=NIEDERHORN&start=2026-09-25&end=2026-10-08`; without dates, the last 5 UTC days, today included.
|
||||
|
||||
## L'onglet Analysis
|
||||
## The Analysis tab
|
||||
|
||||
- **Analyser la plage** : une plage sélectionnée, des indices facultatifs, puis `serveur/analyse.py` lance l'analyste du dépôt agents par `claude -p` (non interactif), avec ses consignes (`consignes/analyste.md`) et sans les détecteurs (`DETECTEURS : non`). Il écrit dans `labeling/auto/analyses/<STATION>_<début>_<fin>/` ses étiquettes et un rapport de diagnostic (`rapport.md` et ses figures ; son travail et le journal de claude dans `travail/`, hors de git). La page suit son avancement, puis montre le rapport ; le serveur verse ses étiquettes dans `labeling/auto/<STATION>.jsonl` (champ `analyse`). « Voir le rapport » sur une telle étiquette le rouvre.
|
||||
- **La barre latérale** range les analyses de `labeling/auto/analyses/` en dossiers, un par station (ouvert d'office s'il en a une en cours), et dedans une analyse par plage, la plus récente en tête, avec son état ; un clic ouvre Data sur elle.
|
||||
- **Les messages** : après le rapport, l'utilisateur écrit à l'analyste, la même session reprend (`--resume`). Une analyse s'arrête avec le serveur (« interrompue ») ; « continue » dans le champ de message la reprend.
|
||||
- **Optimiser les détecteurs**, sur une analyse finie : `/improve` dans Optimizer (`optimizer/`, avec le plugin `agents/`) sur la plage de l'analyse, puis le `tool-builder` du dépôt agents ; les détecteurs commités dans `detectors/`, les outils de lecture dans `tools/`, chacun sur sa branche courante. La page suit le fil de la séance (`travail/optimisation.jsonl`), puis montre son bilan sous le rapport (`optimizer/seances/<analyse>_<heure>/summary.md`, puis `<analyse>/outils.md`). Une optimisation à la fois ; pendant, ni analyse ni message à l'analyste ; une optimisation coupée par l'arrêt du serveur se relance par le bouton. Les événements des détecteurs sont gardés en mémoire jusqu'à la fin d'une optimisation : après un `/improve` lancé hors de l'app, relancer le serveur.
|
||||
- **Analyze the range**: a range selected, optional hints, then `server/analysis.py` launches the analyst of the agents repository by `claude -p` (non-interactive), with its instructions (`instructions/analyst.md`) and without the detectors (`DETECTORS: no`). It writes in `labeling/auto/analyses/<STATION>_<start>_<end>/` its labels and a diagnostic report (`report.md` and its figures; its work and claude's log in `work/`, outside git). The page follows its progress, then shows the report; the server submits its labels to `labeling/auto/<STATION>.jsonl` (field `analysis`). "View the report" on such a label opens it again.
|
||||
- **The sidebar** sorts the analyses of `labeling/auto/analyses/` into folders, one per station (open by default if it has one running), and inside one analysis per range, the most recent first, with its state; a click opens Data on it.
|
||||
- **The messages**: after the report, the user writes to the analyst, the same session resumes (`--resume`). An analysis stops with the server ("interrupted"); "continue" in the message field resumes it.
|
||||
- **Optimize the detectors**, on a finished analysis: `/improve` in Optimizer (`optimizer/`, with the plugin `agents/`) on the range of the analysis, then the `tool-builder` of the agents repository; the detectors committed in `detectors/`, the reading tools in `tools/`, each on its current branch. The page follows the thread of the session (`work/optimization.jsonl`), then shows its summary under the report (`optimizer/sessions/<analysis>_<time>/summary.md`, then `<analysis>/tools.md`). One optimization at a time; meanwhile, no analysis and no message to the analyst; an optimization cut by the server stopping is rerun with the button. The detectors' events are kept in memory until the end of an optimization: after an `/improve` run outside the app, restart the server.
|
||||
|
||||
## Lancer
|
||||
## Launch
|
||||
|
||||
La tâche VS Code **Run app** (`.vscode/tasks.json`), ou à la main (Node 22 pour le front) :
|
||||
The VS Code task **Run app** (`.vscode/tasks.json`), or by hand (Node 22 for the front end):
|
||||
|
||||
```bash
|
||||
cd app/web && npm install && npm run build # une fois, et après chaque changement de la page
|
||||
.venv/bin/python app/serveur/serveur.py # .venv : voir le README à la racine
|
||||
cd app/web && npm install && npm run build # once, and after each change of the page
|
||||
.venv/bin/python app/server/server.py # .venv: see the README at the root
|
||||
```
|
||||
|
||||
La page s'ouvre sur `http://127.0.0.1:8770`. Réglages par variables : `AIMSIGHT_DATA` (données, défaut `data/`), `AIMSIGHT_LABELS` (étiquettes, défaut `labeling/`), `AIMSIGHT_PORT` (8770). Pour travailler sur la page : `npm run dev` dans `app/web/` (http://localhost:5173), le serveur Python lancé à côté.
|
||||
The page opens on `http://127.0.0.1:8770`. Settings by variables: `AIMSIGHT_DATA` (data, default `data/`), `AIMSIGHT_LABELS` (labels, default `labeling/`), `AIMSIGHT_PORT` (8770). To work on the page: `npm run dev` in `app/web/` (http://localhost:5173), the Python server launched next to it.
|
||||
|
||||
## Les dossiers
|
||||
## The folders
|
||||
|
||||
- `serveur/serveur.py` : FastAPI ; sert la page construite et l'API sous `/api` (stations, images, courbes, détecteurs, étiquettes, analyses) ; ne répond qu'aux pages du poste (Host local, JSON déclaré).
|
||||
- `serveur/station.py` : les données de la page Data ; lecture seule sur `data/` (par `tools/scripts/donnees.py`), écrit seulement dans `labeling/{auto,manual}/`.
|
||||
- `serveur/analyse.py` : l'onglet Analysis ; lance `claude` avec l'agent `agents:analyste` (Read, Write, Bash autorisés sans demande) ; et l'optimisation, `/improve` dans Optimizer (Read, Write, Edit, Bash, Agent) puis l'agent `agents:tool-builder` (Read, Write, Edit, Bash).
|
||||
- `serveur/check.py` : vérifications, sans appel à claude : `.venv/bin/python app/serveur/check.py`.
|
||||
- `web/` : React (Vite) et Mantine ; `src/App.jsx` (en-tête, barre latérale, adresse), `src/pages/Data.jsx` (la page), `src/pages/Images.jsx` (les images, dans Data), `src/commun.jsx` (requêtes, cartes, sélecteur), `src/main.jsx` (thème). Plotly, habillé aux couleurs de Mantine, ne se charge qu'avec Data. `dist/` et `node_modules/` hors de git.
|
||||
- `server/server.py`: FastAPI; serves the built page and the API under `/api` (stations, images, curves, detectors, labels, analyses); answers only the pages of the workstation (local Host, declared JSON).
|
||||
- `server/station.py`: the data of the Data page; read-only on `data/` (through `tools/scripts/data.py`), writes only in `labeling/{auto,manual}/`.
|
||||
- `server/analysis.py`: the Analysis tab; launches `claude` with the agent `agents:analyst` (Read, Write, Bash allowed without asking); and the optimization, `/improve` in Optimizer (Read, Write, Edit, Bash, Agent) then the agent `agents:tool-builder` (Read, Write, Edit, Bash).
|
||||
- `server/check.py`: checks, without calling claude: `.venv/bin/python app/server/check.py`.
|
||||
- `web/`: React (Vite) and Mantine; `src/App.jsx` (header, sidebar, address), `src/pages/Data.jsx` (the page), `src/pages/Images.jsx` (the images, in Data), `src/common.jsx` (requests, selector), `src/main.jsx` (theme). Plotly, dressed in Mantine's colours, loads only with Data. `dist/` and `node_modules/` outside git.
|
||||
|
||||
Le calcul de `serveur/station.py` (images, courbes, étiquettes) vient d'outils du dépôt Support-Agent, copiés le 2026-11-05 (commit 6e9f69a) ; il évolue désormais ici.
|
||||
The computation of `server/station.py` (images, curves, labels) comes from tools of the Support-Agent repository, copied on 2026-11-05 (commit 6e9f69a); it now evolves here.
|
||||
@@ -0,0 +1,317 @@
|
||||
"""The Analysis tab of the Data page: the analyst of the agents repository (agents/, a plugin: --agent agents:analyst), launched
|
||||
by `claude -p`, non-interactive, on a range chosen in the page, with the user's optional hints, its instructions
|
||||
(instructions/analyst.md) and without the detectors (DETECTORS: no: its labels will be used to improve them). It writes in
|
||||
labeling/auto/analyses/<STATION>_<start>_<end>/ its labels (labels.jsonl) and a diagnostic report (report.md, its figures
|
||||
next to it); at the end, the server submits its labels to labeling/auto/<STATION>.jsonl, marked with their analysis (field
|
||||
`analysis`). Then the user can write to it: the same session resumes (`claude -p --resume`), and its labels, if they
|
||||
change, replace those it had submitted.
|
||||
On a finished analysis, "Optimize" launches `/improve` in Optimizer (optimizer/, the skill that improves the detectors on the
|
||||
range of the analysis, its labels as truth), then the tool-builder of the agents repository, which brings the analyst's
|
||||
scripts into the reading tools (tools/); one optimization at a time, no analysis or message meanwhile; the summary of the
|
||||
session (optimizer/sessions/<analysis>_<time>/summary.md) and that of the tools (<analysis>/tools.md) show under the report.
|
||||
"""
|
||||
import html
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
|
||||
import markdown
|
||||
|
||||
import station
|
||||
|
||||
FOLDER = station.LABELS / "auto" / "analyses"
|
||||
DETECTORS = station.ROOT / "detectors"
|
||||
AGENTS = station.ROOT / "agents" # the plugin of the generic agents (~/agents)
|
||||
OPTIMIZER = station.ROOT / "optimizer" # the Optimizer repository (~/Optimizer), where /improve runs
|
||||
SESSIONS = OPTIMIZER / "sessions"
|
||||
# the request of /improve (optimizer/README.md), paths relative to the root of Optimizer (its links data, labeling, detectors)
|
||||
IMPROVE = """/improve DETECTORS: detectors/. STATION: data/{station}. FROM: {start}. TO: {end}. \
|
||||
LABELS: labeling/auto/{station}.jsonl. REFERENCE: --labels labeling/auto --data data. FAMILIES: labeling/families.md. \
|
||||
CONTEXT: labeling/auto/analyses/{name}/report.md. OUT: sessions/{name}_{time}/. Caller's context: Diagnostic-app, through \
|
||||
Optimizer; read-only on data/ and labeling/; commit only in detectors/, on its current branch; do not commit OUT."""
|
||||
REQUEST = """Station: data/{station}/. Range: {start} → {end} (UTC). Hints: {hints}. \
|
||||
Output folder: {out}/. DETECTORS: no. INSTRUCTIONS: instructions/analyst.md."""
|
||||
|
||||
# name -> {"state": "running" | "done" | "error", "lines": [...], "submitted": n}; one analysis at a time per range
|
||||
analyses, lock = {}, threading.Lock()
|
||||
# name of the analysis -> {"state": "running" | "done" | "error", "lines": [...]}; one optimization at a time, all analyses
|
||||
optimizations = {}
|
||||
BUSY = "an optimization of the detectors is running"
|
||||
|
||||
|
||||
ISOLATED = ["--setting-sources", "project,local", "--strict-mcp-config"] # no plugins, hooks or MCP servers of the workstation
|
||||
|
||||
|
||||
def _env():
|
||||
"""The environment of claude -p, without an API key: the agents run on the Claude subscription, never on a key."""
|
||||
return {k: v for k, v in os.environ.items() if k not in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN")}
|
||||
|
||||
|
||||
def _optimizing():
|
||||
return any(o["state"] == "running" for o in optimizations.values())
|
||||
|
||||
|
||||
def launch(body):
|
||||
st, start, end = body.get("station"), body.get("start"), body.get("end")
|
||||
station._validate({"description": "-"}, start, end)
|
||||
station.folder(st)
|
||||
if not (AGENTS / ".claude-plugin" / "plugin.json").exists():
|
||||
raise station.Refusal("no agents in agents/ (the link: README.md)")
|
||||
name = f"{st}_{start[:16].replace(':', '')}_{end[:16].replace(':', '')}"
|
||||
with lock:
|
||||
if _optimizing():
|
||||
raise station.Refusal(BUSY)
|
||||
if analyses.get(name, {}).get("state") == "running":
|
||||
raise station.Refusal("this range is already being analyzed")
|
||||
out = FOLDER / name
|
||||
if out.exists(): # a new analysis of the same range: the old one stays, next to it
|
||||
name += datetime.now(timezone.utc).strftime("_%H%M%S")
|
||||
out = FOLDER / name
|
||||
hints = (body.get("hints") or "").strip()
|
||||
request = {"station": st, "start": start, "end": end, "hints": hints}
|
||||
analyses[name] = {"state": "running", "lines": [], "submitted": 0, "station": st, "request": request}
|
||||
(out / "work").mkdir(parents=True)
|
||||
(out / "request.json").write_text(json.dumps(request, ensure_ascii=False, indent=1), encoding="utf-8")
|
||||
prompt = REQUEST.format(station=st, start=start, end=end, out=out.relative_to(station.ROOT), hints=hints or "none")
|
||||
threading.Thread(target=work, args=(name, out, prompt), daemon=True).start()
|
||||
return {"name": name}
|
||||
|
||||
|
||||
def work(name, out, prompt, resume=None):
|
||||
"""A turn of the analyst: the request, or a message of the user in the same session (resume: its id)."""
|
||||
a = analyses[name]
|
||||
try:
|
||||
p = subprocess.Popen(["claude", "-p", prompt, "--plugin-dir", str(AGENTS), "--agent", "agents:analyst", *ISOLATED,
|
||||
"--output-format", "stream-json", "--verbose",
|
||||
"--allowedTools", "Read,Write,Bash", "--permission-mode", "acceptEdits", *station.links()]
|
||||
+ (["--resume", resume] if resume else []),
|
||||
cwd=station.ROOT, env=_env(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
with open(out / "work" / "log.jsonl", "a", encoding="utf-8") as log:
|
||||
for line in p.stdout:
|
||||
log.write(line)
|
||||
a["lines"] += digest(line)
|
||||
if '"subtype":"init"' in line: # the session, for the next messages
|
||||
(out / "work" / "session").write_text(json.loads(line)["session_id"])
|
||||
if p.wait():
|
||||
raise RuntimeError(f"claude ended with code {p.returncode}")
|
||||
a["submitted"] = submit(a["station"], name, out)
|
||||
a["state"] = "done"
|
||||
except Exception as e: # noqa: BLE001 — the error shows in the page, the server goes on
|
||||
a["lines"].append(f"Error: {e}")
|
||||
a["state"] = "error"
|
||||
|
||||
|
||||
def message(name, body):
|
||||
"""A message of the user to the analyst, after its report: the same session resumes (claude -p --resume)."""
|
||||
text, out = (body.get("text") or "").strip(), FOLDER / name
|
||||
state(name) # the name checked, the state read again after a restart
|
||||
a, session = analyses[name], out / "work" / "session"
|
||||
if not text or not session.exists():
|
||||
raise station.Refusal("empty message, or the analysis has no session to resume")
|
||||
with lock:
|
||||
if _optimizing():
|
||||
raise station.Refusal(BUSY)
|
||||
if a["state"] == "running":
|
||||
raise station.Refusal("the analyst is still working")
|
||||
a["state"] = "running"
|
||||
line = json.dumps({"type": "page", "text": text}, ensure_ascii=False) + "\n"
|
||||
with open(out / "work" / "log.jsonl", "a", encoding="utf-8") as log:
|
||||
log.write(line)
|
||||
a["lines"] += digest(line)
|
||||
prompt = (f"{text}\n\n(If you change your labels, rewrite {out.relative_to(station.ROOT)}/labels.jsonl in full, the "
|
||||
"server will submit them again; if the diagnosis changes, update report.md.)")
|
||||
threading.Thread(target=work, args=(name, out, prompt, session.read_text()), daemon=True).start()
|
||||
return {}
|
||||
|
||||
|
||||
def digest(line):
|
||||
"""A line of the log -> what the page shows of it: the user's messages ("› "), the analyst's texts, its tools and their
|
||||
command, one line each."""
|
||||
try:
|
||||
m = json.loads(line)
|
||||
except json.JSONDecodeError:
|
||||
return [line.strip()[:200]] if line.strip() else []
|
||||
if m.get("type") == "page":
|
||||
return [f"› {m['text']}"]
|
||||
if m.get("type") != "assistant":
|
||||
return []
|
||||
out = []
|
||||
for c in m["message"].get("content", []):
|
||||
if c.get("type") == "text" and c["text"].strip():
|
||||
out.append(c["text"].strip()[:300])
|
||||
elif c.get("type") == "tool_use":
|
||||
i = c.get("input", {})
|
||||
out.append(f"{c['name']}: {i.get('command') or i.get('file_path') or ''}"[:200])
|
||||
return out
|
||||
|
||||
|
||||
def submit(st, name, out):
|
||||
"""The labels of the analysis into labeling/auto/<STATION>.jsonl, with their id and the name of the analysis; if the
|
||||
analyst changed them since the last submission, they replace those it had submitted -> their number."""
|
||||
labels, copy = out / "labels.jsonl", out / "work" / "submitted.jsonl"
|
||||
text = labels.read_text(encoding="utf-8") if labels.exists() else ""
|
||||
new = []
|
||||
for l in text.split("\n"):
|
||||
if l.strip():
|
||||
e = json.loads(l)
|
||||
station._validate(e, e.get("start"), e.get("end"))
|
||||
new.append({k: e[k] for k in ("start", "end", "family", "description") if k in e} | {"analysis": name})
|
||||
if copy.exists() and copy.read_text(encoding="utf-8") == text:
|
||||
return len(new)
|
||||
# ponytail: removed then put back, the labels of the analysis lose the edits made in the page; keeping them would
|
||||
# require matching them one by one
|
||||
with lock:
|
||||
f = station.label_file(st, "auto") # the other lines stay as they are
|
||||
kept = [l for l in (f.read_text(encoding="utf-8").split("\n") if f.exists() else [])
|
||||
if l.strip() and json.loads(l).get("analysis") != name]
|
||||
station.assign_ids(st, [json.loads(l) for l in kept] + new)
|
||||
temp = f.with_name(f.name + ".tmp")
|
||||
temp.write_text("".join(l + "\n" for l in kept + [json.dumps(e, ensure_ascii=False) for e in new]), encoding="utf-8")
|
||||
os.replace(temp, f)
|
||||
copy.write_text(text, encoding="utf-8")
|
||||
return len(new)
|
||||
|
||||
|
||||
def state(name):
|
||||
out = FOLDER / name
|
||||
if "/" in name or name.startswith(".") or not out.is_dir():
|
||||
raise station.Refusal("unknown analysis")
|
||||
if name not in analyses: # after a restart of the server, only the folder is left: the thread is read again in the log
|
||||
log = out / "work" / "log.jsonl"
|
||||
lines = [r for l in open(log, encoding="utf-8") for r in digest(l)] if log.exists() else []
|
||||
request = json.loads((out / "request.json").read_text(encoding="utf-8"))
|
||||
analyses[name] = {"state": "done" if (out / "report.md").exists() else "interrupted", "lines": lines,
|
||||
"submitted": None, "station": request["station"], "request": request}
|
||||
a, r = analyses[name], out / "report.md"
|
||||
return {"name": name, "state": a["state"], "lines": a["lines"], "submitted": a["submitted"],
|
||||
"report": int(r.stat().st_mtime) if r.exists() else None,
|
||||
"dialogue": (out / "work" / "session").exists(), "request": a["request"], "optimization": optimization(name)}
|
||||
|
||||
|
||||
def list_analyses():
|
||||
"""The history of the analyses (the sidebar), the most recent first: those of the server with their state, the others
|
||||
read on disk (done with a report, otherwise interrupted: an analysis stops with the server)."""
|
||||
out = []
|
||||
for d in sorted(FOLDER.glob("*/request.json"), key=lambda f: f.parent.stat().st_mtime, reverse=True):
|
||||
name = d.parent.name
|
||||
a = analyses.get(name) or {"state": "done" if (d.parent / "report.md").exists() else "interrupted",
|
||||
"request": json.loads(d.read_text(encoding="utf-8"))}
|
||||
out.append({"name": name, "state": a["state"]} | a["request"])
|
||||
return out
|
||||
|
||||
|
||||
def file(name, path):
|
||||
"""A file of the folder of the analysis (the report, its figures), never outside."""
|
||||
f = (FOLDER / name / path).resolve()
|
||||
if not f.is_file() or not f.is_relative_to(FOLDER.resolve()):
|
||||
raise station.Refusal("unknown file")
|
||||
return f
|
||||
|
||||
|
||||
STYLE = """body{font:14px/1.55 Roboto,sans-serif;margin:0;padding:4px 2px;color:#212529;background:transparent}
|
||||
h1{font-size:20px}h2{font-size:16px;margin-top:22px}img{max-width:100%;border:1px solid #dee2e6;border-radius:4px}
|
||||
code{font-size:12px;background:#f1f3f5;padding:1px 4px;border-radius:3px}pre{overflow-x:auto}
|
||||
table{border-collapse:collapse}td,th{border:1px solid #dee2e6;padding:3px 8px;text-align:left}
|
||||
body.dark{color:#c9c9c9}body.dark code{background:#2e2e2e}body.dark td,body.dark th,body.dark img{border-color:#424242}"""
|
||||
|
||||
|
||||
def _page(text, title, dark):
|
||||
"""A markdown in HTML, in the style of the page; its relative images resolve in the folder of the address."""
|
||||
body = markdown.markdown(text, extensions=["tables", "fenced_code"])
|
||||
return (f"<!doctype html><meta charset=utf-8><title>{html.escape(title)}</title><style>{STYLE}</style>"
|
||||
f"<body class='{'dark' if dark else ''}'>{body}</body>")
|
||||
|
||||
|
||||
def report(name, dark):
|
||||
"""report.md in HTML, served in the folder of the analysis: its relative images are found there."""
|
||||
return _page(file(name, "report.md").read_text(encoding="utf-8"), name, dark)
|
||||
|
||||
|
||||
# --- "Optimize": /improve in Optimizer on the analysis, then the tool-builder, by claude -p
|
||||
|
||||
def _summary_file(name):
|
||||
"""The summary of the last session of this analysis, or None. The time pattern rules out the sessions of another
|
||||
analysis of the same range (<name>_<HHMMSS>_<time>)."""
|
||||
summaries = sorted(SESSIONS.glob(f"{name}_????????T????Z/summary.md")) # the order of the name is that of time
|
||||
return summaries[-1] if summaries else None
|
||||
|
||||
|
||||
def optimization(name):
|
||||
"""-> None (never optimized) or {state, lines, summary: mtime of the last summary}. After a restart, the state is read
|
||||
again in the stream: its last line {"type": "end"} says done or error; without it, the optimization was cut
|
||||
(interrupted)."""
|
||||
stream, s = FOLDER / name / "work" / "optimization.jsonl", _summary_file(name)
|
||||
o = optimizations.get(name)
|
||||
if o is None:
|
||||
if not stream.exists():
|
||||
return None
|
||||
lines = stream.read_text(encoding="utf-8").splitlines()
|
||||
end = json.loads(lines[-1]) if lines and lines[-1].startswith('{"type": "end"') else None
|
||||
o = {"state": "interrupted" if end is None else end["state"], "lines": [r for l in lines for r in digest(l)]}
|
||||
optimizations[name] = o
|
||||
return {"state": o["state"], "lines": o["lines"], "summary": int(s.stat().st_mtime) if s else None}
|
||||
|
||||
|
||||
def optimize(name):
|
||||
"""The "Optimize" button on a finished analysis: the session in a thread; refused if an optimization or an analysis runs."""
|
||||
if state(name)["state"] != "done":
|
||||
raise station.Refusal("only a finished analysis can be optimized")
|
||||
if not ((OPTIMIZER / ".claude" / "skills" / "improve" / "SKILL.md").exists()
|
||||
and all((OPTIMIZER / link).exists() for link in ("data", "labeling", "detectors"))
|
||||
and (AGENTS / ".claude-plugin" / "plugin.json").exists()):
|
||||
raise station.Refusal("no Optimizer in optimizer/ or no agents in agents/ (the links: README.md)")
|
||||
with lock:
|
||||
if _optimizing():
|
||||
raise station.Refusal(BUSY)
|
||||
if any(a["state"] == "running" for a in analyses.values()):
|
||||
raise station.Refusal("an analysis is running: the optimization waits for its end")
|
||||
optimizations[name] = {"state": "running", "lines": []}
|
||||
threading.Thread(target=optimize_thread, args=(name,), daemon=True).start()
|
||||
return {}
|
||||
|
||||
|
||||
def optimize_thread(name):
|
||||
"""The two turns of the optimization, one after the other: the detectors (/improve, in Optimizer), then the reading
|
||||
tools (the tool-builder, even if the detectors did not change)."""
|
||||
o, out = optimizations[name], FOLDER / name
|
||||
rel = os.path.relpath(out, station.ROOT)
|
||||
(out / "work").mkdir(exist_ok=True)
|
||||
with open(out / "work" / "optimization.jsonl", "a", encoding="utf-8") as stream:
|
||||
try:
|
||||
d = json.loads((out / "request.json").read_text(encoding="utf-8"))
|
||||
request = IMPROVE.format( # TO excluded: the day after the end of the range, as in the page
|
||||
station=d["station"], start=d["start"][:10], end=date.fromisoformat(d["end"][:10]) + timedelta(days=1),
|
||||
name=name, time=f"{datetime.now(timezone.utc):%Y%m%dT%H%MZ}")
|
||||
# ponytail: Optimizer reads data/ and labeling/ through its own links; AIMSIGHT_DATA / AIMSIGHT_LABELS do not follow
|
||||
turns = [(OPTIMIZER.resolve(), [request, "--plugin-dir", str(AGENTS.resolve()),
|
||||
"--add-dir", str(DETECTORS.resolve()), # the target of the link, where the session writes
|
||||
"--allowedTools", "Read,Write,Edit,Bash,Agent"]),
|
||||
(station.ROOT, [f"Analysis: {rel}/. Tools: tools/. Write {rel}/tools.md.", "--plugin-dir", str(AGENTS),
|
||||
"--agent", "agents:tool-builder", "--allowedTools", "Read,Write,Edit,Bash"])]
|
||||
for cwd, (prompt, *options) in turns:
|
||||
p = subprocess.Popen(["claude", "-p", prompt, *ISOLATED, "--output-format", "stream-json", "--verbose",
|
||||
"--permission-mode", "acceptEdits", *station.links(), *options],
|
||||
cwd=cwd, env=_env(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
for line in p.stdout:
|
||||
stream.write(line)
|
||||
o["lines"] += digest(line)
|
||||
if p.wait():
|
||||
raise RuntimeError(f"claude ended with code {p.returncode}")
|
||||
o["state"] = "done"
|
||||
except Exception as e: # noqa: BLE001 — the error shows in the page, the server goes on
|
||||
o["lines"].append(f"Error: {e}")
|
||||
o["state"] = "error"
|
||||
finally:
|
||||
stream.write(json.dumps({"type": "end", "state": o["state"]}) + "\n")
|
||||
station.detectors.cache_clear() # the detectors may have changed: the Data page computes them again
|
||||
|
||||
|
||||
def summary(name, dark):
|
||||
"""The summary of the detectors' session, then that of the tools (<analysis>/tools.md), in one page."""
|
||||
s, tools = _summary_file(name), FOLDER / name / "tools.md"
|
||||
if s is None and not tools.exists():
|
||||
raise station.Refusal("no summary for this analysis")
|
||||
text = "\n\n".join(f.read_text(encoding="utf-8") for f in (s, tools) if f and f.exists())
|
||||
return _page(text, name, dark)
|
||||
@@ -0,0 +1,266 @@
|
||||
"""Checks of the web app, without calling claude: .venv/bin/python app/server/check.py [name]"""
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
import socket
|
||||
import sys
|
||||
import tempfile
|
||||
import threading
|
||||
import time
|
||||
import urllib.request
|
||||
from pathlib import Path
|
||||
from urllib.error import HTTPError
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
sys.path.insert(0, str(HERE))
|
||||
import uvicorn # noqa: E402
|
||||
import server # noqa: E402
|
||||
import station # noqa: E402
|
||||
|
||||
|
||||
def temp_folder():
|
||||
return Path(tempfile.mkdtemp())
|
||||
|
||||
|
||||
def check_server():
|
||||
# the real server on a free port: the guard (Host, declared JSON), a read
|
||||
with socket.socket() as so:
|
||||
so.bind(("127.0.0.1", 0))
|
||||
port = so.getsockname()[1]
|
||||
srv = uvicorn.Server(uvicorn.Config(server.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 call(path, body=None, headers=None, method=None):
|
||||
r = urllib.request.Request(base + path, json.dumps(body).encode() if body is not None else None,
|
||||
{"Content-Type": "application/json", **(headers or {})}, method=method)
|
||||
try:
|
||||
with urllib.request.urlopen(r) as rep:
|
||||
return rep.status
|
||||
except HTTPError as e:
|
||||
return e.code
|
||||
try:
|
||||
assert call("/api/stations", headers={"Host": "evil.example:80"}) == 403
|
||||
assert call("/api/labels", {"station": "x"}, {"Content-Type": "text/plain"}) == 403
|
||||
assert call("/api/stations") == 200
|
||||
assert call("/api/analyses") == 200
|
||||
assert call("/api/analyses/UNKNOWN/optimize", {}) == 400
|
||||
finally:
|
||||
srv.should_exit = True
|
||||
|
||||
|
||||
def refuses(f, *a):
|
||||
try:
|
||||
f(*a)
|
||||
except station.Refusal as e:
|
||||
return str(e)
|
||||
raise AssertionError(f"{f.__name__}{a} did not refuse")
|
||||
|
||||
|
||||
def check_station():
|
||||
# the data of the Data page, on the real data; the labels in a temporary folder
|
||||
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]["name"]).is_file()
|
||||
refuses(station.image, "NIEDERHORN", "../../../.env")
|
||||
refuses(station.images, "../API", "", "")
|
||||
assert station.images("LENZSPITZE", "2026-10-26", "2026-10-27")["images"]
|
||||
c = station.curves("LENZSPITZE", "2026-10-26", "2026-10-27")
|
||||
titles = [p["title"] for p in c["plots"]]
|
||||
assert "Relative Corrected Vertical Shift" in titles and any(t.startswith("System: ") for t in titles), titles
|
||||
ev = station.detectors("LENZSPITZE", "2026-10-26", "2026-10-27") # the detectors of the journal, ~10 s
|
||||
assert ev and {"start", "end", "family", "description", "sensors"} <= set(ev[0]), ev[:1]
|
||||
assert all(e["message"] and e["action"] and set(e["targets"] + e["references"]) == set(e["sensors"]) 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["end"] >= "2026-10-26T00:00Z" and e["start"] < "2026-10-27T00:00Z" for e in ev), [(e["start"], e["end"]) for e in ev]
|
||||
assert not any("SIM_CCID" in t or t.startswith("System: ip") for t in titles)
|
||||
assert all(len(c["T"][tr["t"]]) == len(tr["y"]) for p in c["plots"] for tr in p["traces"])
|
||||
before, station.LABELS = station.LABELS, temp_folder()
|
||||
try:
|
||||
e = station.add({"station": "NIEDERHORN", "folder": "auto", "start": "2026-09-25T01:00Z",
|
||||
"end": "2026-09-25T02:00Z", "family": "Visibility", "description": "fog"})
|
||||
assert e["id"] == "NIEDERHORN-1" and next(iter(e)) == "id", e
|
||||
e2 = station.update({"station": "NIEDERHORN", "folder": "auto", "before": e,
|
||||
"start": e["start"], "end": e["end"], "family": "", "description": "rain"})
|
||||
assert e2 == {"id": "NIEDERHORN-1", "start": e["start"], "end": e["end"], "description": "rain"}, e2
|
||||
assert station.read("NIEDERHORN", "auto") == [e2]
|
||||
assert "no longer in the file" in refuses(station.delete, {"station": "NIEDERHORN", "folder": "auto", "before": e})
|
||||
station.delete({"station": "NIEDERHORN", "folder": "auto", "before": e2})
|
||||
assert station.read("NIEDERHORN", "auto") == []
|
||||
refuses(station.add, {"station": "NIEDERHORN", "folder": "../x", "start": "a", "end": "b", "description": "x"})
|
||||
refuses(station.add, {"station": "NIEDERHORN", "start": "2026-09-25T02:00Z", "end": "2026-09-25T01:00Z", "description": "x"})
|
||||
finally:
|
||||
shutil.rmtree(station.LABELS)
|
||||
station.LABELS = before
|
||||
|
||||
|
||||
def check_optimization():
|
||||
# the Optimize button (analysis.optimize), without claude: a fake process that ends only on a signal, a finished
|
||||
# analysis copied into a temporary folder; the lock (one optimization at a time, no analysis or message meanwhile), the
|
||||
# state read again after a restart
|
||||
import analysis
|
||||
end = threading.Event()
|
||||
|
||||
launched = []
|
||||
|
||||
class FakeProcess:
|
||||
returncode = 0
|
||||
stdout = iter(lambda: end.wait() and "", "") # nothing, until the signal
|
||||
|
||||
def __init__(self, args, *a, **k):
|
||||
launched.append((args, k))
|
||||
|
||||
def wait(self):
|
||||
return 0
|
||||
|
||||
before = analysis.FOLDER, analysis.subprocess.Popen, analysis.OPTIMIZER, analysis.SESSIONS, analysis.AGENTS
|
||||
key = os.environ.get("ANTHROPIC_API_KEY")
|
||||
os.environ["ANTHROPIC_API_KEY"] = "test-key"
|
||||
root = temp_folder()
|
||||
analysis.FOLDER, analysis.OPTIMIZER, analysis.AGENTS = root / "analyses", root / "optimizer", root / "agents"
|
||||
analysis.SESSIONS = analysis.OPTIMIZER / "sessions"
|
||||
analysis.subprocess.Popen = FakeProcess
|
||||
try:
|
||||
for name in ("NORDEND_a", "NORDEND_b"):
|
||||
(analysis.FOLDER / name / "work").mkdir(parents=True)
|
||||
(analysis.FOLDER / name / "request.json").write_text(json.dumps(
|
||||
{"station": "NORDEND", "start": "2026-11-04T21:12Z", "end": "2026-11-07T14:12Z", "hints": ""}))
|
||||
(analysis.FOLDER / name / "report.md").write_text("# report")
|
||||
assert analysis.state("NORDEND_a")["optimization"] is None
|
||||
# without Optimizer, then without agents: a clear refusal, not an error trace
|
||||
assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a")
|
||||
(analysis.OPTIMIZER / ".claude" / "skills" / "improve").mkdir(parents=True)
|
||||
(analysis.OPTIMIZER / ".claude" / "skills" / "improve" / "SKILL.md").write_text("improve")
|
||||
assert "agents/" in refuses(analysis.optimize, "NORDEND_a")
|
||||
(analysis.AGENTS / ".claude-plugin").mkdir(parents=True)
|
||||
(analysis.AGENTS / ".claude-plugin" / "plugin.json").write_text("{}")
|
||||
assert "optimizer/" in refuses(analysis.optimize, "NORDEND_a") # without the data, labeling, detectors links of Optimizer
|
||||
for link in ("data", "labeling", "detectors"):
|
||||
(analysis.OPTIMIZER / link).mkdir()
|
||||
assert analysis.optimize("NORDEND_a") == {}
|
||||
assert analysis.state("NORDEND_a")["optimization"]["state"] == "running"
|
||||
refuses(analysis.optimize, "NORDEND_a")
|
||||
refuses(analysis.optimize, "NORDEND_b")
|
||||
refuses(analysis.launch, {"station": "NORDEND", "start": "2026-11-04T00:00Z", "end": "2026-11-05T00:00Z"})
|
||||
refuses(analysis.message, "NORDEND_b", {"text": "continue"})
|
||||
end.set()
|
||||
while analysis.state("NORDEND_a")["optimization"]["state"] == "running":
|
||||
time.sleep(0.05)
|
||||
assert analysis.state("NORDEND_a")["optimization"]["state"] == "done"
|
||||
args, k = launched[0] # /improve in Optimizer, on the range of the analysis, TO excluded
|
||||
request = args[2]
|
||||
assert request.startswith("/improve ") and "STATION: data/NORDEND. FROM: 2026-11-04. TO: 2026-11-08." in request \
|
||||
and "OUT: sessions/NORDEND_a_" in request and "FAMILIES: labeling/families.md." in request \
|
||||
and "CONTEXT: labeling/auto/analyses/NORDEND_a/report.md." in request, request
|
||||
assert k["cwd"] == analysis.OPTIMIZER.resolve() and "--plugin-dir" in args, (args, k)
|
||||
assert all("ANTHROPIC_API_KEY" not in kw["env"] for _, kw in launched), "an API key reaches claude"
|
||||
# isolated from the settings of the workstation (the user's plugins, hooks, MCP servers)
|
||||
assert all(a[a.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in a for a, _ in launched)
|
||||
args, k = launched[1] # then the tool-builder of the agents repository, on the tools repository
|
||||
assert args[args.index("--agent") + 1] == "agents:tool-builder" and "Tools: tools/" in args[2] \
|
||||
and args[2].endswith("/tools.md."), args
|
||||
analysis.analyses["NORDEND_b"]["state"] = "running" # a running analysis blocks the optimization
|
||||
refuses(analysis.optimize, "NORDEND_a")
|
||||
# after a restart: the stream ends with {"type": "end"} -> done; without that line -> interrupted; the summary of
|
||||
# the detectors and that of the tools in one page
|
||||
stream = analysis.FOLDER / "NORDEND_a" / "work" / "optimization.jsonl"
|
||||
assert stream.read_text().endswith('{"type": "end", "state": "done"}\n'), stream.read_text()
|
||||
analysis.analyses.clear()
|
||||
analysis.optimizations.clear()
|
||||
assert analysis.state("NORDEND_a")["optimization"]["state"] == "done"
|
||||
stream.write_text('{"type": "assistant", "message": {"content": []}}\n')
|
||||
analysis.optimizations.clear()
|
||||
assert analysis.state("NORDEND_a")["optimization"]["state"] == "interrupted"
|
||||
(analysis.SESSIONS / "NORDEND_a_20261113T1000Z").mkdir(parents=True)
|
||||
(analysis.SESSIONS / "NORDEND_a_20261113T1000Z" / "summary.md").write_text("# improve\n\n**Result**: none")
|
||||
# a new analysis of the same range (NORDEND_a_143000): its session is not the summary of NORDEND_a
|
||||
(analysis.SESSIONS / "NORDEND_a_143000_20261113T1100Z").mkdir(parents=True)
|
||||
(analysis.SESSIONS / "NORDEND_a_143000_20261113T1100Z" / "summary.md").write_text("# other analysis")
|
||||
assert analysis._summary_file("NORDEND_a").parent.name == "NORDEND_a_20261113T1000Z", analysis._summary_file("NORDEND_a")
|
||||
(analysis.FOLDER / "NORDEND_a" / "tools.md").write_text("# Tools\n\n**Tools changed**: none")
|
||||
assert analysis.state("NORDEND_a")["optimization"]["summary"]
|
||||
page = analysis.summary("NORDEND_a", False)
|
||||
assert "<strong>Result</strong>" in page and "<strong>Tools changed</strong>" in page, page
|
||||
assert analysis.state("NORDEND_b")["optimization"] is None
|
||||
finally:
|
||||
end.set()
|
||||
os.environ.pop("ANTHROPIC_API_KEY")
|
||||
if key is not None:
|
||||
os.environ["ANTHROPIC_API_KEY"] = key
|
||||
shutil.rmtree(root)
|
||||
analysis.FOLDER, analysis.subprocess.Popen, analysis.OPTIMIZER, analysis.SESSIONS, analysis.AGENTS = before
|
||||
analysis.analyses.clear()
|
||||
analysis.optimizations.clear()
|
||||
|
||||
|
||||
def check_analysis():
|
||||
# "Analyze the range" then a message (analysis.launch, analysis.message), without claude: the analyst of the agents
|
||||
# repository, its parameters, and the resumption of its session with the same agent
|
||||
import analysis
|
||||
launched = []
|
||||
|
||||
class FakeProcess:
|
||||
returncode = 0
|
||||
|
||||
def __init__(self, args, *a, **k):
|
||||
launched.append((args, k))
|
||||
self.stdout = iter([])
|
||||
|
||||
def wait(self):
|
||||
return 0
|
||||
|
||||
(station.ROOT / "out").mkdir(exist_ok=True) # under the root: the request cites the output folder relative to it
|
||||
before = analysis.FOLDER, analysis.subprocess.Popen, analysis.submit
|
||||
analysis.FOLDER = Path(tempfile.mkdtemp(dir=station.ROOT / "out"))
|
||||
analysis.subprocess.Popen = FakeProcess
|
||||
analysis.submit = lambda *a: 0 # otherwise the test rewrites the real labeling/auto/NORDEND.jsonl
|
||||
key = os.environ.get("ANTHROPIC_API_KEY")
|
||||
os.environ["ANTHROPIC_API_KEY"] = "test-key" # the agent runs on the subscription, never on an API key
|
||||
try:
|
||||
name = analysis.launch({"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z"})["name"]
|
||||
while analysis.analyses[name]["state"] == "running":
|
||||
time.sleep(0.05)
|
||||
assert analysis.analyses[name]["state"] == "done", analysis.analyses[name]
|
||||
assert json.loads((analysis.FOLDER / name / "request.json").read_text()) == \
|
||||
{"station": "NORDEND", "start": "2026-11-05T06:00Z", "end": "2026-11-05T12:00Z", "hints": ""}
|
||||
args, k = launched[0]
|
||||
assert args[args.index("--agent") + 1] == "agents:analyst" and "--plugin-dir" in args, args
|
||||
assert "ANTHROPIC_API_KEY" not in k["env"] and "PATH" in k["env"], sorted(k.get("env") or {})
|
||||
assert args[2].startswith("Station: data/NORDEND/. Range: 2026-11-05T06:00Z → 2026-11-05T12:00Z (UTC). Hints: none. ") \
|
||||
and "DETECTORS: no" in args[2] and "INSTRUCTIONS: instructions/analyst.md" in args[2], args[2]
|
||||
assert args[args.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in args, args
|
||||
(analysis.FOLDER / name / "work" / "session").write_text("s-1")
|
||||
analysis.message(name, {"text": "continue"})
|
||||
while analysis.analyses[name]["state"] == "running":
|
||||
time.sleep(0.05)
|
||||
args, k = launched[1]
|
||||
assert "ANTHROPIC_API_KEY" not in k["env"], sorted(k.get("env") or {})
|
||||
assert args[args.index("--resume") + 1] == "s-1" and args[args.index("--agent") + 1] == "agents:analyst" \
|
||||
and "--plugin-dir" in args, args
|
||||
agents, analysis.AGENTS = analysis.AGENTS, analysis.FOLDER / "no-agents" # the agents/ link is missing: clear refusal
|
||||
try:
|
||||
assert "agents/" in refuses(analysis.launch, {"station": "NORDEND", "start": "2026-11-06T06:00Z", "end": "2026-11-06T12:00Z"})
|
||||
finally:
|
||||
analysis.AGENTS = agents
|
||||
finally:
|
||||
os.environ.pop("ANTHROPIC_API_KEY")
|
||||
if key is not None:
|
||||
os.environ["ANTHROPIC_API_KEY"] = key
|
||||
shutil.rmtree(analysis.FOLDER)
|
||||
analysis.FOLDER, analysis.subprocess.Popen, analysis.submit = before
|
||||
analysis.analyses.clear()
|
||||
|
||||
|
||||
CHECKS = [check_server, check_station, check_optimization, check_analysis]
|
||||
|
||||
if __name__ == "__main__":
|
||||
name = sys.argv[1] if len(sys.argv) > 1 else None
|
||||
for f in CHECKS:
|
||||
if name in (None, f.__name__):
|
||||
f()
|
||||
print("ok", f.__name__)
|
||||
@@ -0,0 +1,165 @@
|
||||
"""The web app: .venv/bin/python app/server/server.py → http://127.0.0.1:8770
|
||||
|
||||
The FastAPI server serves the built React page (app/web/dist, `npm run build` in app/web) and the API under /api.
|
||||
The Data page reads data/ and the labels of labeling/ (station.py); its Analysis tab launches the analyst, and the
|
||||
optimization of the detectors (analysis.py).
|
||||
Settings: AIMSIGHT_DATA (data), AIMSIGHT_LABELS (labels), AIMSIGHT_PORT (port, 8770).
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import webbrowser
|
||||
from pathlib import Path
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, Query, Request
|
||||
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
||||
|
||||
import analysis
|
||||
import station
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
APP = Path(__file__).resolve().parents[1]
|
||||
|
||||
PORT = int(os.environ.get("AIMSIGHT_PORT", 8770))
|
||||
HOSTS = {"127.0.0.1", "localhost"} # against DNS rebinding: another site does not read the data
|
||||
PAGE = APP / "web" / "dist"
|
||||
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
|
||||
|
||||
|
||||
def error(code: int, text: str) -> JSONResponse:
|
||||
return JSONResponse({"error": text}, code)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def guard(request: Request, call_next):
|
||||
"""Only the page itself talks to the server: right Host; in POST, a declared JSON (a page of another site cannot send
|
||||
it without a preflight request, to which the server does not answer)."""
|
||||
host = request.headers.get("host", "").rsplit(":", 1)[0]
|
||||
if host not in HOSTS or (request.method in ("POST", "PUT", "DELETE")
|
||||
and not request.headers.get("content-type", "").startswith("application/json")):
|
||||
return error(403, "request refused")
|
||||
return await call_next(request)
|
||||
|
||||
|
||||
# --- the Data page (station.py)
|
||||
|
||||
@app.exception_handler(station.Refusal)
|
||||
def refusal(_, e):
|
||||
return error(400, str(e))
|
||||
|
||||
|
||||
@app.get("/api/stations")
|
||||
def stations():
|
||||
return station.stations()
|
||||
|
||||
|
||||
@app.get("/api/images")
|
||||
def images(station_: str = Query(alias="station"), start: str = "", end: str = ""):
|
||||
return station.images(station_, start, end)
|
||||
|
||||
|
||||
@app.get("/api/image/{station_}/{name}")
|
||||
def image(station_: str, name: str):
|
||||
return FileResponse(station.image(station_, name), headers={"Cache-Control": "max-age=86400"})
|
||||
|
||||
|
||||
@app.get("/api/curves")
|
||||
def curves(station_: str = Query(alias="station"), start: str = "", end: str = ""):
|
||||
return station.curves(station_, start, end)
|
||||
|
||||
|
||||
@app.get("/api/detectors")
|
||||
def detectors(station_: str = Query(alias="station"), start: str = "", end: str = "", targets: str = ""):
|
||||
return station.detectors(station_, start, end, targets)
|
||||
|
||||
|
||||
@app.get("/api/labels")
|
||||
def labels(station_: str = Query(alias="station"), folder: str = "manual"):
|
||||
try:
|
||||
return station.read(station_, folder)
|
||||
except json.JSONDecodeError as e: # a line damaged by hand: say so, without touching the file
|
||||
return error(500, f"labeling/{folder}/{station_}.jsonl unreadable: {e}")
|
||||
|
||||
|
||||
@app.post("/api/labels")
|
||||
def add_label(body: dict):
|
||||
return station.add(body)
|
||||
|
||||
|
||||
@app.put("/api/labels")
|
||||
def update_label(body: dict):
|
||||
return station.update(body)
|
||||
|
||||
|
||||
@app.delete("/api/labels")
|
||||
def delete_label(body: dict):
|
||||
return station.delete(body)
|
||||
|
||||
|
||||
# --- the Analysis tab of the Data page (analysis.py): the analyst launched by claude -p on a range
|
||||
|
||||
@app.post("/api/analyses")
|
||||
def launch_analysis(body: dict):
|
||||
return analysis.launch(body)
|
||||
|
||||
|
||||
@app.get("/api/analyses")
|
||||
def analyses():
|
||||
return analysis.list_analyses()
|
||||
|
||||
|
||||
@app.get("/api/analyses/{name}")
|
||||
def analysis_state(name: str):
|
||||
return analysis.state(name)
|
||||
|
||||
|
||||
@app.post("/api/analyses/{name}/message")
|
||||
def analysis_message(name: str, body: dict):
|
||||
return analysis.message(name, body)
|
||||
|
||||
|
||||
@app.get("/api/analyses/{name}/report.html")
|
||||
def analysis_report(name: str, dark: bool = False):
|
||||
return HTMLResponse(analysis.report(name, dark))
|
||||
|
||||
|
||||
@app.post("/api/analyses/{name}/optimize")
|
||||
def optimize_analysis(name: str):
|
||||
return analysis.optimize(name)
|
||||
|
||||
|
||||
@app.get("/api/analyses/{name}/summary.html")
|
||||
def analysis_summary(name: str, dark: bool = False):
|
||||
return HTMLResponse(analysis.summary(name, dark))
|
||||
|
||||
|
||||
@app.get("/api/analyses/{name}/{path:path}")
|
||||
def analysis_file(name: str, path: str):
|
||||
return FileResponse(analysis.file(name, path))
|
||||
|
||||
|
||||
if PAGE.is_dir():
|
||||
app.mount("/", StaticFiles(directory=PAGE, html=True), name="page")
|
||||
else:
|
||||
@app.get("/")
|
||||
def no_page():
|
||||
return error(503, "the page is not built: cd app/web && npm install && npm run build")
|
||||
|
||||
|
||||
def open_browser(url: str) -> None:
|
||||
if "microsoft" in platform.uname().release.lower(): # WSL: the Windows browser
|
||||
subprocess.Popen(["explorer.exe", url], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
else:
|
||||
webbrowser.open(url)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
url = f"http://127.0.0.1:{PORT}"
|
||||
print(f"AIMsight diagnostic: {url}")
|
||||
if "--no-browser" not in sys.argv:
|
||||
threading.Timer(1, open_browser, [url]).start()
|
||||
uvicorn.run(app, host="127.0.0.1", port=PORT, log_level="warning", timeout_graceful_shutdown=1)
|
||||
@@ -0,0 +1,338 @@
|
||||
"""The data of the Data page: a station's images, its curves, its labels.
|
||||
|
||||
Data are read as the agents read them, tools/scripts/data.py (the tools repository, through the tools/ link); the detectors'
|
||||
events are those of detectors/scripts/categories.py (events). Read-only on data/; writes only in labeling/{auto,manual}/.
|
||||
"""
|
||||
import bisect
|
||||
import functools
|
||||
import csv
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import statistics
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
ROOT = Path(__file__).resolve().parents[2]
|
||||
sys.path[:0] = [str(ROOT / "tools" / "scripts"), str(ROOT / "detectors" / "scripts")]
|
||||
import data # noqa: E402
|
||||
from categories import events # noqa: E402
|
||||
|
||||
# data/ and labeling/ are links to ~/aimsight-data/data and ~/labeling: no resolve(), the agents see data/ and labeling/
|
||||
DATA = Path(os.environ.get("AIMSIGHT_DATA", ROOT / "data")).absolute()
|
||||
LABELS = Path(os.environ.get("AIMSIGHT_LABELS", ROOT / "labeling")).absolute()
|
||||
TOOLS = ROOT / "tools" # the reading harness (~/tools), read by the analyst and written by the tool-builder
|
||||
|
||||
|
||||
def links():
|
||||
"""--add-dir for claude -p: the real target of the data/, labeling/ and tools/ links, outside the repository."""
|
||||
return [a for d in (DATA, LABELS, TOOLS) for a in ("--add-dir", str(d.resolve()))]
|
||||
|
||||
|
||||
FOLDERS = ("auto", "manual") # those of the agents and of the page (D-105), those of the expert (D-102)
|
||||
FAMILIES = ("Visibility", "Obstruction", "Thermal displacement", "Displacement", "Target issue", "No data", "None") # D-109
|
||||
FORMAT = "%Y-%m-%dT%H:%MZ"
|
||||
MARGIN_PX = 150 # Support-Agent detecteurs.MARGE_PX: x, y are positions in the image only far from the corner
|
||||
COLORS = ["#2a78d6", "#eb6834", "#1baf7a", "#8a5cd6", "#d6a12a", "#d63b8a", "#3bb3d6", "#6b6b66"] # one per sensor
|
||||
POSE = (("pan", "#1a1a19"), ("tilt", "#8a8a85"), ("rotation", "#b5651d"))
|
||||
# The Advanced menu of Explore, in its order (Support-Agent labeling.PLOTS, QUALITE)
|
||||
PLOTS = (("error_value", "Error value"), ("x", "X [px]"), ("y", "Y [px]"),
|
||||
("relative_x", "Relative Horizontal Shift"), ("relative_y", "Relative Vertical Shift"),
|
||||
("corr_x", "Relative Corrected Horizontal Shift"), ("corr_y", "Relative Corrected Vertical Shift"),
|
||||
("reproj_err", "References (reproj. error)"))
|
||||
QUALITY = (("peak", "Quality: Peak"), ("flux", "Quality: Flux"), ("sharpness", "Quality: Sharpness"),
|
||||
("fwhm", "Quality: FWHM [px]"), ("dominance", "Quality: Dominance"), ("snr", "Quality: SNR"))
|
||||
IDENTIFIER = re.compile(r"(SIM_CCID|ip(_.*)?)$") # never read (CLAUDE.md, "data/")
|
||||
METRIC = re.compile(r"^(.+?)_\d{4}_\d{2}_\d{2}_")
|
||||
|
||||
|
||||
class Refusal(ValueError):
|
||||
"""A request to refuse (400), with its reason."""
|
||||
|
||||
|
||||
def stations():
|
||||
return sorted(d.name for d in DATA.iterdir() if d.is_dir())
|
||||
|
||||
|
||||
def folder(station):
|
||||
if station not in stations(): # the name becomes a path: a folder of data/, nothing else
|
||||
raise Refusal(f"unknown station: {station}")
|
||||
return DATA / station
|
||||
|
||||
|
||||
def bounds(start, end):
|
||||
return data.parse_time(start or "0001-01-01"), data.parse_time(end or "9999-01-01")
|
||||
|
||||
|
||||
# --- the positions of the sensors in the image
|
||||
|
||||
def positions(measurements, a, b):
|
||||
"""The median position of each sensor in the image over [a, b) ({} if x, y are not positions there)."""
|
||||
homes = {s: (statistics.median(xs), statistics.median(ys)) for s, ms in measurements.items()
|
||||
if (xs := [m["x"] for m in ms if a <= m["t"] < b and m["x"] is not None])
|
||||
and (ys := [m["y"] for m in ms if a <= m["t"] < b and m["y"] is not None])}
|
||||
return homes if any(abs(x) > MARGIN_PX or abs(y) > MARGIN_PX for x, y in homes.values()) else {}
|
||||
|
||||
|
||||
def references(measurements, a, b):
|
||||
"""Sensors of the compensation over the range: those with a reproj_err (references only)."""
|
||||
return {s for s, ms in measurements.items() if any(m["reproj_err"] is not None for m in ms if a <= m["t"] < b)}
|
||||
|
||||
|
||||
# --- the images of the Data page
|
||||
|
||||
def images(station, start, end):
|
||||
"""The images of [start, end), the median positions of the sensors, the references, and for each circled sensor its
|
||||
corr_x, corr_y at the time of each image: the nearest valid measurement, within 30 min, otherwise 0."""
|
||||
d, (a, b) = folder(station), bounds(start, end)
|
||||
views = [(t, im) for im in sorted(glob.glob(str(d / "images" / "*"))) if (t := data.image_time(im)) and a <= t < b]
|
||||
if not views:
|
||||
raise Refusal(f"no image of {station} in this range")
|
||||
measurements = data.load(d)
|
||||
homes, refs = positions(measurements, a, b), references(measurements, a, b)
|
||||
# ponytail: a single measurement at each bound, noisy; a median over a window if the arrows shake too much
|
||||
margin, corr = timedelta(minutes=30), {}
|
||||
for s in homes:
|
||||
series = [(m["t"], m["corr_x"], m["corr_y"]) for m in measurements[s] if m["corr_x"] is not None and m["corr_y"] is not None]
|
||||
times, corr[s] = [m[0] for m in series], []
|
||||
for t, _ in views:
|
||||
k = bisect.bisect_left(times, t)
|
||||
k = min((n for n in (k - 1, k) if 0 <= n < len(series)), key=lambda n: abs(times[n] - t), default=None)
|
||||
corr[s].append([round(series[k][1], 4), round(series[k][2], 4)] if k is not None and abs(times[k] - t) <= margin else 0)
|
||||
return {"images": [{"t": data.iso(t).replace("T", " ").rstrip("Z"), "name": os.path.basename(im)} for t, im in views],
|
||||
"positions": {s: [round(x, 1), round(y, 1)] for s, (x, y) in sorted(homes.items())},
|
||||
"references": sorted(refs), "corr": corr}
|
||||
|
||||
|
||||
def image(station, name):
|
||||
f = (folder(station) / "images" / name).resolve()
|
||||
if f.parent != (DATA / station / "images").resolve() or not f.is_file():
|
||||
raise Refusal("unknown image")
|
||||
return f
|
||||
|
||||
|
||||
# --- the curves of the Data page
|
||||
|
||||
def metrics(d, a, b):
|
||||
"""The numeric system metrics of metrics/ over [a, b) -> {name: {t: value or None}} (an empty value: None, the gap
|
||||
shows), without the text metrics, the identifiers, or those with no value at all."""
|
||||
station, series, text = d.name + "_", {}, set()
|
||||
for p in sorted(glob.glob(str(d / "metrics" / "*.csv"))):
|
||||
if not (m := METRIC.match(os.path.basename(p))) or IDENTIFIER.match(name := m.group(1).removeprefix(station)):
|
||||
continue
|
||||
with open(p, newline="") as f:
|
||||
for r in csv.DictReader(f):
|
||||
t, v = data.parse_time(r["time_utc"]), (r.get("metric") or "").strip("[]'\" ")
|
||||
if not a <= t < b:
|
||||
continue
|
||||
try:
|
||||
series.setdefault(name, {})[t] = float(v) if v else None
|
||||
except ValueError:
|
||||
text.add(name) # link, mode…: no curve
|
||||
return {n: dict(sorted(s.items())) for n, s in series.items()
|
||||
if n not in text and any(v is not None for v in s.values())}
|
||||
|
||||
|
||||
def span(measurements, start, end):
|
||||
"""The range read: from start to end (excluded), otherwise from the first measurement to the last, plus one step."""
|
||||
every = sorted({m["t"] for ms in measurements.values() for m in ms})
|
||||
a = data.parse_time(start) if start else every[0]
|
||||
b = data.parse_time(end) if end else every[-1] + (every[-1] - every[-2] if len(every) > 1 else timedelta(hours=1))
|
||||
return a, b
|
||||
|
||||
|
||||
def curves(station, start, end):
|
||||
"""-> {A, B, T, plots}: T, the times of each series, once (key: the sensor, "" for the camera, "system:" and its
|
||||
name for a metric); plot: title and traces (name, t, y, line, mode), without empty plots or sensors."""
|
||||
d = folder(station)
|
||||
measurements = data.load(d)
|
||||
if not measurements:
|
||||
raise Refusal(f"no \"advanced\" export for {station}")
|
||||
a, b = span(measurements, start, end)
|
||||
poses = [p for p in data.load_camera(d) if a <= p["t"] < b]
|
||||
hour = lambda t: t.strftime("%Y-%m-%d %H:%M") # noqa: E731 (UTC, without time zone: as Plotly shows it)
|
||||
values = lambda s, c: [None if m[c] is None else round(m[c], 5) for m in s] # noqa: E731
|
||||
inside = {s: [m for m in measurements[s] if a <= m["t"] < b] for s in sorted(measurements)}
|
||||
refs, out = references(measurements, a, b), []
|
||||
|
||||
def per_sensor(columns):
|
||||
for col, title in columns:
|
||||
traces = [{"name": s + " (ref.)" * (s in refs), "t": s, "y": y,
|
||||
"line": {"width": 1.2, "color": COLORS[i % len(COLORS)], "dash": "dot" if s in refs else "solid"}}
|
||||
for i, (s, y) in enumerate((s, values(ms, col)) for s, ms in inside.items()) if any(v is not None for v in y)]
|
||||
if traces:
|
||||
out.append({"title": title, "traces": traces})
|
||||
|
||||
per_sensor(PLOTS)
|
||||
if poses:
|
||||
out.append({"title": "Camera movement (deg)", "traces": [{"name": c, "t": "", "y": values(poses, c),
|
||||
"line": {"width": 1.2, "color": col}} for c, col in POSE]})
|
||||
per_sensor(QUALITY)
|
||||
T = {"": [hour(p["t"]) for p in poses], **{s: [hour(m["t"]) for m in ms] for s, ms in inside.items()}}
|
||||
system = metrics(d, a, b)
|
||||
for name in sorted(system, key=str.lower):
|
||||
T["system:" + name] = [hour(t) for t in system[name]]
|
||||
out.append({"title": "System: " + name, "traces": [{"name": name, "t": "system:" + name, "mode": "lines+markers",
|
||||
"y": [None if v is None else round(v, 5) for v in system[name].values()],
|
||||
"line": {"width": 1.2, "color": COLORS[0]}}]})
|
||||
if not out:
|
||||
raise Refusal(f"no measurement of {station} in this range")
|
||||
return {"A": data.iso(a), "B": data.iso(b), "T": T, "plots": out}
|
||||
|
||||
|
||||
# --- the Data page, "Detectors" labels: the events of the journal, read-only
|
||||
|
||||
HISTORY = timedelta(days=3) # days before given to the detectors, as categories.py does (rule of 2026-10-24)
|
||||
|
||||
|
||||
# ponytail: kept until the server restarts (a minute or more per range); restart it after an /improve run outside the app
|
||||
@functools.lru_cache(maxsize=32)
|
||||
def detectors(station, start, end, targets=""):
|
||||
"""-> the detectors' events over the range, in the label format (start, end, family, description); plus kind,
|
||||
sensors, ignore (greyed area of the journal). targets: the sensors kept, comma-separated (empty: all); the others are
|
||||
removed before the detectors, as if they did not exist."""
|
||||
d = folder(station)
|
||||
every = data.load(d)
|
||||
if targets:
|
||||
keep = set(targets.split(","))
|
||||
every = {s: ms for s, ms in every.items() if s in keep}
|
||||
if not every:
|
||||
raise Refusal(f"no \"advanced\" export for {station}" + " with these targets" * bool(targets))
|
||||
a, b = span(every, start, end)
|
||||
measurements = {s: k for s, ms in every.items() if (k := [m for m in ms if a - HISTORY <= m["t"] < b])}
|
||||
evs = [v for v in events(measurements, b, str(d)) if v["end"] >= data.iso(a)] if measurements else []
|
||||
refs = {s for s, ms in measurements.items() if ms[0]["pattern_type"] == "global_ref"}
|
||||
return [{"id": f"D-{i + 1}", "start": v["start"], "end": v["end"], "family": v["category"], "kind": v["episodes"][0]["label"],
|
||||
"description": f"{v['episodes'][0]['label']}. {v['text']}", "sensors": v["sensors"], "ignore": v["ignore"],
|
||||
"targets": [s for s in v["sensors"] if s not in refs], "references": [s for s in v["sensors"] if s in refs],
|
||||
"message": message(v["text"]), "action": action(v)}
|
||||
for i, v in enumerate(evs)]
|
||||
|
||||
|
||||
def message(text):
|
||||
"""What happens: the first sentence of the journal (detectors/scripts/categories.py, sentence), up to its ";"; the rest
|
||||
says what to do about it, except the sentence of the absorbed jumps, kept. ". " only: "1.5×" or "0.30 px" do not cut."""
|
||||
same = re.search(r" (The jumps? of .* same cause\.)$", text)
|
||||
return re.split(r"\. |; ", text, maxsplit=1)[0].rstrip(".") + "." + (" " + same.group(1) if same else "")
|
||||
|
||||
|
||||
# What to do about it: the "For the client" column of labeling/families.md, per family; refined per kind (the label of the
|
||||
# main episode, with its level) after the journal sentences validated on 2026-10-24 (detectors/scripts/categories.py, sentence)
|
||||
ACTION_FAMILY = {
|
||||
"Visibility": "Do not trust the curves in this period: this is not a movement.",
|
||||
"Obstruction": "Ignore the curves of the hidden targets in this period.",
|
||||
"Thermal displacement": "Normal, nothing to do.",
|
||||
"Displacement": "To check: look at the images, then keep watching.",
|
||||
"Target issue": "Act on the target (prism, mount); ignore its curve in the meantime.",
|
||||
"No data": "The curves are missing in this period; its cause tells whether to act.",
|
||||
}
|
||||
ACTION_KIND = {
|
||||
"Poor visibility reduced": "Use the data in this period with care: they are less precise.",
|
||||
"Heat shimmer": "Nothing to do: the small fast wiggles are not movement.",
|
||||
"Camera thermal movement": "Nothing to do: this is not a movement of the structure.",
|
||||
"Brief obstruction": "Ignore this single point on the curves.",
|
||||
"Sudden jump, back to normal": "Look at the images: the cause is not established.",
|
||||
"Lasting shift": "Check whether the structure moved: look at the images.",
|
||||
"Jump, not back yet": "Check whether the structure moved: look at the images.",
|
||||
"Large shift": "Check whether the structure moved: look at the images.",
|
||||
"Slow drift": "Keep watching it.",
|
||||
}
|
||||
|
||||
|
||||
def action(v):
|
||||
ep = v["episodes"][0]
|
||||
return ACTION_KIND.get(f"{ep['label']} {ep.get('level') or ''}".strip(), ACTION_KIND.get(ep["label"])) \
|
||||
or ACTION_FAMILY.get(v["category"], "")
|
||||
|
||||
|
||||
# --- the labels
|
||||
|
||||
def label_file(station, source):
|
||||
if source not in FOLDERS:
|
||||
raise Refusal("unknown label folder")
|
||||
folder(station)
|
||||
return LABELS / source / f"{station}.jsonl"
|
||||
|
||||
|
||||
def read(station, source):
|
||||
f = label_file(station, source)
|
||||
# split and not splitlines: the latter would also cut on the Unicode separators the text may contain
|
||||
return [json.loads(l) for l in f.read_text(encoding="utf-8").split("\n") if l.strip()] if f.exists() else []
|
||||
|
||||
|
||||
def assign_ids(station, labels):
|
||||
"""As labeling/ids.py does (D-131): an id for the labels without one, the next of the station, as first field."""
|
||||
n = max((int(e["id"].rsplit("-", 1)[1]) for e in labels if "id" in e), default=0)
|
||||
for e in labels:
|
||||
if "id" not in e:
|
||||
n += 1
|
||||
e["id"] = f"{station}-{n}"
|
||||
if next(iter(e)) != "id":
|
||||
rest = {k: v for k, v in e.items() if k != "id"}
|
||||
ident = e["id"]
|
||||
e.clear()
|
||||
e.update({"id": ident}, **rest)
|
||||
|
||||
|
||||
def _validate(body, start, end):
|
||||
description = (body.get("description") or "").strip()
|
||||
try:
|
||||
empty = datetime.strptime(start, FORMAT) >= datetime.strptime(end, FORMAT)
|
||||
except (TypeError, ValueError):
|
||||
raise Refusal("unreadable bounds (YYYY-MM-DDTHH:MMZ)") from None
|
||||
if not description or empty:
|
||||
raise Refusal("empty text or empty range")
|
||||
family = body.get("family") or ""
|
||||
if family and family not in FAMILIES:
|
||||
raise Refusal("unknown family")
|
||||
return description, family
|
||||
|
||||
|
||||
def add(body):
|
||||
station, source = body.get("station"), body.get("folder", "manual")
|
||||
f = label_file(station, source)
|
||||
description, family = _validate(body, body.get("start"), body.get("end"))
|
||||
label = {"start": body["start"], "end": body["end"]} | ({"family": family} if family else {}) | {"description": description}
|
||||
if source == "auto": # an automatic label carries a stable id, the next of its station (D-131)
|
||||
assign_ids(station, read(station, "auto") + [label])
|
||||
f.parent.mkdir(parents=True, exist_ok=True)
|
||||
before = f.read_bytes() if f.exists() else b"" # a line removed by hand may leave the file without a final newline
|
||||
with open(f, "ab") as out:
|
||||
out.write(b"\n" * bool(before and not before.endswith(b"\n")) + json.dumps(label, ensure_ascii=False).encode() + b"\n")
|
||||
return label
|
||||
|
||||
|
||||
def update(body):
|
||||
before = body.get("before") or {}
|
||||
start, end = body.get("start", before.get("start")), body.get("end", before.get("end"))
|
||||
description, family = _validate(body, start, end)
|
||||
label = dict.fromkeys(("id", "start", "end", "family")) | before | {"start": start, "end": end, "family": family,
|
||||
"description": description}
|
||||
if label["id"] is None:
|
||||
del label["id"]
|
||||
if not family:
|
||||
del label["family"]
|
||||
label.pop("review", None) # changed: no longer reviewed
|
||||
return replace(body.get("station"), body.get("folder", "manual"), before, label)
|
||||
|
||||
|
||||
def delete(body):
|
||||
replace(body.get("station"), body.get("folder", "manual"), body.get("before"), None)
|
||||
return body.get("before")
|
||||
|
||||
|
||||
def replace(station, source, before, label):
|
||||
"""Replaces the label before by label (None: removes it), found by its content, not by its rank: the file may have
|
||||
changed since the page was loaded. Writes next to it then replaces: an interruption does not damage the file."""
|
||||
f = label_file(station, source)
|
||||
lines = f.read_text(encoding="utf-8").split("\n") if f.exists() else []
|
||||
for i, line in enumerate(lines):
|
||||
if line.strip() and json.loads(line) == before:
|
||||
lines[i:i + 1] = [] if label is None else [json.dumps(label, ensure_ascii=False)]
|
||||
temp = f.with_name(f.name + ".tmp")
|
||||
temp.write_text("\n".join(lines), encoding="utf-8")
|
||||
os.replace(temp, f)
|
||||
return label
|
||||
raise Refusal("this label is no longer in the file: reload the page")
|
||||
@@ -1,318 +0,0 @@
|
||||
"""L'onglet Analysis de la page Data : l'analyste du dépôt agents (agents/, un plugin : --agent agents:analyste), lancé par
|
||||
`claude -p`, non interactif, sur une plage choisie dans la page, avec les indices facultatifs de l'utilisateur, ses consignes
|
||||
(consignes/analyste.md) et sans les détecteurs (DETECTEURS : non : ses étiquettes serviront à les améliorer). Il écrit dans
|
||||
labeling/auto/analyses/<STATION>_<début>_<fin>/ ses étiquettes (etiquettes.jsonl) et un rapport de diagnostic (rapport.md, ses
|
||||
figures à côté) ; à la fin, le serveur verse ses étiquettes dans labeling/auto/<STATION>.jsonl, marquées de leur analyse
|
||||
(champ `analyse`). Puis l'utilisateur peut lui écrire : la même session reprend (`claude -p --resume`), et ses étiquettes, si
|
||||
elles changent, remplacent celles qu'il avait versées.
|
||||
Sur une analyse finie, « Optimiser » lance `/improve` dans Optimizer (optimizer/, le skill qui améliore les détecteurs sur la
|
||||
plage de l'analyse, ses étiquettes pour vérité), puis le tool-builder du dépôt agents, qui fait entrer les scripts de
|
||||
l'analyste dans les outils de lecture (tools/) ; une optimisation à la fois, ni analyse ni message pendant ; le bilan de la
|
||||
séance (optimizer/seances/<analyse>_<heure>/summary.md) et celui des outils (<analyse>/outils.md) s'affichent sous le rapport.
|
||||
"""
|
||||
import html
|
||||
import json
|
||||
import os
|
||||
import subprocess
|
||||
import threading
|
||||
from datetime import date, datetime, timedelta, timezone
|
||||
|
||||
import markdown
|
||||
|
||||
import station
|
||||
|
||||
DOSSIER = station.ETIQUETTES / "auto" / "analyses"
|
||||
DETECTEURS = station.RACINE / "detectors"
|
||||
AGENTS = station.RACINE / "agents" # le plugin des agents génériques (~/agents)
|
||||
OPTIMIZER = station.RACINE / "optimizer" # le dépôt Optimizer (~/Optimizer), où tourne /improve
|
||||
SEANCES = OPTIMIZER / "seances"
|
||||
# la requête de /improve (optimizer/README.md), chemins relatifs à la racine d'Optimizer (ses liens data, labeling, detectors)
|
||||
AMELIORER = """/improve DETECTORS: detectors/. STATION: data/{station}. FROM: {du}. TO: {au}. \
|
||||
LABELS: labeling/auto/{station}.jsonl. REFERENCE: --labels labeling/auto --data data. FAMILIES: labeling/familles.md. \
|
||||
CONTEXT: labeling/auto/analyses/{nom}/rapport.md. OUT: seances/{nom}_{heure}/. Caller's context: Diagnostic-app, through \
|
||||
Optimizer; read-only on data/ and labeling/; commit only in detectors/, on its current branch; do not commit OUT."""
|
||||
DEMANDE = """Station : data/{station}/. Plage : {debut} → {fin} (UTC). Indices de l'utilisateur : {indices}. \
|
||||
Dossier de sortie : {sortie}/. DETECTEURS : non. CONSIGNES : consignes/analyste.md."""
|
||||
|
||||
# nom -> {"etat": "en cours" | "fini" | "erreur", "lignes": [...], "versees": n} ; une analyse à la fois par plage
|
||||
analyses, verrou = {}, threading.Lock()
|
||||
# nom de l'analyse -> {"etat": "en cours" | "fini" | "erreur", "lignes": [...]} ; une optimisation à la fois, toutes analyses
|
||||
optimisations = {}
|
||||
OCCUPE = "une optimisation des détecteurs est en cours"
|
||||
|
||||
|
||||
ISOLE = ["--setting-sources", "project,local", "--strict-mcp-config"] # ni plugins, ni hooks, ni serveurs MCP du poste
|
||||
|
||||
|
||||
def _env():
|
||||
"""L'environnement de claude -p, sans clé d'API : les agents tournent sur l'abonnement Claude, jamais sur une clé."""
|
||||
return {k: v for k, v in os.environ.items() if k not in ("ANTHROPIC_API_KEY", "ANTHROPIC_AUTH_TOKEN")}
|
||||
|
||||
|
||||
def _optimise():
|
||||
return any(o["etat"] == "en cours" for o in optimisations.values())
|
||||
|
||||
|
||||
def lancer(corps):
|
||||
st, debut, fin = corps.get("station"), corps.get("debut"), corps.get("fin")
|
||||
station._valide({"description": "-"}, debut, fin)
|
||||
station.dossier(st)
|
||||
if not (AGENTS / ".claude-plugin" / "plugin.json").exists():
|
||||
raise station.Refus("pas d'agents en agents/ (le lien : README.md)")
|
||||
nom = f"{st}_{debut[:16].replace(':', '')}_{fin[:16].replace(':', '')}"
|
||||
with verrou:
|
||||
if _optimise():
|
||||
raise station.Refus(OCCUPE)
|
||||
if analyses.get(nom, {}).get("etat") == "en cours":
|
||||
raise station.Refus("cette plage est déjà en cours d'analyse")
|
||||
sortie = DOSSIER / nom
|
||||
if sortie.exists(): # une nouvelle analyse de la même plage : l'ancienne reste, à côté
|
||||
nom += datetime.now(timezone.utc).strftime("_%H%M%S")
|
||||
sortie = DOSSIER / nom
|
||||
indices = (corps.get("indices") or "").strip()
|
||||
demande = {"station": st, "debut": debut, "fin": fin, "indices": indices}
|
||||
analyses[nom] = {"etat": "en cours", "lignes": [], "versees": 0, "station": st, "demande": demande}
|
||||
(sortie / "travail").mkdir(parents=True)
|
||||
(sortie / "demande.json").write_text(json.dumps(demande, ensure_ascii=False, indent=1), encoding="utf-8")
|
||||
prompt = DEMANDE.format(station=st, debut=debut, fin=fin, sortie=sortie.relative_to(station.RACINE),
|
||||
indices=indices or "aucun")
|
||||
threading.Thread(target=travailler, args=(nom, sortie, prompt), daemon=True).start()
|
||||
return {"nom": nom}
|
||||
|
||||
|
||||
def travailler(nom, sortie, prompt, reprise=None):
|
||||
"""Un tour de l'analyste : la demande, ou un message de l'utilisateur dans la même session (reprise : son id)."""
|
||||
a = analyses[nom]
|
||||
try:
|
||||
p = subprocess.Popen(["claude", "-p", prompt, "--plugin-dir", str(AGENTS), "--agent", "agents:analyste", *ISOLE,
|
||||
"--output-format", "stream-json", "--verbose",
|
||||
"--allowedTools", "Read,Write,Bash", "--permission-mode", "acceptEdits", *station.liens()]
|
||||
+ (["--resume", reprise] if reprise else []),
|
||||
cwd=station.RACINE, env=_env(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
with open(sortie / "travail" / "journal.jsonl", "a", encoding="utf-8") as journal:
|
||||
for ligne in p.stdout:
|
||||
journal.write(ligne)
|
||||
a["lignes"] += resume(ligne)
|
||||
if '"subtype":"init"' in ligne: # la session, pour les messages suivants
|
||||
(sortie / "travail" / "session").write_text(json.loads(ligne)["session_id"])
|
||||
if p.wait():
|
||||
raise RuntimeError(f"claude a fini avec le code {p.returncode}")
|
||||
a["versees"] = verser(a["station"], nom, sortie)
|
||||
a["etat"] = "fini"
|
||||
except Exception as e: # noqa: BLE001 — l'erreur s'affiche dans la page, le serveur continue
|
||||
a["lignes"].append(f"Erreur : {e}")
|
||||
a["etat"] = "erreur"
|
||||
|
||||
|
||||
def message(nom, corps):
|
||||
"""Un message de l'utilisateur à l'analyste, après son rapport : la même session reprend (claude -p --resume)."""
|
||||
texte, sortie = (corps.get("texte") or "").strip(), DOSSIER / nom
|
||||
etat(nom) # le nom vérifié, l'état relu après un redémarrage
|
||||
a, session = analyses[nom], sortie / "travail" / "session"
|
||||
if not texte or not session.exists():
|
||||
raise station.Refus("message vide, ou l'analyse n'a pas de session à reprendre")
|
||||
with verrou:
|
||||
if _optimise():
|
||||
raise station.Refus(OCCUPE)
|
||||
if a["etat"] == "en cours":
|
||||
raise station.Refus("l'analyste travaille encore")
|
||||
a["etat"] = "en cours"
|
||||
ligne = json.dumps({"type": "page", "texte": texte}, ensure_ascii=False) + "\n"
|
||||
with open(sortie / "travail" / "journal.jsonl", "a", encoding="utf-8") as journal:
|
||||
journal.write(ligne)
|
||||
a["lignes"] += resume(ligne)
|
||||
prompt = (f"{texte}\n\n(Si tu changes tes étiquettes, réécris {sortie.relative_to(station.RACINE)}/etiquettes.jsonl en "
|
||||
"entier, le serveur les reversera ; si le diagnostic change, mets rapport.md à jour.)")
|
||||
threading.Thread(target=travailler, args=(nom, sortie, prompt, session.read_text()), daemon=True).start()
|
||||
return {}
|
||||
|
||||
|
||||
def resume(ligne):
|
||||
"""Une ligne du journal -> ce que la page en montre : les messages de l'utilisateur (« › »), les textes de l'analyste, ses
|
||||
outils et leur commande, en une ligne chacun."""
|
||||
try:
|
||||
m = json.loads(ligne)
|
||||
except json.JSONDecodeError:
|
||||
return [ligne.strip()[:200]] if ligne.strip() else []
|
||||
if m.get("type") == "page":
|
||||
return [f"› {m['texte']}"]
|
||||
if m.get("type") != "assistant":
|
||||
return []
|
||||
out = []
|
||||
for c in m["message"].get("content", []):
|
||||
if c.get("type") == "text" and c["text"].strip():
|
||||
out.append(c["text"].strip()[:300])
|
||||
elif c.get("type") == "tool_use":
|
||||
i = c.get("input", {})
|
||||
out.append(f"{c['name']} : {i.get('command') or i.get('file_path') or ''}"[:200])
|
||||
return out
|
||||
|
||||
|
||||
def verser(st, nom, sortie):
|
||||
"""Les étiquettes de l'analyse dans labeling/auto/<STATION>.jsonl, avec leur id et le nom de l'analyse ; si l'analyste les
|
||||
a changées depuis le dernier versement, elles remplacent celles qu'il y avait versées -> leur nombre."""
|
||||
fichier, copie = sortie / "etiquettes.jsonl", sortie / "travail" / "versees.jsonl"
|
||||
texte = fichier.read_text(encoding="utf-8") if fichier.exists() else ""
|
||||
nouvelles = []
|
||||
for l in texte.split("\n"):
|
||||
if l.strip():
|
||||
e = json.loads(l)
|
||||
station._valide(e, e.get("debut"), e.get("fin"))
|
||||
nouvelles.append({k: e[k] for k in ("debut", "fin", "categorie", "description") if k in e} | {"analyse": nom})
|
||||
if copie.exists() and copie.read_text(encoding="utf-8") == texte:
|
||||
return len(nouvelles)
|
||||
# ponytail: retirées puis remises, les étiquettes de l'analyse perdent les retouches faites dans la page ; les garder
|
||||
# demanderait de les apparier une à une
|
||||
with verrou:
|
||||
f = station.fichier(st, "auto") # les autres lignes restent telles quelles
|
||||
gardees = [l for l in (f.read_text(encoding="utf-8").split("\n") if f.exists() else [])
|
||||
if l.strip() and json.loads(l).get("analyse") != nom]
|
||||
station.attribuer(st, [json.loads(l) for l in gardees] + nouvelles)
|
||||
provisoire = f.with_name(f.name + ".tmp")
|
||||
provisoire.write_text("".join(l + "\n" for l in gardees + [json.dumps(e, ensure_ascii=False) for e in nouvelles]),
|
||||
encoding="utf-8")
|
||||
os.replace(provisoire, f)
|
||||
copie.write_text(texte, encoding="utf-8")
|
||||
return len(nouvelles)
|
||||
|
||||
|
||||
def etat(nom):
|
||||
sortie = DOSSIER / nom
|
||||
if "/" in nom or nom.startswith(".") or not sortie.is_dir():
|
||||
raise station.Refus("analyse inconnue")
|
||||
if nom not in analyses: # après un redémarrage du serveur, il ne reste que le dossier : le fil se relit dans le journal
|
||||
journal = sortie / "travail" / "journal.jsonl"
|
||||
lignes = [r for l in open(journal, encoding="utf-8") for r in resume(l)] if journal.exists() else []
|
||||
demande = json.loads((sortie / "demande.json").read_text(encoding="utf-8"))
|
||||
analyses[nom] = {"etat": "fini" if (sortie / "rapport.md").exists() else "interrompue", "lignes": lignes,
|
||||
"versees": None, "station": demande["station"], "demande": demande}
|
||||
a, r = analyses[nom], sortie / "rapport.md"
|
||||
return {"nom": nom, "etat": a["etat"], "lignes": a["lignes"], "versees": a["versees"],
|
||||
"rapport": int(r.stat().st_mtime) if r.exists() else None,
|
||||
"dialogue": (sortie / "travail" / "session").exists(), "demande": a["demande"], "optimisation": optimisation(nom)}
|
||||
|
||||
|
||||
def liste():
|
||||
"""L'historique des analyses (la barre latérale), la plus récente en tête : celles du serveur avec leur état, les autres
|
||||
lues sur le disque (fini avec un rapport, sinon interrompue : une analyse s'arrête avec le serveur)."""
|
||||
out = []
|
||||
for d in sorted(DOSSIER.glob("*/demande.json"), key=lambda f: f.parent.stat().st_mtime, reverse=True):
|
||||
nom = d.parent.name
|
||||
a = analyses.get(nom) or {"etat": "fini" if (d.parent / "rapport.md").exists() else "interrompue",
|
||||
"demande": json.loads(d.read_text(encoding="utf-8"))}
|
||||
out.append({"nom": nom, "etat": a["etat"]} | a["demande"])
|
||||
return out
|
||||
|
||||
|
||||
def fichier(nom, chemin):
|
||||
"""Un fichier du dossier de l'analyse (le rapport, ses figures), jamais au-dehors."""
|
||||
f = (DOSSIER / nom / chemin).resolve()
|
||||
if not f.is_file() or not f.is_relative_to(DOSSIER.resolve()):
|
||||
raise station.Refus("fichier inconnu")
|
||||
return f
|
||||
|
||||
|
||||
STYLE = """body{font:14px/1.55 Roboto,sans-serif;margin:0;padding:4px 2px;color:#212529;background:transparent}
|
||||
h1{font-size:20px}h2{font-size:16px;margin-top:22px}img{max-width:100%;border:1px solid #dee2e6;border-radius:4px}
|
||||
code{font-size:12px;background:#f1f3f5;padding:1px 4px;border-radius:3px}pre{overflow-x:auto}
|
||||
table{border-collapse:collapse}td,th{border:1px solid #dee2e6;padding:3px 8px;text-align:left}
|
||||
body.sombre{color:#c9c9c9}body.sombre code{background:#2e2e2e}body.sombre td,body.sombre th,body.sombre img{border-color:#424242}"""
|
||||
|
||||
|
||||
def _page(texte, titre, sombre):
|
||||
"""Un markdown en HTML, au style de la page ; ses images relatives se résolvent dans le dossier de l'adresse."""
|
||||
corps = markdown.markdown(texte, extensions=["tables", "fenced_code"])
|
||||
return (f"<!doctype html><meta charset=utf-8><title>{html.escape(titre)}</title><style>{STYLE}</style>"
|
||||
f"<body class='{'sombre' if sombre else ''}'>{corps}</body>")
|
||||
|
||||
|
||||
def rapport(nom, sombre):
|
||||
"""rapport.md en HTML, servi dans le dossier de l'analyse : ses images relatives s'y retrouvent."""
|
||||
return _page(fichier(nom, "rapport.md").read_text(encoding="utf-8"), nom, sombre)
|
||||
|
||||
|
||||
# --- « Optimiser » : /improve dans Optimizer sur l'analyse, puis le tool-builder, par claude -p
|
||||
|
||||
def _bilan(nom):
|
||||
"""Le bilan de la dernière séance de cette analyse, ou None. Le motif de l'heure écarte les séances d'une autre analyse
|
||||
de la même plage (<nom>_<HHMMSS>_<heure>)."""
|
||||
bilans = sorted(SEANCES.glob(f"{nom}_????????T????Z/summary.md")) # l'ordre du nom est celui du temps
|
||||
return bilans[-1] if bilans else None
|
||||
|
||||
|
||||
def optimisation(nom):
|
||||
"""-> None (jamais optimisée) ou {etat, lignes, bilan : mtime du dernier bilan}. Après un redémarrage, l'état se relit dans
|
||||
le flux : sa dernière ligne {"type": "fin"} dit fini ou erreur ; sans elle, l'optimisation a été coupée (interrompue)."""
|
||||
flux, b = DOSSIER / nom / "travail" / "optimisation.jsonl", _bilan(nom)
|
||||
o = optimisations.get(nom)
|
||||
if o is None:
|
||||
if not flux.exists():
|
||||
return None
|
||||
lignes = flux.read_text(encoding="utf-8").splitlines()
|
||||
fin = json.loads(lignes[-1]) if lignes and lignes[-1].startswith('{"type": "fin"') else None
|
||||
o = {"etat": "interrompue" if fin is None else fin["etat"], "lignes": [r for l in lignes for r in resume(l)]}
|
||||
optimisations[nom] = o
|
||||
return {"etat": o["etat"], "lignes": o["lignes"], "bilan": int(b.stat().st_mtime) if b else None}
|
||||
|
||||
|
||||
def optimiser(nom):
|
||||
"""Le bouton « Optimiser » sur une analyse finie : la séance dans un fil ; refusée si une optimisation ou une analyse tourne."""
|
||||
if etat(nom)["etat"] != "fini":
|
||||
raise station.Refus("seule une analyse finie s'optimise")
|
||||
if not ((OPTIMIZER / ".claude" / "skills" / "improve" / "SKILL.md").exists()
|
||||
and all((OPTIMIZER / lien).exists() for lien in ("data", "labeling", "detectors"))
|
||||
and (AGENTS / ".claude-plugin" / "plugin.json").exists()):
|
||||
raise station.Refus("pas d'Optimizer en optimizer/ ou pas d'agents en agents/ (les liens : README.md)")
|
||||
with verrou:
|
||||
if _optimise():
|
||||
raise station.Refus(OCCUPE)
|
||||
if any(a["etat"] == "en cours" for a in analyses.values()):
|
||||
raise station.Refus("une analyse est en cours : l'optimisation attend sa fin")
|
||||
optimisations[nom] = {"etat": "en cours", "lignes": []}
|
||||
threading.Thread(target=optimiser_fil, args=(nom,), daemon=True).start()
|
||||
return {}
|
||||
|
||||
|
||||
def optimiser_fil(nom):
|
||||
"""Les deux tours de l'optimisation, l'un après l'autre : les détecteurs (/improve, dans Optimizer), puis les outils de
|
||||
lecture (le tool-builder, même si les détecteurs n'ont pas changé)."""
|
||||
o, sortie = optimisations[nom], DOSSIER / nom
|
||||
rel = os.path.relpath(sortie, station.RACINE)
|
||||
(sortie / "travail").mkdir(exist_ok=True)
|
||||
with open(sortie / "travail" / "optimisation.jsonl", "a", encoding="utf-8") as flux:
|
||||
try:
|
||||
d = json.loads((sortie / "demande.json").read_text(encoding="utf-8"))
|
||||
requete = AMELIORER.format( # TO exclu : le jour après la fin de la plage, comme la page
|
||||
station=d["station"], du=d["debut"][:10], au=date.fromisoformat(d["fin"][:10]) + timedelta(days=1),
|
||||
nom=nom, heure=f"{datetime.now(timezone.utc):%Y%m%dT%H%MZ}")
|
||||
# ponytail: Optimizer lit data/ et labeling/ par ses propres liens ; AIMSIGHT_DATA / AIMSIGHT_LABELS ne le suivent pas
|
||||
tours = [(OPTIMIZER.resolve(), [requete, "--plugin-dir", str(AGENTS.resolve()),
|
||||
"--add-dir", str(DETECTEURS.resolve()), # la cible du lien, où la séance écrit
|
||||
"--allowedTools", "Read,Write,Edit,Bash,Agent"]),
|
||||
(station.RACINE, [f"Analyse : {rel}/. Outils : tools/. Écris {rel}/outils.md.", "--plugin-dir", str(AGENTS),
|
||||
"--agent", "agents:tool-builder", "--allowedTools", "Read,Write,Edit,Bash"])]
|
||||
for dossier, (prompt, *options) in tours:
|
||||
p = subprocess.Popen(["claude", "-p", prompt, *ISOLE, "--output-format", "stream-json", "--verbose",
|
||||
"--permission-mode", "acceptEdits", *station.liens(), *options],
|
||||
cwd=dossier, env=_env(), stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True)
|
||||
for ligne in p.stdout:
|
||||
flux.write(ligne)
|
||||
o["lignes"] += resume(ligne)
|
||||
if p.wait():
|
||||
raise RuntimeError(f"claude a fini avec le code {p.returncode}")
|
||||
o["etat"] = "fini"
|
||||
except Exception as e: # noqa: BLE001 — l'erreur s'affiche dans la page, le serveur continue
|
||||
o["lignes"].append(f"Erreur : {e}")
|
||||
o["etat"] = "erreur"
|
||||
finally:
|
||||
flux.write(json.dumps({"type": "fin", "etat": o["etat"]}) + "\n")
|
||||
station.detecteurs.cache_clear() # les détecteurs ont pu changer : la page Data les recalcule
|
||||
|
||||
|
||||
def bilan(nom, sombre):
|
||||
"""Le bilan de la séance des détecteurs, puis celui des outils (<analyse>/outils.md), en une page."""
|
||||
b, outils = _bilan(nom), DOSSIER / nom / "outils.md"
|
||||
if b is None and not outils.exists():
|
||||
raise station.Refus("aucun bilan pour cette analyse")
|
||||
texte = "\n\n".join(f.read_text(encoding="utf-8") for f in (b, outils) if f and f.exists())
|
||||
return _page(texte, nom, sombre)
|
||||
@@ -1,261 +0,0 @@
|
||||
"""Vérifications de la web app, sans appel à claude : .venv/bin/python app/serveur/check.py [nom]"""
|
||||
import json
|
||||
import os
|
||||
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, k))
|
||||
|
||||
def wait(self):
|
||||
return 0
|
||||
|
||||
avant = analyse.DOSSIER, analyse.subprocess.Popen, analyse.OPTIMIZER, analyse.SEANCES, analyse.AGENTS
|
||||
cle = os.environ.get("ANTHROPIC_API_KEY")
|
||||
os.environ["ANTHROPIC_API_KEY"] = "cle-de-test"
|
||||
racine = seance_temporaire()
|
||||
analyse.DOSSIER, analyse.OPTIMIZER, analyse.AGENTS = racine / "analyses", racine / "optimizer", racine / "agents"
|
||||
analyse.SEANCES = analyse.OPTIMIZER / "seances"
|
||||
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
|
||||
# sans Optimizer, puis sans agents : un refus clair, pas une trace d'erreur
|
||||
assert "optimizer/" in leve(analyse.optimiser, "NORDEND_a")
|
||||
(analyse.OPTIMIZER / ".claude" / "skills" / "improve").mkdir(parents=True)
|
||||
(analyse.OPTIMIZER / ".claude" / "skills" / "improve" / "SKILL.md").write_text("improve")
|
||||
assert "agents/" in leve(analyse.optimiser, "NORDEND_a")
|
||||
(analyse.AGENTS / ".claude-plugin").mkdir(parents=True)
|
||||
(analyse.AGENTS / ".claude-plugin" / "plugin.json").write_text("{}")
|
||||
assert "optimizer/" in leve(analyse.optimiser, "NORDEND_a") # sans les liens data, labeling, detectors d'Optimizer
|
||||
for lien in ("data", "labeling", "detectors"):
|
||||
(analyse.OPTIMIZER / lien).mkdir()
|
||||
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"
|
||||
args, k = lances[0] # /improve dans Optimizer, sur la plage de l'analyse, TO exclu
|
||||
requete = args[2]
|
||||
assert requete.startswith("/improve ") and "STATION: data/NORDEND. FROM: 2026-11-04. TO: 2026-11-08." in requete \
|
||||
and "OUT: seances/NORDEND_a_" in requete, requete
|
||||
assert k["cwd"] == analyse.OPTIMIZER.resolve() and "--plugin-dir" in args, (args, k)
|
||||
assert all("ANTHROPIC_API_KEY" not in kw["env"] for _, kw in lances), "une clé d'API passe à claude"
|
||||
# isolés des réglages du poste (plugins, hooks, serveurs MCP de l'utilisateur)
|
||||
assert all(a[a.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in a for a, _ in lances)
|
||||
args, k = lances[1] # puis le tool-builder du dépôt agents, sur le dépôt tools
|
||||
assert args[args.index("--agent") + 1] == "agents:tool-builder" and "Outils : tools/" in args[2], args
|
||||
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.SEANCES / "NORDEND_a_20261113T1000Z").mkdir(parents=True)
|
||||
(analyse.SEANCES / "NORDEND_a_20261113T1000Z" / "summary.md").write_text("# improve\n\n**Result**: none")
|
||||
# une nouvelle analyse de la même plage (NORDEND_a_143000) : sa séance n'est pas le bilan de NORDEND_a
|
||||
(analyse.SEANCES / "NORDEND_a_143000_20261113T1100Z").mkdir(parents=True)
|
||||
(analyse.SEANCES / "NORDEND_a_143000_20261113T1100Z" / "summary.md").write_text("# autre analyse")
|
||||
assert analyse._bilan("NORDEND_a").parent.name == "NORDEND_a_20261113T1000Z", analyse._bilan("NORDEND_a")
|
||||
(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()
|
||||
os.environ.pop("ANTHROPIC_API_KEY")
|
||||
if cle is not None:
|
||||
os.environ["ANTHROPIC_API_KEY"] = cle
|
||||
shutil.rmtree(racine)
|
||||
analyse.DOSSIER, analyse.subprocess.Popen, analyse.OPTIMIZER, analyse.SEANCES, analyse.AGENTS = avant
|
||||
analyse.analyses.clear()
|
||||
analyse.optimisations.clear()
|
||||
|
||||
|
||||
def check_analyse():
|
||||
# « Analyser la plage » puis un message (analyse.lancer, analyse.message), sans claude : l'analyste du dépôt agents, ses
|
||||
# paramètres, et la reprise de sa session avec le même agent
|
||||
import analyse
|
||||
lances = []
|
||||
|
||||
class FauxProcessus:
|
||||
returncode = 0
|
||||
|
||||
def __init__(self, args, *a, **k):
|
||||
lances.append((args, k))
|
||||
self.stdout = iter([])
|
||||
|
||||
def wait(self):
|
||||
return 0
|
||||
|
||||
(station.RACINE / "out").mkdir(exist_ok=True) # sous la racine : la demande cite le dossier de sortie par rapport à elle
|
||||
avant = analyse.DOSSIER, analyse.subprocess.Popen, analyse.verser
|
||||
analyse.DOSSIER = Path(tempfile.mkdtemp(dir=station.RACINE / "out"))
|
||||
analyse.subprocess.Popen = FauxProcessus
|
||||
analyse.verser = lambda *a: 0 # sinon le test réécrit le vrai labeling/auto/NORDEND.jsonl
|
||||
cle = os.environ.get("ANTHROPIC_API_KEY")
|
||||
os.environ["ANTHROPIC_API_KEY"] = "cle-de-test" # l'agent tourne sur l'abonnement, jamais sur une clé d'API
|
||||
try:
|
||||
nom = analyse.lancer({"station": "NORDEND", "debut": "2026-11-05T06:00Z", "fin": "2026-11-05T12:00Z"})["nom"]
|
||||
while analyse.analyses[nom]["etat"] == "en cours":
|
||||
time.sleep(0.05)
|
||||
assert analyse.analyses[nom]["etat"] == "fini", analyse.analyses[nom]
|
||||
args, k = lances[0]
|
||||
assert args[args.index("--agent") + 1] == "agents:analyste" and "--plugin-dir" in args, args
|
||||
assert "ANTHROPIC_API_KEY" not in k["env"] and "PATH" in k["env"], sorted(k.get("env") or {})
|
||||
assert "DETECTEURS : non" in args[2] and "CONSIGNES : consignes/analyste.md" in args[2], args[2]
|
||||
assert args[args.index("--setting-sources") + 1] == "project,local" and "--strict-mcp-config" in args, args
|
||||
(analyse.DOSSIER / nom / "travail" / "session").write_text("s-1")
|
||||
analyse.message(nom, {"texte": "continue"})
|
||||
while analyse.analyses[nom]["etat"] == "en cours":
|
||||
time.sleep(0.05)
|
||||
args, k = lances[1]
|
||||
assert "ANTHROPIC_API_KEY" not in k["env"], sorted(k.get("env") or {})
|
||||
assert args[args.index("--resume") + 1] == "s-1" and args[args.index("--agent") + 1] == "agents:analyste" \
|
||||
and "--plugin-dir" in args, args
|
||||
agents, analyse.AGENTS = analyse.AGENTS, analyse.DOSSIER / "sans-agents" # le lien agents/ manque : refus clair
|
||||
try:
|
||||
assert "agents/" in leve(analyse.lancer, {"station": "NORDEND", "debut": "2026-11-06T06:00Z", "fin": "2026-11-06T12:00Z"})
|
||||
finally:
|
||||
analyse.AGENTS = agents
|
||||
finally:
|
||||
os.environ.pop("ANTHROPIC_API_KEY")
|
||||
if cle is not None:
|
||||
os.environ["ANTHROPIC_API_KEY"] = cle
|
||||
shutil.rmtree(analyse.DOSSIER)
|
||||
analyse.DOSSIER, analyse.subprocess.Popen, analyse.verser = avant
|
||||
analyse.analyses.clear()
|
||||
|
||||
|
||||
CHECKS = [check_serveur, check_station, check_optimisation, check_analyse]
|
||||
|
||||
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__)
|
||||
@@ -1,165 +0,0 @@
|
||||
"""La web app : .venv/bin/python app/serveur/serveur.py → http://127.0.0.1:8770
|
||||
|
||||
Le serveur FastAPI sert la page React construite (app/web/dist, `npm run build` dans app/web) et l'API sous /api.
|
||||
La page Data lit data/ et les étiquettes de labeling/ (station.py) ; son onglet Analysis lance l'analyste, et l'optimisation
|
||||
des détecteurs (analyse.py).
|
||||
Réglages : AIMSIGHT_DATA (données), AIMSIGHT_LABELS (étiquettes), AIMSIGHT_PORT (port, 8770).
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import platform
|
||||
import subprocess
|
||||
import sys
|
||||
import threading
|
||||
import webbrowser
|
||||
from pathlib import Path
|
||||
|
||||
import uvicorn
|
||||
from fastapi import FastAPI, Query, Request
|
||||
from fastapi.responses import FileResponse, HTMLResponse, JSONResponse
|
||||
|
||||
import analyse
|
||||
import station
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
|
||||
APP = Path(__file__).resolve().parents[1]
|
||||
|
||||
PORT = int(os.environ.get("AIMSIGHT_PORT", 8770))
|
||||
HOTES = {"127.0.0.1", "localhost"} # contre le DNS rebinding : un autre site ne lit pas les données
|
||||
PAGE = APP / "web" / "dist"
|
||||
app = FastAPI(docs_url=None, redoc_url=None, openapi_url=None)
|
||||
|
||||
|
||||
def erreur(code: int, texte: str) -> JSONResponse:
|
||||
return JSONResponse({"erreur": texte}, code)
|
||||
|
||||
|
||||
@app.middleware("http")
|
||||
async def garde(request: Request, suite):
|
||||
"""Seule la page elle-même parle au serveur : bon Host ; en POST, un JSON déclaré (une page d'un autre site ne
|
||||
peut l'envoyer sans requête préalable, à laquelle le serveur ne répond pas)."""
|
||||
hote = request.headers.get("host", "").rsplit(":", 1)[0]
|
||||
if hote not in HOTES or (request.method in ("POST", "PUT", "DELETE")
|
||||
and not request.headers.get("content-type", "").startswith("application/json")):
|
||||
return erreur(403, "requête refusée")
|
||||
return await suite(request)
|
||||
|
||||
|
||||
# --- la page Data (station.py)
|
||||
|
||||
@app.exception_handler(station.Refus)
|
||||
def refus(_, e):
|
||||
return erreur(400, str(e))
|
||||
|
||||
|
||||
@app.get("/api/stations")
|
||||
def stations():
|
||||
return station.stations()
|
||||
|
||||
|
||||
@app.get("/api/images")
|
||||
def images(station_: str = Query(alias="station"), du: str = "", au: str = ""):
|
||||
return station.images(station_, du, au)
|
||||
|
||||
|
||||
@app.get("/api/image/{station_}/{nom}")
|
||||
def image(station_: str, nom: str):
|
||||
return FileResponse(station.image(station_, nom), headers={"Cache-Control": "max-age=86400"})
|
||||
|
||||
|
||||
@app.get("/api/courbes")
|
||||
def courbes(station_: str = Query(alias="station"), du: str = "", au: str = ""):
|
||||
return station.courbes(station_, du, au)
|
||||
|
||||
|
||||
@app.get("/api/detecteurs")
|
||||
def detecteurs(station_: str = Query(alias="station"), du: str = "", au: str = "", cibles: str = ""):
|
||||
return station.detecteurs(station_, du, au, cibles)
|
||||
|
||||
|
||||
@app.get("/api/etiquettes")
|
||||
def etiquettes(station_: str = Query(alias="station"), dossier: str = "manual"):
|
||||
try:
|
||||
return station.lire(station_, dossier)
|
||||
except json.JSONDecodeError as e: # une ligne abîmée à la main : le dire, sans toucher au fichier
|
||||
return erreur(500, f"labeling/{dossier}/{station_}.jsonl illisible : {e}")
|
||||
|
||||
|
||||
@app.post("/api/etiquettes")
|
||||
def ajouter(corps: dict):
|
||||
return station.ajouter(corps)
|
||||
|
||||
|
||||
@app.put("/api/etiquettes")
|
||||
def modifier(corps: dict):
|
||||
return station.modifier(corps)
|
||||
|
||||
|
||||
@app.delete("/api/etiquettes")
|
||||
def supprimer(corps: dict):
|
||||
return station.supprimer(corps)
|
||||
|
||||
|
||||
# --- l'onglet Analysis de la page Data (analyse.py) : l'analyste lancé par claude -p sur une plage
|
||||
|
||||
@app.post("/api/analyses")
|
||||
def lancer_analyse(corps: dict):
|
||||
return analyse.lancer(corps)
|
||||
|
||||
|
||||
@app.get("/api/analyses")
|
||||
def analyses():
|
||||
return analyse.liste()
|
||||
|
||||
|
||||
@app.get("/api/analyses/{nom}")
|
||||
def etat_analyse(nom: str):
|
||||
return analyse.etat(nom)
|
||||
|
||||
|
||||
@app.post("/api/analyses/{nom}/message")
|
||||
def message_analyse(nom: str, corps: dict):
|
||||
return analyse.message(nom, corps)
|
||||
|
||||
|
||||
@app.get("/api/analyses/{nom}/rapport.html")
|
||||
def rapport_analyse(nom: str, sombre: bool = False):
|
||||
return HTMLResponse(analyse.rapport(nom, sombre))
|
||||
|
||||
|
||||
@app.post("/api/analyses/{nom}/optimiser")
|
||||
def optimiser_analyse(nom: str):
|
||||
return analyse.optimiser(nom)
|
||||
|
||||
|
||||
@app.get("/api/analyses/{nom}/bilan.html")
|
||||
def bilan_analyse(nom: str, sombre: bool = False):
|
||||
return HTMLResponse(analyse.bilan(nom, sombre))
|
||||
|
||||
|
||||
@app.get("/api/analyses/{nom}/{chemin:path}")
|
||||
def fichier_analyse(nom: str, chemin: str):
|
||||
return FileResponse(analyse.fichier(nom, chemin))
|
||||
|
||||
|
||||
if PAGE.is_dir():
|
||||
app.mount("/", StaticFiles(directory=PAGE, html=True), name="page")
|
||||
else:
|
||||
@app.get("/")
|
||||
def sans_page():
|
||||
return erreur(503, "la page n'est pas construite : cd app/web && npm install && npm run build")
|
||||
|
||||
|
||||
def ouvrir(url: str) -> None:
|
||||
if "microsoft" in platform.uname().release.lower(): # WSL : le navigateur Windows
|
||||
subprocess.Popen(["explorer.exe", url], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
||||
else:
|
||||
webbrowser.open(url)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
url = f"http://127.0.0.1:{PORT}"
|
||||
print(f"Diagnostic AIMsight : {url}")
|
||||
if "--sans-navigateur" not in sys.argv:
|
||||
threading.Timer(1, ouvrir, [url]).start()
|
||||
uvicorn.run(app, host="127.0.0.1", port=PORT, log_level="warning", timeout_graceful_shutdown=1)
|
||||
@@ -1,338 +0,0 @@
|
||||
"""Les données de la page Data : les images d'une station, ses courbes, ses étiquettes.
|
||||
|
||||
La lecture des données est celle des agents, tools/scripts/donnees.py (le dépôt tools, par le lien tools/) ; les événements des détecteurs, ceux de
|
||||
detectors/scripts/categories.py (events). Lecture seule sur data/ ; écrit seulement dans labeling/{auto,manual}/.
|
||||
"""
|
||||
import bisect
|
||||
import functools
|
||||
import csv
|
||||
import glob
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import statistics
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
RACINE = Path(__file__).resolve().parents[2]
|
||||
sys.path[:0] = [str(RACINE / "tools" / "scripts"), str(RACINE / "detectors" / "scripts")]
|
||||
import donnees # noqa: E402
|
||||
from categories import events # noqa: E402
|
||||
|
||||
# data/ et labeling/ sont des liens vers ~/aimsight-data/data et ~/labeling : pas de resolve(), les agents voient data/ et labeling/
|
||||
DONNEES = Path(os.environ.get("AIMSIGHT_DATA", RACINE / "data")).absolute()
|
||||
ETIQUETTES = Path(os.environ.get("AIMSIGHT_LABELS", RACINE / "labeling")).absolute()
|
||||
TOOLS = RACINE / "tools" # le harnais de lecture (~/tools), que lit l'analyste et où écrit le tool-builder
|
||||
|
||||
|
||||
def liens():
|
||||
"""--add-dir pour claude -p : la cible réelle des liens data/, labeling/ et tools/, hors du dépôt."""
|
||||
return [a for d in (DONNEES, ETIQUETTES, TOOLS) for a in ("--add-dir", str(d.resolve()))]
|
||||
|
||||
|
||||
DOSSIERS = ("auto", "manual") # celles des agents et de la page (D-105), celles de l'expert (D-102)
|
||||
FAMILLES = ("Visibility", "Obstruction", "Thermal displacement", "Displacement", "Target issue", "No data", "None") # D-109
|
||||
FORMAT = "%Y-%m-%dT%H:%MZ"
|
||||
MARGE_PX = 150 # detecteurs.MARGE_PX : x, y ne sont des positions dans l'image que loin du coin
|
||||
COULEURS = ["#2a78d6", "#eb6834", "#1baf7a", "#8a5cd6", "#d6a12a", "#d63b8a", "#3bb3d6", "#6b6b66"] # un capteur chacun
|
||||
POSE = (("pan", "#1a1a19"), ("tilt", "#8a8a85"), ("rotation", "#b5651d"))
|
||||
# Le menu Advanced d'Explore, dans son ordre (labeling.PLOTS, QUALITE)
|
||||
PLOTS = (("error_value", "Error value"), ("x", "X [px]"), ("y", "Y [px]"),
|
||||
("relative_x", "Relative Horizontal Shift"), ("relative_y", "Relative Vertical Shift"),
|
||||
("corr_x", "Relative Corrected Horizontal Shift"), ("corr_y", "Relative Corrected Vertical Shift"),
|
||||
("reproj_err", "References (reproj. error)"))
|
||||
QUALITE = (("peak", "Quality: Peak"), ("flux", "Quality: Flux"), ("sharpness", "Quality: Sharpness"),
|
||||
("fwhm", "Quality: FWHM [px]"), ("dominance", "Quality: Dominance"), ("snr", "Quality: SNR"))
|
||||
IDENTIFIANT = re.compile(r"(SIM_CCID|ip(_.*)?)$") # jamais lus (CLAUDE.md, « data/ »)
|
||||
METRIQUE = re.compile(r"^(.+?)_\d{4}_\d{2}_\d{2}_")
|
||||
|
||||
|
||||
class Refus(ValueError):
|
||||
"""Une demande à refuser (400), avec sa raison."""
|
||||
|
||||
|
||||
def stations():
|
||||
return sorted(d.name for d in DONNEES.iterdir() if d.is_dir())
|
||||
|
||||
|
||||
def dossier(station):
|
||||
if station not in stations(): # le nom devient un chemin : un dossier de data/, rien d'autre
|
||||
raise Refus(f"station inconnue : {station}")
|
||||
return DONNEES / station
|
||||
|
||||
|
||||
def bornes(du, au):
|
||||
return donnees.instant(du or "0001-01-01"), donnees.instant(au or "9999-01-01")
|
||||
|
||||
|
||||
# --- les positions des capteurs dans l'image
|
||||
|
||||
def positions(mesures, a, b):
|
||||
"""La position médiane de chaque capteur dans l'image sur [a, b) ({} si x, y n'y sont pas des positions)."""
|
||||
maisons = {s: (statistics.median(xs), statistics.median(ys)) for s, ms in mesures.items()
|
||||
if (xs := [m["x"] for m in ms if a <= m["t"] < b and m["x"] is not None])
|
||||
and (ys := [m["y"] for m in ms if a <= m["t"] < b and m["y"] is not None])}
|
||||
return maisons if any(abs(x) > MARGE_PX or abs(y) > MARGE_PX for x, y in maisons.values()) else {}
|
||||
|
||||
|
||||
def references(mesures, a, b):
|
||||
"""Capteurs de la compensation sur la plage : ceux qui ont un reproj_err (références seulement)."""
|
||||
return {s for s, ms in mesures.items() if any(m["reproj_err"] is not None for m in ms if a <= m["t"] < b)}
|
||||
|
||||
|
||||
# --- les images de la page Data
|
||||
|
||||
def images(station, du, au):
|
||||
"""Les images de [du, au), les positions médianes des capteurs, les références, et par capteur entouré son corr_x,
|
||||
corr_y à l'heure de chaque image : la mesure valide la plus proche, à 30 min près, sinon 0."""
|
||||
d, (a, b) = dossier(station), bornes(du, au)
|
||||
vues = [(t, im) for im in sorted(glob.glob(str(d / "images" / "*"))) if (t := donnees.heure_image(im)) and a <= t < b]
|
||||
if not vues:
|
||||
raise Refus(f"aucune image de {station} sur cette plage")
|
||||
mesures = donnees.charger(d)
|
||||
maisons, refs = positions(mesures, a, b), references(mesures, a, b)
|
||||
# ponytail: une seule mesure à chaque borne, bruitée ; une médiane sur une fenêtre si les flèches tremblent trop
|
||||
marge, corr = timedelta(minutes=30), {}
|
||||
for s in maisons:
|
||||
serie = [(m["t"], m["corr_x"], m["corr_y"]) for m in mesures[s] if m["corr_x"] is not None and m["corr_y"] is not None]
|
||||
temps, corr[s] = [m[0] for m in serie], []
|
||||
for t, _ in vues:
|
||||
k = bisect.bisect_left(temps, t)
|
||||
k = min((n for n in (k - 1, k) if 0 <= n < len(serie)), key=lambda n: abs(temps[n] - t), default=None)
|
||||
corr[s].append([round(serie[k][1], 4), round(serie[k][2], 4)] if k is not None and abs(temps[k] - t) <= marge else 0)
|
||||
return {"images": [{"t": donnees.iso(t).replace("T", " ").rstrip("Z"), "nom": os.path.basename(im)} for t, im in vues],
|
||||
"positions": {s: [round(x, 1), round(y, 1)] for s, (x, y) in sorted(maisons.items())},
|
||||
"references": sorted(refs), "corr": corr}
|
||||
|
||||
|
||||
def image(station, nom):
|
||||
f = (dossier(station) / "images" / nom).resolve()
|
||||
if f.parent != (DONNEES / station / "images").resolve() or not f.is_file():
|
||||
raise Refus("image inconnue")
|
||||
return f
|
||||
|
||||
|
||||
# --- les courbes de la page Data
|
||||
|
||||
def metriques(d, a, b):
|
||||
"""Les métriques système numériques de metrics/ sur [a, b) -> {nom: {t: valeur ou None}} (une valeur vide : None, le
|
||||
trou se voit), sans les métriques texte, les identifiants, ni celles qui n'ont aucune valeur."""
|
||||
station, series, texte = d.name + "_", {}, set()
|
||||
for p in sorted(glob.glob(str(d / "metrics" / "*.csv"))):
|
||||
if not (m := METRIQUE.match(os.path.basename(p))) or IDENTIFIANT.match(nom := m.group(1).removeprefix(station)):
|
||||
continue
|
||||
with open(p, newline="") as f:
|
||||
for r in csv.DictReader(f):
|
||||
t, v = donnees.instant(r["time_utc"]), (r.get("metric") or "").strip("[]'\" ")
|
||||
if not a <= t < b:
|
||||
continue
|
||||
try:
|
||||
series.setdefault(nom, {})[t] = float(v) if v else None
|
||||
except ValueError:
|
||||
texte.add(nom) # link, mode… : pas de courbe
|
||||
return {n: dict(sorted(s.items())) for n, s in series.items()
|
||||
if n not in texte and any(v is not None for v in s.values())}
|
||||
|
||||
|
||||
def plage(mesures, du, au):
|
||||
"""La plage lue : de du à au (exclu), sinon de la première mesure à la dernière, plus un pas."""
|
||||
tous = sorted({m["t"] for ms in mesures.values() for m in ms})
|
||||
a = donnees.instant(du) if du else tous[0]
|
||||
b = donnees.instant(au) if au else tous[-1] + (tous[-1] - tous[-2] if len(tous) > 1 else timedelta(hours=1))
|
||||
return a, b
|
||||
|
||||
|
||||
def courbes(station, du, au):
|
||||
"""-> {A, B, T, plots} : T, les heures de chaque série, une fois (clé : le capteur, "" pour la caméra, « system: »
|
||||
et son nom pour une métrique) ; plot : titre et traces (name, t, y, line, mode), sans plots ni capteurs vides."""
|
||||
d = dossier(station)
|
||||
mesures = donnees.charger(d)
|
||||
if not mesures:
|
||||
raise Refus(f"aucun export « advanced » pour {station}")
|
||||
a, b = plage(mesures, du, au)
|
||||
poses = [p for p in donnees.charger_camera(d) if a <= p["t"] < b]
|
||||
heure = lambda t: t.strftime("%Y-%m-%d %H:%M") # noqa: E731 (UTC, sans fuseau : comme l'affiche Plotly)
|
||||
valeurs = lambda s, c: [None if m[c] is None else round(m[c], 5) for m in s] # noqa: E731
|
||||
dans = {s: [m for m in mesures[s] if a <= m["t"] < b] for s in sorted(mesures)}
|
||||
refs, out = references(mesures, a, b), []
|
||||
|
||||
def par_capteur(colonnes):
|
||||
for col, titre in colonnes:
|
||||
traces = [{"name": s + " (ref.)" * (s in refs), "t": s, "y": y,
|
||||
"line": {"width": 1.2, "color": COULEURS[i % len(COULEURS)], "dash": "dot" if s in refs else "solid"}}
|
||||
for i, (s, y) in enumerate((s, valeurs(ms, col)) for s, ms in dans.items()) if any(v is not None for v in y)]
|
||||
if traces:
|
||||
out.append({"titre": titre, "traces": traces})
|
||||
|
||||
par_capteur(PLOTS)
|
||||
if poses:
|
||||
out.append({"titre": "Camera movement (deg)", "traces": [{"name": c, "t": "", "y": valeurs(poses, c),
|
||||
"line": {"width": 1.2, "color": col}} for c, col in POSE]})
|
||||
par_capteur(QUALITE)
|
||||
T = {"": [heure(p["t"]) for p in poses], **{s: [heure(m["t"]) for m in ms] for s, ms in dans.items()}}
|
||||
systeme = metriques(d, a, b)
|
||||
for nom in sorted(systeme, key=str.lower):
|
||||
T["system:" + nom] = [heure(t) for t in systeme[nom]]
|
||||
out.append({"titre": "System: " + nom, "traces": [{"name": nom, "t": "system:" + nom, "mode": "lines+markers",
|
||||
"y": [None if v is None else round(v, 5) for v in systeme[nom].values()],
|
||||
"line": {"width": 1.2, "color": COULEURS[0]}}]})
|
||||
if not out:
|
||||
raise Refus(f"aucune mesure de {station} sur cette plage")
|
||||
return {"A": donnees.iso(a), "B": donnees.iso(b), "T": T, "plots": out}
|
||||
|
||||
|
||||
# --- la page Data, étiquettes « Detectors » : les événements du journal, en lecture seule
|
||||
|
||||
HISTORIQUE = timedelta(days=3) # jours d'avant donnés aux détecteurs, comme categories.py (règle du 2026-10-24)
|
||||
|
||||
|
||||
# ponytail: gardés jusqu'au redémarrage du serveur (une minute ou plus par plage) ; relancer après un /improve lancé hors de l'app
|
||||
@functools.lru_cache(maxsize=32)
|
||||
def detecteurs(station, du, au, cibles=""):
|
||||
"""-> les événements des détecteurs sur la plage, au format des étiquettes (debut, fin, categorie, description) ;
|
||||
plus sorte, capteurs, ignore (zone grisée du journal). cibles : les capteurs gardés, séparés par des virgules (vide :
|
||||
tous) ; les autres sont écartés avant les détecteurs, comme s'ils n'existaient pas."""
|
||||
d = dossier(station)
|
||||
tout = donnees.charger(d)
|
||||
if cibles:
|
||||
garde = set(cibles.split(","))
|
||||
tout = {s: ms for s, ms in tout.items() if s in garde}
|
||||
if not tout:
|
||||
raise Refus(f"aucun export « advanced » pour {station}" + " avec ces cibles" * bool(cibles))
|
||||
a, b = plage(tout, du, au)
|
||||
mesures = {s: k for s, ms in tout.items() if (k := [m for m in ms if a - HISTORIQUE <= m["t"] < b])}
|
||||
evs = [v for v in events(mesures, b, str(d)) if v["end"] >= donnees.iso(a)] if mesures else []
|
||||
refs = {s for s, ms in mesures.items() if ms[0]["pattern_type"] == "global_ref"}
|
||||
return [{"id": f"D-{i + 1}", "debut": v["start"], "fin": v["end"], "categorie": v["category"], "sorte": v["episodes"][0]["label"],
|
||||
"description": f"{v['episodes'][0]['label']}. {v['text']}", "capteurs": v["sensors"], "ignore": v["ignore"],
|
||||
"cibles": [s for s in v["sensors"] if s not in refs], "references": [s for s in v["sensors"] if s in refs],
|
||||
"message": message(v["text"]), "action": action(v)}
|
||||
for i, v in enumerate(evs)]
|
||||
|
||||
|
||||
def message(texte):
|
||||
"""Ce qui arrive : la première phrase du journal (detectors/scripts/categories.py, sentence), jusqu'à son « ; » ; la suite y dit quoi
|
||||
en faire, sauf la phrase des sauts absorbés, gardée. ". " seulement : « 1.5× » ou « 0.30 px » ne coupent pas."""
|
||||
meme = re.search(r" (The jumps? of .* same cause\.)$", texte)
|
||||
return re.split(r"\. |; ", texte, maxsplit=1)[0].rstrip(".") + "." + (" " + meme.group(1) if meme else "")
|
||||
|
||||
|
||||
# Quoi en faire : la colonne « Pour le client » de labeling/familles.md, par famille ; précisée par sorte (le label de l'épisode
|
||||
# principal, avec son niveau) d'après les phrases du journal validées le 2026-10-24 (detectors/scripts/categories.py, sentence)
|
||||
ACTION_FAMILLE = {
|
||||
"Visibility": "Do not trust the curves in this period: this is not a movement.",
|
||||
"Obstruction": "Ignore the curves of the hidden targets in this period.",
|
||||
"Thermal displacement": "Normal, nothing to do.",
|
||||
"Displacement": "To check: look at the images, then keep watching.",
|
||||
"Target issue": "Act on the target (prism, mount); ignore its curve in the meantime.",
|
||||
"No data": "The curves are missing in this period; its cause tells whether to act.",
|
||||
}
|
||||
ACTION_SORTE = {
|
||||
"Poor visibility reduced": "Use the data in this period with care: they are less precise.",
|
||||
"Heat shimmer": "Nothing to do: the small fast wiggles are not movement.",
|
||||
"Camera thermal movement": "Nothing to do: this is not a movement of the structure.",
|
||||
"Brief obstruction": "Ignore this single point on the curves.",
|
||||
"Sudden jump, back to normal": "Look at the images: the cause is not established.",
|
||||
"Lasting shift": "Check whether the structure moved: look at the images.",
|
||||
"Jump, not back yet": "Check whether the structure moved: look at the images.",
|
||||
"Large shift": "Check whether the structure moved: look at the images.",
|
||||
"Slow drift": "Keep watching it.",
|
||||
}
|
||||
|
||||
|
||||
def action(v):
|
||||
ep = v["episodes"][0]
|
||||
return ACTION_SORTE.get(f"{ep['label']} {ep.get('level') or ''}".strip(), ACTION_SORTE.get(ep["label"])) \
|
||||
or ACTION_FAMILLE.get(v["category"], "")
|
||||
|
||||
|
||||
# --- les étiquettes
|
||||
|
||||
def fichier(station, source):
|
||||
if source not in DOSSIERS:
|
||||
raise Refus("dossier d'étiquettes inconnu")
|
||||
dossier(station)
|
||||
return ETIQUETTES / source / f"{station}.jsonl"
|
||||
|
||||
|
||||
def lire(station, source):
|
||||
f = fichier(station, source)
|
||||
# split et non splitlines : celui-ci couperait aussi sur les séparateurs Unicode que le texte peut contenir
|
||||
return [json.loads(l) for l in f.read_text(encoding="utf-8").split("\n") if l.strip()] if f.exists() else []
|
||||
|
||||
|
||||
def attribuer(station, etiquettes):
|
||||
"""labeling/ids.attribuer (D-131) : un id aux étiquettes qui n'en ont pas, le suivant de la station, en premier champ."""
|
||||
n = max((int(e["id"].rsplit("-", 1)[1]) for e in etiquettes if "id" in e), default=0)
|
||||
for e in etiquettes:
|
||||
if "id" not in e:
|
||||
n += 1
|
||||
e["id"] = f"{station}-{n}"
|
||||
if next(iter(e)) != "id":
|
||||
reste = {k: v for k, v in e.items() if k != "id"}
|
||||
ident = e["id"]
|
||||
e.clear()
|
||||
e.update({"id": ident}, **reste)
|
||||
|
||||
|
||||
def _valide(corps, debut, fin):
|
||||
description = (corps.get("description") or "").strip()
|
||||
try:
|
||||
vide = datetime.strptime(debut, FORMAT) >= datetime.strptime(fin, FORMAT)
|
||||
except (TypeError, ValueError):
|
||||
raise Refus("bornes illisibles (AAAA-MM-JJTHH:MMZ)") from None
|
||||
if not description or vide:
|
||||
raise Refus("texte vide ou plage vide")
|
||||
c = corps.get("categorie") or ""
|
||||
if c and c not in FAMILLES:
|
||||
raise Refus("famille inconnue")
|
||||
return description, c
|
||||
|
||||
|
||||
def ajouter(corps):
|
||||
station, source = corps.get("station"), corps.get("dossier", "manual")
|
||||
f = fichier(station, source)
|
||||
description, c = _valide(corps, corps.get("debut"), corps.get("fin"))
|
||||
label = {"debut": corps["debut"], "fin": corps["fin"]} | ({"categorie": c} if c else {}) | {"description": description}
|
||||
if source == "auto": # une étiquette automatique porte un id stable, le suivant de sa station (D-131)
|
||||
attribuer(station, lire(station, "auto") + [label])
|
||||
f.parent.mkdir(parents=True, exist_ok=True)
|
||||
avant = f.read_bytes() if f.exists() else b"" # une ligne retirée à la main peut laisser le fichier sans fin de ligne
|
||||
with open(f, "ab") as sortie:
|
||||
sortie.write(b"\n" * bool(avant and not avant.endswith(b"\n")) + json.dumps(label, ensure_ascii=False).encode() + b"\n")
|
||||
return label
|
||||
|
||||
|
||||
def modifier(corps):
|
||||
avant = corps.get("avant") or {}
|
||||
debut, fin = corps.get("debut", avant.get("debut")), corps.get("fin", avant.get("fin"))
|
||||
description, c = _valide(corps, debut, fin)
|
||||
label = dict.fromkeys(("id", "debut", "fin", "categorie")) | avant | {"debut": debut, "fin": fin, "categorie": c,
|
||||
"description": description}
|
||||
if label["id"] is None:
|
||||
del label["id"]
|
||||
if not c:
|
||||
del label["categorie"]
|
||||
label.pop("revue", None) # changée : plus revue
|
||||
return remplace(corps.get("station"), corps.get("dossier", "manual"), avant, label)
|
||||
|
||||
|
||||
def supprimer(corps):
|
||||
remplace(corps.get("station"), corps.get("dossier", "manual"), corps.get("avant"), None)
|
||||
return corps.get("avant")
|
||||
|
||||
|
||||
def remplace(station, source, avant, label):
|
||||
"""Remplace l'étiquette avant par label (None : la retire), retrouvée par son contenu, pas par son rang : le fichier
|
||||
a pu changer depuis le chargement de la page. Écrit à côté puis remplace : une coupure n'abîme pas le fichier."""
|
||||
f = fichier(station, source)
|
||||
lignes = f.read_text(encoding="utf-8").split("\n") if f.exists() else []
|
||||
for i, ligne in enumerate(lignes):
|
||||
if ligne.strip() and json.loads(ligne) == avant:
|
||||
lignes[i:i + 1] = [] if label is None else [json.dumps(label, ensure_ascii=False)]
|
||||
provisoire = f.with_name(f.name + ".tmp")
|
||||
provisoire.write_text("\n".join(lignes), encoding="utf-8")
|
||||
os.replace(provisoire, f)
|
||||
return label
|
||||
raise Refus("cette étiquette n'est plus dans le fichier : recharger la page")
|
||||
+3
-3
@@ -1,14 +1,14 @@
|
||||
<!doctype html>
|
||||
<html lang="fr">
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>Diagnostic AIMsight</title>
|
||||
<title>AIMsight Diagnostic</title>
|
||||
<link rel="preconnect" href="https://fonts.googleapis.com">
|
||||
<link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500;700&display=swap" rel="stylesheet">
|
||||
</head>
|
||||
<body>
|
||||
<div id="racine"></div>
|
||||
<div id="root"></div>
|
||||
<script type="module" src="/src/main.jsx"></script>
|
||||
</body>
|
||||
</html>
|
||||
+66
-66
@@ -5,40 +5,40 @@ import {
|
||||
import { useDisclosure } from "@mantine/hooks";
|
||||
import { IconChartLine, IconFolder, IconMoon, IconSun } from "@tabler/icons-react";
|
||||
|
||||
const Data = lazy(() => import("./pages/Data.jsx")); // Plotly, chargé seulement ici
|
||||
const Data = lazy(() => import("./pages/Data.jsx")); // Plotly, loaded only here
|
||||
const PAGES = [["data", "Data", IconChartLine]];
|
||||
|
||||
// L'adresse : #/<page>?station=…&du=…&au=… ; recharger garde la page et la sélection, Retour marche ; sans dates, les 5 derniers
|
||||
// jours UTC, aujourd'hui compris (au exclu : demain)
|
||||
const jour = n => new Date(Date.now() + n * 864e5).toISOString().slice(0, 10);
|
||||
function lireAdresse() {
|
||||
// The address: #/<page>?station=…&start=…&end=…; reloading keeps the page and the selection, Back works; without dates, the
|
||||
// last 5 UTC days, today included (end excluded: tomorrow)
|
||||
const day = n => new Date(Date.now() + n * 864e5).toISOString().slice(0, 10);
|
||||
function readAddress() {
|
||||
const [page, q = ""] = location.hash.replace(/^#\/?/, "").split("?");
|
||||
const p = new URLSearchParams(q);
|
||||
return { page: PAGES.some(([n]) => n === page) ? page : "data",
|
||||
selection: { station: p.get("station") || "", du: p.get("du") || jour(-4), au: p.get("au") || jour(1) } };
|
||||
selection: { station: p.get("station") || "", start: p.get("start") || day(-4), end: p.get("end") || day(1) } };
|
||||
}
|
||||
|
||||
function adresse(page, s) {
|
||||
function address(page, s) {
|
||||
const q = new URLSearchParams(Object.entries(s).filter(([, v]) => v)).toString();
|
||||
return `#/${page}${q ? "?" + q : ""}`;
|
||||
}
|
||||
|
||||
function Entete({ mobile, bureau, basculer }) {
|
||||
const { setColorScheme } = useMantineColorScheme(), schema = useComputedColorScheme("light");
|
||||
function Header({ mobile, desktop, toggle }) {
|
||||
const { setColorScheme } = useMantineColorScheme(), scheme = useComputedColorScheme("light");
|
||||
return (
|
||||
<AppShell.Header className="entete">
|
||||
<AppShell.Header className="header">
|
||||
<Group h="100%" px="md" justify="space-between" wrap="nowrap">
|
||||
<Group gap="sm" wrap="nowrap">
|
||||
<Burger opened={mobile} onClick={basculer.mobile} hiddenFrom="sm" size="sm" color="white" aria-label="Menu" />
|
||||
<Burger opened={bureau} onClick={basculer.bureau} visibleFrom="sm" size="sm" color="white" aria-label="Barre latérale" />
|
||||
<Burger opened={mobile} onClick={toggle.mobile} hiddenFrom="sm" size="sm" color="white" aria-label="Menu" />
|
||||
<Burger opened={desktop} onClick={toggle.desktop} visibleFrom="sm" size="sm" color="white" aria-label="Sidebar" />
|
||||
<Text fw={700} size="lg">AIMsight</Text>
|
||||
<Text size="sm" opacity={0.6}>Diagnostic</Text>
|
||||
</Group>
|
||||
<Group gap="sm" wrap="nowrap">
|
||||
<Tooltip label={schema === "dark" ? "Mode clair" : "Mode sombre"}>
|
||||
<ActionIcon variant="subtle" color="white" size="lg" aria-label="Changer de thème"
|
||||
onClick={() => setColorScheme(schema === "dark" ? "light" : "dark")}>
|
||||
{schema === "dark" ? <IconSun size={20} /> : <IconMoon size={20} />}
|
||||
<Tooltip label={scheme === "dark" ? "Light mode" : "Dark mode"}>
|
||||
<ActionIcon variant="subtle" color="white" size="lg" aria-label="Change theme"
|
||||
onClick={() => setColorScheme(scheme === "dark" ? "light" : "dark")}>
|
||||
{scheme === "dark" ? <IconSun size={20} /> : <IconMoon size={20} />}
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
</Group>
|
||||
@@ -47,37 +47,37 @@ function Entete({ mobile, bureau, basculer }) {
|
||||
);
|
||||
}
|
||||
|
||||
// Les analyses de la page Data (labeling/auto/analyses/), relues toutes les 3 s : un dossier par station, ouvert d'office s'il en
|
||||
// a une en cours, et dedans une analyse par plage ; un clic ouvre Data sur sa station et ses jours, et y montre son rapport
|
||||
// La plage d'une analyse sur une ligne : « 01.11 · 10:30–11:40 » dans le jour, sinon « 04.11 21:12 – 07.11 14:12 » (UTC)
|
||||
const jourMois = t => `${t.slice(8, 10)}.${t.slice(5, 7)}`, heure = t => t.slice(11, 16);
|
||||
const plage = a => (a.debut.slice(0, 10) === a.fin.slice(0, 10)
|
||||
? `${jourMois(a.debut)} · ${heure(a.debut)}–${heure(a.fin)}` : `${jourMois(a.debut)} ${heure(a.debut)} – ${jourMois(a.fin)} ${heure(a.fin)}`);
|
||||
const COULEUR_ETAT = { fini: "teal", erreur: "red", interrompue: "gray" };
|
||||
// L'état : une roue qui tourne en cours, sinon une pastille de couleur (son nom dans la bulle)
|
||||
const Pastille = ({ etat }) => (etat === "en cours" ? <Loader size={10} color="orange" />
|
||||
: <Tooltip label={etat}><ColorSwatch size={8} color={`var(--mantine-color-${COULEUR_ETAT[etat] || "gray"}-6)`} withShadow={false} /></Tooltip>);
|
||||
function Analyses({ ouvrir }) {
|
||||
const [liste, setListe] = useState([]);
|
||||
// The analyses of the Data page (labeling/auto/analyses/), read again every 3 s: one folder per station, open by default if
|
||||
// it has one running, and inside one analysis per range; a click opens Data on its station and its days, and shows its report there
|
||||
// The range of an analysis on one line: "01.11 · 10:30–11:40" within the day, otherwise "04.11 21:12 – 07.11 14:12" (UTC)
|
||||
const dayMonth = t => `${t.slice(8, 10)}.${t.slice(5, 7)}`, hour = t => t.slice(11, 16);
|
||||
const range = a => (a.start.slice(0, 10) === a.end.slice(0, 10)
|
||||
? `${dayMonth(a.start)} · ${hour(a.start)}–${hour(a.end)}` : `${dayMonth(a.start)} ${hour(a.start)} – ${dayMonth(a.end)} ${hour(a.end)}`);
|
||||
const STATE_COLOR = { done: "teal", error: "red", interrupted: "gray" };
|
||||
// The state: a spinning wheel while running, otherwise a coloured dot (its name in the tooltip)
|
||||
const Dot = ({ state }) => (state === "running" ? <Loader size={10} color="orange" />
|
||||
: <Tooltip label={state}><ColorSwatch size={8} color={`var(--mantine-color-${STATE_COLOR[state] || "gray"}-6)`} withShadow={false} /></Tooltip>);
|
||||
function Analyses({ open }) {
|
||||
const [list, setList] = useState([]);
|
||||
useEffect(() => {
|
||||
const lire = () => fetch("/api/analyses").then(r => r.ok && r.json()).then(l => l && setListe(l)).catch(() => {}); // en silence : toutes les 3 s
|
||||
lire();
|
||||
const minuterie = setInterval(lire, 3000);
|
||||
return () => clearInterval(minuterie);
|
||||
const read = () => fetch("/api/analyses").then(r => r.ok && r.json()).then(l => l && setList(l)).catch(() => {}); // silently: every 3 s
|
||||
read();
|
||||
const timer = setInterval(read, 3000);
|
||||
return () => clearInterval(timer);
|
||||
}, []);
|
||||
if (!liste.length) return null;
|
||||
const stations = [...new Set(liste.map(a => a.station))].sort();
|
||||
if (!list.length) return null;
|
||||
const stations = [...new Set(list.map(a => a.station))].sort();
|
||||
return <>
|
||||
<Text size="xs" fw={700} tt="uppercase" c="dimmed" px="sm" pt="lg" pb="xs">Analyses (UTC)</Text>
|
||||
{stations.map(st => {
|
||||
const siennes = liste.filter(a => a.station === st), occupee = siennes.some(a => a.etat === "en cours");
|
||||
// le chevron du dossier reste celui de Mantine : un rightSection prendrait sa place, et tournerait avec lui
|
||||
const its = list.filter(a => a.station === st), busy = its.some(a => a.state === "running");
|
||||
// the chevron of the folder stays Mantine's: a rightSection would take its place, and rotate with it
|
||||
return (
|
||||
<NavLink key={st} defaultOpened={occupee} childrenOffset={22} leftSection={<IconFolder size={16} />}
|
||||
label={<Group gap={6} wrap="nowrap">{st}<Text span size="xs" c="dimmed">{siennes.length}</Text>{occupee && <Loader size={10} color="orange" />}</Group>}>
|
||||
{siennes.map(a => (
|
||||
<NavLink key={a.nom} py={4} onClick={() => ouvrir(a)} leftSection={<Pastille etat={a.etat} />}
|
||||
label={<Text size="xs" ff="monospace" style={{ whiteSpace: "nowrap" }}>{plage(a)}</Text>} />
|
||||
<NavLink key={st} defaultOpened={busy} childrenOffset={22} leftSection={<IconFolder size={16} />}
|
||||
label={<Group gap={6} wrap="nowrap">{st}<Text span size="xs" c="dimmed">{its.length}</Text>{busy && <Loader size={10} color="orange" />}</Group>}>
|
||||
{its.map(a => (
|
||||
<NavLink key={a.name} py={4} onClick={() => open(a)} leftSection={<Dot state={a.state} />}
|
||||
label={<Text size="xs" ff="monospace" style={{ whiteSpace: "nowrap" }}>{range(a)}</Text>} />
|
||||
))}
|
||||
</NavLink>
|
||||
);
|
||||
@@ -85,49 +85,49 @@ function Analyses({ ouvrir }) {
|
||||
</>;
|
||||
}
|
||||
|
||||
function Barre({ page, selection, fermer, ouvrir }) {
|
||||
const titre = t => <Text size="xs" fw={700} tt="uppercase" c="dimmed" px="sm" pt="lg" pb="xs">{t}</Text>;
|
||||
function Sidebar({ page, selection, close, open }) {
|
||||
const title = t => <Text size="xs" fw={700} tt="uppercase" c="dimmed" px="sm" pt="lg" pb="xs">{t}</Text>;
|
||||
return (
|
||||
<AppShell.Navbar p="xs">
|
||||
<ScrollArea type="scroll">
|
||||
{titre("Pages")}
|
||||
{PAGES.map(([n, nom, Icone]) => (
|
||||
<NavLink key={n} label={nom} leftSection={<Icone size={18} />} active={page === n} onClick={fermer}
|
||||
href={adresse(n, selection)} />
|
||||
{title("Pages")}
|
||||
{PAGES.map(([n, name, Icon]) => (
|
||||
<NavLink key={n} label={name} leftSection={<Icon size={18} />} active={page === n} onClick={close}
|
||||
href={address(n, selection)} />
|
||||
))}
|
||||
<Analyses ouvrir={a => { fermer(); ouvrir(a); }} />
|
||||
<Analyses open={a => { close(); open(a); }} />
|
||||
</ScrollArea>
|
||||
</AppShell.Navbar>
|
||||
);
|
||||
}
|
||||
|
||||
export default function App() {
|
||||
const [{ page, selection }, setLieu] = useState(lireAdresse);
|
||||
const [mobile, { toggle: basculerMobile, close: fermerMobile }] = useDisclosure(false);
|
||||
const [bureau, { toggle: basculerBureau }] = useDisclosure(true);
|
||||
const [analyse, setAnalyse] = useState(null); // l'analyse dont Data montre le rapport
|
||||
const [{ page, selection }, setPlace] = useState(readAddress);
|
||||
const [mobile, { toggle: toggleMobile, close: closeMobile }] = useDisclosure(false);
|
||||
const [desktop, { toggle: toggleDesktop }] = useDisclosure(true);
|
||||
const [analysis, setAnalysis] = useState(null); // the analysis whose report Data shows
|
||||
|
||||
useEffect(() => {
|
||||
const suivre = () => setLieu(lireAdresse());
|
||||
addEventListener("hashchange", suivre);
|
||||
return () => removeEventListener("hashchange", suivre);
|
||||
const follow = () => setPlace(readAddress());
|
||||
addEventListener("hashchange", follow);
|
||||
return () => removeEventListener("hashchange", follow);
|
||||
}, []);
|
||||
|
||||
const choisir = s => { location.hash = adresse(page, s); };
|
||||
// Une analyse de la barre : Data sur sa station, des jours qui couvrent sa plage (sauf si la plage affichée les couvre déjà)
|
||||
function ouvrirAnalyse(a) {
|
||||
const s = selection, couvre = s.station === a.station && s.du <= a.debut.slice(0, 10) && a.fin < s.au;
|
||||
const au = new Date(Date.parse(a.fin.slice(0, 10)) + 864e5).toISOString().slice(0, 10);
|
||||
setAnalyse(a.nom);
|
||||
location.hash = adresse("data", couvre ? s : { station: a.station, du: a.debut.slice(0, 10), au });
|
||||
const choose = s => { location.hash = address(page, s); };
|
||||
// An analysis of the sidebar: Data on its station, days that cover its range (unless the range shown already covers them)
|
||||
function openAnalysis(a) {
|
||||
const s = selection, covers = s.station === a.station && s.start <= a.start.slice(0, 10) && a.end < s.end;
|
||||
const end = new Date(Date.parse(a.end.slice(0, 10)) + 864e5).toISOString().slice(0, 10);
|
||||
setAnalysis(a.name);
|
||||
location.hash = address("data", covers ? s : { station: a.station, start: a.start.slice(0, 10), end });
|
||||
}
|
||||
return (
|
||||
<AppShell header={{ height: 64 }} padding="lg"
|
||||
navbar={{ width: 260, breakpoint: "sm", collapsed: { mobile: !mobile, desktop: !bureau } }}>
|
||||
<Entete mobile={mobile} bureau={bureau} basculer={{ mobile: basculerMobile, bureau: basculerBureau }} />
|
||||
<Barre page={page} selection={selection} fermer={fermerMobile} ouvrir={ouvrirAnalyse} />
|
||||
navbar={{ width: 260, breakpoint: "sm", collapsed: { mobile: !mobile, desktop: !desktop } }}>
|
||||
<Header mobile={mobile} desktop={desktop} toggle={{ mobile: toggleMobile, desktop: toggleDesktop }} />
|
||||
<Sidebar page={page} selection={selection} close={closeMobile} open={openAnalysis} />
|
||||
<AppShell.Main bg="var(--mantine-color-default-hover)">
|
||||
<Suspense fallback={<Loader m="xl" />}><Data selection={selection} choisir={choisir} analyse={analyse} setAnalyse={setAnalyse} /></Suspense>
|
||||
<Suspense fallback={<Loader m="xl" />}><Data selection={selection} choose={choose} analysis={analysis} setAnalysis={setAnalysis} /></Suspense>
|
||||
</AppShell.Main>
|
||||
</AppShell>
|
||||
);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Button, Card, Group, Select, TextInput } from "@mantine/core";
|
||||
import { notifications } from "@mantine/notifications";
|
||||
|
||||
// A request to the API; a refusal of the server shows as a notification -> the JSON returned, or null
|
||||
export async function call(path, method = "GET", body) {
|
||||
try {
|
||||
const r = await fetch(path, body === undefined ? { method }
|
||||
: { method, headers: { "Content-Type": "application/json" }, body: JSON.stringify(body) });
|
||||
const json = await r.json().catch(() => ({ error: r.statusText }));
|
||||
if (r.ok) return json;
|
||||
notifications.show({ color: "red", title: "Refused", message: json.error || r.statusText });
|
||||
} catch (e) {
|
||||
notifications.show({ color: "red", title: "Server unreachable", message: e.message });
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
// The station and the range (UTC, To excluded; empty: the whole period), shared by the pages through the address
|
||||
export function Selector({ selection, choose }) {
|
||||
const [stations, setStations] = useState([]), [s, setS] = useState(selection);
|
||||
useEffect(() => { call("/api/stations").then(l => l && setStations(l)); }, []);
|
||||
useEffect(() => setS(selection), [selection.station, selection.start, selection.end]);
|
||||
return (
|
||||
<Card shadow="sm" withBorder>
|
||||
<Group align="flex-end" gap="md">
|
||||
<Select label="Station" placeholder="Choose" data={stations} value={s.station || null} searchable w={200}
|
||||
onChange={v => setS({ ...s, station: v || "" })} />
|
||||
<TextInput label="From (UTC)" type="date" value={s.start} onChange={e => setS({ ...s, start: e.currentTarget.value })} />
|
||||
<TextInput label="To (excluded)" type="date" value={s.end} onChange={e => setS({ ...s, end: e.currentTarget.value })} />
|
||||
<Button onClick={() => choose(s)} disabled={!s.station}>Load</Button>
|
||||
</Group>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -1,52 +0,0 @@
|
||||
import { useEffect, useState } from "react";
|
||||
import { Button, Card, Group, Select, Text, TextInput } from "@mantine/core";
|
||||
import { notifications } from "@mantine/notifications";
|
||||
|
||||
// Une requête à l'API ; un refus du serveur s'affiche en notification -> le JSON rendu, ou null
|
||||
export async function appel(chemin, methode = "GET", corps) {
|
||||
try {
|
||||
const r = await fetch(chemin, corps === undefined ? { method: methode }
|
||||
: { method: methode, headers: { "Content-Type": "application/json" }, body: JSON.stringify(corps) });
|
||||
const json = await r.json().catch(() => ({ erreur: r.statusText }));
|
||||
if (r.ok) return json;
|
||||
notifications.show({ color: "red", title: "Refusé", message: json.erreur || r.statusText });
|
||||
} catch (e) {
|
||||
notifications.show({ color: "red", title: "Serveur injoignable", message: e.message });
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export const poster = async (chemin, corps) => (await appel(chemin, "POST", corps)) !== null;
|
||||
|
||||
export function Carte({ surtitre, titre, action, children, ...reste }) {
|
||||
return (
|
||||
<Card shadow="sm" padding={0} withBorder {...reste}>
|
||||
<Group px="lg" py="md" justify="space-between" wrap="nowrap" style={{ borderBottom: "1px solid var(--mantine-color-default-border)" }}>
|
||||
<div>
|
||||
<Text size="xs" fw={700} tt="uppercase" c="dimmed">{surtitre}</Text>
|
||||
<Text fw={500}>{titre}</Text>
|
||||
</div>
|
||||
{action}
|
||||
</Group>
|
||||
{children}
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// La station et la plage (UTC, Au exclue ; vides : toute la période), partagées par les pages via l'adresse
|
||||
export function Selecteur({ selection, choisir }) {
|
||||
const [stations, setStations] = useState([]), [s, setS] = useState(selection);
|
||||
useEffect(() => { appel("/api/stations").then(l => l && setStations(l)); }, []);
|
||||
useEffect(() => setS(selection), [selection.station, selection.du, selection.au]);
|
||||
return (
|
||||
<Card shadow="sm" withBorder>
|
||||
<Group align="flex-end" gap="md">
|
||||
<Select label="Station" placeholder="Choisir" data={stations} value={s.station || null} searchable w={200}
|
||||
onChange={v => setS({ ...s, station: v || "" })} />
|
||||
<TextInput label="Du (UTC)" type="date" value={s.du} onChange={e => setS({ ...s, du: e.currentTarget.value })} />
|
||||
<TextInput label="Au (exclu)" type="date" value={s.au} onChange={e => setS({ ...s, au: e.currentTarget.value })} />
|
||||
<Button onClick={() => choisir(s)} disabled={!s.station}>Charger</Button>
|
||||
</Group>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
@@ -6,7 +6,7 @@ import "@mantine/notifications/styles.css";
|
||||
import "./style.css";
|
||||
import App from "./App.jsx";
|
||||
|
||||
// Bleu primaire #5383ff (shade 5), la palette retenue pour la page
|
||||
// Primary blue #5383ff (shade 5), the palette chosen for the page
|
||||
const theme = createTheme({
|
||||
primaryColor: "aimsight",
|
||||
primaryShade: 5,
|
||||
@@ -16,7 +16,7 @@ const theme = createTheme({
|
||||
defaultRadius: "md",
|
||||
});
|
||||
|
||||
createRoot(document.getElementById("racine")).render(
|
||||
createRoot(document.getElementById("root")).render(
|
||||
<MantineProvider theme={theme} defaultColorScheme="auto">
|
||||
<Notifications position="top-right" />
|
||||
<App />
|
||||
|
||||
+353
-352
@@ -4,80 +4,80 @@ import {
|
||||
useComputedColorScheme,
|
||||
} from "@mantine/core";
|
||||
import { IconPlus, IconX } from "@tabler/icons-react";
|
||||
import { appel, Selecteur } from "../commun.jsx";
|
||||
import { ImagesDe } from "./Images.jsx";
|
||||
import { call, Selector } from "../common.jsx";
|
||||
import { StationImages } from "./Images.jsx";
|
||||
|
||||
// Data, le visualiseur de courbes et l'étiquetage : un plot au départ, d'autres à ajouter ou enlever,
|
||||
// tous alignés sur le temps ; sous eux les étiquettes : Detectors (les événements du journal), LLM (labeling/auto/, en lecture)
|
||||
// ou Analysis (les mêmes, à modifier, supprimer ; glisser sur la barre sélectionne une plage, que l'analyste diagnostique et
|
||||
// étiquette ; ailleurs, glisser zoome) ; un clic ouvre une
|
||||
// étiquette et la met en évidence. À droite, les images de la station, et sous elles les paramètres du diagnostic.
|
||||
const FAMILLES = { "Visibility": "#8ec5e8", "Obstruction": "#f0b072", "Thermal displacement": "#f08a7e", "Displacement": "#b39ddb",
|
||||
// Data, the curve viewer and the labelling: one plot at the start, others to add or remove,
|
||||
// all aligned in time; under them the labels: Detectors (the events of the journal), LLM (labeling/auto/, read-only)
|
||||
// or Analysis (the same, to edit, delete; dragging on the bar selects a range, which the analyst diagnoses and
|
||||
// labels; elsewhere, dragging zooms); a click opens a
|
||||
// label and highlights it. On the right, the images of the station, and under them the diagnostic settings.
|
||||
const FAMILIES = { "Visibility": "#8ec5e8", "Obstruction": "#f0b072", "Thermal displacement": "#f08a7e", "Displacement": "#b39ddb",
|
||||
"Target issue": "#e6d46a", "No data": "#c4c4be", "None": "#a8d5a2" };
|
||||
const NOM_FAMILLE = f => (f === "None" ? "None (courbes intactes)" : f);
|
||||
const CHOIX_FAMILLE = [{ value: "", label: "(sans famille)" }, ...Object.keys(FAMILLES).map(f => ({ value: f, label: NOM_FAMILLE(f) }))];
|
||||
// LLM et Analysis : les mêmes étiquettes, labeling/auto/ ; LLM les lit, Analysis les modifie, supprime, et en fait faire par
|
||||
// l'analyste sur une plage (app/serveur/analyse.py)
|
||||
const SOURCES = [{ value: "detecteurs", label: "Detectors" }, { value: "auto", label: "LLM" }, { value: "analysis", label: "Analysis" }];
|
||||
const GRIS = "#b0b0aa", BLEU = "#2a78d6", VOIE = 26, HAUT = 330; // VOIE : px par rangée d'étiquettes ; HAUT : d'un plot
|
||||
const couleur = l => FAMILLES[l.categorie] || GRIS;
|
||||
const lisible = s => s.slice(0, 16).replace("T", " ");
|
||||
const marge = () => ({ l: 60, r: 20, t: 10, b: 30 });
|
||||
// L'habillage, aux couleurs de Mantine (dark.0 à dark.7 en sombre, gray en clair)
|
||||
const COULEURS = { clair: { texte: "#495057", faible: "#868e96", grille: "#eef0f2", axe: "#dee2e6", fond: "#f1f3f5", bulle: "#ffffff", zone: 0.05 },
|
||||
sombre: { texte: "#c9c9c9", faible: "#828282", grille: "#2e2e2e", axe: "#424242", fond: "#3b3b3b", bulle: "#242424", zone: 0.07 } };
|
||||
const POLICE = "Roboto, sans-serif";
|
||||
const habillage = c => ({ paper_bgcolor: "rgba(0,0,0,0)", plot_bgcolor: "rgba(0,0,0,0)", font: { family: POLICE, size: 11, color: c.texte },
|
||||
hoverlabel: { bgcolor: c.bulle, bordercolor: c.axe, align: "left", font: { family: POLICE, size: 12, color: c.texte } },
|
||||
modebar: { bgcolor: "rgba(0,0,0,0)", color: c.faible, activecolor: "#5383ff" } });
|
||||
const axe = c => ({ gridcolor: c.grille, linecolor: c.axe, showline: true, zeroline: false, ticks: "" });
|
||||
const OUTILS = { responsive: true, doubleClick: "reset", displaylogo: false, showTips: false,
|
||||
const FAMILY_NAME = f => (f === "None" ? "None (curves intact)" : f);
|
||||
const FAMILY_CHOICES = [{ value: "", label: "(no family)" }, ...Object.keys(FAMILIES).map(f => ({ value: f, label: FAMILY_NAME(f) }))];
|
||||
// LLM and Analysis: the same labels, labeling/auto/; LLM reads them, Analysis edits, deletes them, and has the analyst make
|
||||
// some on a range (app/server/analysis.py)
|
||||
const SOURCES = [{ value: "detectors", label: "Detectors" }, { value: "auto", label: "LLM" }, { value: "analysis", label: "Analysis" }];
|
||||
const GREY = "#b0b0aa", BLUE = "#2a78d6", LANE = 26, HEIGHT = 330; // LANE: px per row of labels; HEIGHT: of a plot
|
||||
const color = l => FAMILIES[l.family] || GREY;
|
||||
const readable = s => s.slice(0, 16).replace("T", " ");
|
||||
const margin = () => ({ l: 60, r: 20, t: 10, b: 30 });
|
||||
// The skin, in Mantine's colours (dark.0 to dark.7 in dark, gray in light)
|
||||
const THEMES = { light: { text: "#495057", faint: "#868e96", grid: "#eef0f2", axis: "#dee2e6", bg: "#f1f3f5", tooltip: "#ffffff", zone: 0.05 },
|
||||
dark: { text: "#c9c9c9", faint: "#828282", grid: "#2e2e2e", axis: "#424242", bg: "#3b3b3b", tooltip: "#242424", zone: 0.07 } };
|
||||
const FONT = "Roboto, sans-serif";
|
||||
const skin = c => ({ paper_bgcolor: "rgba(0,0,0,0)", plot_bgcolor: "rgba(0,0,0,0)", font: { family: FONT, size: 11, color: c.text },
|
||||
hoverlabel: { bgcolor: c.tooltip, bordercolor: c.axis, align: "left", font: { family: FONT, size: 12, color: c.text } },
|
||||
modebar: { bgcolor: "rgba(0,0,0,0)", color: c.faint, activecolor: "#5383ff" } });
|
||||
const axis = c => ({ gridcolor: c.grid, linecolor: c.axis, showline: true, zeroline: false, ticks: "" });
|
||||
const CONFIG = { responsive: true, doubleClick: "reset", displaylogo: false, showTips: false,
|
||||
modeBarButtonsToRemove: ["lasso2d", "select2d", "autoScale2d", "zoomIn2d", "zoomOut2d", "hoverClosestCartesian", "hoverCompareCartesian", "toggleSpikelines"] };
|
||||
const date = v => Date.parse(String(v).replace(" ", "T").replace(/Z?$/, "Z")); // une date Plotly ou d'étiquette -> ms UTC
|
||||
const coupe = s => s.replace(/(.{1,80})(\s|$)/g, "$1<br>"); // la bulle : des lignes de 80 caractères au plus
|
||||
const date = v => Date.parse(String(v).replace(" ", "T").replace(/Z?$/, "Z")); // a Plotly or label date -> ms UTC
|
||||
const wrap = s => s.replace(/(.{1,80})(\s|$)/g, "$1<br>"); // the tooltip: lines of 80 characters at most
|
||||
|
||||
const DETECTEES = Object.keys(FAMILLES).filter(f => f !== "None"); // les familles des détecteurs (categories.py)
|
||||
const DETECTED = Object.keys(FAMILIES).filter(f => f !== "None"); // the families of the detectors (categories.py)
|
||||
|
||||
export default function Data({ selection, choisir, analyse, setAnalyse }) {
|
||||
const [donnees, setDonnees] = useState(null), [charge, setCharge] = useState(false), [large, setLarge] = useState(false);
|
||||
const gauche = useRef(), [instant, setInstant] = useState(null); // instant : où caler les images (le début de l'événement cliqué)
|
||||
// Les paramètres du diagnostic, sous l'image (Detectors et LLM) : familles montrées (filtre de la page) ; cibles gardées ("" :
|
||||
// toutes ; sinon les détecteurs retournent sans les autres)
|
||||
const [source, setSource] = useState("detecteurs"), [familles, setFamilles] = useState(DETECTEES), [cibles, setCibles] = useState("");
|
||||
const [focus, setFocus] = useState(false); // un événement ouvert : les plots ne montrent que ses capteurs
|
||||
const { station, du, au } = selection;
|
||||
export default function Data({ selection, choose, analysis, setAnalysis }) {
|
||||
const [data, setData] = useState(null), [loading, setLoading] = useState(false), [wide, setWide] = useState(false);
|
||||
const left = useRef(), [moment, setMoment] = useState(null); // moment: where to set the images (the start of the clicked event)
|
||||
// The diagnostic settings, under the image (Detectors and LLM): families shown (filter of the page); targets kept ("":
|
||||
// all; otherwise the detectors run again without the others)
|
||||
const [source, setSource] = useState("detectors"), [families, setFamilies] = useState(DETECTED), [targets, setTargets] = useState("");
|
||||
const [focus, setFocus] = useState(false); // with an event open, the plots show only its sensors
|
||||
const { station, start, end } = selection;
|
||||
useEffect(() => {
|
||||
setDonnees(null); setCibles("");
|
||||
setData(null); setTargets("");
|
||||
if (!station) return;
|
||||
setCharge(true);
|
||||
appel(`/api/courbes?${new URLSearchParams({ station, du, au })}`).then(d => { setDonnees(d); setCharge(false); });
|
||||
}, [station, du, au]);
|
||||
// Les colonnes glissent (style.css) ; Plotly ne suit que la fenêtre : à la fin du glissement, les plots se recalent sur leur
|
||||
// nouvelle largeur (les redessiner à chaque image du glissement serait trop lourd)
|
||||
const finGlissement = e => { if (e.target === gauche.current && e.propertyName === "max-width") dispatchEvent(new Event("resize")); };
|
||||
setLoading(true);
|
||||
call(`/api/curves?${new URLSearchParams({ station, start, end })}`).then(d => { setData(d); setLoading(false); });
|
||||
}, [station, start, end]);
|
||||
// The columns slide (style.css); Plotly only follows the window: at the end of the slide, the plots refit to their
|
||||
// new width (redrawing them at every frame of the slide would be too heavy)
|
||||
const slideEnd = e => { if (e.target === left.current && e.propertyName === "max-width") dispatchEvent(new Event("resize")); };
|
||||
return (
|
||||
<Grid gutter="lg" className="data-grille" onTransitionEnd={finGlissement}>
|
||||
<Grid.Col span={{ base: 12, lg: large ? 5 : 8 }} ref={gauche}>
|
||||
<Grid gutter="lg" className="data-grid" onTransitionEnd={slideEnd}>
|
||||
<Grid.Col span={{ base: 12, lg: wide ? 5 : 8 }} ref={left}>
|
||||
<Stack gap="lg">
|
||||
<div>
|
||||
<Title order={2}>Data</Title>
|
||||
<Text c="dimmed">Les courbes d'une station, un plot ou plusieurs ; sous eux les étiquettes : un clic sur l'une la met en évidence et
|
||||
l'ouvre.</Text>
|
||||
<Text c="dimmed">The curves of a station, one plot or several; under them the labels: a click on one highlights it and
|
||||
opens it.</Text>
|
||||
</div>
|
||||
<Selecteur selection={selection} choisir={choisir} />
|
||||
{charge && <Loader />}
|
||||
{donnees && <Courbes key={`${station}${du}${au}`} selection={selection} d={donnees} caler={(t, proche) => setInstant({ t, proche })}
|
||||
source={source} setSource={setSource} familles={familles} cibles={cibles} focus={focus}
|
||||
analyse={analyse} setAnalyse={setAnalyse} />}
|
||||
<Selector selection={selection} choose={choose} />
|
||||
{loading && <Loader />}
|
||||
{data && <Curves key={`${station}${start}${end}`} selection={selection} d={data} setImage={(t, nearest) => setMoment({ t, nearest })}
|
||||
source={source} setSource={setSource} families={families} targets={targets} focus={focus}
|
||||
analysis={analysis} setAnalysis={setAnalysis} />}
|
||||
</Stack>
|
||||
</Grid.Col>
|
||||
<Grid.Col span={{ base: 12, lg: large ? 7 : 4 }}>
|
||||
<div className="colle">
|
||||
<Grid.Col span={{ base: 12, lg: wide ? 7 : 4 }}>
|
||||
<div className="sticky">
|
||||
<Stack gap="lg">
|
||||
<ImagesDe selection={selection} compacte large={large} basculer={() => setLarge(!large)} instant={instant} />
|
||||
{donnees && <Parametres key={`${station}${du}${au}`} d={donnees} familles={familles} setFamilles={setFamilles}
|
||||
cibles={cibles} setCibles={setCibles} detect={source === "detecteurs"}
|
||||
focus={focus} setFocus={setFocus} />}
|
||||
<StationImages selection={selection} compact wide={wide} toggle={() => setWide(!wide)} moment={moment} />
|
||||
{data && <Settings key={`${station}${start}${end}`} d={data} families={families} setFamilies={setFamilies}
|
||||
targets={targets} setTargets={setTargets} detect={source === "detectors"}
|
||||
focus={focus} setFocus={setFocus} />}
|
||||
</Stack>
|
||||
</div>
|
||||
</Grid.Col>
|
||||
@@ -85,97 +85,97 @@ export default function Data({ selection, choisir, analyse, setAnalyse }) {
|
||||
);
|
||||
}
|
||||
|
||||
// Les familles s'appliquent tout de suite (filtre de la page, Detectors et LLM) ; les cibles, au bouton : chaque jeu de cibles
|
||||
// relance les détecteurs (les étiquettes LLM ne disent pas leurs capteurs : elles ne les filtrent pas)
|
||||
function Parametres({ d, familles, setFamilles, cibles, setCibles, detect, focus, setFocus }) {
|
||||
const capteurs = Object.keys(d.T).filter(k => k && !k.startsWith("system:"));
|
||||
// The families apply at once (filter of the page, Detectors and LLM); the targets, with the button: each set of targets
|
||||
// runs the detectors again (the LLM labels do not state their sensors: they do not filter them)
|
||||
function Settings({ d, families, setFamilies, targets, setTargets, detect, focus, setFocus }) {
|
||||
const sensors = Object.keys(d.T).filter(k => k && !k.startsWith("system:"));
|
||||
const refs = new Set(d.plots.flatMap(p => p.traces).filter(t => t.name.endsWith(" (ref.)")).map(t => t.t));
|
||||
const [coches, setCoches] = useState(cibles ? cibles.split(",") : capteurs);
|
||||
const voulu = coches.length === capteurs.length ? "" : capteurs.filter(c => coches.includes(c)).join(",");
|
||||
const [checked, setChecked] = useState(targets ? targets.split(",") : sensors);
|
||||
const wanted = checked.length === sensors.length ? "" : sensors.filter(c => checked.includes(c)).join(",");
|
||||
return (
|
||||
<Card shadow="sm" withBorder>
|
||||
<Stack gap="sm">
|
||||
<Title order={5}>Paramètres du diagnostic</Title>
|
||||
<Switch label="Focus on event curves" description="Un événement ouvert : les plots ne montrent que ses capteurs"
|
||||
<Title order={5}>Diagnostic settings</Title>
|
||||
<Switch label="Focus on event curves" description="With an event open, the plots show only its sensors"
|
||||
checked={focus} onChange={e => setFocus(e.currentTarget.checked)} />
|
||||
<Checkbox.Group label="Familles montrées" value={familles} onChange={setFamilles}>
|
||||
<Checkbox.Group label="Families shown" value={families} onChange={setFamilies}>
|
||||
<SimpleGrid cols={2} spacing={6} verticalSpacing={6} mt={6}>
|
||||
{DETECTEES.map(f => <Checkbox key={f} size="xs" value={f} label={f} color={FAMILLES[f]} iconColor="#1a1b1e" />)}
|
||||
{DETECTED.map(f => <Checkbox key={f} size="xs" value={f} label={f} color={FAMILIES[f]} iconColor="#1a1b1e" />)}
|
||||
</SimpleGrid>
|
||||
</Checkbox.Group>
|
||||
<Checkbox.Group label="Cibles considérées (les autres sont écartées)" value={coches} onChange={setCoches}>
|
||||
<Checkbox.Group label="Targets considered (the others are removed)" value={checked} onChange={setChecked}>
|
||||
<SimpleGrid cols={3} spacing={6} verticalSpacing={6} mt={6}>
|
||||
{capteurs.map(c => <Checkbox key={c} size="xs" value={c} label={c + (refs.has(c) ? " (ref.)" : "")} />)}
|
||||
{sensors.map(c => <Checkbox key={c} size="xs" value={c} label={c + (refs.has(c) ? " (ref.)" : "")} />)}
|
||||
</SimpleGrid>
|
||||
</Checkbox.Group>
|
||||
{!detect && <Text size="xs" c="dimmed">Les étiquettes ne disent pas leurs cibles : ce choix ne vaut que pour Detectors.</Text>}
|
||||
{!detect && <Text size="xs" c="dimmed">Labels do not state their targets: this choice applies to Detectors only.</Text>}
|
||||
<Group gap="sm">
|
||||
<Button size="xs" onClick={() => setCibles(voulu)} disabled={!coches.length || voulu === cibles}>Relancer les détecteurs</Button>
|
||||
<Button size="xs" variant="default" onClick={() => setCoches(coches.length ? [] : capteurs)}>{coches.length ? "Tout décocher" : "Tout cocher"}</Button>
|
||||
<Button size="xs" onClick={() => setTargets(wanted)} disabled={!checked.length || wanted === targets}>Rerun the detectors</Button>
|
||||
<Button size="xs" variant="default" onClick={() => setChecked(checked.length ? [] : sensors)}>{checked.length ? "Uncheck all" : "Check all"}</Button>
|
||||
</Group>
|
||||
</Stack>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// Une ligne de la fiche d'un événement des détecteurs : son nom, puis sa valeur
|
||||
const Champ = ({ nom, children }) => (
|
||||
// A line of the card of a detectors' event: its name, then its value
|
||||
const Field = ({ name, children }) => (
|
||||
<Group gap="sm" align="flex-start" wrap="nowrap">
|
||||
<Text size="sm" c="dimmed" w={80} style={{ flexShrink: 0 }}>{nom}</Text>
|
||||
<Text size="sm" c="dimmed" w={80} style={{ flexShrink: 0 }}>{name}</Text>
|
||||
<Text size="sm" component="div">{children}</Text>
|
||||
</Group>
|
||||
);
|
||||
|
||||
// Les capteurs d'un événement : ceux des détecteurs, sinon ceux que nomme le texte de l'étiquette (LLM, Analysis)
|
||||
const nomme = (texte, s) => new RegExp(`(^|[^\\w])${s.replace(/[^\w]/g, "\\$&")}(?![\\w])`).test(texte);
|
||||
const capteursDe = (l, noms) => l.capteurs ?? noms.filter(s => nomme(l.description, s));
|
||||
// The sensors of an event: those of the detectors, otherwise those the text of the label names (LLM, Analysis)
|
||||
const mentions = (text, s) => new RegExp(`(^|[^\\w])${s.replace(/[^\w]/g, "\\$&")}(?![\\w])`).test(text);
|
||||
const sensorsOf = (l, names) => l.sensors ?? names.filter(s => mentions(l.description, s));
|
||||
|
||||
// analyse : le dossier de l'analyse montrée, tenu par App (la barre latérale en ouvre une)
|
||||
function Courbes({ selection: { station, du, au }, d, caler, source, setSource, familles, cibles, focus, analyse, setAnalyse }) {
|
||||
// analysis: the folder of the analysis shown, held by App (the sidebar opens one)
|
||||
function Curves({ selection: { station, start, end }, d, setImage, source, setSource, families, targets, focus, analysis, setAnalysis }) {
|
||||
const { A, B, T, plots: PLOTS } = d;
|
||||
const indice = (titre, sinon) => { const i = PLOTS.findIndex(p => p.titre === titre); return i < 0 ? sinon : i; };
|
||||
const [Plotly, setPlotly] = useState(null), [pret, setPret] = useState(false);
|
||||
const [vues, setVues] = useState([{ id: 1, m: indice("Relative Corrected Horizontal Shift", 0) }]); // les plots affichés
|
||||
const [choix, setChoix] = useState(null);
|
||||
const [indices, setIndices] = useState(""), [rev, setRev] = useState(0);
|
||||
const [ouverte, setOuverte] = useState(null), [edit, setEdit] = useState(null), [note, setNote] = useState("");
|
||||
const [toutes, setToutes] = useState([]), [lecture, setLecture] = useState(true), sombre = useComputedColorScheme("light") === "dark";
|
||||
// toutes : le fichier (ou les détecteurs) ; labels : sur la plage, dans les familles cochées (sans famille ou None : toujours)
|
||||
const labels = useMemo(() => toutes.filter(x => x.debut < B && x.fin > A
|
||||
&& (!DETECTEES.includes(x.categorie) || familles.includes(x.categorie))), [toutes, familles]);
|
||||
const detect = source === "detecteurs", lecteur = source !== "analysis"; // seul Analysis écrit
|
||||
const dossier = source === "analysis" ? "auto" : source; // le dossier de labeling/ des étiquettes
|
||||
// figs : id -> div du plot ; dessines : id -> plot qu'il montre ; vue : la plage et la largeur où le texte des étiquettes a été tronqué
|
||||
const figs = useRef({}), dessines = useRef({}), prochain = useRef(2), barre = useRef(), tries = useRef([]), vue = useRef(""), courant = useRef({});
|
||||
// le focus : les capteurs de l'événement ouvert (aucun : tous, comme pour No data) ; montres, leur clé
|
||||
const noms = useMemo(() => Object.keys(T).filter(k => k && !k.startsWith("system:")), [T]);
|
||||
const gardes = focus && ouverte ? capteursDe(ouverte, noms) : [], montres = gardes.join(",");
|
||||
courant.current = { vues, labels, choix, ouverte, ouvrir, gardes: new Set(gardes), montres, c: COULEURS[sombre ? "sombre" : "clair"] }; // ce que lisent les événements Plotly
|
||||
const plotIndex = (title, otherwise) => { const i = PLOTS.findIndex(p => p.title === title); return i < 0 ? otherwise : i; };
|
||||
const [Plotly, setPlotly] = useState(null), [ready, setReady] = useState(false);
|
||||
const [views, setViews] = useState([{ id: 1, m: plotIndex("Relative Corrected Horizontal Shift", 0) }]); // the plots shown
|
||||
const [choice, setChoice] = useState(null);
|
||||
const [hints, setHints] = useState(""), [rev, setRev] = useState(0);
|
||||
const [opened, setOpened] = useState(null), [edit, setEdit] = useState(null), [note, setNote] = useState("");
|
||||
const [allLabels, setAllLabels] = useState([]), [reading, setReading] = useState(true), dark = useComputedColorScheme("light") === "dark";
|
||||
// allLabels: the file (or the detectors); labels: in the range, in the checked families (no family or None: always)
|
||||
const labels = useMemo(() => allLabels.filter(x => x.start < B && x.end > A
|
||||
&& (!DETECTED.includes(x.family) || families.includes(x.family))), [allLabels, families]);
|
||||
const detect = source === "detectors", readOnly = source !== "analysis"; // only Analysis writes
|
||||
const folder = source === "analysis" ? "auto" : source; // the labeling/ folder of the labels
|
||||
// figs: id -> div of the plot; drawn: id -> plot it shows; viewKey: the range and the width where the text of the labels was truncated
|
||||
const figs = useRef({}), drawn = useRef({}), nextId = useRef(2), bar = useRef(), sorted = useRef([]), viewKey = useRef(""), current = useRef({});
|
||||
// the focus: the sensors of the open event (none: all, as for No data); shown, their key
|
||||
const names = useMemo(() => Object.keys(T).filter(k => k && !k.startsWith("system:")), [T]);
|
||||
const kept = focus && opened ? sensorsOf(opened, names) : [], shown = kept.join(",");
|
||||
current.current = { views, labels, choice, opened, open, kept: new Set(kept), shown, c: THEMES[dark ? "dark" : "light"] }; // what the Plotly events read
|
||||
|
||||
useEffect(() => { import("plotly.js-dist-min").then(m => setPlotly(() => m.default)); }, []);
|
||||
|
||||
const liste = () => courant.current.vues.map(v => figs.current[v.id]).filter(g => g?._fullLayout);
|
||||
const plotDivs = () => current.current.views.map(v => figs.current[v.id]).filter(g => g?._fullLayout);
|
||||
|
||||
// Les zones des étiquettes, la plage choisie, et l'étiquette ouverte : plus marquée, entourée
|
||||
// The zones of the labels, the chosen range, and the open label: more marked, outlined
|
||||
function zones() {
|
||||
const { labels, choix, ouverte, c } = courant.current;
|
||||
const z = (a, b, f, o, ligne = { width: 0 }, couche = "below") => ({ type: "rect", xref: "x", yref: "paper", x0: lisible(a), x1: lisible(b),
|
||||
y0: 0, y1: 1, fillcolor: f, opacity: o, line: ligne, layer: couche });
|
||||
return labels.map(l => z(l.debut, l.fin, couleur(l), c.zone)).concat(choix ? [z(choix[0], choix[1], BLEU, 0.25)] : [])
|
||||
.concat(ouverte ? [z(ouverte.debut, ouverte.fin, couleur(ouverte), 0.3),
|
||||
z(ouverte.debut, ouverte.fin, "rgba(0,0,0,0)", 1, { color: c.texte, width: 1.5, dash: "dot" }, "above")] : []);
|
||||
const { labels, choice, opened, c } = current.current;
|
||||
const z = (a, b, f, o, line = { width: 0 }, layer = "below") => ({ type: "rect", xref: "x", yref: "paper", x0: readable(a), x1: readable(b),
|
||||
y0: 0, y1: 1, fillcolor: f, opacity: o, line, layer });
|
||||
return labels.map(l => z(l.start, l.end, color(l), c.zone)).concat(choice ? [z(choice[0], choice[1], BLUE, 0.25)] : [])
|
||||
.concat(opened ? [z(opened.start, opened.end, color(opened), 0.3),
|
||||
z(opened.start, opened.end, "rgba(0,0,0,0)", 1, { color: c.text, width: 1.5, dash: "dot" }, "above")] : []);
|
||||
}
|
||||
|
||||
// Le contour des barres : épais pour l'étiquette ouverte
|
||||
const contour = t => {
|
||||
const { ouverte, c } = courant.current;
|
||||
return { color: t.map(l => (l === ouverte ? c.texte : c.fond)), width: t.map(l => (l === ouverte ? 3 : 1)) };
|
||||
// The outline of the bars: thick for the open label
|
||||
const outline = t => {
|
||||
const { opened, c } = current.current;
|
||||
return { color: t.map(l => (l === opened ? c.text : c.bg)), width: t.map(l => (l === opened ? 3 : 1)) };
|
||||
};
|
||||
|
||||
// Les plots et la barre partagent plage et marges : celui qui change impose les siens aux autres (rien si déjà alignés,
|
||||
// sinon le relayout appelle aligne en boucle).
|
||||
function aligne(src) {
|
||||
const gs = liste(), b = barre.current;
|
||||
// The plots and the bar share range and margins: the one that changes imposes its own on the others (nothing if already
|
||||
// aligned, otherwise the relayout calls align in a loop).
|
||||
function align(src) {
|
||||
const gs = plotDivs(), b = bar.current;
|
||||
if (!b?._fullLayout || !gs.length) return;
|
||||
const rg = (src?._fullLayout ? src : gs[0])._fullLayout.xaxis.range.slice();
|
||||
const l = Math.max(...gs.map(g => g._fullLayout._size.l)), r = Math.max(...gs.map(g => g._fullLayout._size.r));
|
||||
@@ -183,379 +183,380 @@ function Courbes({ selection: { station, du, au }, d, caler, source, setSource,
|
||||
const m = g._fullLayout.margin, x = g._fullLayout.xaxis.range;
|
||||
if (m.l === l && m.r === r && date(x[0]) === date(rg[0]) && date(x[1]) === date(rg[1])) return;
|
||||
return Plotly.relayout(g, { "xaxis.range": rg, "margin.l": l, "margin.r": r });
|
||||
})).then(recaleTexte);
|
||||
})).then(refitText);
|
||||
}
|
||||
|
||||
// Le texte des étiquettes suit le zoom et la largeur de la barre (rien si les deux n'ont pas changé)
|
||||
function recaleTexte() {
|
||||
const b = barre.current, L = b?._fullLayout;
|
||||
// The text of the labels follows the zoom and the width of the bar (nothing if neither changed)
|
||||
function refitText() {
|
||||
const b = bar.current, L = b?._fullLayout;
|
||||
if (!L) return;
|
||||
const v = `${L.xaxis.range}|${L._size.w}`;
|
||||
if (v !== vue.current) { vue.current = v; return Plotly.restyle(b, { text: [textes(tries.current, L)] }); }
|
||||
if (v !== viewKey.current) { viewKey.current = v; return Plotly.restyle(b, { text: [texts(sorted.current, L)] }); }
|
||||
}
|
||||
|
||||
// Le texte de chaque étiquette, tronqué à la largeur visible de sa barre (~6 px par caractère en 11 px) ; pour un événement
|
||||
// des détecteurs, sa famille seulement (ses capteurs dans la bulle, le reste au clic)
|
||||
function textes(t, L) {
|
||||
// The text of each label, truncated to the visible width of its bar (~6 px per character at 11 px); for a detectors'
|
||||
// event, its family only (its sensors in the tooltip, the rest on click)
|
||||
function texts(t, L) {
|
||||
const [r0, r1] = L.xaxis.range.map(date), w = L._size.w;
|
||||
return t.map(l => {
|
||||
const x = l.message ? l.categorie : l.description;
|
||||
const px = (Math.min(date(l.fin), r1) - Math.max(date(l.debut), r0)) / (r1 - r0) * w, n = Math.floor((px - 10) / 6);
|
||||
const x = l.message ? l.family : l.description;
|
||||
const px = (Math.min(date(l.end), r1) - Math.max(date(l.start), r0)) / (r1 - r0) * w, n = Math.floor((px - 10) / 6);
|
||||
return n < 3 ? "" : x.length <= n ? x : x.slice(0, n - 1) + "…";
|
||||
});
|
||||
}
|
||||
|
||||
// Une trace de capteur sous le focus : visible si c'est l'un des siens, sinon dans la légende seulement ; uirevision de la
|
||||
// trace : le focus qui change l'emporte sur les clics de légende, qui restent tant qu'il ne change pas
|
||||
function focalise(t) {
|
||||
const { gardes, montres } = courant.current;
|
||||
if (!montres || !t.t || t.t.startsWith("system:")) return { uirevision: montres || 1 };
|
||||
return { visible: gardes.has(t.t) ? true : "legendonly", uirevision: montres };
|
||||
// A sensor trace under the focus: visible if it is one of its own, otherwise in the legend only; uirevision of the
|
||||
// trace: the focus that changes wins over legend clicks, which stay as long as it does not change
|
||||
function focusTrace(t) {
|
||||
const { kept, shown } = current.current;
|
||||
if (!shown || !t.t || t.t.startsWith("system:")) return { uirevision: shown || 1 };
|
||||
return { visible: kept.has(t.t) ? true : "legendonly", uirevision: shown };
|
||||
}
|
||||
|
||||
function dessineFig(g, m) {
|
||||
const p = PLOTS[m], { c } = courant.current, b = barre.current?._fullLayout;
|
||||
g.style.height = `${HAUT}px`; // responsive : Plotly reprend la hauteur du div à chaque restyle, il lui en faut une
|
||||
// uirevision : le zoom et les capteurs cachés restent quand on change de plot ; l'ordonnée, propre à chaque plot, se recale.
|
||||
// Un nouveau plot prend la plage déjà affichée. t : la place du menu du plot (à gauche) et de la barre d'outils (à droite).
|
||||
function drawFig(g, m) {
|
||||
const p = PLOTS[m], { c } = current.current, b = bar.current?._fullLayout;
|
||||
g.style.height = `${HEIGHT}px`; // responsive: Plotly takes the height of the div again at each restyle, it needs one
|
||||
// uirevision: the zoom and the hidden sensors stay when the plot changes; the y axis, specific to each plot, refits.
|
||||
// A new plot takes the range already shown. t: the room for the plot menu (left) and the toolbar (right).
|
||||
return Plotly.react(g, p.traces.map(t => ({ type: "scatter", mode: t.mode || "lines", marker: { size: 4 }, x: T[t.t], y: t.y, name: t.name,
|
||||
uid: t.name.replace(/\W/g, "_"), line: t.line, ...focalise(t),
|
||||
hovertemplate: `${t.name} : %{y}<br>%{x|%Y-%m-%d %H:%M} UTC<extra></extra>` })),
|
||||
{ ...habillage(c), height: HAUT, hovermode: "closest", margin: { ...marge(), t: 42, b: 36 }, uirevision: 1, shapes: zones(),
|
||||
uid: t.name.replace(/\W/g, "_"), line: t.line, ...focusTrace(t),
|
||||
hovertemplate: `${t.name}: %{y}<br>%{x|%Y-%m-%d %H:%M} UTC<extra></extra>` })),
|
||||
{ ...skin(c), height: HEIGHT, hovermode: "closest", margin: { ...margin(), t: 42, b: 36 }, uirevision: 1, shapes: zones(),
|
||||
legend: { x: 1.01, y: 1, itemwidth: 30, font: { size: 11 } },
|
||||
xaxis: { type: "date", range: b ? b.xaxis.range.slice() : [lisible(A), lisible(B)], ...axe(c) },
|
||||
yaxis: { title: { text: p.titre, font: { size: 11, color: c.faible } }, uirevision: p.titre, ...axe(c) } },
|
||||
OUTILS).then(() => {
|
||||
if (!g.suivi) {
|
||||
g.suivi = true;
|
||||
g.on("plotly_relayout", () => aligne(g));
|
||||
g.on("plotly_click", ev => caler(String(ev.points[0].x), true)); // un clic sur une courbe : l'image la plus proche
|
||||
xaxis: { type: "date", range: b ? b.xaxis.range.slice() : [readable(A), readable(B)], ...axis(c) },
|
||||
yaxis: { title: { text: p.title, font: { size: 11, color: c.faint } }, uirevision: p.title, ...axis(c) } },
|
||||
CONFIG).then(() => {
|
||||
if (!g.followed) {
|
||||
g.followed = true;
|
||||
g.on("plotly_relayout", () => align(g));
|
||||
g.on("plotly_click", ev => setImage(String(ev.points[0].x), true)); // a click on a curve: the nearest image
|
||||
}
|
||||
return aligne(g);
|
||||
return align(g);
|
||||
});
|
||||
}
|
||||
|
||||
// Les plots nouveaux ou changés (tous : au changement de thème)
|
||||
function dessinePlots(tous) {
|
||||
return Promise.all(courant.current.vues.filter(v => tous || dessines.current[v.id] !== v.m)
|
||||
.map(v => { dessines.current[v.id] = v.m; return dessineFig(figs.current[v.id], v.m); }));
|
||||
// The new or changed plots (all: when the theme changes)
|
||||
function drawPlots(all) {
|
||||
return Promise.all(current.current.views.filter(v => all || drawn.current[v.id] !== v.m)
|
||||
.map(v => { drawn.current[v.id] = v.m; return drawFig(figs.current[v.id], v.m); }));
|
||||
}
|
||||
|
||||
function dessineBarre() {
|
||||
const t = [...courant.current.labels].sort((a, b) => (a.debut < b.debut ? -1 : 1));
|
||||
tries.current = t;
|
||||
const voies = [], voie = t.map(l => { // la première rangée libre, sinon une nouvelle
|
||||
const r = voies.findIndex(f => f <= l.debut), i = r < 0 ? voies.length : r;
|
||||
voies[i] = l.fin;
|
||||
function drawBar() {
|
||||
const t = [...current.current.labels].sort((a, b) => (a.start < b.start ? -1 : 1));
|
||||
sorted.current = t;
|
||||
const lanes = [], lane = t.map(l => { // the first free row, otherwise a new one
|
||||
const r = lanes.findIndex(f => f <= l.start), i = r < 0 ? lanes.length : r;
|
||||
lanes[i] = l.end;
|
||||
return i;
|
||||
});
|
||||
const n = Math.max(1, voies.length), gs = liste(), f = gs[0]._fullLayout, { c } = courant.current;
|
||||
vue.current = "";
|
||||
barre.current.style.height = `${40 + VOIE * n}px`; // comme les plots
|
||||
return Plotly.react(barre.current, [{ type: "bar", orientation: "h", y: voie, base: t.map(l => lisible(l.debut)),
|
||||
x: t.map(l => Date.parse(l.fin) - Date.parse(l.debut)), width: 0.8,
|
||||
marker: { color: t.map(couleur), cornerradius: 4, line: contour(t) },
|
||||
text: textes(t, barre.current._fullLayout || f), textposition: "inside", insidetextanchor: "start", textangle: 0,
|
||||
textfont: { family: POLICE, size: 11, color: "#1a1b1e" },
|
||||
unselected: { marker: { opacity: 1 } }, // sinon une sélection les pâlit
|
||||
// la bulle : pour un détecteur, ses cibles seulement ; sinon la famille et tout le texte, que la barre tronque
|
||||
customdata: t.map(l => (l.message ? coupe(l.cibles.length ? l.cibles.join(", ") : l.references.length
|
||||
? l.references.map(s => s + " (ref.)").join(", ") : "Tous les capteurs")
|
||||
: (l.categorie ? `<b>${NOM_FAMILLE(l.categorie)}</b><br>` : "") + coupe(l.description))),
|
||||
const n = Math.max(1, lanes.length), gs = plotDivs(), f = gs[0]._fullLayout, { c } = current.current;
|
||||
viewKey.current = "";
|
||||
bar.current.style.height = `${40 + LANE * n}px`; // as the plots
|
||||
return Plotly.react(bar.current, [{ type: "bar", orientation: "h", y: lane, base: t.map(l => readable(l.start)),
|
||||
x: t.map(l => Date.parse(l.end) - Date.parse(l.start)), width: 0.8,
|
||||
marker: { color: t.map(color), cornerradius: 4, line: outline(t) },
|
||||
text: texts(t, bar.current._fullLayout || f), textposition: "inside", insidetextanchor: "start", textangle: 0,
|
||||
textfont: { family: FONT, size: 11, color: "#1a1b1e" },
|
||||
unselected: { marker: { opacity: 1 } }, // otherwise a selection fades them
|
||||
// the tooltip: for a detector, its targets only; otherwise the family and the whole text, which the bar truncates
|
||||
customdata: t.map(l => (l.message ? wrap(l.targets.length ? l.targets.join(", ") : l.references.length
|
||||
? l.references.map(s => s + " (ref.)").join(", ") : "All sensors")
|
||||
: (l.family ? `<b>${FAMILY_NAME(l.family)}</b><br>` : "") + wrap(l.description))),
|
||||
hovertemplate: "%{customdata}<extra></extra>" }],
|
||||
{ ...habillage(c), height: 40 + VOIE * n, dragmode: lecteur ? "zoom" : "select", selectdirection: "h", hovermode: "closest", plot_bgcolor: c.fond, showlegend: false,
|
||||
margin: { ...marge(), l: Math.max(...gs.map(g => g._fullLayout._size.l)), r: Math.max(...gs.map(g => g._fullLayout._size.r)) },
|
||||
xaxis: { type: "date", range: f.xaxis.range.slice(), showgrid: false, ticks: "", color: c.texte },
|
||||
{ ...skin(c), height: 40 + LANE * n, dragmode: readOnly ? "zoom" : "select", selectdirection: "h", hovermode: "closest", plot_bgcolor: c.bg, showlegend: false,
|
||||
margin: { ...margin(), l: Math.max(...gs.map(g => g._fullLayout._size.l)), r: Math.max(...gs.map(g => g._fullLayout._size.r)) },
|
||||
xaxis: { type: "date", range: f.xaxis.range.slice(), showgrid: false, ticks: "", color: c.text },
|
||||
yaxis: { range: [n - 0.5, -0.5], fixedrange: true, showticklabels: false, showgrid: false, zeroline: false },
|
||||
newselection: { line: { color: BLEU, width: 1 } }, activeselection: { fillcolor: BLEU, opacity: 0.25 } },
|
||||
newselection: { line: { color: BLUE, width: 1 } }, activeselection: { fillcolor: BLUE, opacity: 0.25 } },
|
||||
{ responsive: true, displayModeBar: false, doubleClick: false });
|
||||
}
|
||||
|
||||
// Le premier dessin, puis les événements de la barre, une fois
|
||||
// The first drawing, then the events of the bar, once
|
||||
useEffect(() => {
|
||||
if (!Plotly) return;
|
||||
dessinePlots().then(dessineBarre).then(() => aligne()).then(() => {
|
||||
barre.current.on("plotly_selected", ev => {
|
||||
drawPlots().then(drawBar).then(() => align()).then(() => {
|
||||
bar.current.on("plotly_selected", ev => {
|
||||
const r = ev?.range?.x;
|
||||
if (r) setChoix(r.map(v => String(v).slice(0, 16).replace(" ", "T") + "Z"));
|
||||
if (r) setChoice(r.map(v => String(v).slice(0, 16).replace(" ", "T") + "Z"));
|
||||
});
|
||||
barre.current.on("plotly_deselect", () => setChoix(null));
|
||||
barre.current.on("plotly_relayout", () => aligne(barre.current)); // zoomée (Detectors, LLM) ou redimensionnée seule (l'image agrandie)
|
||||
// Un clic sur une étiquette l'ouvre ; un clic ailleurs sur la barre (pas un glisser) la referme
|
||||
let surEtiquette = false, x0 = 0;
|
||||
barre.current.on("plotly_click", ev => { surEtiquette = true; courant.current.ouvrir(tries.current[ev.points[0].pointIndex]); });
|
||||
barre.current.addEventListener("mousedown", e => { x0 = e.clientX; }, true);
|
||||
barre.current.addEventListener("click", e => {
|
||||
if (!surEtiquette && Math.abs(e.clientX - x0) < 4) courant.current.ouvrir(null);
|
||||
surEtiquette = false;
|
||||
bar.current.on("plotly_deselect", () => setChoice(null));
|
||||
bar.current.on("plotly_relayout", () => align(bar.current)); // zoomed (Detectors, LLM) or resized alone (the image enlarged)
|
||||
// A click on a label opens it; a click elsewhere on the bar (not a drag) closes it
|
||||
let onLabel = false, x0 = 0;
|
||||
bar.current.on("plotly_click", ev => { onLabel = true; current.current.open(sorted.current[ev.points[0].pointIndex]); });
|
||||
bar.current.addEventListener("mousedown", e => { x0 = e.clientX; }, true);
|
||||
bar.current.addEventListener("click", e => {
|
||||
if (!onLabel && Math.abs(e.clientX - x0) < 4) current.current.open(null);
|
||||
onLabel = false;
|
||||
});
|
||||
setPret(true);
|
||||
setReady(true);
|
||||
});
|
||||
return () => [...Object.values(figs.current), barre.current].forEach(g => g && Plotly.purge(g));
|
||||
return () => [...Object.values(figs.current), bar.current].forEach(g => g && Plotly.purge(g));
|
||||
}, [Plotly]);
|
||||
|
||||
useEffect(() => { if (pret) dessinePlots().then(() => aligne(barre.current)); }, [vues]);
|
||||
useEffect(() => { if (pret) dessineBarre(); }, [pret, labels]);
|
||||
// Glisser sur la barre : sélectionner une plage (Labelling), sinon zoomer dans le temps
|
||||
useEffect(() => { if (pret) Plotly.relayout(barre.current, { dragmode: lecteur ? "zoom" : "select", selections: [] }); }, [pret, lecteur]);
|
||||
useEffect(() => { if (pret) dessinePlots(true).then(dessineBarre); }, [sombre]);
|
||||
useEffect(() => { if (pret) dessinePlots(true); }, [montres]);
|
||||
useEffect(() => { if (pret) liste().forEach(g => Plotly.relayout(g, { shapes: zones() })); }, [pret, labels, choix, ouverte]);
|
||||
useEffect(() => { if (ready) drawPlots().then(() => align(bar.current)); }, [views]);
|
||||
useEffect(() => { if (ready) drawBar(); }, [ready, labels]);
|
||||
// Drag on the bar: select a range (Analysis), otherwise zoom in time
|
||||
useEffect(() => { if (ready) Plotly.relayout(bar.current, { dragmode: readOnly ? "zoom" : "select", selections: [] }); }, [ready, readOnly]);
|
||||
useEffect(() => { if (ready) drawPlots(true).then(drawBar); }, [dark]);
|
||||
useEffect(() => { if (ready) drawPlots(true); }, [shown]);
|
||||
useEffect(() => { if (ready) plotDivs().forEach(g => Plotly.relayout(g, { shapes: zones() })); }, [ready, labels, choice, opened]);
|
||||
useEffect(() => {
|
||||
if (!pret || !barre.current._fullLayout) return;
|
||||
const k = contour(tries.current);
|
||||
Plotly.restyle(barre.current, { "marker.line.color": [k.color], "marker.line.width": [k.width] });
|
||||
}, [ouverte]);
|
||||
if (!ready || !bar.current._fullLayout) return;
|
||||
const k = outline(sorted.current);
|
||||
Plotly.restyle(bar.current, { "marker.line.color": [k.color], "marker.line.width": [k.width] });
|
||||
}, [opened]);
|
||||
|
||||
// Lues à chaque chargement et à chaque changement de source : la page montre toujours le fichier ; les détecteurs tournent
|
||||
// sur la plage la première fois (une minute ou plus), le serveur garde leur résultat
|
||||
// Read at each load and at each change of source: the page always shows the file; the detectors run on the range the
|
||||
// first time (a minute or more), the server keeps their result
|
||||
useEffect(() => {
|
||||
let vivant = true;
|
||||
setLecture(true);
|
||||
const url = source === "detecteurs" ? `/api/detecteurs?${new URLSearchParams({ station, du, au, cibles })}`
|
||||
: `/api/etiquettes?${new URLSearchParams({ station, dossier })}`;
|
||||
appel(url).then(l => {
|
||||
if (!vivant) return; // une autre source a été choisie entre-temps
|
||||
setLecture(false); setToutes(l || []);
|
||||
let alive = true;
|
||||
setReading(true);
|
||||
const url = source === "detectors" ? `/api/detectors?${new URLSearchParams({ station, start, end, targets })}`
|
||||
: `/api/labels?${new URLSearchParams({ station, folder })}`;
|
||||
call(url).then(l => {
|
||||
if (!alive) return; // another source was chosen in the meantime
|
||||
setReading(false); setAllLabels(l || []);
|
||||
});
|
||||
return () => { vivant = false; };
|
||||
}, [source, cibles, rev]);
|
||||
return () => { alive = false; };
|
||||
}, [source, targets, rev]);
|
||||
|
||||
// Un plot de plus : le premier que la page ne montre pas encore, déplacement vertical d'abord
|
||||
function ajouterPlot() {
|
||||
const pris = vues.map(v => v.m), id = prochain.current++;
|
||||
const m = [indice("Relative Corrected Vertical Shift", 0), ...PLOTS.keys()].find(i => !pris.includes(i)) ?? 0;
|
||||
setVues([...vues, { id, m }]);
|
||||
// One more plot: the first one the page does not show yet, vertical displacement first
|
||||
function addPlot() {
|
||||
const taken = views.map(v => v.m), id = nextId.current++;
|
||||
const m = [plotIndex("Relative Corrected Vertical Shift", 0), ...PLOTS.keys()].find(i => !taken.includes(i)) ?? 0;
|
||||
setViews([...views, { id, m }]);
|
||||
}
|
||||
|
||||
function enleverPlot(id) {
|
||||
function removePlot(id) {
|
||||
Plotly.purge(figs.current[id]);
|
||||
delete figs.current[id]; delete dessines.current[id];
|
||||
setVues(vues.filter(v => v.id !== id));
|
||||
delete figs.current[id]; delete drawn.current[id];
|
||||
setViews(views.filter(v => v.id !== id));
|
||||
}
|
||||
|
||||
const modifiee = () => ouverte && edit && (edit.texte.trim() !== ouverte.description || edit.categorie !== (ouverte.categorie || "")
|
||||
|| edit.debut + "Z" !== ouverte.debut || edit.fin + "Z" !== ouverte.fin);
|
||||
const modified = () => opened && edit && (edit.text.trim() !== opened.description || edit.family !== (opened.family || "")
|
||||
|| edit.start + "Z" !== opened.start || edit.end + "Z" !== opened.end);
|
||||
|
||||
// l : l'étiquette à afficher, null pour fermer -> false si Marc garde ses changements non enregistrés
|
||||
function ouvrir(l) {
|
||||
if (modifiee() && !confirm("Abandonner les changements de cette étiquette ?")) return false;
|
||||
setOuverte(l); setNote("");
|
||||
if (l) caler(l.debut); // un nouvel objet à chaque clic : recliquer le même événement y ramène les images
|
||||
setEdit(l && !lecteur ? { debut: l.debut.slice(0, 16), fin: l.fin.slice(0, 16), categorie: l.categorie || "", texte: l.description } : null);
|
||||
// l: the label to show, null to close -> false if Marc keeps his unsaved changes
|
||||
function open(l) {
|
||||
if (modified() && !confirm("Discard the changes to this label?")) return false;
|
||||
setOpened(l); setNote("");
|
||||
if (l) setImage(l.start); // a new object at each click: clicking the same event again brings the images back to it
|
||||
setEdit(l && !readOnly ? { start: l.start.slice(0, 16), end: l.end.slice(0, 16), family: l.family || "", text: l.description } : null);
|
||||
return true;
|
||||
}
|
||||
|
||||
async function analyser() {
|
||||
const r = await appel("/api/analyses", "POST", { station, debut: choix[0], fin: choix[1], indices });
|
||||
if (!r) return; // rien n'est perdu : les indices et la plage restent dans la page
|
||||
setAnalyse(r.nom); setIndices("");
|
||||
await Plotly.relayout(barre.current, { selections: [] });
|
||||
setChoix(null);
|
||||
async function runAnalysis() {
|
||||
const r = await call("/api/analyses", "POST", { station, start: choice[0], end: choice[1], hints });
|
||||
if (!r) return; // nothing is lost: the hints and the range stay in the page
|
||||
setAnalysis(r.name); setHints("");
|
||||
await Plotly.relayout(bar.current, { selections: [] });
|
||||
setChoice(null);
|
||||
}
|
||||
|
||||
async function enregistrer() {
|
||||
const l = await appel("/api/etiquettes", "PUT", { station, dossier, avant: ouverte, debut: edit.debut + "Z", fin: edit.fin + "Z",
|
||||
categorie: edit.categorie, description: edit.texte.trim() });
|
||||
if (!l) return; // rien n'est perdu : le texte et les bornes restent dans la fenêtre
|
||||
setToutes(ls => ls.map(x => (x === ouverte ? l : x)));
|
||||
setOuverte(l); setNote("Enregistrée");
|
||||
async function save() {
|
||||
const l = await call("/api/labels", "PUT", { station, folder, before: opened, start: edit.start + "Z", end: edit.end + "Z",
|
||||
family: edit.family, description: edit.text.trim() });
|
||||
if (!l) return; // nothing is lost: the text and the bounds stay in the window
|
||||
setAllLabels(ls => ls.map(x => (x === opened ? l : x)));
|
||||
setOpened(l); setNote("Saved");
|
||||
}
|
||||
|
||||
async function supprimer() {
|
||||
if (!confirm("Supprimer cette étiquette ?")) return;
|
||||
if (await appel("/api/etiquettes", "DELETE", { station, dossier, avant: ouverte }) === null) return;
|
||||
setToutes(ls => ls.filter(x => x !== ouverte));
|
||||
setOuverte(null); setEdit(null);
|
||||
async function remove() {
|
||||
if (!confirm("Delete this label?")) return;
|
||||
if (await call("/api/labels", "DELETE", { station, folder, before: opened }) === null) return;
|
||||
setAllLabels(ls => ls.filter(x => x !== opened));
|
||||
setOpened(null); setEdit(null);
|
||||
}
|
||||
|
||||
const options = PLOTS.map((p, i) => ({ value: String(i), label: p.titre }));
|
||||
const peutEnregistrer = modifiee() && edit.texte.trim() && edit.debut && edit.fin && edit.debut < edit.fin;
|
||||
const bornes = (champ, plus) => lisible(toutes.reduce((m, l) => (plus(l[champ], m) ? l[champ] : m), toutes[0][champ]));
|
||||
const options = PLOTS.map((p, i) => ({ value: String(i), label: p.title }));
|
||||
const canSave = modified() && edit.text.trim() && edit.start && edit.end && edit.start < edit.end;
|
||||
const extent = (field, beyond) => readable(allLabels.reduce((m, l) => (beyond(l[field], m) ? l[field] : m), allLabels[0][field]));
|
||||
return (
|
||||
<Card shadow="sm" withBorder>
|
||||
<Stack gap="sm">
|
||||
<div>
|
||||
{vues.map(v => (
|
||||
{views.map(v => (
|
||||
<div key={v.id} className="plot">
|
||||
<div ref={el => { if (el) figs.current[v.id] = el; }} />
|
||||
<Group gap={4} wrap="nowrap" className="plot-menu">
|
||||
<NativeSelect size="xs" aria-label="Plot" data={options} value={String(v.m)} w={300}
|
||||
onChange={e => { const m = +e.currentTarget.value; setVues(vues.map(x => (x.id === v.id ? { ...x, m } : x))); }} />
|
||||
{vues.length > 1 && (
|
||||
<Tooltip label="Enlever ce plot">
|
||||
<ActionIcon variant="subtle" color="gray" aria-label="Enlever ce plot" onClick={() => enleverPlot(v.id)}><IconX size={16} /></ActionIcon>
|
||||
onChange={e => { const m = +e.currentTarget.value; setViews(views.map(x => (x.id === v.id ? { ...x, m } : x))); }} />
|
||||
{views.length > 1 && (
|
||||
<Tooltip label="Remove this plot">
|
||||
<ActionIcon variant="subtle" color="gray" aria-label="Remove this plot" onClick={() => removePlot(v.id)}><IconX size={16} /></ActionIcon>
|
||||
</Tooltip>
|
||||
)}
|
||||
</Group>
|
||||
</div>
|
||||
))}
|
||||
{!pret && <Loader size="sm" />}
|
||||
{!ready && <Loader size="sm" />}
|
||||
</div>
|
||||
<Group justify="space-between" gap="sm">
|
||||
<Group gap="sm">
|
||||
<SegmentedControl size="xs" data={SOURCES} value={source} onChange={v => { if (ouvrir(null)) { setSource(v); setChoix(null); } }} />
|
||||
{lecture && <Group gap={6}><Loader size="xs" /><Text size="xs" c="dimmed">{detect ? "Les détecteurs tournent sur la plage…" : "Lecture…"}</Text></Group>}
|
||||
<SegmentedControl size="xs" data={SOURCES} value={source} onChange={v => { if (open(null)) { setSource(v); setChoice(null); } }} />
|
||||
{reading && <Group gap={6}><Loader size="xs" /><Text size="xs" c="dimmed">{detect ? "The detectors are running on the range…" : "Reading…"}</Text></Group>}
|
||||
</Group>
|
||||
<Button size="xs" variant="light" leftSection={<IconPlus size={14} />} onClick={ajouterPlot} disabled={!pret}>Ajouter un plot</Button>
|
||||
<Button size="xs" variant="light" leftSection={<IconPlus size={14} />} onClick={addPlot} disabled={!ready}>Add a plot</Button>
|
||||
</Group>
|
||||
<div ref={barre} />
|
||||
{pret && !lecture && !labels.length && ( // sinon une barre vide ne dit pas pourquoi
|
||||
<Text size="sm" c="orange">{detect ? `Les détecteurs ne trouvent aucun événement sur cette plage${toutes.length ? " dans les familles cochées" : ""}.`
|
||||
: toutes.length ? `Aucune étiquette dans les familles cochées sur cette plage : celles de ${station} vont du ${bornes("debut", (a, b) => a < b)} au ${bornes("fin", (a, b) => a > b)} UTC.`
|
||||
: `${station} n'a aucune étiquette dans labeling/${dossier}/.`}</Text>
|
||||
<div ref={bar} />
|
||||
{ready && !reading && !labels.length && ( // otherwise an empty bar does not say why
|
||||
<Text size="sm" c="orange">{detect ? `The detectors find no event in this range${allLabels.length ? " in the checked families" : ""}.`
|
||||
: allLabels.length ? `No label in the checked families in this range: those of ${station} go from ${extent("start", (a, b) => a < b)} to ${extent("end", (a, b) => a > b)} UTC.`
|
||||
: `${station} has no label in labeling/${folder}/.`}</Text>
|
||||
)}
|
||||
<div>
|
||||
{!ouverte && lecteur && <Text size="sm" c="dimmed">Cliquer sur un événement pour l'ouvrir.</Text>}
|
||||
{ouverte && lecteur && (
|
||||
{!opened && readOnly && <Text size="sm" c="dimmed">Click on an event to open it.</Text>}
|
||||
{opened && readOnly && (
|
||||
<Card withBorder bg="var(--mantine-color-default-hover)">
|
||||
<Stack gap={6}>
|
||||
<Group gap="xs">
|
||||
<Badge color={couleur(ouverte)} c="#1a1b1e">{ouverte.categorie}</Badge>
|
||||
<Text size="sm" fw={500}>{ouverte.sorte}</Text>
|
||||
{ouverte.ignore && <Badge variant="outline" color="gray">grisé au journal</Badge>}
|
||||
<Badge color={color(opened)} c="#1a1b1e">{opened.family}</Badge>
|
||||
<Text size="sm" fw={500}>{opened.kind}</Text>
|
||||
{opened.ignore && <Badge variant="outline" color="gray">greyed in the journal</Badge>}
|
||||
</Group>
|
||||
<Text size="sm" ff="monospace" fw={600}>{lisible(ouverte.debut)} → {lisible(ouverte.fin)} UTC</Text>
|
||||
{ouverte.message ? <>
|
||||
<Champ nom="Capteurs">{ouverte.capteurs.length ? <>
|
||||
{ouverte.cibles.length > 0 && <div>Cibles ({ouverte.cibles.length}) : {ouverte.cibles.join(", ")}</div>}
|
||||
{ouverte.references.length > 0 && <div>Références ({ouverte.references.length}) : {ouverte.references.join(", ")}</div>}
|
||||
</> : "tous les capteurs"}</Champ>
|
||||
<Champ nom="Message">{ouverte.message}</Champ>
|
||||
<Champ nom="Action">{ouverte.action}</Champ>
|
||||
</> : <Champ nom="Description">{ouverte.description}</Champ>}
|
||||
<Text size="sm" ff="monospace" fw={600}>{readable(opened.start)} → {readable(opened.end)} UTC</Text>
|
||||
{opened.message ? <>
|
||||
<Field name="Sensors">{opened.sensors.length ? <>
|
||||
{opened.targets.length > 0 && <div>Targets ({opened.targets.length}): {opened.targets.join(", ")}</div>}
|
||||
{opened.references.length > 0 && <div>References ({opened.references.length}): {opened.references.join(", ")}</div>}
|
||||
</> : "all sensors"}</Field>
|
||||
<Field name="Message">{opened.message}</Field>
|
||||
<Field name="Action">{opened.action}</Field>
|
||||
</> : <Field name="Description">{opened.description}</Field>}
|
||||
<Group>
|
||||
<Button size="xs" variant="default" onClick={() => ouvrir(null)}>Fermer</Button>
|
||||
{ouverte.analyse && <Button size="xs" variant="light" onClick={() => setAnalyse(ouverte.analyse)}>Voir le rapport</Button>}
|
||||
<Button size="xs" variant="default" onClick={() => open(null)}>Close</Button>
|
||||
{opened.analysis && <Button size="xs" variant="light" onClick={() => setAnalysis(opened.analysis)}>View the report</Button>}
|
||||
</Group>
|
||||
</Stack>
|
||||
</Card>
|
||||
)}
|
||||
{ouverte && edit && (
|
||||
{opened && edit && (
|
||||
<Card withBorder bg="var(--mantine-color-default-hover)">
|
||||
<Stack gap="xs">
|
||||
<Group gap="sm" align="flex-end">
|
||||
<TextInput label="Du (UTC)" type="datetime-local" step={60} value={edit.debut} onChange={e => setEdit({ ...edit, debut: e.currentTarget.value })} />
|
||||
<TextInput label="Au (UTC)" type="datetime-local" step={60} value={edit.fin} onChange={e => setEdit({ ...edit, fin: e.currentTarget.value })} />
|
||||
<NativeSelect label="Famille" data={CHOIX_FAMILLE} value={edit.categorie} onChange={e => setEdit({ ...edit, categorie: e.currentTarget.value })} />
|
||||
<TextInput label="From (UTC)" type="datetime-local" step={60} value={edit.start} onChange={e => setEdit({ ...edit, start: e.currentTarget.value })} />
|
||||
<TextInput label="To (UTC)" type="datetime-local" step={60} value={edit.end} onChange={e => setEdit({ ...edit, end: e.currentTarget.value })} />
|
||||
<NativeSelect label="Family" data={FAMILY_CHOICES} value={edit.family} onChange={e => setEdit({ ...edit, family: e.currentTarget.value })} />
|
||||
</Group>
|
||||
<Textarea autosize minRows={3} value={edit.texte} onChange={e => setEdit({ ...edit, texte: e.currentTarget.value })} />
|
||||
<Textarea autosize minRows={3} value={edit.text} onChange={e => setEdit({ ...edit, text: e.currentTarget.value })} />
|
||||
<Group gap="sm">
|
||||
<Button onClick={enregistrer} disabled={!peutEnregistrer}>Enregistrer</Button>
|
||||
<Button variant="default" onClick={() => ouvrir(null)}>Fermer</Button>
|
||||
{ouverte.analyse && <Button variant="light" onClick={() => setAnalyse(ouverte.analyse)}>Voir le rapport</Button>}
|
||||
<Button onClick={save} disabled={!canSave}>Save</Button>
|
||||
<Button variant="default" onClick={() => open(null)}>Close</Button>
|
||||
{opened.analysis && <Button variant="light" onClick={() => setAnalysis(opened.analysis)}>View the report</Button>}
|
||||
<Text size="sm" c="teal">{note}</Text>
|
||||
<Button color="red" variant="light" ml="auto" onClick={supprimer}>Supprimer</Button>
|
||||
<Button color="red" variant="light" ml="auto" onClick={remove}>Delete</Button>
|
||||
</Group>
|
||||
</Stack>
|
||||
</Card>
|
||||
)}
|
||||
</div>
|
||||
{!lecteur && <>
|
||||
<Text size="sm">{choix ? `${lisible(choix[0])} → ${lisible(choix[1])} UTC` : "Glisser sur la barre des étiquettes pour sélectionner la plage à analyser."}</Text>
|
||||
<Textarea autosize minRows={2} placeholder="Indices pour l'analyste (facultatif) : ce que tu as vu, ce qu'il faut regarder"
|
||||
value={indices} onChange={e => setIndices(e.currentTarget.value)} />
|
||||
<Group><Button onClick={analyser} disabled={!(choix && choix[0] < choix[1])}>Analyser la plage</Button></Group>
|
||||
{!readOnly && <>
|
||||
<Text size="sm">{choice ? `${readable(choice[0])} → ${readable(choice[1])} UTC` : "Drag on the label bar to select the range to analyze."}</Text>
|
||||
<Textarea autosize minRows={2} placeholder="Hints for the analyst (optional): what you saw, what to look at"
|
||||
value={hints} onChange={e => setHints(e.currentTarget.value)} />
|
||||
<Group><Button onClick={runAnalysis} disabled={!(choice && choice[0] < choice[1])}>Analyze the range</Button></Group>
|
||||
</>}
|
||||
{analyse && <Analyse key={analyse} nom={analyse} sombre={sombre} fini={() => setRev(r => r + 1)} fermer={() => setAnalyse(null)} />}
|
||||
{analysis && <Analysis key={analysis} name={analysis} dark={dark} done={() => setRev(r => r + 1)} close={() => setAnalysis(null)} />}
|
||||
</Stack>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// Le fil d'un agent (analyste, optimisation) : les messages de l'utilisateur (« › ») en gras, le reste en petit, défilé jusqu'en bas
|
||||
function Fil({ lignes }) {
|
||||
const fil = useRef();
|
||||
useEffect(() => { if (fil.current) fil.current.scrollTop = fil.current.scrollHeight; }, [lignes.length]);
|
||||
if (!lignes.length) return null;
|
||||
// The thread of an agent (analyst, optimization): the user's messages ("› ") in bold, the rest small, scrolled to the bottom
|
||||
function Thread({ lines }) {
|
||||
const thread = useRef();
|
||||
useEffect(() => { if (thread.current) thread.current.scrollTop = thread.current.scrollHeight; }, [lines.length]);
|
||||
if (!lines.length) return null;
|
||||
return (
|
||||
<div ref={fil} style={{ maxHeight: 260, overflowY: "auto" }}>
|
||||
{lignes.map((l, i) => l.startsWith("› ")
|
||||
<div ref={thread} style={{ maxHeight: 260, overflowY: "auto" }}>
|
||||
{lines.map((l, i) => l.startsWith("› ")
|
||||
? <Text key={i} size="sm" fw={500} mt={6}>{l}</Text>
|
||||
: <Text key={i} size="xs" ff="monospace" c="dimmed" style={{ whiteSpace: "pre-wrap" }}>{l}</Text>)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// Un markdown rendu par le serveur (rapport, bilan), à la hauteur de son contenu une fois ses images chargées
|
||||
function Page({ titre, src }) {
|
||||
const cadre = useRef();
|
||||
const ajuste = () => { const b = cadre.current?.contentDocument?.body; if (b) cadre.current.style.height = `${b.scrollHeight + 20}px`; };
|
||||
return <iframe ref={cadre} title={titre} sandbox="allow-same-origin" onLoad={ajuste} style={{ border: 0, width: "100%", height: 400 }} src={src} />;
|
||||
// A markdown rendered by the server (report, summary), at the height of its content once its images are loaded
|
||||
function Page({ title, src }) {
|
||||
const frame = useRef();
|
||||
const fit = () => { const b = frame.current?.contentDocument?.body; if (b) frame.current.style.height = `${b.scrollHeight + 20}px`; };
|
||||
return <iframe ref={frame} title={title} sandbox="allow-same-origin" onLoad={fit} style={{ border: 0, width: "100%", height: 400 }} src={src} />;
|
||||
}
|
||||
|
||||
const COULEUR_ETAT = { "en cours": "blue", fini: "teal", erreur: "red", interrompue: "orange" };
|
||||
const STATE_COLOR = { running: "blue", done: "teal", error: "red", interrupted: "orange" };
|
||||
|
||||
// Une analyse de l'onglet Analysis : son fil (relu toutes les 3 s tant qu'elle travaille), son rapport, et un message à
|
||||
// l'analyste, qui reprend sa session ; à la fin de chaque tour, les étiquettes se relisent. Une analyse finie s'optimise
|
||||
// (/improve d'Optimizer sur la plage de l'analyse, puis le tool-builder) : le fil de la séance, puis ses bilans sous le rapport
|
||||
function Analyse({ nom, sombre, fini, fermer }) {
|
||||
const [a, setA] = useState(null), [tour, setTour] = useState(0), [texte, setTexte] = useState("");
|
||||
const boite = useRef(), charge = !!a;
|
||||
useEffect(() => { if (charge) boite.current?.scrollIntoView({ behavior: "smooth", block: "start" }); }, [nom, charge]); // ouverte : on la voit
|
||||
// An analysis of the Analysis tab: its thread (read again every 3 s while it works), its report, and a message to the
|
||||
// analyst, which resumes its session; at the end of each turn, the labels are read again. A finished analysis can be
|
||||
// optimized (/improve of Optimizer on the range of the analysis, then the tool-builder): the thread of the session, then
|
||||
// its summaries under the report
|
||||
function Analysis({ name, dark, done, close }) {
|
||||
const [a, setA] = useState(null), [turn, setTurn] = useState(0), [text, setText] = useState("");
|
||||
const box = useRef(), loaded = !!a;
|
||||
useEffect(() => { if (loaded) box.current?.scrollIntoView({ behavior: "smooth", block: "start" }); }, [name, loaded]); // opened: it is in view
|
||||
useEffect(() => {
|
||||
let vivant = true, minuterie;
|
||||
const lire = () => appel(`/api/analyses/${nom}`).then(r => {
|
||||
if (!vivant || !r) return;
|
||||
let alive = true, timer;
|
||||
const read = () => call(`/api/analyses/${name}`).then(r => {
|
||||
if (!alive || !r) return;
|
||||
setA(r);
|
||||
if (r.etat === "en cours" || r.optimisation?.etat === "en cours") minuterie = setTimeout(lire, 3000);
|
||||
else if (r.versees !== null) fini();
|
||||
if (r.state === "running" || r.optimization?.state === "running") timer = setTimeout(read, 3000);
|
||||
else if (r.submitted !== null) done();
|
||||
});
|
||||
lire();
|
||||
return () => { vivant = false; clearTimeout(minuterie); };
|
||||
}, [nom, tour]);
|
||||
async function envoyer() {
|
||||
if (await appel(`/api/analyses/${nom}/message`, "POST", { texte }) == null) return; // le texte reste dans le champ
|
||||
setTexte(""); setTour(t => t + 1);
|
||||
read();
|
||||
return () => { alive = false; clearTimeout(timer); };
|
||||
}, [name, turn]);
|
||||
async function send() {
|
||||
if (await call(`/api/analyses/${name}/message`, "POST", { text }) == null) return; // the text stays in the field
|
||||
setText(""); setTurn(t => t + 1);
|
||||
}
|
||||
async function optimiser() {
|
||||
if (await appel(`/api/analyses/${nom}/optimiser`, "POST") != null) setTour(t => t + 1);
|
||||
async function optimize() {
|
||||
if (await call(`/api/analyses/${name}/optimize`, "POST") != null) setTurn(t => t + 1);
|
||||
}
|
||||
if (!a) return <Loader size="sm" />;
|
||||
const d = a.demande, o = a.optimisation, occupe = a.etat === "en cours", optimise = o?.etat === "en cours";
|
||||
const au = new Date(Date.parse(d.fin.slice(0, 10)) + 864e5).toISOString().slice(0, 10);
|
||||
const d = a.request, o = a.optimization, busy = a.state === "running", optimizing = o?.state === "running";
|
||||
const end = new Date(Date.parse(d.end.slice(0, 10)) + 864e5).toISOString().slice(0, 10);
|
||||
return (
|
||||
<Card withBorder ref={boite}>
|
||||
<Card withBorder ref={box}>
|
||||
<Stack gap="xs">
|
||||
<Group justify="space-between">
|
||||
<Group gap="xs">
|
||||
<Title order={5}>Analyse {lisible(d.debut)} → {lisible(d.fin)} UTC</Title>
|
||||
<Badge color={COULEUR_ETAT[a.etat] || "gray"}>{a.etat}</Badge>
|
||||
{occupe && <Loader size="xs" />}
|
||||
<Title order={5}>Analysis {readable(d.start)} → {readable(d.end)} UTC</Title>
|
||||
<Badge color={STATE_COLOR[a.state] || "gray"}>{a.state}</Badge>
|
||||
{busy && <Loader size="xs" />}
|
||||
</Group>
|
||||
<Group gap="xs">
|
||||
{a.etat === "fini" && <Tooltip label="Améliorer les détecteurs (Optimizer), puis les outils de lecture (tool-builder), depuis cette analyse">
|
||||
<Button size="xs" variant="light" onClick={optimiser} disabled={optimise}>Optimiser les détecteurs</Button></Tooltip>}
|
||||
<ActionIcon variant="subtle" color="gray" aria-label="Fermer le rapport" onClick={fermer}><IconX size={16} /></ActionIcon>
|
||||
{a.state === "done" && <Tooltip label="Improve the detectors (Optimizer), then the reading tools (tool-builder), from this analysis">
|
||||
<Button size="xs" variant="light" onClick={optimize} disabled={optimizing}>Optimize the detectors</Button></Tooltip>}
|
||||
<ActionIcon variant="subtle" color="gray" aria-label="Close the report" onClick={close}><IconX size={16} /></ActionIcon>
|
||||
</Group>
|
||||
</Group>
|
||||
{d.indices && <Text size="sm" c="dimmed">Indices : {d.indices}</Text>}
|
||||
{a.etat === "interrompue" && <Text size="sm" c="orange">Arrêtée avec le serveur avant la fin.{a.dialogue
|
||||
? " Sa session est gardée : écrire « continue » ci-dessous pour la reprendre là où elle en était." : ""}</Text>}
|
||||
{a.versees !== null && a.etat === "fini" && <Text size="sm">{a.versees} étiquette(s) de cette analyse dans labeling/auto/{d.station}.jsonl, à relire.</Text>}
|
||||
{a.rapport && <Page titre="Rapport" src={`/api/analyses/${nom}/rapport.html?sombre=${sombre}&v=${a.rapport}`} />}
|
||||
<Fil lignes={a.lignes} />
|
||||
{d.hints && <Text size="sm" c="dimmed">Hints: {d.hints}</Text>}
|
||||
{a.state === "interrupted" && <Text size="sm" c="orange">Stopped with the server before the end.{a.dialogue
|
||||
? " Its session is kept: write “continue” below to resume it where it left off." : ""}</Text>}
|
||||
{a.submitted !== null && a.state === "done" && <Text size="sm">{a.submitted} label(s) of this analysis in labeling/auto/{d.station}.jsonl, to review.</Text>}
|
||||
{a.report && <Page title="Report" src={`/api/analyses/${name}/report.html?dark=${dark}&v=${a.report}`} />}
|
||||
<Thread lines={a.lines} />
|
||||
{a.dialogue && (
|
||||
<Group align="flex-end" gap="sm" wrap="nowrap">
|
||||
<Textarea autosize minRows={1} style={{ flex: 1 }} placeholder="Une question, une correction pour l'analyste"
|
||||
value={texte} onChange={e => setTexte(e.currentTarget.value)} />
|
||||
<Button onClick={envoyer} disabled={occupe || optimise || !texte.trim()}>Envoyer</Button>
|
||||
<Textarea autosize minRows={1} style={{ flex: 1 }} placeholder="A question, a correction for the analyst"
|
||||
value={text} onChange={e => setText(e.currentTarget.value)} />
|
||||
<Button onClick={send} disabled={busy || optimizing || !text.trim()}>Send</Button>
|
||||
</Group>
|
||||
)}
|
||||
{o && (
|
||||
<Stack gap="xs" mt="sm">
|
||||
<Group gap="xs">
|
||||
<Title order={5}>Optimisation</Title>
|
||||
<Badge color={COULEUR_ETAT[o.etat] || "gray"}>{o.etat}</Badge>
|
||||
{optimise && <Loader size="xs" />}
|
||||
<Title order={5}>Optimization</Title>
|
||||
<Badge color={STATE_COLOR[o.state] || "gray"}>{o.state}</Badge>
|
||||
{optimizing && <Loader size="xs" />}
|
||||
</Group>
|
||||
{o.etat === "interrompue" && <Text size="sm" c="orange">Arrêtée avec le serveur ; la relancer par le bouton.</Text>}
|
||||
<Fil lignes={o.lignes} />
|
||||
{o.bilan && <Page titre="Bilan" src={`/api/analyses/${nom}/bilan.html?sombre=${sombre}&v=${o.bilan}`} />}
|
||||
{o.bilan && <Text size="xs" c="dimmed" ff="monospace">python3 detectors/bench/score.py data/{d.station} --labels labeling/auto/{d.station}.jsonl --from {d.debut.slice(0, 10)} --to {au}</Text>}
|
||||
{o.state === "interrupted" && <Text size="sm" c="orange">Stopped with the server; rerun it with the button.</Text>}
|
||||
<Thread lines={o.lines} />
|
||||
{o.summary && <Page title="Summary" src={`/api/analyses/${name}/summary.html?dark=${dark}&v=${o.summary}`} />}
|
||||
{o.summary && <Text size="xs" c="dimmed" ff="monospace">python3 detectors/bench/score.py data/{d.station} --labels labeling/auto/{d.station}.jsonl --from {d.start.slice(0, 10)} --to {end}</Text>}
|
||||
</Stack>
|
||||
)}
|
||||
<Text size="xs" c="dimmed">labeling/auto/analyses/{nom}/</Text>
|
||||
<Text size="xs" c="dimmed">labeling/auto/analyses/{name}/</Text>
|
||||
</Stack>
|
||||
</Card>
|
||||
);
|
||||
|
||||
+141
-141
@@ -1,240 +1,240 @@
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { ActionIcon, Card, Group, Input, Loader, Stack, Text, Tooltip } from "@mantine/core";
|
||||
import { IconArrowLeft, IconArrowRight, IconChevronLeft, IconChevronRight } from "@tabler/icons-react";
|
||||
import { appel } from "../commun.jsx";
|
||||
import { call } from "../common.jsx";
|
||||
|
||||
// Les images de la station et de la plage choisies ; compacte : à la largeur d'une colonne (page Data), au format de l'image ;
|
||||
// basculer : la flèche qui agrandit l'image vers la gauche (large) ou la ramène ; instant {t} : montrer la première image à t ou après
|
||||
// ({t, proche} : la plus proche de t, avant ou après)
|
||||
export function ImagesDe({ selection, compacte, large, basculer, instant }) {
|
||||
const [donnees, setDonnees] = useState(null), [charge, setCharge] = useState(false);
|
||||
const { station, du, au } = selection;
|
||||
// The images of the chosen station and range; compact: the width of a column (Data page), in the format of the image;
|
||||
// toggle: the arrow that enlarges the image to the left (wide) or brings it back; moment {t}: show the first image at t or
|
||||
// after ({t, nearest}: the nearest to t, before or after)
|
||||
export function StationImages({ selection, compact, wide, toggle, moment }) {
|
||||
const [data, setData] = useState(null), [loading, setLoading] = useState(false);
|
||||
const { station, start, end } = selection;
|
||||
useEffect(() => {
|
||||
setDonnees(null);
|
||||
setData(null);
|
||||
if (!station) return;
|
||||
setCharge(true);
|
||||
appel(`/api/images?${new URLSearchParams({ station, du, au })}`).then(d => { setDonnees(d); setCharge(false); });
|
||||
}, [station, du, au]);
|
||||
if (charge) return <Loader />;
|
||||
if (donnees && !donnees.images.length) return <Text size="sm" c="dimmed">Aucune image de {station} sur cette plage.</Text>;
|
||||
return donnees && <Visionneuse key={`${station}${du}${au}`} station={station} d={donnees} compacte={compacte} large={large} basculer={basculer} instant={instant} />;
|
||||
setLoading(true);
|
||||
call(`/api/images?${new URLSearchParams({ station, start, end })}`).then(d => { setData(d); setLoading(false); });
|
||||
}, [station, start, end]);
|
||||
if (loading) return <Loader />;
|
||||
if (data && !data.images.length) return <Text size="sm" c="dimmed">No image of {station} in this range.</Text>;
|
||||
return data && <Viewer key={`${station}${start}${end}`} station={station} d={data} compact={compact} wide={wide} toggle={toggle} moment={moment} />;
|
||||
}
|
||||
|
||||
function Visionneuse({ station, d, compacte, large, basculer, instant }) {
|
||||
const r = { cadre: useRef(), couche: useRef(), img: useRef(), marques: useRef(), vecteurs: useRef(), glis: useRef(),
|
||||
heure: useRef(), noms: useRef(), barre: useRef(), bande: useRef(), plage: useRef(), echelle: useRef(),
|
||||
prec: useRef(), suiv: useRef(), montre: useRef() };
|
||||
const XY = d.positions, refs = new Set(d.references), avecPositions = Object.keys(XY).length > 0;
|
||||
function Viewer({ station, d, compact, wide, toggle, moment }) {
|
||||
const r = { frame: useRef(), layer: useRef(), img: useRef(), marks: useRef(), vectors: useRef(), slider: useRef(),
|
||||
time: useRef(), names: useRef(), bar: useRef(), band: useRef(), range: useRef(), scale: useRef(),
|
||||
prev: useRef(), next: useRef(), show: useRef() };
|
||||
const XY = d.positions, refs = new Set(d.references), withPositions = Object.keys(XY).length > 0;
|
||||
|
||||
useEffect(() => installer(r, station, d), [d]);
|
||||
useEffect(() => { // « AAAA-MM-JJTHH:MMZ » face aux heures des images, « AAAA-MM-JJ HH:MM » (UTC)
|
||||
if (!instant) return;
|
||||
const t = instant.t.slice(0, 16).replace("T", " "), i = d.images.findIndex(im => im.t >= t);
|
||||
const ms = s => Date.parse(s.slice(0, 16).replace(" ", "T") + "Z"), ecart = j => Math.abs(ms(d.images[j].t) - ms(t));
|
||||
if (instant.proche && i !== 0) r.montre.current(i < 0 || ecart(i - 1) <= ecart(i) ? (i < 0 ? d.images.length : i) - 1 : i);
|
||||
else r.montre.current(i < 0 ? d.images.length - 1 : i);
|
||||
}, [instant, d]);
|
||||
useEffect(() => install(r, station, d), [d]);
|
||||
useEffect(() => { // "YYYY-MM-DDTHH:MMZ" against the times of the images, "YYYY-MM-DD HH:MM" (UTC)
|
||||
if (!moment) return;
|
||||
const t = moment.t.slice(0, 16).replace("T", " "), i = d.images.findIndex(im => im.t >= t);
|
||||
const ms = s => Date.parse(s.slice(0, 16).replace(" ", "T") + "Z"), gap = j => Math.abs(ms(d.images[j].t) - ms(t));
|
||||
if (moment.nearest && i !== 0) r.show.current(i < 0 || gap(i - 1) <= gap(i) ? (i < 0 ? d.images.length : i) - 1 : i);
|
||||
else r.show.current(i < 0 ? d.images.length - 1 : i);
|
||||
}, [moment, d]);
|
||||
|
||||
return (
|
||||
<Card shadow="sm" withBorder padding={0} className="visionneuse">
|
||||
{basculer && (
|
||||
<Tooltip label={large ? "Réduire l'image" : "Agrandir l'image"}>
|
||||
<ActionIcon className="agrandir" variant="filled" color="dark" aria-label={large ? "Réduire l'image" : "Agrandir l'image"} onClick={basculer}>
|
||||
{large ? <IconArrowRight size={18} /> : <IconArrowLeft size={18} />}
|
||||
<Card shadow="sm" withBorder padding={0} className="viewer">
|
||||
{toggle && (
|
||||
<Tooltip label={wide ? "Shrink the image" : "Enlarge the image"}>
|
||||
<ActionIcon className="enlarge" variant="filled" color="dark" aria-label={wide ? "Shrink the image" : "Enlarge the image"} onClick={toggle}>
|
||||
{wide ? <IconArrowRight size={18} /> : <IconArrowLeft size={18} />}
|
||||
</ActionIcon>
|
||||
</Tooltip>
|
||||
)}
|
||||
<div className={compacte ? "cadre compacte" : "cadre"} ref={r.cadre}>
|
||||
<div className="couche" ref={r.couche}>
|
||||
<div className={compact ? "frame compact" : "frame"} ref={r.frame}>
|
||||
<div className="layer" ref={r.layer}>
|
||||
<img ref={r.img} alt="" draggable="false" />
|
||||
<svg className="marques" ref={r.marques} style={{ display: "none" }}>
|
||||
<svg className="marks" ref={r.marks} style={{ display: "none" }}>
|
||||
{Object.entries(XY).map(([s, [x, y]]) => (
|
||||
<g key={s} className={refs.has(s) ? "ref" : "cible"}>
|
||||
<g key={s} className={refs.has(s) ? "ref" : "target"}>
|
||||
<circle cx={x} cy={y} r=".7em" /><text x={x} y={y} dy="-1em">{s}</text>
|
||||
</g>
|
||||
))}
|
||||
</svg>
|
||||
<svg className="vecteurs" ref={r.vecteurs} style={{ display: "none" }}>
|
||||
<defs><marker id="pointe" viewBox="0 0 10 14" refX="9" refY="7" markerWidth="5" markerHeight="7" orient="auto">
|
||||
<svg className="vectors" ref={r.vectors} style={{ display: "none" }}>
|
||||
<defs><marker id="arrowhead" viewBox="0 0 10 14" refX="9" refY="7" markerWidth="5" markerHeight="7" orient="auto">
|
||||
<path d="M0,0 L10,7 L0,14 z" fill="#2ecc40" /></marker></defs>
|
||||
</svg>
|
||||
</div>
|
||||
</div>
|
||||
<Stack gap={6} p="md">
|
||||
<Group gap="sm" wrap="nowrap">
|
||||
<ActionIcon ref={r.prec} variant="light" aria-label="Image précédente"><IconChevronLeft size={18} /></ActionIcon>
|
||||
<input ref={r.glis} className="glisseur" type="range" min="0" max={d.images.length - 1} defaultValue="0" />
|
||||
<ActionIcon ref={r.suiv} variant="light" aria-label="Image suivante"><IconChevronRight size={18} /></ActionIcon>
|
||||
<ActionIcon ref={r.prev} variant="light" aria-label="Previous image"><IconChevronLeft size={18} /></ActionIcon>
|
||||
<input ref={r.slider} className="slider" type="range" min="0" max={d.images.length - 1} defaultValue="0" />
|
||||
<ActionIcon ref={r.next} variant="light" aria-label="Next image"><IconChevronRight size={18} /></ActionIcon>
|
||||
</Group>
|
||||
<div className="barre-plages" ref={r.barre}><div className="bande" ref={r.bande} style={{ display: "none" }} /></div>
|
||||
<div className="range-bar" ref={r.bar}><div className="band" ref={r.band} style={{ display: "none" }} /></div>
|
||||
<Group gap="lg" justify="space-between">
|
||||
<Text ref={r.heure} ff="monospace" size="sm" />
|
||||
<Text ref={r.time} ff="monospace" size="sm" />
|
||||
<Group gap="md">
|
||||
<label className="case"><input type="checkbox" ref={r.noms} disabled={!avecPositions} /> Noms</label>
|
||||
{avecPositions
|
||||
? <span className="legende"><span className="cible">○ Cible</span> <span className="ref">◌ Référence</span></span>
|
||||
: <Text size="sm" c="dimmed">x, y ne sont pas des positions dans l'image sur cette station</Text>}
|
||||
<label className="check"><input type="checkbox" ref={r.names} disabled={!withPositions} /> Names</label>
|
||||
{withPositions
|
||||
? <span className="legend"><span className="target">○ Target</span> <span className="ref">◌ Reference</span></span>
|
||||
: <Text size="sm" c="dimmed">x, y are not positions in the image on this station</Text>}
|
||||
</Group>
|
||||
</Group>
|
||||
<Group gap="lg" justify="space-between">
|
||||
<Text ref={r.plage} size="sm" />
|
||||
<Text ref={r.range} size="sm" />
|
||||
<Group gap="xs" wrap="nowrap">
|
||||
<Text size="sm">Flèches ×</Text>
|
||||
<Input ref={r.echelle} type="number" min={1} defaultValue={100} w={90} size="xs" />
|
||||
<Text size="sm">Arrows ×</Text>
|
||||
<Input ref={r.scale} type="number" min={1} defaultValue={100} w={90} size="xs" />
|
||||
</Group>
|
||||
</Group>
|
||||
<Text size="xs" c="dimmed">← → image · molette : zoom · glisser : se déplacer · double clic : image entière · flèche :
|
||||
corr_x, corr_y à la fin moins au début (px), dans les axes de l'image</Text>
|
||||
<Text size="xs" c="dimmed">← → image · wheel: zoom · drag: move · double click: whole image · arrow:
|
||||
corr_x, corr_y at the end minus at the start (px), in the axes of the image</Text>
|
||||
</Stack>
|
||||
</Card>
|
||||
);
|
||||
}
|
||||
|
||||
// Le comportement de l'image (zoom, déplacement, flèches), sur les éléments de la page -> la fonction qui défait ce qu'il a installé
|
||||
function installer(r, station, d) {
|
||||
const IM = d.images.map(i => ({ t: i.t, src: `/api/image/${encodeURIComponent(station)}/${encodeURIComponent(i.nom)}` }));
|
||||
// The behaviour of the image (zoom, move, arrows), on the elements of the page -> the function that undoes what it installed
|
||||
function install(r, station, d) {
|
||||
const IM = d.images.map(i => ({ t: i.t, src: `/api/image/${encodeURIComponent(station)}/${encodeURIComponent(i.name)}` }));
|
||||
const C = d.corr, XY = d.positions;
|
||||
const cadre = r.cadre.current, couche = r.couche.current, img = r.img.current, glis = r.glis.current, heure = r.heure.current;
|
||||
const marques = r.marques.current, vecteurs = r.vecteurs.current, labels = r.noms.current;
|
||||
const barre = r.barre.current, bande = r.bande.current, plage = r.plage.current, echelle = r.echelle.current;
|
||||
const frame = r.frame.current, layer = r.layer.current, img = r.img.current, slider = r.slider.current, time = r.time.current;
|
||||
const marks = r.marks.current, vectors = r.vectors.current, labels = r.names.current;
|
||||
const bar = r.bar.current, band = r.band.current, range = r.range.current, scale = r.scale.current;
|
||||
let V = 0, Z = { k: 1, x: 0, y: 0 }, P = null;
|
||||
|
||||
function zoom() {
|
||||
Z.x = Math.min(0, Math.max(1 - Z.k, Z.x)); // l'image couvre toujours le cadre : on ne la perd pas en la glissant
|
||||
Z.x = Math.min(0, Math.max(1 - Z.k, Z.x)); // the image always covers the frame: it is not lost by dragging it
|
||||
Z.y = Math.min(0, Math.max(1 - Z.k, Z.y));
|
||||
couche.style.transform = `translate(${Z.x * 100}%, ${Z.y * 100}%) scale(${Z.k})`;
|
||||
// Les noms restent à 12 px à l'écran : les tailles des marques sont en em, et 1 px du svg vaut 1 px de l'image.
|
||||
layer.style.transform = `translate(${Z.x * 100}%, ${Z.y * 100}%) scale(${Z.k})`;
|
||||
// The names stay at 12 px on screen: the sizes of the marks are in em, and 1 px of the svg is 1 px of the image.
|
||||
const W = img.naturalWidth, H = img.naturalHeight;
|
||||
if (W) marques.style.fontSize = vecteurs.style.fontSize = `${12 / (Z.k * Math.min(cadre.clientWidth / W, cadre.clientHeight / H))}px`;
|
||||
cale();
|
||||
if (W) marks.style.fontSize = vectors.style.fontSize = `${12 / (Z.k * Math.min(frame.clientWidth / W, frame.clientHeight / H))}px`;
|
||||
align();
|
||||
}
|
||||
const affiche = () => { marques.style.display = labels.checked && img.naturalWidth ? "" : "none"; };
|
||||
labels.onchange = affiche;
|
||||
r.montre.current = i => montre(i); // pour la page Data : l'image du début d'un événement
|
||||
const display = () => { marks.style.display = labels.checked && img.naturalWidth ? "" : "none"; };
|
||||
labels.onchange = display;
|
||||
r.show.current = i => show(i); // for the Data page: the image of the start of an event
|
||||
|
||||
function montre(i) {
|
||||
function show(i) {
|
||||
V = Math.max(0, Math.min(IM.length - 1, i));
|
||||
glis.value = V;
|
||||
slider.value = V;
|
||||
img.src = IM[V].src;
|
||||
for (const n of [V - 1, V + 1]) if (IM[n]) new Image().src = IM[n].src; // les voisines, en cache
|
||||
for (const n of [V - 1, V + 1]) if (IM[n]) new Image().src = IM[n].src; // the neighbours, in cache
|
||||
}
|
||||
// L'heure change avec l'image, pas avant : l'image d'avant reste affichée tant que la nouvelle se charge.
|
||||
// The time changes with the image, not before: the previous image stays shown while the new one loads.
|
||||
img.onload = () => {
|
||||
heure.textContent = `${IM[V].t} UTC (${V + 1}/${IM.length})`;
|
||||
marques.setAttribute("viewBox", `0 0 ${img.naturalWidth} ${img.naturalHeight}`); // cadré comme l'image, object-fit: contain
|
||||
vecteurs.setAttribute("viewBox", marques.getAttribute("viewBox"));
|
||||
fleches(); zoom(); affiche();
|
||||
time.textContent = `${IM[V].t} UTC (${V + 1}/${IM.length})`;
|
||||
marks.setAttribute("viewBox", `0 0 ${img.naturalWidth} ${img.naturalHeight}`); // framed as the image, object-fit: contain
|
||||
vectors.setAttribute("viewBox", marks.getAttribute("viewBox"));
|
||||
arrows(); zoom(); display();
|
||||
};
|
||||
img.onerror = () => {
|
||||
img.removeAttribute("src");
|
||||
heure.textContent = `${IM[V].t} UTC (${V + 1}/${IM.length}) : image illisible`;
|
||||
affiche();
|
||||
time.textContent = `${IM[V].t} UTC (${V + 1}/${IM.length}): unreadable image`;
|
||||
display();
|
||||
};
|
||||
|
||||
glis.oninput = () => montre(+glis.value);
|
||||
r.prec.current.onclick = () => montre(V - 1);
|
||||
r.suiv.current.onclick = () => montre(V + 1);
|
||||
const clavier = e => {
|
||||
if (!glis.offsetParent // la page Data cachée (une autre page affichée) : les flèches ne sont pas pour elle
|
||||
slider.oninput = () => show(+slider.value);
|
||||
r.prev.current.onclick = () => show(V - 1);
|
||||
r.next.current.onclick = () => show(V + 1);
|
||||
const keyboard = e => {
|
||||
if (!slider.offsetParent // the Data page hidden (another page shown): the arrows are not for it
|
||||
|| e.target.matches("textarea, select, input:not([type=range])") || !["ArrowLeft", "ArrowRight"].includes(e.key)) return;
|
||||
e.preventDefault(); // sinon le glisseur, s'il a le focus, avance une seconde fois
|
||||
montre(V + (e.key === "ArrowLeft" ? -1 : 1));
|
||||
e.preventDefault(); // otherwise the slider, if it has the focus, moves a second time
|
||||
show(V + (e.key === "ArrowLeft" ? -1 : 1));
|
||||
};
|
||||
document.addEventListener("keydown", clavier);
|
||||
document.addEventListener("keydown", keyboard);
|
||||
|
||||
const molette = ev => { // zoom autour du pointeur
|
||||
const wheel = ev => { // zoom around the pointer
|
||||
ev.preventDefault();
|
||||
const b = cadre.getBoundingClientRect(), u = (ev.clientX - b.left) / b.width, v = (ev.clientY - b.top) / b.height;
|
||||
const b = frame.getBoundingClientRect(), u = (ev.clientX - b.left) / b.width, v = (ev.clientY - b.top) / b.height;
|
||||
const k = Math.min(40, Math.max(1, Z.k * Math.exp(-ev.deltaY / 500)));
|
||||
Z = { k, x: u - k * (u - Z.x) / Z.k, y: v - k * (v - Z.y) / Z.k };
|
||||
zoom();
|
||||
};
|
||||
cadre.addEventListener("wheel", molette, { passive: false });
|
||||
cadre.onpointerdown = ev => { // déplacement à la souris
|
||||
cadre.setPointerCapture(ev.pointerId);
|
||||
const b = cadre.getBoundingClientRect(), de = { ...Z, cx: ev.clientX, cy: ev.clientY };
|
||||
cadre.onpointermove = e => { Z.x = de.x + (e.clientX - de.cx) / b.width; Z.y = de.y + (e.clientY - de.cy) / b.height; zoom(); };
|
||||
cadre.onpointerup = () => { cadre.onpointermove = null; };
|
||||
frame.addEventListener("wheel", wheel, { passive: false });
|
||||
frame.onpointerdown = ev => { // move with the mouse
|
||||
frame.setPointerCapture(ev.pointerId);
|
||||
const b = frame.getBoundingClientRect(), from = { ...Z, cx: ev.clientX, cy: ev.clientY };
|
||||
frame.onpointermove = e => { Z.x = from.x + (e.clientX - from.cx) / b.width; Z.y = from.y + (e.clientY - from.cy) / b.height; zoom(); };
|
||||
frame.onpointerup = () => { frame.onpointermove = null; };
|
||||
};
|
||||
cadre.ondblclick = () => { Z = { k: 1, x: 0, y: 0 }; zoom(); };
|
||||
frame.ondblclick = () => { Z = { k: 1, x: 0, y: 0 }; zoom(); };
|
||||
|
||||
// La plage : glisser sur la barre, alignée sur le glisseur ; les flèches vont de la position du capteur à position + k × déplacement.
|
||||
function cale() {
|
||||
const g = glis.getBoundingClientRect(), b = barre.getBoundingClientRect();
|
||||
barre.style.marginLeft = `${(parseFloat(barre.style.marginLeft) || 0) + g.left - b.left}px`; barre.style.width = `${g.width}px`;
|
||||
// The range: drag on the bar, aligned with the slider; the arrows go from the position of the sensor to position + k × displacement.
|
||||
function align() {
|
||||
const g = slider.getBoundingClientRect(), b = bar.getBoundingClientRect();
|
||||
bar.style.marginLeft = `${(parseFloat(bar.style.marginLeft) || 0) + g.left - b.left}px`; bar.style.width = `${g.width}px`;
|
||||
}
|
||||
const indice = ev => { const b = barre.getBoundingClientRect(); return Math.round(Math.max(0, Math.min(1, (ev.clientX - b.left) / b.width)) * (IM.length - 1)); };
|
||||
function bandeau() {
|
||||
const index = ev => { const b = bar.getBoundingClientRect(); return Math.round(Math.max(0, Math.min(1, (ev.clientX - b.left) / b.width)) * (IM.length - 1)); };
|
||||
function drawBand() {
|
||||
const n = Math.max(1, IM.length - 1), [i, j] = P ? [Math.min(...P), Math.max(...P)] : [0, 0];
|
||||
bande.style.left = `${i / n * 100}%`; bande.style.width = `${(j - i) / n * 100}%`; bande.style.display = P ? "" : "none";
|
||||
band.style.left = `${i / n * 100}%`; band.style.width = `${(j - i) / n * 100}%`; band.style.display = P ? "" : "none";
|
||||
}
|
||||
function deplacements() {
|
||||
function displacements() {
|
||||
if (!P || P[0] === P[1]) return {};
|
||||
const [i, j] = [Math.min(...P), Math.max(...P)], dep = {};
|
||||
for (const [s, c] of Object.entries(C)) if (c[i] && c[j]) dep[s] = [c[j][0] - c[i][0], c[j][1] - c[i][1]];
|
||||
return dep;
|
||||
}
|
||||
function fleches() {
|
||||
const dep = deplacements(), W = img.naturalWidth, k = +echelle.value;
|
||||
vecteurs.style.display = Object.keys(dep).length && W ? "" : "none";
|
||||
vecteurs.querySelectorAll("line").forEach(l => l.remove());
|
||||
function arrows() {
|
||||
const dep = displacements(), W = img.naturalWidth, k = +scale.value;
|
||||
vectors.style.display = Object.keys(dep).length && W ? "" : "none";
|
||||
vectors.querySelectorAll("line").forEach(l => l.remove());
|
||||
for (const [s, [dx, dy]] of Object.entries(dep)) {
|
||||
const [x, y] = XY[s], l = document.createElementNS("http://www.w3.org/2000/svg", "line");
|
||||
Object.entries({ x1: x, y1: y, x2: x + k * dx, y2: y + k * dy }).forEach(([a, v]) => l.setAttribute(a, v));
|
||||
const titre = document.createElementNS("http://www.w3.org/2000/svg", "title");
|
||||
titre.textContent = `${s} : corr_x ${dx.toFixed(3)} px, corr_y ${dy.toFixed(3)} px`;
|
||||
l.appendChild(titre);
|
||||
vecteurs.appendChild(l);
|
||||
const title = document.createElementNS("http://www.w3.org/2000/svg", "title");
|
||||
title.textContent = `${s}: corr_x ${dx.toFixed(3)} px, corr_y ${dy.toFixed(3)} px`;
|
||||
l.appendChild(title);
|
||||
vectors.appendChild(l);
|
||||
}
|
||||
if (P && P[0] !== P[1]) {
|
||||
const [i, j] = [Math.min(...P), Math.max(...P)], manque = Object.keys(C).filter(s => !(s in dep));
|
||||
plage.textContent = `${IM[i].t} → ${IM[j].t} UTC` + (manque.length ? ` · pas de mesure valide à une borne : ${manque.join(", ")}` : "");
|
||||
} else plage.textContent = Object.keys(C).length ? "Glisser sur la barre pour choisir une plage" : "Pas de flèches : x, y ne sont pas des positions dans l'image";
|
||||
const [i, j] = [Math.min(...P), Math.max(...P)], missing = Object.keys(C).filter(s => !(s in dep));
|
||||
range.textContent = `${IM[i].t} → ${IM[j].t} UTC` + (missing.length ? ` · no valid measurement at a bound: ${missing.join(", ")}` : "");
|
||||
} else range.textContent = Object.keys(C).length ? "Drag on the bar to choose a range" : "No arrows: x, y are not positions in the image";
|
||||
}
|
||||
// Ce que saisit le pointeur sur une plage placée : 0 son début, 1 sa fin (à 6 px près), 2 la bande entière, null rien.
|
||||
function prise(ev) {
|
||||
// What the pointer grabs on a placed range: 0 its start, 1 its end (within 6 px), 2 the whole band, null nothing.
|
||||
function grab(ev) {
|
||||
if (!P) return null;
|
||||
const x = ev.clientX - barre.getBoundingClientRect().left, px = i => i / Math.max(1, IM.length - 1) * barre.clientWidth;
|
||||
const x = ev.clientX - bar.getBoundingClientRect().left, px = i => i / Math.max(1, IM.length - 1) * bar.clientWidth;
|
||||
return Math.abs(x - px(P[0])) < 6 ? 0 : Math.abs(x - px(P[1])) < 6 ? 1 : x > px(P[0]) && x < px(P[1]) ? 2 : null;
|
||||
}
|
||||
barre.onpointermove = e => {
|
||||
if (!barre.hasPointerCapture(e.pointerId)) barre.style.cursor = ["ew-resize", "ew-resize", "grab"][prise(e)] || "crosshair";
|
||||
bar.onpointermove = e => {
|
||||
if (!bar.hasPointerCapture(e.pointerId)) bar.style.cursor = ["ew-resize", "ew-resize", "grab"][grab(e)] || "crosshair";
|
||||
};
|
||||
barre.onpointerdown = ev => {
|
||||
barre.setPointerCapture(ev.pointerId);
|
||||
const g = prise(ev), i0 = indice(ev), de = P && [...P];
|
||||
bar.onpointerdown = ev => {
|
||||
bar.setPointerCapture(ev.pointerId);
|
||||
const g = grab(ev), i0 = index(ev), from = P && [...P];
|
||||
if (g === null) P = [i0, i0];
|
||||
bandeau(); montre(i0);
|
||||
const survol = barre.onpointermove;
|
||||
barre.onpointermove = e => {
|
||||
const i = indice(e);
|
||||
if (g === 2) { // la bande glisse sans sortir de la barre
|
||||
const dd = Math.max(-de[0], Math.min(IM.length - 1 - de[1], i - i0));
|
||||
P = [de[0] + dd, de[1] + dd];
|
||||
drawBand(); show(i0);
|
||||
const hover = bar.onpointermove;
|
||||
bar.onpointermove = e => {
|
||||
const i = index(e);
|
||||
if (g === 2) { // the band slides without leaving the bar
|
||||
const dd = Math.max(-from[0], Math.min(IM.length - 1 - from[1], i - i0));
|
||||
P = [from[0] + dd, from[1] + dd];
|
||||
} else P[g === null ? 1 : g] = i;
|
||||
bandeau(); fleches(); montre(i);
|
||||
drawBand(); arrows(); show(i);
|
||||
};
|
||||
barre.onpointerup = () => {
|
||||
barre.onpointermove = survol;
|
||||
bar.onpointerup = () => {
|
||||
bar.onpointermove = hover;
|
||||
P = [Math.min(...P), Math.max(...P)];
|
||||
if (P[0] === P[1]) P = null;
|
||||
else if (g === null) { // nouvelle plage, facteur proposé : la plus grande flèche fait 8 % de la largeur de l'image
|
||||
const m = Math.max(...Object.values(deplacements()).map(([a, b]) => Math.hypot(a, b)), 0);
|
||||
if (m && img.naturalWidth) echelle.value = Math.max(1, Math.round(0.08 * img.naturalWidth / m));
|
||||
else if (g === null) { // new range, proposed factor: the largest arrow is 8 % of the width of the image
|
||||
const m = Math.max(...Object.values(displacements()).map(([a, b]) => Math.hypot(a, b)), 0);
|
||||
if (m && img.naturalWidth) scale.value = Math.max(1, Math.round(0.08 * img.naturalWidth / m));
|
||||
}
|
||||
bandeau(); fleches();
|
||||
drawBand(); arrows();
|
||||
};
|
||||
};
|
||||
echelle.oninput = fleches;
|
||||
scale.oninput = arrows;
|
||||
addEventListener("resize", zoom);
|
||||
const observateur = new ResizeObserver(zoom); // la barre latérale qui se replie change la largeur sans resize
|
||||
observateur.observe(cadre);
|
||||
const observer = new ResizeObserver(zoom); // the sidebar that folds changes the width without resize
|
||||
observer.observe(frame);
|
||||
|
||||
montre(0); cale(); fleches();
|
||||
show(0); align(); arrows();
|
||||
return () => {
|
||||
document.removeEventListener("keydown", clavier);
|
||||
document.removeEventListener("keydown", keyboard);
|
||||
removeEventListener("resize", zoom);
|
||||
cadre.removeEventListener("wheel", molette);
|
||||
observateur.disconnect();
|
||||
frame.removeEventListener("wheel", wheel);
|
||||
observer.disconnect();
|
||||
};
|
||||
}
|
||||
+29
-35
@@ -1,39 +1,33 @@
|
||||
/* Le reste du visuel vient de Mantine (thème dans main.jsx) */
|
||||
.entete { background: #3d4977; color: #fff; border-bottom: 0; }
|
||||
.message { white-space: pre-wrap; overflow-wrap: anywhere; }
|
||||
.message.support, .message.relai { background: var(--mantine-color-aimsight-light); }
|
||||
.message.analyste { background: var(--mantine-color-teal-light); }
|
||||
.message.marc { background: var(--mantine-color-yellow-light); align-self: flex-end; max-width: 85%; }
|
||||
.message.relai { border-left: 3px solid var(--mantine-color-aimsight-filled); }
|
||||
.rapport { width: 100%; height: 78vh; border: 0; display: block; background: #fff; }
|
||||
/* The rest of the visuals comes from Mantine (theme in main.jsx) */
|
||||
.header { background: #3d4977; color: #fff; border-bottom: 0; }
|
||||
|
||||
/* Les images de la caméra (Images.jsx) */
|
||||
.cadre { position: relative; height: calc(100vh - 420px); min-height: 300px; background: #000; overflow: hidden; cursor: grab;
|
||||
/* The camera images (Images.jsx) */
|
||||
.frame { position: relative; height: calc(100vh - 420px); min-height: 300px; background: #000; overflow: hidden; cursor: grab;
|
||||
touch-action: none; border-radius: var(--mantine-radius-md) var(--mantine-radius-md) 0 0; }
|
||||
.cadre:active { cursor: grabbing; }
|
||||
.cadre.compacte { height: auto; min-height: 0; aspect-ratio: 3 / 2; } /* page Data : la colonne de droite */
|
||||
.couche { position: absolute; inset: 0; transform-origin: 0 0; }
|
||||
.couche > * { position: absolute; inset: 0; width: 100%; height: 100%; }
|
||||
.couche img { object-fit: contain; user-select: none; }
|
||||
.marques { overflow: visible; pointer-events: none; font-family: sans-serif; }
|
||||
.marques circle { fill: none; stroke: currentColor; stroke-width: .12em; }
|
||||
.marques .ref circle { stroke-dasharray: .3em .2em; }
|
||||
.marques text { fill: currentColor; text-anchor: middle; paint-order: stroke; stroke: rgba(0, 0, 0, .85); stroke-width: .25em; stroke-linejoin: round; }
|
||||
.cible { color: #ffd400; } .ref { color: #3cc8ff; }
|
||||
.legende { background: #000; padding: 2px 8px; border-radius: 3px; white-space: nowrap; font-size: var(--mantine-font-size-sm); }
|
||||
.vecteurs { overflow: visible; pointer-events: none; }
|
||||
.vecteurs line { stroke: #2ecc40; stroke-width: .18em; marker-end: url(#pointe); }
|
||||
.glisseur { flex: 1; min-width: 0; accent-color: var(--mantine-primary-color-filled); }
|
||||
.barre-plages { position: relative; height: 22px; background: var(--mantine-color-default-hover); border: 1px solid var(--mantine-color-default-border);
|
||||
border-radius: 3px; cursor: crosshair; touch-action: none; }
|
||||
.bande { position: absolute; top: 0; bottom: 0; background: #e0730080; border-left: 2px solid #e07300; border-right: 2px solid #e07300; }
|
||||
.case { display: flex; gap: 6px; align-items: center; font-size: var(--mantine-font-size-sm); }
|
||||
/* page Data : le menu d'un plot dans sa marge du haut ; les images restent en vue quand on fait défiler les plots */
|
||||
.frame:active { cursor: grabbing; }
|
||||
.frame.compact { height: auto; min-height: 0; aspect-ratio: 3 / 2; } /* Data page: the right column */
|
||||
.layer { position: absolute; inset: 0; transform-origin: 0 0; }
|
||||
.layer > * { position: absolute; inset: 0; width: 100%; height: 100%; }
|
||||
.layer img { object-fit: contain; user-select: none; }
|
||||
.marks { overflow: visible; pointer-events: none; font-family: sans-serif; }
|
||||
.marks circle { fill: none; stroke: currentColor; stroke-width: .12em; }
|
||||
.marks .ref circle { stroke-dasharray: .3em .2em; }
|
||||
.marks text { fill: currentColor; text-anchor: middle; paint-order: stroke; stroke: rgba(0, 0, 0, .85); stroke-width: .25em; stroke-linejoin: round; }
|
||||
.target { color: #ffd400; } .ref { color: #3cc8ff; }
|
||||
.legend { background: #000; padding: 2px 8px; border-radius: 3px; white-space: nowrap; font-size: var(--mantine-font-size-sm); }
|
||||
.vectors { overflow: visible; pointer-events: none; }
|
||||
.vectors line { stroke: #2ecc40; stroke-width: .18em; marker-end: url(#arrowhead); }
|
||||
.slider { flex: 1; min-width: 0; accent-color: var(--mantine-primary-color-filled); }
|
||||
.range-bar { position: relative; height: 22px; background: var(--mantine-color-default-hover); border: 1px solid var(--mantine-color-default-border);
|
||||
border-radius: 3px; cursor: crosshair; touch-action: none; }
|
||||
.band { position: absolute; top: 0; bottom: 0; background: #e0730080; border-left: 2px solid #e07300; border-right: 2px solid #e07300; }
|
||||
.check { display: flex; gap: 6px; align-items: center; font-size: var(--mantine-font-size-sm); }
|
||||
/* Data page: the menu of a plot in its top margin; the images stay in view when the plots scroll */
|
||||
.plot { position: relative; }
|
||||
.plot-menu { position: absolute; top: 2px; left: 56px; z-index: 2; }
|
||||
.colle { position: sticky; top: calc(64px + var(--mantine-spacing-lg)); }
|
||||
/* page Data : la flèche qui agrandit l'image, en haut à gauche de l'image (du côté des plots qu'elle pousse) */
|
||||
.visionneuse { position: relative; }
|
||||
.agrandir { position: absolute; top: 8px; left: 8px; z-index: 3; opacity: .75; }
|
||||
.agrandir:hover { opacity: 1; }
|
||||
.data-grille .mantine-Grid-col { transition: flex-basis .35s ease, max-width .35s ease; min-width: 0; } /* l'image qui grandit pousse les plots */
|
||||
.sticky { position: sticky; top: calc(64px + var(--mantine-spacing-lg)); }
|
||||
/* Data page: the arrow that enlarges the image, at the top left of the image (on the side of the plots it pushes) */
|
||||
.viewer { position: relative; }
|
||||
.enlarge { position: absolute; top: 8px; left: 8px; z-index: 3; opacity: .75; }
|
||||
.enlarge:hover { opacity: 1; }
|
||||
.data-grid .mantine-Grid-col { transition: flex-basis .35s ease, max-width .35s ease; min-width: 0; } /* the image that grows pushes the plots */
|
||||
@@ -1,7 +1,7 @@
|
||||
import { defineConfig } from "vite";
|
||||
import react from "@vitejs/plugin-react";
|
||||
|
||||
// npm run dev : la page sur http://localhost:5173, l'API demandée au serveur Python (port 8770)
|
||||
// npm run dev: the page on http://localhost:5173, the API asked of the Python server (port 8770)
|
||||
export default defineConfig({
|
||||
plugins: [react()],
|
||||
server: { proxy: { "/api": "http://127.0.0.1:8770" } },
|
||||
|
||||
Reference in new issue
Block a user