// Peers Page JavaScript // Format bytes function formatBytes(bytes) { if (bytes === 0) return '0 B'; const k = 1024; const sizes = ['B', 'KB', 'MB', 'GB', 'TB']; const i = Math.floor(Math.log(bytes) / Math.log(k)); return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i]; } // Format duration function formatDuration(seconds) { const days = Math.floor(seconds / 86400); const hours = Math.floor((seconds % 86400) / 3600); const minutes = Math.floor((seconds % 3600) / 60); if (days > 0) return `${days}d ${hours}h ${minutes}m`; if (hours > 0) return `${hours}h ${minutes}m`; return `${minutes}m`; } // Update peers table and statistics async function updatePeers() { try { const response = await fetch('/api/palladium/peers'); const data = await response.json(); if (data.error) { console.error('Peers error:', data.error); return; } const tbody = document.getElementById('peersTableBody'); tbody.innerHTML = ''; if (data.peers && data.peers.length > 0) { let inboundCount = 0; let outboundCount = 0; let totalSent = 0; let totalReceived = 0; data.peers.forEach(peer => { const row = document.createElement('tr'); const direction = peer.inbound ? '⬇️ Inbound' : '⬆️ Outbound'; const directionClass = peer.inbound ? 'peer-inbound' : 'peer-outbound'; // Count inbound/outbound if (peer.inbound) { inboundCount++; } else { outboundCount++; } // Sum traffic totalSent += peer.bytessent || 0; totalReceived += peer.bytesrecv || 0; // Extract version number let version = peer.subver || peer.version || 'Unknown'; const versionMatch = version.match(/([\d.]+)/); if (versionMatch) { version = 'v' + versionMatch[1]; } // Format connection time const connTime = peer.conntime ? formatDuration(Math.floor(Date.now() / 1000) - peer.conntime) : '--'; // Calculate total traffic for this peer const peerTotal = (peer.bytessent || 0) + (peer.bytesrecv || 0); row.innerHTML = `