Add dashboard for node and server statistics

This commit is contained in:
2026-02-09 11:41:46 +01:00
parent 20fd299905
commit b27f0b5778
12 changed files with 2368 additions and 471 deletions

View File

@@ -0,0 +1,271 @@
// Dashboard JavaScript
// Format numbers
function formatNumber(num) {
if (num >= 1000000000) return (num / 1000000000).toFixed(2) + 'B';
if (num >= 1000000) return (num / 1000000).toFixed(2) + 'M';
if (num >= 1000) return (num / 1000).toFixed(2) + 'K';
return num.toString();
}
// Format difficulty (always with M suffix and 1 decimal)
function formatDifficulty(num) {
if (num >= 1000000000) return (num / 1000000000).toFixed(1) + ' B';
if (num >= 1000000) return (num / 1000000).toFixed(1) + ' M';
if (num >= 1000) return (num / 1000).toFixed(1) + ' K';
return num.toFixed(1);
}
// Format block height (always full integer with thousands separator)
function formatBlockHeight(num) {
return num.toLocaleString('en-US');
}
// 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 time
function formatTime(timestamp) {
const date = new Date(timestamp * 1000);
return date.toLocaleTimeString();
}
// 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`;
if (hours > 0) return `${hours}h ${minutes}m`;
return `${minutes}m`;
}
// Update health status
function updateHealthStatus(data) {
const statusEl = document.getElementById('healthStatus');
const statusDot = statusEl.querySelector('.status-dot');
const statusText = statusEl.querySelector('.status-text');
if (data.status === 'healthy') {
statusEl.classList.remove('degraded');
statusText.textContent = 'All Systems Operational';
} else {
statusEl.classList.add('degraded');
statusText.textContent = 'Service Degraded';
}
}
// Update system resources
async function updateSystemResources() {
try {
const response = await fetch('/api/system/resources');
const data = await response.json();
if (data.error) {
console.error('System resources error:', data.error);
return;
}
// Update CPU
const cpuPercent = data.cpu.percent.toFixed(1);
document.getElementById('cpuValue').textContent = cpuPercent + '%';
document.getElementById('cpuProgress').style.width = cpuPercent + '%';
// Update Memory
const memPercent = data.memory.percent.toFixed(1);
document.getElementById('memoryValue').textContent = memPercent + '%';
document.getElementById('memoryProgress').style.width = memPercent + '%';
// Update Disk
const diskPercent = data.disk.percent.toFixed(1);
document.getElementById('diskValue').textContent = diskPercent + '%';
document.getElementById('diskProgress').style.width = diskPercent + '%';
} catch (error) {
console.error('Error fetching system resources:', error);
}
}
// Update Palladium info
async function updatePalladiumInfo() {
try {
const response = await fetch('/api/palladium/info');
const data = await response.json();
if (data.error) {
console.error('Palladium info error:', data.error);
return;
}
// Update blockchain info
if (data.blockchain) {
document.getElementById('blockHeight').textContent = formatBlockHeight(data.blockchain.blocks || 0);
document.getElementById('difficulty').textContent = formatDifficulty(data.blockchain.difficulty || 0);
document.getElementById('network').textContent = (data.blockchain.chain || 'unknown').toUpperCase();
const progress = ((data.blockchain.verificationprogress || 0) * 100).toFixed(2);
document.getElementById('syncProgress').textContent = progress + '%';
}
// Update network info
if (data.network) {
let version = data.network.subversion || 'Unknown';
// Extract version number from /Palladium:2.0.0/ format
const match = version.match(/:([\d.]+)/);
if (match) {
version = 'v' + match[1];
}
document.getElementById('nodeVersion').textContent = version;
}
// Update connections
document.getElementById('connections').textContent = data.peers || 0;
// Update mempool info
if (data.mempool) {
document.getElementById('mempoolSize').textContent = data.mempool.size || 0;
document.getElementById('mempoolBytes').textContent = formatBytes(data.mempool.bytes || 0);
document.getElementById('mempoolMax').textContent = formatBytes(data.mempool.maxmempool || 0);
// Calculate usage percentage
if (data.mempool.maxmempool && data.mempool.bytes) {
const usage = ((data.mempool.bytes / data.mempool.maxmempool) * 100).toFixed(1);
document.getElementById('mempoolUsage').textContent = usage + '%';
} else {
document.getElementById('mempoolUsage').textContent = '0%';
}
}
} catch (error) {
console.error('Error fetching Palladium info:', error);
}
}
// Update recent blocks
async function updateRecentBlocks() {
try {
const response = await fetch('/api/palladium/blocks/recent');
const data = await response.json();
if (data.error) {
console.error('Recent blocks error:', data.error);
return;
}
const tbody = document.getElementById('recentBlocksTable');
tbody.innerHTML = '';
if (data.blocks && data.blocks.length > 0) {
data.blocks.forEach(block => {
const row = document.createElement('tr');
row.innerHTML = `
<td><strong>${block.height}</strong></td>
<td class="hash-cell" title="${block.hash}">${block.hash.substring(0, 20)}...</td>
<td>${formatTime(block.time)}</td>
<td>${formatBytes(block.size)}</td>
<td>${block.tx_count}</td>
`;
tbody.appendChild(row);
});
} else {
tbody.innerHTML = '<tr><td colspan="5" class="loading">No blocks available</td></tr>';
}
} catch (error) {
console.error('Error fetching recent blocks:', error);
}
}
// Peers are now on a separate page, no update needed here
// Update ElectrumX stats
async function updateElectrumXStats() {
try {
const response = await fetch('/api/electrumx/stats');
const data = await response.json();
if (data.error) {
console.error('ElectrumX stats error:', data.error);
return;
}
if (data.stats) {
// Server version (extract version number like we do for node)
let serverVersion = data.stats.server_version || 'Unknown';
const versionMatch = serverVersion.match(/([\d.]+)/);
if (versionMatch) {
serverVersion = 'v' + versionMatch[1];
}
document.getElementById('serverVersion').textContent = serverVersion;
// Active sessions
const sessions = typeof data.stats.sessions === 'number' ? data.stats.sessions : '--';
document.getElementById('activeSessions').textContent = sessions;
// Database size
const dbSize = data.stats.db_size > 0 ? formatBytes(data.stats.db_size) : '--';
document.getElementById('dbSize').textContent = dbSize;
// Uptime
const uptime = data.stats.uptime > 0 ? formatDuration(data.stats.uptime) : '--';
document.getElementById('uptime').textContent = uptime;
// Server IP
document.getElementById('serverIP').textContent = data.stats.server_ip || '--';
// TCP Port
document.getElementById('tcpPort').textContent = data.stats.tcp_port || 50001;
// SSL Port
document.getElementById('sslPort').textContent = data.stats.ssl_port || 50002;
}
} catch (error) {
console.error('Error fetching ElectrumX stats:', error);
}
}
// Update health check
async function updateHealth() {
try {
const response = await fetch('/api/health');
const data = await response.json();
updateHealthStatus(data);
} catch (error) {
console.error('Error fetching health:', error);
}
}
// Update last update time
function updateLastUpdateTime() {
const now = new Date().toLocaleString();
document.getElementById('lastUpdate').textContent = now;
}
// Update all data
async function updateAll() {
updateLastUpdateTime();
await Promise.all([
updateHealth(),
updateSystemResources(),
updatePalladiumInfo(),
updateRecentBlocks(),
updateElectrumXStats()
]);
}
// Initialize dashboard
document.addEventListener('DOMContentLoaded', async () => {
// Initial update
await updateAll();
// Auto-refresh every 10 seconds
setInterval(updateAll, 10000);
});

