/** * 页面渲染模块 * 包含各类型页面的渲染函数 */ // 渲染日志查看页面 function renderLogViewerPage(config) { return `

${config.title}

请选择日志文件
日志内容将在这里显示...
`; } // 渲染工具卡片页面 function renderToolsPage(config) { const renderToolCard = (tool, canDelete = false, isCustom = false) => `

${tool.name}

${tool.description}

${canDelete ? ` ` : ''}
`; const defaultCards = config.defaultTools.map(t => renderToolCard(t, false, false)).join(''); const customCards = config.customTools.map(t => renderToolCard(t, true, true)).join(''); return `

${config.title}

默认工具

${defaultCards || '

暂无默认工具

'}

自定义工具

${customCards || '

暂无自定义工具,点击右上角添加

'}
`; } // 删除自定义工具 function deleteCustomTool(toolId) { window.showConfirm('确认删除', '确定要删除这个自定义工具吗?', () => { const config = window.tableConfigs['data-generate']; config.customTools = config.customTools.filter(t => t.id !== toolId); localStorage.setItem('customTools', JSON.stringify(config.customTools)); document.getElementById('page-content').innerHTML = renderToolsPage(config); }); } // 修改自定义工具 function editCustomTool(toolId) { const config = window.tableConfigs['data-generate']; const tool = config.customTools.find(t => t.id === toolId); if (tool) { localStorage.setItem('editTool', JSON.stringify(tool)); window.location.href = 'custom-tool-create.html?edit=true'; } } // 显示创建工具弹窗 function showCreateToolModal() { window.location.href = 'custom-tool-create.html'; } // 跳转到工具页面 function navigateToTool(toolId, url, isCustom = false) { if (isCustom && url) { if (url.startsWith('http')) { window.open(url, '_blank'); } else { window.location.href = url; } } else if (toolId === 'data-generate') { window.location.href = 'data-generate.html'; } else if (toolId === 'json2jsonl') { window.showMessage('提示', 'JSON转JSONL 工具开发中...', 'info'); } else if (toolId === 'md-convert') { window.showMessage('提示', '转换Markdown 工具开发中...', 'info'); } else { window.showMessage('提示', `${toolId} 功能开发中...`, 'info'); } } // 渲染配置页面(硬件监控) function renderConfigPage(config, data) { return `

${config.title}

刷新频率:

CPU 使用率

4 核心

0%
核心1
0%
核心2
0%
核心3
0%
核心4
0%

内存使用

总计: 16 GB

0%
已用
0 GB
可用
0 GB
缓存
0 GB

磁盘使用

SSD 512 GB

0%
已用空间
0 GB
可用空间
0 GB
读写速度
0 MB/s

GPU监控

多GPU并行监控

网络流量

实时带宽使用

下载速度
0 MB/s
上传速度
0 MB/s
总流入: 0 GB 总流出: 0 GB

系统信息

服务器状态

操作系统 Ubuntu 22.04 LTS
运行时间 0 天 0 时 0 分
进程数 0
负载均值 0.00, 0.00, 0.00
`; } // 刷新间隔定时器 let refreshTimer = null; let currentRefreshInterval = 5000; // 刷新硬件信息 async function refreshHardwareInfo() { try { const response = await fetch(`${window.API_BASE}/system-info`); const result = await response.json(); if (result.code === 0 && result.data) { const data = result.data; // 更新CPU const cpu = data.cpu || {}; const cpuPercent = cpu.percent || 0; const cpuEl = document.getElementById('cpuPercent'); if (cpuEl) { cpuEl.textContent = cpuPercent + '%'; document.getElementById('cpuBar').style.width = cpuPercent + '%'; document.getElementById('cpuCores').textContent = (cpu.cores || 0) + ' 核心'; } // 更新内存 const mem = data.memory || {}; const memUsed = mem.used_gb || 0; const memTotal = mem.total_gb || 0; const memPercent = mem.percent || 0; document.getElementById('memoryPercent').textContent = memPercent + '%'; document.getElementById('memoryBar').style.width = memPercent + '%'; document.getElementById('memoryUsed').textContent = memUsed + ' GB'; document.getElementById('memoryAvailable').textContent = (mem.available_gb || 0) + ' GB'; document.getElementById('memoryCached').textContent = (mem.cached_gb || 0) + ' GB'; // 更新磁盘 const disk = data.disk || {}; const diskUsed = disk.used_gb || 0; const diskTotal = disk.total_gb || 0; const diskPercent = disk.percent || 0; document.getElementById('diskPercent').textContent = diskPercent + '%'; document.getElementById('diskBar').style.width = diskPercent + '%'; document.getElementById('diskUsed').textContent = diskUsed + ' GB'; document.getElementById('diskAvailable').textContent = (diskTotal - diskUsed) + ' GB'; // 更新网络 const net = data.network || {}; document.getElementById('totalDownload').textContent = (net.download_mb || 0) + ' GB'; document.getElementById('totalUpload').textContent = (net.upload_mb || 0) + ' GB'; // 更新系统信息 const sys = data.system || {}; const uptime = sys.uptime_seconds || 0; const days = Math.floor(uptime / 86400); const hours = Math.floor((uptime % 86400) / 3600); const mins = Math.floor((uptime % 3600) / 60); document.getElementById('uptime').textContent = days + ' 天 ' + hours + ' 时 ' + mins + ' 分'; document.getElementById('processCount').textContent = sys.process_count || 0; // 更新GPU信息 updateGPUInfo(data.gpu || []); } } catch (error) { console.error('获取系统信息失败:', error); useMockData(); } } // 使用模拟数据 function useMockData() { const cpuUsage = Math.floor(Math.random() * 30) + 20; document.getElementById('cpuPercent').textContent = cpuUsage + '%'; document.getElementById('cpuBar').style.width = cpuUsage + '%'; document.getElementById('core1').textContent = Math.floor(Math.random() * 40 + 20) + '%'; document.getElementById('core2').textContent = Math.floor(Math.random() * 40 + 15) + '%'; document.getElementById('core3').textContent = Math.floor(Math.random() * 40 + 25) + '%'; document.getElementById('core4').textContent = Math.floor(Math.random() * 40 + 10) + '%'; const memUsed = (Math.random() * 4 + 6).toFixed(1); const memTotal = 16; const memPercent = Math.floor((memUsed / memTotal) * 100); document.getElementById('memoryPercent').textContent = memPercent + '%'; document.getElementById('memoryBar').style.width = memPercent + '%'; document.getElementById('memoryUsed').textContent = memUsed + ' GB'; document.getElementById('memoryAvailable').textContent = (memTotal - memUsed).toFixed(1) + ' GB'; document.getElementById('memoryCached').textContent = (Math.random() * 3 + 1).toFixed(1) + ' GB'; const diskUsed = Math.floor(Math.random() * 100 + 150); const diskTotal = 512; const diskPercent = Math.floor((diskUsed / diskTotal) * 100); document.getElementById('diskPercent').textContent = diskPercent + '%'; document.getElementById('diskBar').style.width = diskPercent + '%'; document.getElementById('diskUsed').textContent = diskUsed + ' GB'; document.getElementById('diskAvailable').textContent = (diskTotal - diskUsed) + ' GB'; document.getElementById('diskSpeed').textContent = (Math.random() * 500 + 100).toFixed(0) + ' MB/s'; updateGPUInfo(); document.getElementById('downloadSpeed').textContent = (Math.random() * 100 + 10).toFixed(1) + ' MB/s'; document.getElementById('uploadSpeed').textContent = (Math.random() * 50 + 5).toFixed(1) + ' MB/s'; document.getElementById('totalDownload').textContent = (Math.random() * 500 + 100).toFixed(1) + ' GB'; document.getElementById('totalUpload').textContent = (Math.random() * 200 + 50).toFixed(1) + ' GB'; const days = Math.floor(Math.random() * 30); const hours = Math.floor(Math.random() * 24); const mins = Math.floor(Math.random() * 60); document.getElementById('uptime').textContent = days + ' 天 ' + hours + ' 时 ' + mins + ' 分'; document.getElementById('processCount').textContent = Math.floor(Math.random() * 200 + 100); document.getElementById('loadAvg').textContent = (Math.random() * 2).toFixed(2) + ', ' + (Math.random() * 1.5).toFixed(2) + ', ' + (Math.random() * 1).toFixed(2); } // GPU配置 const GPU_COUNT = 4; const gpuConfigs = [ { name: 'NVIDIA RTX 3090', memory: 24 }, { name: 'NVIDIA RTX 4090', memory: 24 }, { name: 'NVIDIA A100', memory: 80 }, { name: 'NVIDIA V100', memory: 32 }, { name: 'NVIDIA T4', memory: 16 }, { name: 'NVIDIA L40S', memory: 48 }, { name: 'NVIDIA H100', memory: 80 }, { name: 'NVIDIA RTX 4080', memory: 16 } ]; // 初始化GPU列表 async function initGPUList() { try { const response = await fetch(`${window.API_BASE}/system-info`); const result = await response.json(); const gpuData = (result.data && result.data.gpu) || []; updateGPUInfo(gpuData); } catch (error) { console.error('初始化GPU列表失败:', error); useMockGPUData(); } } // 更新GPU信息 function updateGPUInfo(gpuData) { if (gpuData && gpuData.length > 0) { const gpuCount = gpuData.length; document.getElementById('gpuCount').textContent = `检测到 ${gpuCount} 块 GPU`; let totalUsedMemory = 0; let totalMemory = 0; const gpuList = document.getElementById('gpuList'); if (gpuList) { let gpuCardsHTML = ''; for (let i = 0; i < gpuCount; i++) { const gpu = gpuData[i]; totalUsedMemory += gpu.memory_used_gb; totalMemory += gpu.memory_total_gb; gpuCardsHTML += `
${gpu.name}
PCIe
${gpu.gpu_percent}%
显存
${gpu.memory_used_gb}/${gpu.memory_total_gb} GB
温度
${gpu.temperature}°C
功耗
${gpu.power_w} W
Fan
${gpu.fan_speed || 0}%
Clock: ${gpu.clock_mhz || 0} MHz
Driver: ${gpu.driver_version || '-'}
`; } // 如果GPU数量不足4个,补充显示总显存 if (gpuCount < 4) { gpuCardsHTML += `
总显存使用
${totalUsedMemory}/${totalMemory} GB
`; } gpuList.innerHTML = gpuCardsHTML; } return; } useMockGPUData(); } // 使用模拟GPU数据 function useMockGPUData() { const gpuCount = Math.min(GPU_COUNT, 8); let totalUsedMemory = 0; let totalMemory = 0; const gpuList = document.getElementById('gpuList'); if (gpuList) { let gpuCardsHTML = ''; for (let i = 0; i < gpuCount; i++) { const config = gpuConfigs[i % gpuConfigs.length]; const gpuUsage = Math.floor(Math.random() * 60 + 20); const memUsed = (Math.random() * config.memory * 0.7 + config.memory * 0.1).toFixed(1); const temp = Math.floor(Math.random() * 30 + 40); const power = Math.floor(Math.random() * 150 + 100); const fan = Math.floor(gpuUsage + Math.random() * 10); totalUsedMemory += parseFloat(memUsed); totalMemory += config.memory; gpuCardsHTML += `
${config.name}
PCIe ${Math.floor(Math.random() * 4 + 1)}:00.0
${gpuUsage}%
显存
${parseFloat(memUsed).toFixed(1)}/${config.memory} GB
温度
${temp}°C
功耗
${power} W
Fan
${fan}%
`; } gpuList.innerHTML = gpuCardsHTML; document.getElementById('gpuCount').textContent = `检测到 ${gpuCount} 块 GPU`; } const gpuTotalMem = document.getElementById('gpuTotalMemory'); if (gpuTotalMem) { gpuTotalMem.textContent = `${totalUsedMemory.toFixed(1)}/${totalMemory} GB`; } } // 启动硬件监控自动刷新 function startRefreshTimer() { if (refreshTimer) { clearInterval(refreshTimer); } refreshTimer = setInterval(refreshHardwareInfo, currentRefreshInterval); } // 改变刷新频率 function changeRefreshRate() { const select = document.getElementById('refreshInterval'); currentRefreshInterval = parseInt(select.value); startRefreshTimer(); } // 保存配置 function saveConfig() { window.showMessage('提示', '配置保存功能开发中...', 'info'); } // 导出页面渲染模块 window.PageRenderer = { renderLogViewerPage, renderToolsPage, renderConfigPage, refreshHardwareInfo, useMockData, initGPUList, updateGPUInfo, useMockGPUData, startRefreshTimer, changeRefreshRate, saveConfig };