126 lines
3.4 KiB
JavaScript
126 lines
3.4 KiB
JavaScript
let scanInterval = null;
|
|
|
|
/* ============================================================
|
|
UTILS
|
|
============================================================ */
|
|
function stopScanPolling() {
|
|
if (scanInterval) {
|
|
clearInterval(scanInterval);
|
|
scanInterval = null;
|
|
}
|
|
}
|
|
|
|
function updateScanUi(data) {
|
|
const current = Number(data?.current || 0);
|
|
const total = Number(data?.total || 0);
|
|
|
|
const percent = Number.isFinite(Number(data?.percent))
|
|
? Number(data.percent)
|
|
: total > 0
|
|
? Math.round((current / total) * 100)
|
|
: 0;
|
|
|
|
const eta = data?.eta || "calcolo...";
|
|
|
|
const progressEl = document.getElementById("scanProgress");
|
|
const etaEl = document.getElementById("scanEta");
|
|
const barEl = document.getElementById("progressBar");
|
|
|
|
if (progressEl) {
|
|
progressEl.textContent = `Progresso: ${current}/${total} (${percent}%)`;
|
|
}
|
|
|
|
if (etaEl) {
|
|
etaEl.textContent = `ETA: ${eta}`;
|
|
}
|
|
|
|
if (barEl) {
|
|
barEl.style.width = `${Math.max(0, Math.min(100, percent))}%`;
|
|
}
|
|
|
|
return { current, total, percent };
|
|
}
|
|
|
|
/* ============================================================
|
|
FOTO — SCANSIONE
|
|
============================================================ */
|
|
async function scan() {
|
|
startScanStatusPolling();
|
|
|
|
try {
|
|
await apiGet(`/scan`);
|
|
} catch (err) {
|
|
console.error("Errore avvio scan foto:", err);
|
|
out?.({ error: err.message });
|
|
stopScanPolling();
|
|
}
|
|
}
|
|
|
|
function startScanStatusPolling() {
|
|
stopScanPolling();
|
|
|
|
scanInterval = setInterval(async () => {
|
|
try {
|
|
const data = await apiGet(`/scanStatus/photos?ts=${Date.now()}`);
|
|
|
|
const { current, total } = updateScanUi(data);
|
|
|
|
if (current >= total && total > 0) {
|
|
stopScanPolling();
|
|
|
|
if (window.AdminCacheSync?.progressiveSyncPhotosCacheFromAdmin) {
|
|
const result = await AdminCacheSync.progressiveSyncPhotosCacheFromAdmin();
|
|
console.log("[admin scan foto] cache aggiornata:", result);
|
|
} else {
|
|
console.warn("[admin scan foto] AdminCacheSync non disponibile");
|
|
}
|
|
|
|
await readDBUser();
|
|
}
|
|
} catch (err) {
|
|
console.warn("[scan foto] lettura scan status fallita, riprovo:", err.message);
|
|
}
|
|
}, 1000);
|
|
}
|
|
|
|
/* ============================================================
|
|
ATTIVITÀ — SCANSIONE
|
|
============================================================ */
|
|
async function scanActivities() {
|
|
startScanActivitiesPolling();
|
|
|
|
try {
|
|
await apiGet(`/scanActivities`);
|
|
} catch (err) {
|
|
console.error("Errore avvio scan attività:", err);
|
|
outActivities?.({ error: err.message });
|
|
stopScanPolling();
|
|
}
|
|
}
|
|
|
|
function startScanActivitiesPolling() {
|
|
stopScanPolling();
|
|
|
|
scanInterval = setInterval(async () => {
|
|
try {
|
|
const data = await apiGet(`/scanStatus/activities?ts=${Date.now()}`);
|
|
|
|
const { current, total } = updateScanUi(data);
|
|
|
|
if (current >= total && total > 0) {
|
|
stopScanPolling();
|
|
|
|
if (window.AdminCacheSync?.progressiveSyncActivitiesCacheFromAdmin) {
|
|
const result = await AdminCacheSync.progressiveSyncActivitiesCacheFromAdmin();
|
|
console.log("[admin scan attività] cache aggiornata:", result);
|
|
} else {
|
|
console.warn("[admin scan attività] AdminCacheSync non disponibile");
|
|
}
|
|
|
|
await readActivitiesDBUser();
|
|
}
|
|
} catch (err) {
|
|
console.warn("[scan attività] lettura scan status fallita, riprovo:", err.message);
|
|
}
|
|
}, 1000);
|
|
}
|