diff --git a/public/index.html b/public/index.html index 624234a..bc9e69d 100644 --- a/public/index.html +++ b/public/index.html @@ -210,77 +210,53 @@ async function loadMetrics() { try { - const response = await fetch('/metrics'); - const metrics = await response.json(); - updateMetricsDisplay(metrics); + const response = await fetch('/api/metrics'); + const data = await response.json(); + + // 添加数据验证 + if (!data || typeof data !== 'object') { + throw new Error('Invalid metrics data received'); + } + + updateMetricsDisplay(data); } catch (error) { console.error('Error loading metrics:', error); + document.getElementById('metrics-container').innerHTML = + '
加载指标数据时出错,请稍后重试
'; } } - function updateMetricsDisplay(metrics) { - const metricsHtml = ` -${escapeHtml(String(value))}
+ `; + container.appendChild(metricDiv); + }); } - function formatBytes(bytes) { - const units = ['B', 'KB', 'MB', 'GB']; - let size = bytes; - let unitIndex = 0; - while (size >= 1024 && unitIndex < units.length - 1) { - size /= 1024; - unitIndex++; - } - return `${size.toFixed(2)} ${units[unitIndex]}`; - } - - function formatDuration(ns) { - const duration = ns / 1e9; // 转换为秒 - const days = Math.floor(duration / 86400); - const hours = Math.floor((duration % 86400) / 3600); - const minutes = Math.floor((duration % 3600) / 60); - const seconds = Math.floor(duration % 60); - return `${days}天 ${hours}时 ${minutes}分 ${seconds}秒`; + function escapeHtml(unsafe) { + return unsafe + .replace(/&/g, "&") + .replace(//g, ">") + .replace(/"/g, """) + .replace(/'/g, "'"); } // 定期更新监控数据