refator_comm1

This commit is contained in:
Mateusz Gruszczyński
2025-10-18 22:45:56 +02:00
parent 8e3d191233
commit a841a1f9ad
3 changed files with 71 additions and 10 deletions

13
app.py
View File

@@ -537,6 +537,7 @@ def ws_observe(ws):
if not node:
ws.send(json.dumps({"type":"error","error":"could not resolve node"})); return
last_hash = None
seen_upids = set()
prev_node = node
@@ -549,6 +550,18 @@ def ws_observe(ws):
ws.send(json.dumps({"type":"moved","old_node":node,"new_node":cur_node,"meta":{"sid":sid,"vmid":vmid,"typ":typ}}))
prev_node, node = node, cur_node
# bieżący status VM/CT -> event "vm"
try:
base = f"/nodes/{node}/{typ}/{vmid}"
cur = get_json(["pvesh", "get", f"{base}/status/current"]) or {}
cur_hash = json.dumps(cur, sort_keys=True)
if cur_hash != last_hash:
last_hash = cur_hash
ws.send(json.dumps({"type":"vm","current":cur,"meta":{"sid":sid,"node":node,"typ":typ,"vmid":vmid}}))
except Exception:
pass
# zadania na aktualnym i poprzednim nodzie
nodes_to_scan = [node] + ([prev_node] if prev_node and prev_node != node else [])
for nX in nodes_to_scan:
tasks = get_json(["pvesh","get",f"/nodes/{nX}/tasks","-limit","50"]) or []

View File

