scaffold — FastAPI + SQLite + HTMX dashboard, ingest + dispatcher
- app/main.py : dashboard /, partials /partials/{jobs,monitor} (htmx polling)
- app/templates/ : index, jobs table, monitor card par worker
- app/static/style.css : thème sombre cohérent
- scripts/ingest.py : scan SSD d'acquisition, EXIF CreateDate → segments
continus par (AUV, GoPro serial) avec seuil configurable
- scripts/dispatcher.py : polling queue, pick worker selon VRAM free,
extraction ffmpeg + lingbot-map windowed --offload_to_cpu, progression DB
- DB : SQLite (acquisitions + jobs), lifecycle queued→extracting→running→done
- Workers par défaut : .87 (3060 12GB) + .84 (3090 24GB)
Contexte : QC terrain le jour-même (avant photogrammétrie à 30 jours),
plusieurs heures × 2 GoPros × 2-3 AUVs d'enregistrement à traiter en parallèle.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
168
app/main.py
Normal file
168
app/main.py
Normal file
@@ -0,0 +1,168 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sqlite3
|
||||
from contextlib import asynccontextmanager, closing
|
||||
from datetime import datetime
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from fastapi import FastAPI, Form, HTTPException, Request
|
||||
from fastapi.responses import HTMLResponse, JSONResponse
|
||||
from fastapi.staticfiles import StaticFiles
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
DB_PATH = Path(os.environ.get("COSMA_QC_DB", "/var/lib/cosma-qc/jobs.db"))
|
||||
WORKERS = json.loads(os.environ.get("COSMA_QC_WORKERS", json.dumps([
|
||||
{"host": "192.168.0.87", "ssh_alias": "gpu", "gpu": "RTX 3060 12GB"},
|
||||
{"host": "192.168.0.84", "ssh_alias": "cosma-vm","gpu": "RTX 3090 24GB"},
|
||||
])))
|
||||
|
||||
STATUSES = ("queued", "extracting", "running", "done", "error")
|
||||
|
||||
|
||||
def db() -> sqlite3.Connection:
|
||||
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
|
||||
conn = sqlite3.connect(DB_PATH, isolation_level=None)
|
||||
conn.execute("PRAGMA journal_mode=WAL")
|
||||
conn.execute("PRAGMA foreign_keys=ON")
|
||||
conn.row_factory = sqlite3.Row
|
||||
return conn
|
||||
|
||||
|
||||
def init_schema() -> None:
|
||||
with closing(db()) as conn:
|
||||
conn.executescript("""
|
||||
CREATE TABLE IF NOT EXISTS acquisitions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
source_path TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS jobs (
|
||||
id INTEGER PRIMARY KEY,
|
||||
acquisition_id INTEGER NOT NULL REFERENCES acquisitions(id) ON DELETE CASCADE,
|
||||
auv TEXT NOT NULL,
|
||||
gopro_serial TEXT NOT NULL,
|
||||
segment_label TEXT NOT NULL,
|
||||
video_paths TEXT NOT NULL,
|
||||
frame_count INTEGER,
|
||||
frames_dir TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'queued',
|
||||
worker_host TEXT,
|
||||
viser_url TEXT,
|
||||
ply_path TEXT,
|
||||
progress INTEGER NOT NULL DEFAULT 0,
|
||||
log_tail TEXT,
|
||||
error TEXT,
|
||||
started_at TEXT,
|
||||
finished_at TEXT,
|
||||
created_at TEXT NOT NULL DEFAULT (datetime('now'))
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS jobs_status_idx ON jobs(status);
|
||||
CREATE INDEX IF NOT EXISTS jobs_acq_idx ON jobs(acquisition_id);
|
||||
""")
|
||||
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(_: FastAPI):
|
||||
init_schema()
|
||||
yield
|
||||
|
||||
|
||||
app = FastAPI(title="cosma-qc", lifespan=lifespan)
|
||||
templates = Jinja2Templates(directory=Path(__file__).parent / "templates")
|
||||
app.mount("/static", StaticFiles(directory=Path(__file__).parent / "static"), name="static")
|
||||
|
||||
|
||||
@app.get("/", response_class=HTMLResponse)
|
||||
async def index(request: Request):
|
||||
with closing(db()) as conn:
|
||||
jobs = conn.execute("""
|
||||
SELECT j.*, a.name AS acquisition_name
|
||||
FROM jobs j
|
||||
LEFT JOIN acquisitions a ON a.id = j.acquisition_id
|
||||
ORDER BY j.created_at DESC
|
||||
LIMIT 200
|
||||
""").fetchall()
|
||||
return templates.TemplateResponse("index.html", {
|
||||
"request": request,
|
||||
"jobs": jobs,
|
||||
"workers": WORKERS,
|
||||
})
|
||||
|
||||
|
||||
@app.get("/api/jobs")
|
||||
async def list_jobs():
|
||||
with closing(db()) as conn:
|
||||
rows = conn.execute("SELECT * FROM jobs ORDER BY created_at DESC LIMIT 500").fetchall()
|
||||
return [dict(r) for r in rows]
|
||||
|
||||
|
||||
@app.get("/partials/jobs", response_class=HTMLResponse)
|
||||
async def partial_jobs(request: Request):
|
||||
with closing(db()) as conn:
|
||||
jobs = conn.execute("""
|
||||
SELECT j.*, a.name AS acquisition_name
|
||||
FROM jobs j
|
||||
LEFT JOIN acquisitions a ON a.id = j.acquisition_id
|
||||
ORDER BY j.created_at DESC
|
||||
LIMIT 200
|
||||
""").fetchall()
|
||||
return templates.TemplateResponse("_jobs_table.html", {"request": request, "jobs": jobs})
|
||||
|
||||
|
||||
@app.get("/partials/monitor", response_class=HTMLResponse)
|
||||
async def partial_monitor(request: Request):
|
||||
stats = await asyncio.gather(*[_worker_stats(w) for w in WORKERS])
|
||||
return templates.TemplateResponse("_monitor.html", {"request": request, "workers": stats})
|
||||
|
||||
|
||||
async def _worker_stats(worker: dict) -> dict:
|
||||
alias = worker["ssh_alias"]
|
||||
try:
|
||||
proc = await asyncio.create_subprocess_exec(
|
||||
"ssh", "-o", "ConnectTimeout=3", "-o", "BatchMode=yes", alias,
|
||||
"nvidia-smi --query-gpu=memory.used,memory.total,utilization.gpu --format=csv,noheader,nounits && df -h / | tail -1",
|
||||
stdout=asyncio.subprocess.PIPE, stderr=asyncio.subprocess.PIPE,
|
||||
)
|
||||
out, _ = await asyncio.wait_for(proc.communicate(), timeout=4)
|
||||
text = out.decode().strip().splitlines()
|
||||
gpu_line = text[0].split(",") if text else ["?", "?", "?"]
|
||||
disk = text[1].split() if len(text) > 1 else ["?"] * 6
|
||||
return {
|
||||
**worker,
|
||||
"online": True,
|
||||
"vram_used_mib": int(gpu_line[0].strip()) if gpu_line[0].strip().isdigit() else None,
|
||||
"vram_total_mib": int(gpu_line[1].strip()) if gpu_line[1].strip().isdigit() else None,
|
||||
"gpu_util_pct": int(gpu_line[2].strip()) if gpu_line[2].strip().isdigit() else None,
|
||||
"disk_used_pct": disk[4] if len(disk) > 4 else "?",
|
||||
}
|
||||
except Exception as e:
|
||||
return {**worker, "online": False, "error": str(e)[:80]}
|
||||
|
||||
|
||||
@app.post("/jobs/{job_id}/cancel")
|
||||
async def cancel_job(job_id: int):
|
||||
with closing(db()) as conn:
|
||||
conn.execute(
|
||||
"UPDATE jobs SET status='error', error='cancelled by user', finished_at=datetime('now') "
|
||||
"WHERE id=? AND status IN ('queued','extracting','running')",
|
||||
(job_id,),
|
||||
)
|
||||
return {"ok": True}
|
||||
|
||||
|
||||
@app.post("/jobs/{job_id}/retry")
|
||||
async def retry_job(job_id: int):
|
||||
with closing(db()) as conn:
|
||||
conn.execute(
|
||||
"UPDATE jobs SET status='queued', error=NULL, progress=0, started_at=NULL, "
|
||||
"finished_at=NULL, worker_host=NULL WHERE id=? AND status='error'",
|
||||
(job_id,),
|
||||
)
|
||||
return {"ok": True}
|
||||
69
app/static/style.css
Normal file
69
app/static/style.css
Normal file
@@ -0,0 +1,69 @@
|
||||
:root {
|
||||
--bg: #0b1220;
|
||||
--panel: #121a2b;
|
||||
--border: #1f2a44;
|
||||
--text: #e6edf7;
|
||||
--muted: #7b8aa8;
|
||||
--accent: #5fd0ff;
|
||||
--ok: #3ddc84;
|
||||
--warn: #f5c518;
|
||||
--err: #ff5c7a;
|
||||
}
|
||||
* { box-sizing: border-box; }
|
||||
body {
|
||||
font-family: ui-monospace, "JetBrains Mono", Menlo, monospace;
|
||||
background: var(--bg); color: var(--text);
|
||||
margin: 0; padding: 1.5rem;
|
||||
}
|
||||
header { display: flex; align-items: baseline; gap: 1rem; margin-bottom: 1.25rem; }
|
||||
header h1 { margin: 0; font-size: 1.25rem; color: var(--accent); }
|
||||
.sub { color: var(--muted); font-size: 0.85rem; }
|
||||
h2 { font-size: 1rem; color: var(--accent); margin: 1.5rem 0 0.5rem; }
|
||||
|
||||
.muted { color: var(--muted); }
|
||||
.err { color: var(--err); }
|
||||
|
||||
section { background: var(--panel); border: 1px solid var(--border);
|
||||
border-radius: 10px; padding: 1rem; margin-bottom: 1rem; }
|
||||
|
||||
.worker-grid { display: grid; gap: 1rem;
|
||||
grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); }
|
||||
.worker { background: rgba(255,255,255,0.02); border: 1px solid var(--border);
|
||||
border-radius: 8px; padding: 0.75rem; }
|
||||
.worker.offline { opacity: 0.55; border-color: var(--err); }
|
||||
.worker .hdr { display: flex; justify-content: space-between;
|
||||
gap: 0.5rem; align-items: center; margin-bottom: 0.5rem; flex-wrap: wrap; }
|
||||
.worker .gpu { color: var(--muted); font-size: 0.8rem; }
|
||||
.worker .state { color: var(--ok); font-size: 0.75rem; text-transform: uppercase; }
|
||||
.worker.offline .state { color: var(--err); }
|
||||
.bar { display: grid; grid-template-columns: 60px 1fr auto; gap: 0.5rem;
|
||||
align-items: center; margin-bottom: 0.25rem; font-size: 0.8rem; }
|
||||
.bar small { color: var(--muted); }
|
||||
|
||||
progress { appearance: none; height: 10px; width: 100%;
|
||||
border-radius: 6px; overflow: hidden; border: 1px solid var(--border); background: #0a1020; }
|
||||
progress::-webkit-progress-bar { background: #0a1020; }
|
||||
progress::-webkit-progress-value { background: var(--accent); }
|
||||
progress::-moz-progress-bar { background: var(--accent); }
|
||||
|
||||
table.jobs { width: 100%; border-collapse: collapse; font-size: 0.85rem; }
|
||||
table.jobs th, table.jobs td { text-align: left; padding: 0.45rem 0.55rem;
|
||||
border-bottom: 1px solid var(--border); }
|
||||
table.jobs th { color: var(--muted); font-weight: normal; text-transform: uppercase;
|
||||
font-size: 0.72rem; letter-spacing: 0.04em; }
|
||||
tr.status-done td { color: var(--ok); }
|
||||
tr.status-error td { color: var(--err); }
|
||||
tr.err-row td { color: var(--err); padding-top: 0; border-top: none; }
|
||||
|
||||
.pill { padding: 0.15rem 0.5rem; border-radius: 999px; font-size: 0.7rem;
|
||||
background: rgba(255,255,255,0.05); border: 1px solid var(--border); }
|
||||
.pill.queued { color: var(--muted); }
|
||||
.pill.extracting, .pill.running { color: var(--warn); border-color: var(--warn); }
|
||||
.pill.done { color: var(--ok); border-color: var(--ok); }
|
||||
.pill.error { color: var(--err); border-color: var(--err); }
|
||||
|
||||
button { background: transparent; color: var(--accent); border: 1px solid var(--border);
|
||||
padding: 0.2rem 0.6rem; border-radius: 6px; cursor: pointer; font-family: inherit; font-size: 0.75rem; }
|
||||
button:hover { border-color: var(--accent); }
|
||||
a { color: var(--accent); }
|
||||
code { background: rgba(255,255,255,0.05); padding: 0 0.25rem; border-radius: 3px; }
|
||||
44
app/templates/_jobs_table.html
Normal file
44
app/templates/_jobs_table.html
Normal file
@@ -0,0 +1,44 @@
|
||||
{% if not jobs %}
|
||||
<p class="muted">Aucun job. Ingeste un dossier d'acquisition via <code>scripts/ingest.py</code>.</p>
|
||||
{% else %}
|
||||
<table class="jobs">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>#</th><th>Acquisition</th><th>AUV</th><th>GoPro</th><th>Segment</th>
|
||||
<th>Frames</th><th>Status</th><th>Worker</th><th>Progress</th><th>Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for j in jobs %}
|
||||
<tr class="status-{{ j.status }}">
|
||||
<td>{{ j.id }}</td>
|
||||
<td>{{ j.acquisition_name }}</td>
|
||||
<td>{{ j.auv }}</td>
|
||||
<td><code>{{ j.gopro_serial }}</code></td>
|
||||
<td>{{ j.segment_label }}</td>
|
||||
<td>{{ j.frame_count or "—" }}</td>
|
||||
<td><span class="pill {{ j.status }}">{{ j.status }}</span></td>
|
||||
<td>{{ j.worker_host or "—" }}</td>
|
||||
<td>
|
||||
{% if j.status == 'running' or j.status == 'extracting' %}
|
||||
<progress value="{{ j.progress }}" max="100"></progress> {{ j.progress }}%
|
||||
{% elif j.status == 'done' and j.viser_url %}
|
||||
<a href="{{ j.viser_url }}" target="_blank">viser</a>
|
||||
{% if j.ply_path %} · <a href="/api/jobs/{{ j.id }}/ply">PLY</a>{% endif %}
|
||||
{% else %}—{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
{% if j.status in ['queued','extracting','running'] %}
|
||||
<button hx-post="/jobs/{{ j.id }}/cancel" hx-target="#jobs-table" hx-swap="outerHTML">Stop</button>
|
||||
{% elif j.status == 'error' %}
|
||||
<button hx-post="/jobs/{{ j.id }}/retry" hx-target="#jobs-table" hx-swap="outerHTML">Retry</button>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% if j.error %}
|
||||
<tr class="err-row"><td colspan="10"><small>{{ j.error }}</small></td></tr>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
{% endif %}
|
||||
26
app/templates/_monitor.html
Normal file
26
app/templates/_monitor.html
Normal file
@@ -0,0 +1,26 @@
|
||||
<div class="worker-grid">
|
||||
{% for w in workers %}
|
||||
<div class="worker {% if not w.online %}offline{% endif %}">
|
||||
<div class="hdr">
|
||||
<b>{{ w.host }}</b>
|
||||
<span class="gpu">{{ w.gpu }}</span>
|
||||
<span class="state">{% if w.online %}online{% else %}offline{% endif %}</span>
|
||||
</div>
|
||||
{% if w.online %}
|
||||
<div class="bar">
|
||||
<span>VRAM</span>
|
||||
<progress value="{{ w.vram_used_mib or 0 }}" max="{{ w.vram_total_mib or 1 }}"></progress>
|
||||
<small>{{ w.vram_used_mib }} / {{ w.vram_total_mib }} MiB</small>
|
||||
</div>
|
||||
<div class="bar">
|
||||
<span>GPU</span>
|
||||
<progress value="{{ w.gpu_util_pct or 0 }}" max="100"></progress>
|
||||
<small>{{ w.gpu_util_pct }}%</small>
|
||||
</div>
|
||||
<div class="bar"><span>Disk /</span><small>{{ w.disk_used_pct }}</small></div>
|
||||
{% else %}
|
||||
<div class="err">{{ w.error or "unreachable" }}</div>
|
||||
{% endif %}
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
27
app/templates/index.html
Normal file
27
app/templates/index.html
Normal file
@@ -0,0 +1,27 @@
|
||||
<!doctype html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>cosma-qc — dashboard</title>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<script src="https://unpkg.com/htmx.org@2.0.4"></script>
|
||||
<link rel="stylesheet" href="/static/style.css">
|
||||
</head>
|
||||
<body>
|
||||
<header>
|
||||
<h1>cosma-qc</h1>
|
||||
<span class="sub">post-acquisition QC · lingbot-map pipeline</span>
|
||||
</header>
|
||||
|
||||
<section id="monitor" hx-get="/partials/monitor" hx-trigger="load, every 5s" hx-swap="innerHTML">
|
||||
<p class="muted">Chargement des workers…</p>
|
||||
</section>
|
||||
|
||||
<section id="jobs">
|
||||
<h2>Jobs</h2>
|
||||
<div id="jobs-table" hx-get="/partials/jobs" hx-trigger="load, every 3s" hx-swap="innerHTML">
|
||||
<p class="muted">Chargement…</p>
|
||||
</div>
|
||||
</section>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user