fix
This commit is contained in:
parent
3177976845
commit
d9549454e1
86
app.js
86
app.js
@ -20,6 +20,7 @@ const connections = {};
|
|||||||
window.openTerminals = {};
|
window.openTerminals = {};
|
||||||
let activePeer = null;
|
let activePeer = null;
|
||||||
window.activePeer = null; // Expose to other modules
|
window.activePeer = null; // Expose to other modules
|
||||||
|
|
||||||
hideStatusIndicator();
|
hideStatusIndicator();
|
||||||
let statsInterval = null;
|
let statsInterval = null;
|
||||||
let lastStatsUpdate = Date.now();
|
let lastStatsUpdate = Date.now();
|
||||||
@ -47,15 +48,25 @@ function startStatsInterval() {
|
|||||||
} else {
|
} else {
|
||||||
console.warn('[WARN] No active peer; skipping stats request.');
|
console.warn('[WARN] No active peer; skipping stats request.');
|
||||||
}
|
}
|
||||||
}, 100); // Poll every 100ms for better reactivity
|
}, 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 };
|
||||||
}
|
}
|
||||||
|
|
||||||
let smoothedStats = { cpu: 0, memory: 0 };
|
smoothedStats[containerId].cpu =
|
||||||
|
smoothedStats[containerId].cpu * (1 - smoothingFactor) +
|
||||||
|
newStats.cpu * smoothingFactor;
|
||||||
|
|
||||||
function smoothStats(newStats, smoothingFactor = 0.2) {
|
smoothedStats[containerId].memory =
|
||||||
smoothedStats.cpu = smoothedStats.cpu * (1 - smoothingFactor) + newStats.cpu * smoothingFactor;
|
smoothedStats[containerId].memory * (1 - smoothingFactor) +
|
||||||
smoothedStats.memory = smoothedStats.memory * (1 - smoothingFactor) + newStats.memory * smoothingFactor;
|
newStats.memory * smoothingFactor;
|
||||||
return smoothedStats;
|
|
||||||
|
return smoothedStats[containerId];
|
||||||
}
|
}
|
||||||
|
|
||||||
function refreshContainerStats() {
|
function refreshContainerStats() {
|
||||||
@ -271,43 +282,73 @@ collapseSidebarBtn.addEventListener('click', () => {
|
|||||||
|
|
||||||
function handlePeerData(data, topicId, peer) {
|
function handlePeerData(data, topicId, peer) {
|
||||||
try {
|
try {
|
||||||
|
// Parse the incoming data
|
||||||
const response = JSON.parse(data.toString());
|
const response = JSON.parse(data.toString());
|
||||||
console.log(`[DEBUG] Received data from peer (topic: ${topicId}): ${JSON.stringify(response)}`);
|
console.log(`[DEBUG] Received data from peer (topic: ${topicId}): ${JSON.stringify(response)}`);
|
||||||
|
|
||||||
// Ensure the data is for the active connection
|
// Ensure the data is for the active connection
|
||||||
if (!connections[topicId] || peer !== window.activePeer) {
|
if (!connections[topicId]) {
|
||||||
console.warn(`[WARN] Ignoring data from inactive peer or topic: ${topicId}`);
|
console.warn(`[WARN] No connection found for topic: ${topicId}. Ignoring data.`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Process the response based on its type
|
if (peer !== connections[topicId].peer) {
|
||||||
|
console.warn(`[WARN] Ignoring data from a non-active peer for topic: ${topicId}`);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Handle errors in the response
|
||||||
if (response.error) {
|
if (response.error) {
|
||||||
console.error(`[ERROR] Server error: ${response.error}`);
|
console.error(`[ERROR] Server error received: ${response.error}`);
|
||||||
showAlert('danger', response.error);
|
showAlert('danger', response.error);
|
||||||
hideStatusIndicator();
|
hideStatusIndicator();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (response.type === 'containers') {
|
// Delegate handling based on the response type
|
||||||
renderContainers(response.data, topicId); // Scope containers to this topic
|
switch (response.type) {
|
||||||
} else if (response.type === 'stats') {
|
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
|
response.data.topicId = topicId; // Attach the topicId to the stats
|
||||||
updateContainerStats(response.data); // Update stats for specific containers
|
updateContainerStats(response.data); // Update stats for the specified container
|
||||||
} else if (response.type === 'terminalOutput') {
|
break;
|
||||||
|
|
||||||
|
case 'terminalOutput':
|
||||||
|
console.log('[INFO] Appending terminal output...');
|
||||||
appendTerminalOutput(response.data, response.containerId, response.encoding);
|
appendTerminalOutput(response.data, response.containerId, response.encoding);
|
||||||
} else if (response.type === 'containerConfig') {
|
break;
|
||||||
|
|
||||||
|
case 'containerConfig':
|
||||||
|
console.log('[INFO] Handling container configuration...');
|
||||||
if (window.inspectContainerCallback) {
|
if (window.inspectContainerCallback) {
|
||||||
window.inspectContainerCallback(response.data);
|
window.inspectContainerCallback(response.data);
|
||||||
window.inspectContainerCallback = null; // Reset the callback
|
window.inspectContainerCallback = null; // Reset the callback
|
||||||
}
|
}
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
console.warn(`[WARN] Unhandled response type: ${response.type}`);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Handle peer response callback if defined
|
||||||
if (typeof window.handlePeerResponse === 'function') {
|
if (typeof window.handlePeerResponse === 'function') {
|
||||||
window.handlePeerResponse(response);
|
window.handlePeerResponse(response);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
// Catch and log any parsing or processing errors
|
||||||
console.error(`[ERROR] Failed to process peer data: ${err.message}`);
|
console.error(`[ERROR] Failed to process peer data: ${err.message}`);
|
||||||
showAlert('danger', 'Failed to process peer data.');
|
console.error(`[DEBUG] Raw data received: ${data.toString()}`);
|
||||||
|
showAlert('danger', 'Failed to process peer data. Check the console for details.');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -315,6 +356,7 @@ function handlePeerData(data, topicId, peer) {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Add a new connection
|
// Add a new connection
|
||||||
addConnectionForm.addEventListener('submit', (e) => {
|
addConnectionForm.addEventListener('submit', (e) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
@ -799,24 +841,26 @@ function updateStatsUI(row, stats) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function updateContainerStats(stats) {
|
function updateContainerStats(stats) {
|
||||||
if (!window.activePeer || !connections[stats.topicId] || window.activePeer !== connections[stats.topicId].peer) {
|
if (!stats || !stats.id || typeof stats.cpu === 'undefined' || typeof stats.memory === 'undefined') {
|
||||||
|
console.error('[ERROR] Invalid stats object:', stats);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
console.log(`[DEBUG] Updating stats for container ID: ${stats.id}`);
|
||||||
|
|
||||||
const row = containerList.querySelector(`tr[data-container-id="${stats.id}"]`);
|
const row = containerList.querySelector(`tr[data-container-id="${stats.id}"]`);
|
||||||
if (!row) {
|
if (!row) {
|
||||||
|
console.warn(`[WARN] No matching row for container ID: ${stats.id}`);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Smooth incoming stats
|
const smoothed = smoothStats(stats.id, stats);
|
||||||
const smoothed = smoothStats(stats);
|
|
||||||
updateStatsUI(row, smoothed);
|
updateStatsUI(row, smoothed);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// Function to open the Duplicate Modal with container configurations
|
// Function to open the Duplicate Modal with container configurations
|
||||||
function openDuplicateModal(container) {
|
function openDuplicateModal(container) {
|
||||||
console.log(`[INFO] Opening Duplicate Modal for container: ${container.Id}`);
|
console.log(`[INFO] Opening Duplicate Modal for container: ${container.Id}`);
|
||||||
|
Loading…
Reference in New Issue
Block a user