diff --git a/static/js/admin.js b/static/js/admin.js index 25660e3..7c9f1c4 100644 --- a/static/js/admin.js +++ b/static/js/admin.js @@ -41,19 +41,25 @@ function setBadgeCell(cell, textOrState) { } function rebuildTargetSelect(selectEl, currentNode, nodes) { - if (!selectEl) return; - const html = nodes.map(n => - `` - ).join(''); - selectEl.innerHTML = html; - const idx = Array.from(selectEl.options).findIndex(o => o.disabled); - selectEl.selectedIndex = idx >= 0 ? idx : 0; + if (!selectEl) return []; + const others = (nodes || []) + .map(n => String(n).trim()) + .filter(Boolean) + .filter(n => n !== String(currentNode || '').trim()); + selectEl.innerHTML = others.map(n => ``).join(''); + if (selectEl.options.length > 0) { + selectEl.selectedIndex = 0; + } + return others; // return list to decide enable/disable of MIGRATE } -function setMigrateDisabled(tr, isRunning) { +function updateMigrateButton(tr, isRunning) { const btn = tr?.querySelector('.act-migrate'); + const targetSel = tr?.querySelector('.target-node'); if (!btn) return; - if (isRunning) { + const hasTarget = targetSel && targetSel.options && targetSel.options.length > 0; + const enable = isRunning && hasTarget; + if (enable) { btn.removeAttribute('disabled'); btn.classList.remove('disabled'); } else { @@ -62,74 +68,24 @@ function setMigrateDisabled(tr, isRunning) { } } -export function stopAllAdminWatches() { - liveSockets.forEach(ws => { try { ws.close(); } catch {} }); - liveSockets.clear(); - if (slowTimer) clearInterval(slowTimer); - if (fastTimer) clearInterval(fastTimer); - slowTimer = null; - fastTimer = null; - activeSids.clear(); +function updateActionButtons(tr, isRunning) { + const bStart = tr?.querySelector('.act-start'); + const bStop = tr?.querySelector('.act-stop'); + const bShutdown = tr?.querySelector('.act-shutdown'); + if (bStart) { + if (isRunning) { bStart.setAttribute('disabled',''); bStart.classList.add('disabled'); } + else { bStart.removeAttribute('disabled'); bStart.classList.remove('disabled'); } + } + if (bStop) { + if (isRunning) { bStop.removeAttribute('disabled'); bStop.classList.remove('disabled'); } + else { bStop.setAttribute('disabled',''); bStop.classList.add('disabled'); } + } + if (bShutdown) { + if (isRunning) { bShutdown.removeAttribute('disabled'); bShutdown.classList.remove('disabled'); } + else { bShutdown.setAttribute('disabled',''); bShutdown.classList.add('disabled'); } + } } -function ensureWatchOn() { - const tbody = document.querySelector('#vm-admin tbody'); - if (!tbody) return; - Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => { - const sid = tr.getAttribute('data-sid'); - if (!sid) return; - if (liveSockets.has(sid)) return; - - try { - const ws = new WebSocket(`${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/ws/observe?sid=${encodeURIComponent(sid)}`); - liveSockets.set(sid, ws); - ws.onopen = () => {}; - ws.onclose = () => { liveSockets.delete(sid); }; - ws.onerror = () => {}; - ws.onmessage = (ev) => { - try { - const msg = JSON.parse(ev.data || '{}'); - if (!msg || !msg.type) return; - const tr = tbody.querySelector(`tr[data-sid="${sid}"]`); - if (!tr) return; - const statusCell = tr.children[4]; - const nameCell = tr.children[2]; - const nodeCell = tr.children[3]; - const targetSel = tr.querySelector('.target-node'); - - if (msg.type === 'status') { - const stRaw = String(msg.status || '').toLowerCase(); - const changed = setBadgeCell(statusCell, stRaw); - const isRunning = /running|online|started/.test(stRaw); - setMigrateDisabled(tr, isRunning); - if (changed) flashDot(nameCell); - if (stRaw && /running|stopped|shutdown/.test(stRaw)) { - setTimeout(() => activeSids.delete(sid), 3000); - } - } - - if (msg.type === 'node' && msg.node) { - const newNode = String(msg.node).trim(); - if (nodeCell && newNode && nodeCell.textContent.trim() !== newNode) { - nodeCell.textContent = newNode; - rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []); - flashDot(nameCell); - } - } - } catch {} - }; - } catch {} - }); -} - -export async function startAdminWatches() { - injectOnceCSS(); - - const tbody = document.querySelector('#vm-admin tbody'); - if (!tbody) return; - - const ns = await api.nodesSummary(); - const availableNodes = Array.isArray(ns?.nodes) ? Array.from(new Set(ns.nodes.map(n => String(n.name || n.node || n).trim()).filter(Boolean))) : (Array.isArray(ns) ? Array.from(new Set(ns.map(n => String(n.name || n.node || n).trim()).filter(Boolean))) : []); setRows(tbody, []); @@ -149,17 +105,19 @@ export async function startAdminWatches() { })); const htmlRows = rows.map(r => rowHTML([ - ``, + ``, safe(r.sid), safe(r.name), safe(r.node), badge(safe(r.status), /running|online|started/i.test(r.status) ? 'ok' : 'dark'), - ``, - `