diff --git a/README.md b/README.md index 0a42389..e3304e1 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,8 @@ Endpointy: `POST/GET /games`, `POST/GET /snapshots` (import deduplikowany po sha Po starcie serwera otwórz **http://127.0.0.1:8000/** (`/` → `/ui/`). Statyczny UI bez build-stepu (czysty HTML/CSS/JS w `ams/api/static/`, serwowany przez FastAPI): lista gier/wersji, wybór dwóch wersji (A/B), wizualny diff po 4 osiach z filtrem klasy i przeglądarka pojedynczej powierzchni. +Przycisk **+ wgraj** w panelu gier otwiera upload ISO/ZIP/DLL → `POST /jobs`; status zadania +(queued→started→finished/failed) jest odpytywany na żywo, a po zakończeniu lista wersji odświeża się sama. ## Format snapshotu diff --git a/ams/api/static/app.js b/ams/api/static/app.js index 991e461..40ef282 100644 --- a/ams/api/static/app.js +++ b/ams/api/static/app.js @@ -1,6 +1,6 @@ "use strict"; -const state = { games: [], snaps: [], byId: {}, a: null, b: null, axes: { types: true, methods: true, events: true, fields: true, layout: false } }; +const state = { games: [], snaps: [], byId: {}, a: null, b: null, jobStatus: {}, axes: { types: true, methods: true, events: true, fields: true, layout: false } }; // tiny DOM helper function el(tag, props, ...kids) { @@ -103,6 +103,92 @@ function renderAxesControl() { } } +// --- upload + jobs ---------------------------------------------------------------------------- +const ACTIVE = new Set(["queued", "started"]); + +function setupUpload() { + const form = $("upload-form"); + $("upload-toggle").addEventListener("click", () => { + form.hidden = !form.hidden; + if (!form.hidden) $("upload-file").focus(); + }); + form.addEventListener("submit", submitUpload); +} + +async function submitUpload(e) { + e.preventDefault(); + const file = $("upload-file").files[0]; + if (!file) return; + const msg = $("upload-msg"); + const fd = new FormData(); + fd.append("file", file); + const game = $("upload-game").value.trim(); + if (game) fd.append("game", game); + + $("upload-submit").disabled = true; + msg.className = "upload-msg"; + msg.textContent = "wysyłanie…"; + try { + const r = await fetch("/jobs", { method: "POST", body: fd }); + if (!r.ok) { + const detail = await r.json().catch(() => ({})); + throw new Error(detail.detail || ("HTTP " + r.status)); + } + const job = await r.json(); + msg.textContent = "zlecono #" + job.id + " — analiza w toku…"; + $("upload-form").reset(); + pollJobs(); // kick the poller so the new job shows up + tracks to completion + } catch (err) { + msg.className = "upload-msg err"; + msg.textContent = "błąd: " + err.message; + } finally { + $("upload-submit").disabled = false; + } +} + +let _jobTimer = null; +async function pollJobs() { + let jobs; + try { jobs = await jget("/jobs"); } + catch { return; } // endpoint may be absent in a stripped deploy — fail quiet + + // when a tracked job flips to finished, a new snapshot is in the catalog → refresh the list + let finished = false; + for (const j of jobs) { + if (state.jobStatus[j.id] && state.jobStatus[j.id] !== j.status && j.status === "finished") + finished = true; + state.jobStatus[j.id] = j.status; + } + renderJobs(jobs); + if (finished) load(); + + clearTimeout(_jobTimer); + if (jobs.some((j) => ACTIVE.has(j.status))) + _jobTimer = setTimeout(pollJobs, 2500); +} + +function jobBadge(status) { + const cls = { queued: "b-q", started: "b-s", finished: "b-add", failed: "b-del" }[status] || "b-q"; + return el("span", { class: "badge " + cls }, status); +} + +function renderJobs(jobs) { + const root = $("jobs"); root.innerHTML = ""; + if (!jobs.length) return; + root.append(el("div", { class: "jobs-title" }, "Zadania")); + for (const j of jobs.slice(0, 8)) { + const row = el("div", { class: "job", title: j.error || "" }, + el("span", { class: "jname" }, j.source_name), + jobBadge(j.status)); + if (j.status === "finished" && j.snapshot_id != null) + row.append(el("span", { class: "jlink", onclick: () => browse(j.snapshot_id) }, + j.dll_name || "snapshot")); + if (j.status === "failed" && j.error) + row.append(el("span", { class: "jerr" }, j.error)); + root.append(row); + } +} + // --- diff ------------------------------------------------------------------------------------- async function compare() { if (!state.a || !state.b) return; @@ -209,4 +295,6 @@ async function browse(id) { // --- boot ------------------------------------------------------------------------------------- $("compare").addEventListener("click", compare); $("owner").addEventListener("keydown", (e) => { if (e.key === "Enter") compare(); }); +setupUpload(); +pollJobs(); load().catch((e) => { $("results").innerHTML = ""; $("results").append(el("div", { class: "err" }, "Nie udało się załadować: " + e.message)); }); diff --git a/ams/api/static/index.html b/ams/api/static/index.html index e88c039..1fc2e39 100644 --- a/ams/api/static/index.html +++ b/ams/api/static/index.html @@ -14,7 +14,19 @@