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'), - ``, - `
- - - -
` + `
+ + + + +
`, + ``, + `` ])); setRows(tbody, htmlRows); @@ -168,7 +126,8 @@ export async function startAdminWatches() { Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => { const nodeCell = tr.children[3]; const targetSel = tr.querySelector('.target-node'); - rebuildTargetSelect(targetSel, nodeCell?.textContent.trim(), availableNodes); + const _others = rebuildTargetSelect(targetSel, nodeCell?.textContent.trim(), availableNodes); + updateMigrateButton(tr, /running|online|started/i.test(tr.children[4].innerText)); const sid = tr.getAttribute('data-sid'); const nameCell = tr.children[2]; @@ -178,7 +137,7 @@ export async function startAdminWatches() { const targetNode = needsTarget ? targetSel?.value : undefined; activeSids.add(sid); setBadgeCell(tr.children[4], 'working'); - setMigrateDisabled(tr, false); + updateMigrateButton(tr, false); const res = await api.vmAction(sid, kind, targetNode); if (res?.ok) { showToast(`Task ${kind} started for ${safe(nameCell.textContent)}`); @@ -190,8 +149,10 @@ export async function startAdminWatches() { } } + tr.querySelector('.act-start')?.addEventListener('click', () => doAction('start')); tr.querySelector('.act-stop')?.addEventListener('click', () => doAction('stop')); tr.querySelector('.act-shutdown')?.addEventListener('click', () => doAction('shutdown')); + tr.querySelector('.act-unlock')?.addEventListener('click', () => doAction('unlock')); tr.querySelector('.act-migrate')?.addEventListener('click', () => doAction('migrate', true)); ensureWatchOn(); @@ -221,7 +182,8 @@ export async function startAdminWatches() { const newNode = String(rowData.node || '').trim(); if (nodeCell && newNode && nodeCell.textContent.trim() !== newNode) { nodeCell.textContent = newNode; - rebuildTargetSelect(targetSel, newNode, nodesNow); + const _others = rebuildTargetSelect(targetSel, newNode, nodesNow); + updateMigrateButton(tr, /running|online|started/i.test(tr.children[4].innerText)); flashDot(nameCell); } @@ -232,7 +194,9 @@ export async function startAdminWatches() { if (stRaw) { const changed = setBadgeCell(statusCell, stRaw); const isRunning = /running|online|started/.test(stRaw); - setMigrateDisabled(tr, isRunning); + updateMigrateButton(tr, isRunning); + updateActionButtons(tr, isRunning); + updateActionButtons(tr, isRunning); if (changed) flashDot(nameCell); } } @@ -258,8 +222,10 @@ export async function startAdminWatches() { const stRaw = String((detail.current && (detail.current.status || detail.current.qmpstatus)) || '').toLowerCase(); const changed = setBadgeCell(statusCell, stRaw); - const isRunning = /running|online|started/.test(stRaw); - setMigrateDisabled(tr, isRunning); + const isRunning = /running|online|started/.test(stRaw); + updateMigrateButton(tr, isRunning); + updateActionButtons(tr, isRunning); + updateActionButtons(tr, isRunning); if (changed) flashDot(nameCell); const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim();