const API = '/api/runs';
const app = document.getElementById('app');
let pollTimer = null;
function stopPolling() {
if (pollTimer) { clearInterval(pollTimer); pollTimer = null; }
}
async function api(path, opts) {
const res = await fetch(path, opts);
if (!res.ok) {
let msg = res.statusText;
try { const j = await res.json(); msg = j.message || j.error || msg; } catch {}
throw new Error(msg);
}
return res.json();
}
function fmt(n, digits = 4) {
if (n === null || n === undefined) return '-';
if (typeof n !== 'number') return String(n);
return n.toLocaleString('fr-FR', { maximumFractionDigits: digits });
}
function statusBadge(status) {
const map = {
pending: 'bg-slate-700 text-slate-200',
running: 'bg-blue-600/30 text-blue-300',
done: 'bg-emerald-600/30 text-emerald-300',
error: 'bg-red-600/30 text-red-300',
};
return `${status}`;
}
function router() {
const hash = location.hash || '#/';
stopPolling();
if (hash === '#/' || hash === '') return renderList();
if (hash === '#/new') return renderNew();
const m = hash.match(/^#\/runs\/([^/]+)$/);
if (m) return renderDetail(m[1]);
app.innerHTML = '
Page introuvable.
';
}
window.addEventListener('hashchange', router);
window.addEventListener('DOMContentLoaded', router);
// ---------------- Liste des runs ----------------
async function renderList() {
app.innerHTML = 'Chargement...
';
const runs = await api(API);
if (runs.length === 0) {
app.innerHTML = `
Aucun run pour le moment.
Lancer un nouveau run
`;
return;
}
const rows = runs.map((r) => `
| ${r.id.slice(0, 8)} |
${escapeHtml(r.originalFilename)} |
${statusBadge(r.status)} |
${new Date(r.createdAt).toLocaleString('fr-FR')} |
${r.summary ? `${r.summary.levelsOk}/${r.summary.levelsTotal} niveaux` : '-'} |
${r.summary && r.summary.volumeDeltaPct !== null ? fmt(r.summary.volumeDeltaPct, 2) + ' %' : '-'} |
`).join('');
app.innerHTML = `
| ID | Fichier | Statut |
Cree le | Niveaux OK | Ecart volume (L0 -> dernier) |
${rows}
`;
}
// ---------------- Nouveau run ----------------
function renderNew() {
app.innerHTML = `
Nouveau run
`;
document.getElementById('upload-form').addEventListener('submit', async (e) => {
e.preventDefault();
const form = e.target;
const fd = new FormData(form);
const errEl = document.getElementById('upload-error');
errEl.classList.add('hidden');
const btn = form.querySelector('button[type=submit]');
btn.disabled = true;
btn.textContent = 'Envoi...';
try {
const res = await fetch(API, { method: 'POST', body: fd });
const json = await res.json();
if (!res.ok) throw new Error(json.message || 'Echec de l\'envoi');
location.hash = `#/runs/${json.id}`;
} catch (err) {
errEl.textContent = err.message;
errEl.classList.remove('hidden');
btn.disabled = false;
btn.textContent = 'Lancer le run';
}
});
}
// ---------------- Detail d'un run ----------------
async function renderDetail(id) {
app.innerHTML = 'Chargement...
';
await loadDetail(id);
pollTimer = setInterval(async () => {
const run = await api(`${API}/${id}`);
if (run.status === 'pending' || run.status === 'running') {
paintDetail(run);
} else {
stopPolling();
paintDetail(run);
}
}, 2000);
}
async function loadDetail(id) {
const run = await api(`${API}/${id}`);
paintDetail(run);
}
function levelRow(l, id) {
const warnCls = l.status === 'error' ? 'bg-red-950/40' : l.warn ? 'bg-amber-950/30' : '';
const statusTxt = l.status === 'ok' ? '' : `${escapeHtml(l.message || '')}
`;
return `
| ${l.level} |
${l.spatialStep !== null ? fmt(l.spatialStep, 5) + ' m' : 'complet'} |
${l.gridStep !== null ? fmt(l.gridStep, 5) + ' m' : '-'} |
${l.status === 'ok' ? l.pointCount.toLocaleString('fr-FR') : '-'} |
${l.status === 'ok' ? fmt(l.pointRatio * 100, 2) + ' %' : '-'} |
${l.status === 'ok' ? fmt(l.volume, 4) + ' m3' : '-'} |
${l.status === 'ok' ? fmt(l.surface, 3) + ' m2' : '-'} |
${l.status === 'ok' ? fmt(l.matchingCellsPct, 1) + ' %' : '-'} |
${statusBadge(l.status)} ${l.warn && l.status === 'ok' ? 'a verifier' : ''}${statusTxt} |
`;
}
function paintDetail(run) {
const r = run.report;
const levelsTable = r ? `
| Niveau | Pas spatial | Grille volume | Points |
% points vs L0 | Volume | Surface |
Matching cells | Statut |
${r.levels.map((l) => levelRow(l, run.id)).join('')}
` : '';
const params = r ? `
- Zref
- ${fmt(r.params.zref, 4)} (${r.params.zrefSource})
- Pas initial
- ${fmt(r.params.initialStep, 5)} m (${r.params.initialStepSource})
- Grille volume
- x${r.params.gridStepMultiplier} le pas spatial, adaptee a chaque niveau
- Facteur / etapes
- x${r.params.factor} sur ${r.params.steps}
` : '';
const heightmaps = r ? r.levels.filter((l) => l.imageFile).map((l) => `
Niveau ${l.level}
`).join('') : '';
const summaryCharts = run.status === 'done' ? ['volume_vs_level.png', 'points_vs_level.png', 'robustness_vs_level.png'].map((f) => `
`).join('') : '';
const downloads = run.status === 'done' ? `
` : '';
const errorBlock = run.error ? `${escapeHtml(run.error)}
` : '';
app.innerHTML = `
${escapeHtml(run.originalFilename)}
${statusBadge(run.status)}
Cree le ${new Date(run.createdAt).toLocaleString('fr-FR')}${run.finishedAt ? ' - termine le ' + new Date(run.finishedAt).toLocaleString('fr-FR') : ''}
${errorBlock}
${params}
${levelsTable}
${downloads}
${heightmaps ? `Cartes de hauteur par niveau
${heightmaps}
` : ''}
${summaryCharts ? `Graphiques de synthese
${summaryCharts}
` : ''}
Journal
${escapeHtml(run.log || '')}
`;
}
function escapeHtml(s) {
return String(s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]));
}