UI: upload panel — POST /jobs with live status polling
Adds a "+ wgraj" control to the sidebar that uploads an ISO/ZIP/DLL to the acquisition endpoint and tracks the job to completion, then refreshes the version list so the new snapshot appears without a reload. - index.html: upload form + #jobs panel in the sidebar - app.js: submitUpload() (FormData → POST /jobs), pollJobs() (2.5s while any job is queued/started; finished → load(); failed → inline error) - style.css: mini-btn / upload form / job rows + queued/started badges Verified: node --check clean; uvicorn serves /ui assets 200 and GET /jobs. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -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)); });
|
||||
|
||||
Reference in New Issue
Block a user