@@ -1,4 +1,4 @@
import { rowHTML, setRows, safe, showToast } from './helpers.js';
import { rowHTML, setRows, safe, showToast, badge } from './helpers.js';
import { api } from './api.js';
const liveSockets = new Map();
@@ -45,6 +45,21 @@ function flashDot(cell) {
setTimeout(() => { try { dot.remove(); } catch {} }, 4200);
}
function setBadgeCell(cell, textOrState) {
if (!cell) return;
let html = '';
const s = String(textOrState || '').toLowerCase();
if (/running|online|started/.test(s)) html = badge('running','ok');
else if (/stopp|shutdown|offline/.test(s)) html = badge('stopped','dark');
else if (/working|progress|busy/.test(s)) html = badge('working','info');
else html = badge(textOrState || '—','dark');
if (cell.innerHTML !== html) {
cell.innerHTML = html;
return true; // zmiana
}
return false;
}
function rebuildTargetSelect(selectEl, currentNode, nodes) {
if (!selectEl) return;
const html = nodes.map(n =>
@@ -73,6 +88,7 @@ export async function renderVMAdmin() {
const rows = arr.map(x => {
const sid = safe(x.sid), type = safe(x.type), name = safe(x.name), node = safe(x.node);
const nameCell = `<span class="vm-name-wrap">${name}</span>`;
const statusCell = badge('—','dark');
const actions = `
<div class="btn-group btn-group-sm" role="group">
<button class="btn btn-outline-secondary act-unlock">Unlock</button>
@@ -84,8 +100,8 @@ export async function renderVMAdmin() {
${availableNodes.map(n => `<option value="${n}" ${n === x.node ? 'disabled selected' : ''}>${n}</option>`).join('')}
</select>`;
const migrateBtn = `<button class="btn btn-outline-primary btn-sm act-migrate">Migrate (offline)</button>`;
// Kolumny: SID | TYPE | NAME | NODE | ACTIONS | TARGET | MIGRATE
return rowHTML([sid, type.toUpperCase(), nameCell, node, actions, sel, migrateBtn], `data-sid="${sid}"`);
// SID | TYPE | NAME | NODE | STATUS | ACTIONS | TARGET | MIGRATE
return rowHTML([sid, type.toUpperCase(), nameCell, node, statusCell, actions, sel, migrateBtn], `data-sid="${sid}"`);
});
setRows(tbody, rows);
@@ -93,6 +109,7 @@ export async function renderVMAdmin() {
Array.from(tbody.querySelectorAll('tr[data-sid]')).forEach(tr => {
const sid = tr.getAttribute('data-sid');
const nodeCell = tr.children[3];
const statusCell= tr.children[4];
const nameCell = tr.children[2];
const targetSel = tr.querySelector('.target-node');
@@ -108,13 +125,21 @@ export async function renderVMAdmin() {
try {
const msg = JSON.parse(ev.data);
if (msg.type === 'task-start') {
if (msg.type === 'vm' && msg.current) {
const st = String(msg.current.status || msg.current.qmpstatus || '').toLowerCase();
const changed = setBadgeCell(statusCell, st);
const isRunning = /running|online|started/.test(st);
setMigrateDisabled(tr, isRunning);
if (changed) flashDot(nameCell);
}
else if (msg.type === 'task-start') {
setBadgeCell(statusCell, 'working');
activeSids.add(sid);
flashDot(nameCell);
}
else if (msg.type === 'task') {
setBadgeCell(statusCell, 'working');
activeSids.add(sid);
flashDot(nameCell);
}
else if (msg.type === 'moved' && msg.new_node) {
if (nodeCell && nodeCell.textContent.trim() !== msg.new_node) {
@@ -125,6 +150,7 @@ export async function renderVMAdmin() {
activeSids.add(sid);
}
else if (msg.type === 'done') {
// status końcowy dociągnie kolejny pakiet "vm" lub szybki refresh
setTimeout(() => activeSids.delete(sid), 4000);
flashDot(nameCell);
}
@@ -140,7 +166,10 @@ export async function renderVMAdmin() {
const doAction = async (action, withTarget=false) => {
try {
ensureWatchOn();
if (action !== 'unlock') activeSids.add(sid);
if (action !== 'unlock') {
setBadgeCell(statusCell, 'working');
activeSids.add(sid);
}
const target = withTarget ? (targetSel?.value || '') : undefined;
const resp = await api.vmAction(sid, action, target);
if (!resp.ok) throw new Error(resp.error || 'unknown');
@@ -177,6 +206,7 @@ export async function renderVMAdmin() {
if (!rowData) return;
const nodeCell = tr.children[3];
const statusCell= tr.children[4];
const nameCell = tr.children[2];
const targetSel = tr.querySelector('.target-node');
@@ -186,11 +216,17 @@ export async function renderVMAdmin() {
rebuildTargetSelect(targetSel, newNode, nodesNow);
flashDot(nameCell);
}
// status z wolnego reconcile — tylko gdy brak „working”, żeby nie zagłuszać WS
const currentTxt = (statusCell?.innerText || '').toLowerCase();
if (!/working/.test(currentTxt)) {
// brak statusu w liście — zostaw jak jest, dociągnie WS/fastTimer
}
});
} catch {}
}, 30000);
// tylko aktywne co 10 s
// tylko aktywne co 10 s (dociąga precyzyjny status + node)
fastTimer = setInterval(async () => {
try {
const sids = Array.from(activeSids);
@@ -200,20 +236,31 @@ export async function renderVMAdmin() {
if (!detail || !detail.meta) continue;
const tr = tbody.querySelector(`tr[data-sid="${sid}"]`);
if (!tr) continue;
const nodeCell = tr.children[3];
const statusCell= tr.children[4];
const nameCell = tr.children[2];
const targetSel = tr.querySelector('.target-node');
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);
if (changed) flashDot(nameCell);
const newNode = String(detail.node || (detail.meta && detail.meta.node) || '').trim();
if (newNode) {
if (nodeCell && nodeCell.textContent.trim() !== newNode) {
nodeCell.textContent = newNode;
rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []);
flashDot(nameCell);
}
rebuildTargetSelect(targetSel, newNode, window.__nodesCache || []);
}
if (stRaw && /running|stopped|shutdown/.test(stRaw)) {
setTimeout(() => activeSids.delete(sid), 4000);
}
}
} catch {}
}, 10000);

View File

@@ -215,6 +215,7 @@
<th>Type</th>
<th>Name</th>
<th>Node</th>
<th>Status</th>
<th>Actions</th>
<th>Target</th>
<th>Migrate</th>
@@ -222,7 +223,7 @@
</thead>
<tbody>
<tr>
<td colspan="7" class="text-muted">Loading…</td>
<td colspan="8" class="text-muted">Loading…</td>
</tr>
</tbody>
</table>