let currentNetworkId = null; let updateInterval = null; async function setNetwork() { const networkId = document.getElementById('networkId').value.trim(); if (!networkId) { showError('Inserisci un Network ID'); return; } if (networkId.length !== 16) { showError('Il Network ID deve essere lungo 16 caratteri'); return; } currentNetworkId = networkId; clearError(); // Avvia il polling automatico if (updateInterval) { clearInterval(updateInterval); } // Primo aggiornamento immediato await updateNodes(); // Poi ogni 5 secondi updateInterval = setInterval(updateNodes, 5000); } async function updateNodes() { if (!currentNetworkId) { return; } try { const response = await fetch(`/api/nodes?network_id=${currentNetworkId}`); if (!response.ok) { const error = await response.json(); console.error('API Error:', error); updateStatusIndicator(false); showError(error.error || 'Errore nel caricamento dei nodi'); return; } const data = await response.json(); updateStatusIndicator(true); renderNodes(data); updateStats(data); clearError(); } catch (error) { console.error('Fetch error:', error); updateStatusIndicator(false); showError('Errore di connessione al server'); } } function updateStatusIndicator(isOnline) { const indicator = document.getElementById('status-indicator'); if (isOnline) { indicator.classList.remove('offline'); indicator.classList.add('online'); } else { indicator.classList.remove('online'); indicator.classList.add('offline'); } } function showError(message) { let errorDiv = document.getElementById('error-message'); if (!errorDiv) { errorDiv = document.createElement('div'); errorDiv.id = 'error-message'; errorDiv.className = 'error-banner'; document.querySelector('.container').insertBefore(errorDiv, document.querySelector('header').nextSibling); } errorDiv.textContent = message; errorDiv.style.display = 'block'; } function clearError() { const errorDiv = document.getElementById('error-message'); if (errorDiv) { errorDiv.style.display = 'none'; } } function updateStats(data) { document.getElementById('total-nodes').textContent = data.total_nodes || 0; document.getElementById('online-nodes').textContent = data.online_nodes || 0; const now = new Date(); const timeString = now.toLocaleTimeString('it-IT'); document.getElementById('last-update').textContent = timeString; } function renderNodes(data) { const container = document.getElementById('nodes-container'); if (!data.nodes || data.nodes.length === 0) { container.innerHTML = '
Nessun nodo trovato
'; return; } container.innerHTML = data.nodes.map(node => createNodeCard(node)).join(''); } function createNodeCard(node) { const isOnline = node.online; const statusClass = isOnline ? 'online' : 'offline'; const statusText = isOnline ? '🟢 Online' : '🔴 Offline'; const lastCheck = new Date(node.last_check); const timeAgo = getTimeAgo(lastCheck); return `