feat: causal lab — debug endpoint + error display in UI

This commit is contained in:
OpenSquared
2026-06-27 23:36:22 +02:00
parent a7f5369d7b
commit 7a0263989a
2 changed files with 48 additions and 2 deletions

View File

@@ -320,6 +320,27 @@ class AnalyzeRequest(BaseModel):
# ── Endpoints ─────────────────────────────────────────────────────────────────
@router.get("/api/causal-lab/debug")
def debug_causal():
"""Endpoint de diagnostic — vérifie que causal_graphs est bien chargé."""
try:
from services.database import get_conn
from services.causal_graphs import BUILT_IN_TEMPLATES, init_tables, seed_templates
conn = get_conn()
init_tables(conn)
seed_templates(conn)
count = conn.execute("SELECT COUNT(*) FROM causal_graph_templates").fetchone()[0]
conn.close()
return {
"built_in_templates": len(BUILT_IN_TEMPLATES),
"db_templates": count,
"status": "ok",
}
except Exception as e:
import traceback
return {"status": "error", "error": str(e), "trace": traceback.format_exc()}
@router.get("/api/causal-lab/templates")
def list_templates(category: str = Query("")):
try:
@@ -329,8 +350,11 @@ def list_templates(category: str = Query("")):
_init(conn)
data = get_templates(conn, category)
conn.close()
print(f"[causal_lab] list_templates → {len(data)} templates (category={category!r})", flush=True)
return data
except Exception as e:
import traceback
print(f"[causal_lab] list_templates ERROR: {e}\n{traceback.format_exc()}", flush=True)
logger.error(f"[causal_lab] list_templates: {e}")
raise HTTPException(500, str(e))

View File

@@ -173,11 +173,21 @@ function TabLibrary() {
const [saving, setSaving] = useState(false)
const [editCoefs, setEditCoefs] = useState<Record<string, number>>({})
const [loading, setLoading] = useState(true)
const [apiError, setApiError] = useState<string | null>(null)
const [debugInfo, setDebugInfo] = useState<Record<string, unknown> | null>(null)
useEffect(() => {
setLoading(true)
setLoading(true); setApiError(null)
api(`/api/causal-lab/templates${catFilter ? `?category=${catFilter}` : ''}`)
.then(setTemplates).finally(() => setLoading(false))
.then(data => {
setTemplates(data)
if (data.length === 0) {
// Auto-run debug to show diagnostic
api('/api/causal-lab/debug').then(setDebugInfo).catch(() => {})
}
})
.catch(e => setApiError(String(e)))
.finally(() => setLoading(false))
}, [catFilter])
function selectTemplate(t: Template) {
@@ -213,6 +223,18 @@ function TabLibrary() {
{cats.map(c => <option key={c} value={c}>{CAT_LABELS[c] ?? c}</option>)}
</select>
{apiError && (
<div className="mb-2 p-3 bg-red-900/20 border border-red-700/40 rounded text-xs text-red-300 break-all">
<div className="font-bold mb-1">Erreur API</div>
{apiError}
</div>
)}
{debugInfo && (
<div className="mb-2 p-3 bg-yellow-900/20 border border-yellow-700/40 rounded text-xs text-yellow-200">
<div className="font-bold mb-1">Diagnostic</div>
<pre className="whitespace-pre-wrap">{JSON.stringify(debugInfo, null, 2)}</pre>
</div>
)}
{loading
? <div className="text-slate-500 text-xs p-4">Chargement</div>
: <div className="space-y-1 overflow-y-auto max-h-[calc(100vh-230px)]">