const btnIF = document.getElementById("btnIF");
const btnAE = document.getElementById("btnAE");
const startBtn = document.getElementById("startBtn");
let chosenAlgorithm = null;
let chosenAlgorithmName = null;
let startTime = null;
let lastResults = null;
let progressInterval = null;
let backendErrorShown = false;
const JOB_STORAGE_KEY = "custom_active_job";
function selectAlgorithm(selected, other) {
selected.classList.add("scale-105", "border-2", "border-black");
selected.classList.remove("opacity-50");
other.classList.remove("scale-105", "border-2", "border-black");
other.classList.add("opacity-50");
}
function enableStartButton() {
startBtn.classList.remove("bg-gray-600", "text-gray-300", "cursor-not-allowed");
startBtn.classList.add("bg-orange-500", "hover:bg-orange-400", "text-white", "cursor-pointer");
}
btnIF.addEventListener("click", () => {
selectAlgorithm(btnIF, btnAE);
chosenAlgorithm = "iforest_custom";
chosenAlgorithmName = "Isolation Forest (Custom)";
enableStartButton();
});
btnAE.addEventListener("click", () => {
selectAlgorithm(btnAE, btnIF);
chosenAlgorithm = "autoencoder_custom";
chosenAlgorithmName = "Autoencoder (Custom)";
enableStartButton();
});
function getBackendJobName() {
return chosenAlgorithm;
}
function getResultURL() {
if (chosenAlgorithm === "iforest_custom") {
return "get-isolation-forest-result-custom";
}
return "get-autoencoder-result-custom";
}
function saveActiveJob() {
sessionStorage.setItem(JOB_STORAGE_KEY, JSON.stringify({
algorithm: chosenAlgorithm,
algorithmName: chosenAlgorithmName,
backendJob: getBackendJobName(),
startedAt: Date.now()
}));
}
function loadActiveJob() {
const raw = sessionStorage.getItem(JOB_STORAGE_KEY);
if (!raw) return null;
try {
return JSON.parse(raw);
} catch {
sessionStorage.removeItem(JOB_STORAGE_KEY);
return null;
}
}
function clearActiveJob() {
sessionStorage.removeItem(JOB_STORAGE_KEY);
}
function applySavedAlgorithm(saved) {
chosenAlgorithm = saved.algorithm;
chosenAlgorithmName = saved.algorithmName;
startTime = saved.startedAt;
if (chosenAlgorithm === "iforest_custom") {
selectAlgorithm(btnIF, btnAE);
} else if (chosenAlgorithm === "autoencoder_custom") {
selectAlgorithm(btnAE, btnIF);
}
enableStartButton();
}
function restoreCustomRunLayout() {
const step1 = document.getElementById("step-1");
const step2 = document.getElementById("step-2");
const step3 = document.getElementById("step-3");
const step4 = document.getElementById("step-4");
const step5 = document.getElementById("step-5");
if (step1) step1.classList.add("hidden");
if (step2) step2.classList.add("hidden");
if (step3) step3.classList.add("hidden");
if (step4) step4.classList.add("hidden");
if (step5) step5.classList.remove("hidden");
}
function updateProgressUI(progress) {
document.getElementById("progressContainer").classList.remove("hidden");
document.getElementById("progressBar").style.width = progress + "%";
document.getElementById("progressLabel").textContent = progress + "%";
}
function showLoading(customText = null) {
document.getElementById("loadingOverlay").classList.remove("hidden");
document.getElementById("progressBar").style.width = "0%";
document.getElementById("progressLabel").textContent = "0%";
document.getElementById("progressContainer").classList.add("hidden");
const loadingText = document.getElementById("loadingText");
if (loadingText) {
loadingText.innerHTML = customText ?? `Starting ${chosenAlgorithmName}...
This may take a while.`;
}
}
function hideLoading() {
document.getElementById("loadingOverlay").classList.add("hidden");
}
function showErrorPopup(message) {
const popup = document.createElement("div");
popup.innerHTML = `
${message}
| ${h} | `).join('')}
|---|
| ${typeof val === 'number' ? val.toFixed(4) : val} | `; }).join('')}
| Class | Precision | Recall | F1-Score |
|---|---|---|---|
| Normal traffic | ${data.precision_normal?.toFixed(4) ?? '-'} | ${data.recall_normal?.toFixed(4) ?? '-'} | ${data.f1_normal?.toFixed(4) ?? '-'} |
| Anomaly | ${data.precision_attack?.toFixed(4) ?? '-'} | ${data.recall_attack?.toFixed(4) ?? '-'} | ${data.f1_attack?.toFixed(4) ?? '-'} |
| Overall accuracy | - | - | ${data.accuracy?.toFixed(4) ?? '-'} |