View File

@@ -0,0 +1,123 @@
// 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 = `
<td class="peer-addr">${peer.addr}</td>
<td><span class="${directionClass}">${direction}</span></td>
<td>${version}</td>
<td>${connTime}</td>
<td>${formatBytes(peer.bytessent || 0)}</td>
<td>${formatBytes(peer.bytesrecv || 0)}</td>
<td><strong>${formatBytes(peerTotal)}</strong></td>
`;
tbody.appendChild(row);
});
// Update statistics
document.getElementById('totalPeers').textContent = data.peers.length;
document.getElementById('inboundPeers').textContent = inboundCount;
document.getElementById('outboundPeers').textContent = outboundCount;
document.getElementById('totalTraffic').textContent = formatBytes(totalSent + totalReceived);
} else {
tbody.innerHTML = '<tr><td colspan="7" class="loading">No peers connected</td></tr>';
document.getElementById('totalPeers').textContent = '0';
document.getElementById('inboundPeers').textContent = '0';
document.getElementById('outboundPeers').textContent = '0';
document.getElementById('totalTraffic').textContent = '0 B';
}
} catch (error) {
console.error('Error fetching peers:', error);
document.getElementById('peersTableBody').innerHTML = '<tr><td colspan="7" class="loading">Error loading peers</td></tr>';
}
}
// Update last update time
function updateLastUpdateTime() {
const now = new Date().toLocaleString();
document.getElementById('lastUpdate').textContent = now;
}
// Update all data
async function updateAll() {
updateLastUpdateTime();
await updatePeers();
}
// Initialize page
document.addEventListener('DOMContentLoaded', async () => {
// Initial update
await updateAll();
// Auto-refresh every 10 seconds
setInterval(updateAll, 10000);
});

View File

@@ -0,0 +1,506 @@
:root {
--primary-color: #6366f1;
--secondary-color: #8b5cf6;
--success-color: #10b981;
--warning-color: #f59e0b;
--danger-color: #ef4444;
--bg-primary: #0f172a;
--bg-secondary: #1e293b;
--bg-card: #1e293b;
--text-primary: #f1f5f9;
--text-secondary: #94a3b8;
--border-color: #334155;
--shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3);
--shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.4);
}
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', sans-serif;
background: linear-gradient(135deg, var(--bg-primary) 0%, #1a1f35 100%);
color: var(--text-primary);
min-height: 100vh;
line-height: 1.6;
}
.container {
max-width: 1400px;
margin: 0 auto;
padding: 20px;
}
/* Header */
.header {
background: var(--bg-card);
border-radius: 12px;
padding: 24px;
margin-bottom: 24px;
box-shadow: var(--shadow-lg);
border: 1px solid var(--border-color);
}
.header-content {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 16px;
}
.header h1 {
font-size: 28px;
font-weight: 700;
display: flex;
align-items: center;
gap: 12px;
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
}
.logo-icon {
width: 36px;
height: 36px;
stroke: var(--primary-color);
stroke-width: 2;
}
.health-status {
display: flex;
align-items: center;
gap: 8px;
padding: 8px 16px;
background: rgba(16, 185, 129, 0.1);
border-radius: 20px;
border: 1px solid var(--success-color);
}
.status-dot {
width: 10px;
height: 10px;
border-radius: 50%;
background: var(--success-color);
animation: pulse 2s ease-in-out infinite;
}
.status-text {
font-size: 14px;
font-weight: 600;
color: var(--success-color);
}
.health-status.degraded {
background: rgba(239, 68, 68, 0.1);
border-color: var(--danger-color);
}
.health-status.degraded .status-dot {
background: var(--danger-color);
}
.health-status.degraded .status-text {
color: var(--danger-color);
}
@keyframes pulse {
0%, 100% { opacity: 1; }
50% { opacity: 0.5; }
}
/* Dashboard Grid */
.dashboard-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(400px, 1fr));
gap: 24px;
margin-bottom: 24px;
}
.card {
background: var(--bg-card);
border-radius: 12px;
padding: 24px;
box-shadow: var(--shadow);
border: 1px solid var(--border-color);
transition: transform 0.2s, box-shadow 0.2s;
}
.card:hover {
transform: translateY(-2px);
box-shadow: var(--shadow-lg);
}
.card.full-width {
grid-column: 1 / -1;
}
.card.chart-card {
min-height: 350px;
}
.card-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 20px;
padding-bottom: 16px;
border-bottom: 2px solid var(--border-color);
}
.card-header h2 {
font-size: 20px;
font-weight: 600;
color: var(--text-primary);
}
.card-icon {
font-size: 24px;
}
.card-content {
color: var(--text-secondary);
}
/* Stats Grid */
.stat-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 16px;
}
.stat-grid-3col {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
}
/* ElectrumX Grid Layout */
.electrumx-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 16px;
}
.electrumx-grid .full-width {
grid-column: 1 / -1;
}
.stat-item {
padding: 16px;
background: rgba(99, 102, 241, 0.05);
border-radius: 8px;
border: 1px solid rgba(99, 102, 241, 0.2);
}
.stat-link {
text-decoration: none;
color: inherit;
cursor: pointer;
transition: all 0.3s ease;
position: relative;
}
.stat-link:hover {
background: rgba(99, 102, 241, 0.15);
border-color: rgba(99, 102, 241, 0.4);
transform: translateY(-2px);
}
.stat-link::after {
content: '→';
position: absolute;
right: 16px;
top: 50%;
transform: translateY(-50%);
font-size: 18px;
color: var(--primary-color);
opacity: 0;
transition: opacity 0.3s ease;
}
.stat-link:hover::after {
opacity: 1;
}
.stat-label {
font-size: 13px;
color: var(--text-secondary);
margin-bottom: 8px;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.stat-value {
font-size: 24px;
font-weight: 700;
color: var(--text-primary);
word-break: break-word;
}
/* Resource Monitoring */
.resource-item {
display: grid;
grid-template-columns: 120px 1fr 80px;
align-items: center;
gap: 12px;
margin-bottom: 16px;
}
.resource-item:last-child {
margin-bottom: 0;
}
.resource-label {
font-size: 14px;
color: var(--text-secondary);
font-weight: 500;
}
.resource-value {
font-size: 16px;
font-weight: 700;
color: var(--text-primary);
text-align: right;
}
.progress-bar {
height: 8px;
background: var(--bg-primary);
border-radius: 4px;
overflow: hidden;
position: relative;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, var(--primary-color), var(--secondary-color));
border-radius: 4px;
transition: width 0.5s ease;
position: relative;
overflow: hidden;
}
.progress-fill::after {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.3), transparent);
animation: shimmer 2s infinite;
}
@keyframes shimmer {
0% { transform: translateX(-100%); }
100% { transform: translateX(100%); }
}
/* Table */
.table-container {
overflow-x: auto;
}
.blocks-table {
width: 100%;
border-collapse: collapse;
}
.blocks-table thead {
background: rgba(99, 102, 241, 0.1);
}
.blocks-table th {
padding: 12px;
text-align: left;
font-size: 13px;
font-weight: 600;
color: var(--text-secondary);
text-transform: uppercase;
letter-spacing: 0.5px;
border-bottom: 2px solid var(--border-color);
}
.blocks-table td {
padding: 12px;
border-bottom: 1px solid var(--border-color);
font-size: 14px;
}
.blocks-table tbody tr {
transition: background 0.2s;
}
.blocks-table tbody tr:hover {
background: rgba(99, 102, 241, 0.05);
}
.blocks-table .hash-cell {
font-family: 'Courier New', monospace;
font-size: 12px;
color: var(--primary-color);
max-width: 200px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
.blocks-table .loading {
text-align: center;
padding: 24px;
color: var(--text-secondary);
}
.peer-addr {
font-family: 'Courier New', monospace;
font-size: 13px;
}
.peer-inbound {
padding: 4px 8px;
background: rgba(16, 185, 129, 0.1);
border-radius: 4px;
color: var(--success-color);
font-size: 12px;
font-weight: 600;
}
.peer-outbound {
padding: 4px 8px;
background: rgba(99, 102, 241, 0.1);
border-radius: 4px;
color: var(--primary-color);
font-size: 12px;
font-weight: 600;
}
/* Back Button */
.back-button {
display: inline-flex;
align-items: center;
gap: 8px;
padding: 8px 16px;
background: rgba(99, 102, 241, 0.1);
border-radius: 8px;
border: 1px solid var(--primary-color);
color: var(--primary-color);
text-decoration: none;
font-size: 14px;
font-weight: 600;
transition: all 0.3s ease;
}
.back-button:hover {
background: rgba(99, 102, 241, 0.2);
transform: translateX(-4px);
}
/* Peers Table Specific */
.peers-table td {
white-space: nowrap;
}
.peers-table .peer-addr {
font-family: 'Courier New', monospace;
font-size: 13px;
color: var(--text-primary);
}
/* Footer */
.footer {
text-align: center;
padding: 24px;
color: var(--text-secondary);
font-size: 14px;
background: var(--bg-card);
border-radius: 12px;
border: 1px solid var(--border-color);
}
.footer p {
margin: 4px 0;
}
#lastUpdate {
color: var(--primary-color);
font-weight: 600;
}
/* Chart Styling */
.chart-description {
font-size: 12px;
color: var(--text-secondary);
margin-bottom: 12px;
padding: 8px 12px;
background: rgba(99, 102, 241, 0.05);
border-left: 3px solid var(--primary-color);
border-radius: 4px;
font-style: italic;
}
canvas {
max-height: 280px !important;
}
/* Responsive */
@media (max-width: 768px) {
.dashboard-grid {
grid-template-columns: 1fr;
}
.header-content {
flex-direction: column;
align-items: flex-start;
}
.stat-grid {
grid-template-columns: 1fr;
}
.stat-grid-3col {
grid-template-columns: 1fr;
}
.electrumx-grid {
grid-template-columns: 1fr;
}
.resource-item {
grid-template-columns: 1fr;
gap: 8px;
}
.resource-value {
text-align: left;
}
.blocks-table {
font-size: 12px;
}
.blocks-table th,
.blocks-table td {
padding: 8px;
}
}
/* Loading Animation */
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.card {
animation: fadeIn 0.5s ease-out;
}