// 全局变量 let currentSection = 'dashboard'; let refreshInterval = null; let charts = {}; // API基础URL const API_BASE = '/monitor'; // 初始化 document.addEventListener('DOMContentLoaded', function() { initializeDashboard(); startAutoRefresh(); }); // 初始化仪表盘 function initializeDashboard() { showSection('dashboard'); loadDashboardData(); } // 显示指定区域 function showSection(section) { // 隐藏所有区域 document.querySelectorAll('.section').forEach(el => { el.classList.add('hidden'); }); // 显示指定区域 document.getElementById(section + '-section').classList.remove('hidden'); // 更新侧边栏状态 document.querySelectorAll('.sidebar-item').forEach(el => { el.classList.remove('active'); }); event.currentTarget.classList.add('active'); // 更新标题 const titles = { 'dashboard': '仪表盘', 'health': '健康检查', 'metrics': '性能指标', 'errors': '错误追踪', 'logs': '日志分析', 'alerts': '告警管理', 'settings': '系统设置' }; document.getElementById('section-title').textContent = titles[section]; currentSection = section; // 加载对应数据 loadSectionData(section); } // 加载区域数据 function loadSectionData(section) { switch(section) { case 'dashboard': loadDashboardData(); break; case 'health': loadHealthData(); break; case 'metrics': loadMetricsData(); break; case 'errors': loadErrorsData(); break; case 'logs': // 日志数据需要用户主动搜索 break; case 'alerts': loadAlertsData(); break; case 'settings': loadSettingsData(); break; } } // 加载仪表盘数据 async function loadDashboardData() { try { // 加载健康状态 const healthResponse = await fetch(`${API_BASE}/health`); const health = await healthResponse.json(); updateDashboardHealth(health.data); // 加载指标数据 const metricsResponse = await fetch(`${API_BASE}/metrics`); const metrics = await metricsResponse.json(); updateDashboardMetrics(metrics.data); // 加载告警数据 const alertsResponse = await fetch(`${API_BASE}/alerts/active`); const alerts = await alertsResponse.json(); updateDashboardAlerts(alerts.data); // 加载图表数据 loadDashboardCharts(); // 更新时间 updateLastUpdateTime(); } catch (error) { console.error('加载仪表盘数据失败:', error); showError('加载仪表盘数据失败'); } } // 更新仪表盘健康状态 function updateDashboardHealth(health) { const statusEl = document.getElementById('system-status'); const statusClass = health.status === 'healthy' ? 'status-healthy' : health.status === 'warning' ? 'status-warning' : 'status-critical'; statusEl.textContent = health.status === 'healthy' ? '健康' : health.status === 'warning' ? '警告' : '严重'; statusEl.className = `text-2xl font-bold ${statusClass}`; } // 更新仪表盘指标 function updateDashboardMetrics(metrics) { if (metrics.system) { const memoryUsage = metrics.system.memory_usage; const memoryLimit = metrics.system.memory_limit; const memoryPercent = memoryLimit > 0 ? (memoryUsage / memoryLimit * 100).toFixed(1) : 0; document.getElementById('memory-usage').textContent = memoryPercent + '%'; } // 计算平均响应时间 if (metrics.histograms && metrics.histograms.http_request_duration) { const avgTime = metrics.histograms.http_request_duration.mean || 0; document.getElementById('response-time').textContent = avgTime.toFixed(2) + 'ms'; } } // 更新仪表盘告警 function updateDashboardAlerts(alerts) { document.getElementById('active-alerts').textContent = alerts.length; const recentAlertsEl = document.getElementById('recent-alerts'); if (alerts.length === 0) { recentAlertsEl.innerHTML = '
暂无告警
'; } else { recentAlertsEl.innerHTML = alerts.slice(0, 5).map(alert => `${alert.type}
${alert.message}
${formatTime(alert.timestamp)}
${getComponentName(name)}
${check.message || '运行正常'}
${check.duration ? check.duration.toFixed(3) + 's' : ''}
暂无错误记录
'; } else { errorsListEl.innerHTML = result.data.map(error => `${error.message}
文件: ${error.file || 'N/A'}:${error.line || 'N/A'}
时间: ${formatTime(error.timestamp)}
Trace ID: ${error.trace_id || 'N/A'}
未找到匹配的日志
'; } else { logsContainerEl.innerHTML = result.data.logs.map(log => `${log.message}
暂无告警
'; } else { alertsListEl.innerHTML = result.data.map(alert => `${alert.message}
时间: ${formatTime(alert.timestamp)}