Compare commits
No commits in common. "d9549454e1f971a3cac53ae62f61cd99f635f71a" and "1dc39decf749bfa961565194f258f806c3a65121" have entirely different histories.
d9549454e1
...
1dc39decf7
145
app.js
145
app.js
@ -20,10 +20,9 @@ const connections = {};
|
||||
window.openTerminals = {};
|
||||
let activePeer = null;
|
||||
window.activePeer = null; // Expose to other modules
|
||||
|
||||
hideStatusIndicator();
|
||||
let statsInterval = null;
|
||||
let lastStatsUpdate = Date.now();
|
||||
let statsInterval = null; // Global variable to hold the interval
|
||||
|
||||
function stopStatsInterval() {
|
||||
if (statsInterval) {
|
||||
clearInterval(statsInterval);
|
||||
@ -32,41 +31,21 @@ function stopStatsInterval() {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
function startStatsInterval() {
|
||||
// Clear any existing interval
|
||||
if (statsInterval) {
|
||||
clearInterval(statsInterval);
|
||||
}
|
||||
|
||||
// Start a new interval to request stats every second
|
||||
statsInterval = setInterval(() => {
|
||||
if (window.activePeer) {
|
||||
const now = Date.now();
|
||||
if (now - lastStatsUpdate >= 500) { // Ensure at least 500ms between updates
|
||||
sendCommand('stats', {}); // Adjust command if necessary
|
||||
lastStatsUpdate = now;
|
||||
}
|
||||
console.log('[INFO] Requesting container stats...');
|
||||
sendCommand('stats', {}); // Adjust the command if specific arguments are needed
|
||||
} else {
|
||||
console.warn('[WARN] No active peer; skipping stats request.');
|
||||
}
|
||||
}, 500); // Poll every 100ms for better reactivity
|
||||
}
|
||||
const smoothedStats = {}; // Container-specific smoothing storage
|
||||
|
||||
|
||||
function smoothStats(containerId, newStats, smoothingFactor = 0.2) {
|
||||
if (!smoothedStats[containerId]) {
|
||||
smoothedStats[containerId] = { cpu: 0, memory: 0 };
|
||||
}
|
||||
|
||||
smoothedStats[containerId].cpu =
|
||||
smoothedStats[containerId].cpu * (1 - smoothingFactor) +
|
||||
newStats.cpu * smoothingFactor;
|
||||
|
||||
smoothedStats[containerId].memory =
|
||||
smoothedStats[containerId].memory * (1 - smoothingFactor) +
|
||||
newStats.memory * smoothingFactor;
|
||||
|
||||
return smoothedStats[containerId];
|
||||
}, 1000); // 1 second interval
|
||||
}
|
||||
|
||||
function refreshContainerStats() {
|
||||
@ -282,73 +261,43 @@ collapseSidebarBtn.addEventListener('click', () => {
|
||||
|
||||
function handlePeerData(data, topicId, peer) {
|
||||
try {
|
||||
// Parse the incoming data
|
||||
const response = JSON.parse(data.toString());
|
||||
console.log(`[DEBUG] Received data from peer (topic: ${topicId}): ${JSON.stringify(response)}`);
|
||||
|
||||
// Ensure the data is for the active connection
|
||||
if (!connections[topicId]) {
|
||||
console.warn(`[WARN] No connection found for topic: ${topicId}. Ignoring data.`);
|
||||
if (!connections[topicId] || peer !== window.activePeer) {
|
||||
console.warn(`[WARN] Ignoring data from inactive peer or topic: ${topicId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (peer !== connections[topicId].peer) {
|
||||
console.warn(`[WARN] Ignoring data from a non-active peer for topic: ${topicId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
// Handle errors in the response
|
||||
// Process the response based on its type
|
||||
if (response.error) {
|
||||
console.error(`[ERROR] Server error received: ${response.error}`);
|
||||
console.error(`[ERROR] Server error: ${response.error}`);
|
||||
showAlert('danger', response.error);
|
||||
hideStatusIndicator();
|
||||
return;
|
||||
}
|
||||
|
||||
// Delegate handling based on the response type
|
||||
switch (response.type) {
|
||||
case 'containers':
|
||||
console.log('[INFO] Processing container list...');
|
||||
renderContainers(response.data, topicId); // Render containers specific to this topic
|
||||
break;
|
||||
|
||||
case 'stats':
|
||||
console.log('[INFO] Updating container stats...');
|
||||
if (!response.data.id) {
|
||||
console.warn('[WARN] Stats response is missing container ID. Skipping update.');
|
||||
return;
|
||||
}
|
||||
response.data.topicId = topicId; // Attach the topicId to the stats
|
||||
updateContainerStats(response.data); // Update stats for the specified container
|
||||
break;
|
||||
|
||||
case 'terminalOutput':
|
||||
console.log('[INFO] Appending terminal output...');
|
||||
appendTerminalOutput(response.data, response.containerId, response.encoding);
|
||||
break;
|
||||
|
||||
case 'containerConfig':
|
||||
console.log('[INFO] Handling container configuration...');
|
||||
if (window.inspectContainerCallback) {
|
||||
window.inspectContainerCallback(response.data);
|
||||
window.inspectContainerCallback = null; // Reset the callback
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
console.warn(`[WARN] Unhandled response type: ${response.type}`);
|
||||
break;
|
||||
if (response.type === 'containers') {
|
||||
renderContainers(response.data, topicId); // Scope containers to this topic
|
||||
} else if (response.type === 'stats') {
|
||||
response.data.topicId = topicId; // Attach the topicId to the stats
|
||||
updateContainerStats(response.data); // Update stats for specific containers
|
||||
} else if (response.type === 'terminalOutput') {
|
||||
appendTerminalOutput(response.data, response.containerId, response.encoding);
|
||||
} else if (response.type === 'containerConfig') {
|
||||
if (window.inspectContainerCallback) {
|
||||
window.inspectContainerCallback(response.data);
|
||||
window.inspectContainerCallback = null; // Reset the callback
|
||||
}
|
||||
}
|
||||
|
||||
// Handle peer response callback if defined
|
||||
if (typeof window.handlePeerResponse === 'function') {
|
||||
window.handlePeerResponse(response);
|
||||
}
|
||||
} catch (err) {
|
||||
// Catch and log any parsing or processing errors
|
||||
console.error(`[ERROR] Failed to process peer data: ${err.message}`);
|
||||
console.error(`[DEBUG] Raw data received: ${data.toString()}`);
|
||||
showAlert('danger', 'Failed to process peer data. Check the console for details.');
|
||||
showAlert('danger', 'Failed to process peer data.');
|
||||
}
|
||||
}
|
||||
|
||||
@ -356,7 +305,6 @@ function handlePeerData(data, topicId, peer) {
|
||||
|
||||
|
||||
|
||||
|
||||
// Add a new connection
|
||||
addConnectionForm.addEventListener('submit', (e) => {
|
||||
e.preventDefault();
|
||||
@ -398,7 +346,6 @@ function addConnection(topicHex) {
|
||||
e.stopPropagation();
|
||||
disconnectConnection(topicId, connectionItem);
|
||||
});
|
||||
refreshContainerStats();
|
||||
|
||||
connectionList.appendChild(connectionItem);
|
||||
|
||||
@ -423,13 +370,12 @@ function addConnection(topicHex) {
|
||||
window.activePeer = null;
|
||||
dashboard.classList.add('hidden');
|
||||
containerList.innerHTML = '';
|
||||
stopStatsInterval(); // Stop stats polling
|
||||
}
|
||||
});
|
||||
|
||||
if (!window.activePeer) {
|
||||
switchConnection(topicId);
|
||||
}
|
||||
startStatsInterval();
|
||||
});
|
||||
|
||||
// Collapse the sidebar after adding a connection
|
||||
@ -452,15 +398,6 @@ document.addEventListener('DOMContentLoaded', () => {
|
||||
const topicHex = savedConnections[topicId].topic;
|
||||
addConnection(topicHex);
|
||||
});
|
||||
|
||||
if (Object.keys(connections).length > 0) {
|
||||
hideWelcomePage();
|
||||
startStatsInterval(); // Start stats polling for active peers
|
||||
} else {
|
||||
showWelcomePage();
|
||||
}
|
||||
|
||||
assertVisibility();
|
||||
});
|
||||
|
||||
|
||||
@ -832,35 +769,31 @@ function addActionListeners(row, container) {
|
||||
}
|
||||
|
||||
|
||||
function updateStatsUI(row, stats) {
|
||||
requestIdleCallback(() => {
|
||||
row.querySelector('.cpu').textContent = stats.cpu.toFixed(2) || '0.00';
|
||||
row.querySelector('.memory').textContent = (stats.memory / (1024 * 1024)).toFixed(2) || '0.00';
|
||||
row.querySelector('.ip-address').textContent = stats.ip || 'No IP Assigned';
|
||||
});
|
||||
}
|
||||
|
||||
// Function to update container statistics
|
||||
function updateContainerStats(stats) {
|
||||
if (!stats || !stats.id || typeof stats.cpu === 'undefined' || typeof stats.memory === 'undefined') {
|
||||
console.error('[ERROR] Invalid stats object:', stats);
|
||||
return;
|
||||
console.log(`[DEBUG] Updating stats for container ID: ${stats.id}, Topic ID: ${stats.topicId}`);
|
||||
|
||||
// Ensure stats belong to the active connection
|
||||
if (!window.activePeer || !connections[stats.topicId] || window.activePeer !== connections[stats.topicId].peer) {
|
||||
console.warn(`[WARN] Stats received for inactive or unknown connection. Skipping.`);
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`[DEBUG] Updating stats for container ID: ${stats.id}`);
|
||||
|
||||
// Find the row for the container by its ID
|
||||
const row = containerList.querySelector(`tr[data-container-id="${stats.id}"]`);
|
||||
if (!row) {
|
||||
console.warn(`[WARN] No matching row for container ID: ${stats.id}`);
|
||||
return;
|
||||
console.warn(`[WARN] No matching row for container ID: ${stats.id}. Skipping stats update.`);
|
||||
return;
|
||||
}
|
||||
|
||||
const smoothed = smoothStats(stats.id, stats);
|
||||
updateStatsUI(row, smoothed);
|
||||
// Update the container statistics in the UI
|
||||
row.querySelector('.cpu').textContent = stats.cpu.toFixed(2) || '0.00';
|
||||
row.querySelector('.memory').textContent = (stats.memory / (1024 * 1024)).toFixed(2) || '0.00';
|
||||
row.querySelector('.ip-address').textContent = stats.ip || 'No IP Assigned';
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Function to open the Duplicate Modal with container configurations
|
||||
function openDuplicateModal(container) {
|
||||
console.log(`[INFO] Opening Duplicate Modal for container: ${container.Id}`);
|
||||
|
Loading…
Reference in New Issue
Block a user