<script>
// 後端 API URL
const backendApiUrl = 'https://ba75f9d5899b.ngrok.app/api/systeminfo';
// 更新間隔時間(毫秒)
const updateInterval = 1000; // 可以自由設定的參數,例如每 1000 毫秒(1 秒)更新一次
// 取得 API 數據
async function fetchComputerData() {
try {
const response = await fetch(backendApiUrl);
if (!response.ok) {
throw new Error('Failed to fetch data from backend');
}
const computers = await response.json();
updateDashboard(computers);
} catch (error) {
console.error(error);
// 顯示錯誤訊息
const dashboard = document.getElementById("dashboard");
dashboard.innerHTML = `<div class="computer-card warning">
<div class="computer-name">錯誤</div>
<div>無法取得監控數據,請稍後再試。</div>
</div>`;
}
}
function createComputerCard(computer) {
const card = document.createElement("div");
card.classList.add("computer-card");
// 如果是無法取得數據的情況,標示為警告
if (computer.error) {
card.classList.add("warning");
const name = document.createElement("div");
name.classList.add("computer-name");
name.textContent = computer.computerName;
const warningMessage = document.createElement("div");
warningMessage.textContent = computer.message || "無法取得數據,該主機可能停機。";
card.appendChild(name);
card.appendChild(warningMessage);
return card;
}
// 門檻值設置
const cpuThreshold = 80;
const memoryThreshold = 2; // GB
const diskThreshold = 10; // GB
// 檢查是否超過門檻
if (computer.cpuUsage > cpuThreshold || computer.availableMemoryGB < memoryThreshold || computer.availableDiskSpaceGB < diskThreshold) {
card.classList.add("alert");
}
const name = document.createElement("div");
name.classList.add("computer-name");
name.textContent = computer.computerName;
const ipAddress = document.createElement("div");
ipAddress.classList.add("ip-address");
ipAddress.innerHTML = `IP 位址: ${computer.ipAddress}`;
const cpuUsage = document.createElement("div");
cpuUsage.classList.add("cpu-usage");
cpuUsage.innerHTML = `CPU 使用率: ${computer.cpuUsage.toFixed(2)}%`;
const cpuProgressBar = document.createElement("div");
cpuProgressBar.classList.add("progress-bar");
const cpuProgressInner = document.createElement("div");
cpuProgressInner.classList.add("progress-bar-inner", "cpu-bar-inner");
cpuProgressInner.style.width = `${computer.cpuUsage}%`;
cpuProgressInner.textContent = `${computer.cpuUsage.toFixed(2)}%`;
cpuProgressBar.appendChild(cpuProgressInner);
const memoryUsage = document.createElement("div");
memoryUsage.classList.add("memory-usage");
memoryUsage.innerHTML = `剩餘可用記憶體: ${computer.availableMemoryGB.toFixed(2)} GB`;
const memoryProgressBar = document.createElement("div");
memoryProgressBar.classList.add("progress-bar");
const memoryProgressInner = document.createElement("div");
memoryProgressInner.classList.add("progress-bar-inner", "memory-bar-inner");
const memoryPercentage = 100-(((computer.availableMemoryGB - memoryThreshold) / computer.availableMemoryGB) * 100);
memoryProgressInner.style.width = `${Math.min(memoryPercentage, 100)}%`;
memoryProgressInner.textContent = `${Math.min(memoryPercentage.toFixed(1), 100)}%`;
memoryProgressBar.appendChild(memoryProgressInner);
const diskSpace = document.createElement("div");
diskSpace.classList.add("disk-space");
diskSpace.innerHTML = `剩餘磁碟空間: ${computer.availableDiskSpaceGB.toFixed(2)} GB`;
const diskProgressBar = document.createElement("div");
diskProgressBar.classList.add("progress-bar");
const diskProgressInner = document.createElement("div");
diskProgressInner.classList.add("progress-bar-inner", "disk-bar-inner");
const diskPercentage = 100-(((computer.availableDiskSpaceGB - diskThreshold) / computer.availableDiskSpaceGB) * 100);
diskProgressInner.style.width = `${Math.min(diskPercentage, 100)}%`;
diskProgressInner.textContent = `${Math.min(diskPercentage.toFixed(1), 100)}%`;
diskProgressBar.appendChild(diskProgressInner);
card.appendChild(name);
card.appendChild(ipAddress);
card.appendChild(cpuUsage);
card.appendChild(cpuProgressBar);
card.appendChild(memoryUsage);
card.appendChild(memoryProgressBar);
card.appendChild(diskSpace);
card.appendChild(diskProgressBar);
return card;
}
function updateDashboard(computers) {
const dashboard = document.getElementById("dashboard");
dashboard.innerHTML = "";
computers.forEach(computer => {
const card = createComputerCard(computer);
dashboard.appendChild(card);
});
}
// 初始化,呼叫後端 API 並定期更新儀表板
fetchComputerData(); // 立即呼叫一次
setInterval(fetchComputerData, updateInterval);
</script>
-
Notifications
You must be signed in to change notification settings - Fork 0
Sen-CaPoo/Sen-CaPoo.github.io
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
About
No description, website, or topics provided.
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published