diff --git a/CHANGELOG.md b/CHANGELOG.md index d347486..65bc545 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # 更新日志 +## 2026-10-26 → ✨ 流量 / CPU / 内存 三图并列的「性能趋势」 + +- ✨ 概览原流量卡改为**性能趋势**:流量、CPU、内存三张卡片**并排一行**,共用一套时间口径(今天 / 昨天 / 近 30 天 / 近 12 个月 + 任意日期查询) +- ✨ CPU 卡片:当前占整机 %、均值、峰值、核数;纵轴按峰值自适应(不超过 100%) +- ✨ 内存卡片:当前合计、均值、峰值,纵轴按峰值自适应 +- 🔧 小时账本扩展为同时记录 `rx/tx` 字节、CPU 采样(sum/count)、内存(sum/max/count),旧数据自动兼容 +- 🔧 `lineChart` 抽成通用函数(多序列、面积填充、虚线、峰值标注、自适应 x 轴刻度) +- 🔧 `/api/traffic` 返回 `avg`、`peak`、`now.{cpu,mem}`、`cores`,月/年口径按样本数加权平均 + ## 2026-10-26 → ✨ 运行的容器显示已运行时长 - ✨ 容器列表每行副标题改为「项目: xxx · 已运行 1天4小时」(原来是 Docker 的英文状态 `Up 29 hours`) diff --git a/README.md b/README.md index 7f50279..eb008f0 100644 --- a/README.md +++ b/README.md @@ -52,7 +52,7 @@ bash install.sh | 页面 | 内容 | |---|---| -| 概览(默认) | 纯仪表盘:应用总数/异常/注意/资源/运行容器统计条、**实时流量图表**(今天/昨天/近30天/近12个月 + 查某一天,按小时记账落盘)、**CPU 与内存占用 Top 5**、优化建议、**需要关注的应用**(仅异常/注意,最多 6 个,卡片可直接启停)、最近事件;右上「查看全部应用」进入完整列表 | +| 概览(默认) | 纯仪表盘:应用总数/异常/注意/资源/运行容器统计条、**性能趋势三图并列**(流量 / CPU / 内存,今天·昨天·近30天·近12个月 + 查某一天,按小时记账落盘)、**CPU 与内存占用 Top 5**、优化建议、**需要关注的应用**(仅异常/注意,最多 6 个,卡片可直接启停)、最近事件;右上「查看全部应用」进入完整列表 | | 全部应用 | 完整应用卡片列表(50 个):筛选(全部·有问题·已停止·高占用)、排序、状态胶囊、CPU/内存/网络、端口、已运行时长、可用率、重启次数、卡片内启停与详情;顶部可返回概览 | | 应用详情 | 应用级指标 + 全部容器表格(状态/健康/CPU/内存/网络/重启/运行时长/单容器操作)+ 三个标签页:容器、聚合日志(多容器合并、按容器名加前缀)、事件;顶部可整应用启停重启 | | 容器 | 只列运行中容器,可排序(CPU/内存/网络/磁盘IO/进程数/名称)、搜索;行内启停/重启/删除/详情/打开端口 | diff --git a/public/app.js b/public/app.js index 7f3de72..1caaad5 100644 --- a/public/app.js +++ b/public/app.js @@ -159,32 +159,32 @@ function avatar(name) { return `${esc(label)}`; } -/* ---------------- 流量图表 ---------------- */ -function tfChart(points, unit) { - const W = 640, H = 150, PL = 6, PR = 6, PT = 12, PB = 22; +/* ---------------- 趋势图表 ---------------- */ +function lineChart(points, series, unit, opts) { + opts = opts || {}; + const W = 360, H = 118, PL = 4, PR = 4, PT = 12, PB = 17; const iw = W - PL - PR, ih = H - PT - PB; const n = Math.max(points.length, 2); - const maxRx = Math.max(...points.map(p => p.rx || 0), 1); - const maxTx = Math.max(...points.map(p => p.tx || 0), 1); - const max = Math.max(maxRx, maxTx); + const vals = points.flatMap(p => series.map(sr => p[sr.key] || 0)); + const max = opts.max || Math.max(...vals, 1) || 1; const xAt = (i) => PL + (iw * i) / (n - 1); - const yAt = (v) => PT + ih - (v / max) * ih; - const line = (key) => points.map((p, i) => `${xAt(i).toFixed(1)},${yAt(p[key] || 0).toFixed(1)}`).join(' '); - const areaRx = `${PL},${PT + ih} ${line('rx')} ${PL + iw},${PT + ih}`; - const step = unit === 'hour' ? 4 : unit === 'day' ? 5 : 1; - const ticks = points.map((p, i) => ({ i, l: p.label })) - .filter(({ i }) => unit === 'hour' ? i % 4 === 0 : unit === 'day' ? i % 5 === 0 : true); + const yAt = (v) => PT + ih - (Math.min(v, max) / max) * ih; + const gap = unit === 'hour' ? 4 : unit === 'day' ? 5 : 3; + const ticks = points.map((p, i) => ({ i, l: p.label })).filter(({ i }) => i % gap === 0); const gridY = [0, 0.5, 1].map(f => PT + ih * f); + const fmtMax = opts.fmt || ((v) => String(Math.round(v))); return ` - - - + ${series.map((sr, si) => ` + + `).join('')} ${gridY.map(y => ``).join('')} - - - - ${ticks.map(({ i, l }) => `${esc(l)}`).join('')} - 峰值 ${fmtBytes(max)} + ${series.map((sr, si) => { + const pts = points.map((p, i) => `${xAt(i).toFixed(1)},${yAt(p[sr.key] || 0).toFixed(1)}`).join(' '); + const area = sr.area ? `` : ''; + return `${area}`; + }).join('')} + ${ticks.map(({ i, l }) => `${esc(l)}`).join('')} + 峰值 ${fmtMax(max)} `; } @@ -307,26 +307,38 @@ async function renderApps(silent) { } else { const tfPts = (tf && tf.points) || []; const tfTotal = (tf && tf.total) || { rx: 0, tx: 0 }; - const tfNow = (tf && tf.now) || { rx: 0, tx: 0 }; + const tfNow = (tf && tf.now) || { rx: 0, tx: 0, cpu: 0, mem: 0 }; + const tfPeak = (tf && tf.peak) || { mem: 0, cpu: 0 }; + const tfAvg = (tf && tf.avg) || { cpu: 0, mem: 0 }; const tfTabs = [['today', '今天'], ['yesterday', '昨天'], ['month', '近 30 天'], ['year', '近 12 个月']]; + const noData = '
暂无数据
'; const tfHtml = ` -
-
- 实时流量 - ↓ ${fmtRate(tfNow.rx)} · ↑ ${fmtRate(tfNow.tx)} +
+
+ 性能趋势 + 3 秒采样 · 按小时汇总${tf && tf.coverage && tf.coverage.from ? ` · 自 ${esc(tf.coverage.from)} 起` : ''}
${tfTabs.map(([k, l]) => ``).join('')} - +
-
- 总流量 ${fmtBytes(tfTotal.rx + tfTotal.tx)} - 下行 ${fmtBytes(tfTotal.rx)} - 上行 ${fmtBytes(tfTotal.tx)} - ${tf ? esc(tf.label) : ''}${tf && tf.coverage && tf.coverage.from ? ` · 自 ${esc(tf.coverage.from)} 开始记录` : ''} +
+
+
流量↓ ${fmtRate(tfNow.rx)} · ↑ ${fmtRate(tfNow.tx)}
+ ${tfPts.length ? lineChart(tfPts, [{ key: 'rx', color: '#1a73e8', area: true }, { key: 'tx', color: '#f79009', dash: true }], tf.unit, { fmt: fmtBytes }) : noData} +
总 ${fmtBytes(tfTotal.rx + tfTotal.tx)} · 下 ${fmtBytes(tfTotal.rx)} · 上 ${fmtBytes(tfTotal.tx)}
+
+
+
CPU${fmtPct(tfNow.cpu)}
+ ${tfPts.length ? lineChart(tfPts, [{ key: 'cpu', color: '#12b76a', area: true }], tf.unit, { max: Math.min(100, Math.max(2, (tfPeak.cpu || 0) * 1.25)), fmt: (v) => v.toFixed(1) + '%' }) : noData} +
均值 ${(tfAvg.cpu || 0).toFixed(1)}% · 峰值 ${(tfPeak.cpu || 0).toFixed(1)}% · ${tf && tf.cores ? tf.cores + ' 核' : ''}
+
+
+
内存${fmtBytes(tfNow.mem)}
+ ${tfPts.length ? lineChart(tfPts, [{ key: 'mem', color: '#7a5af8', area: true }], tf.unit, { fmt: fmtBytes }) : noData} +
均值 ${fmtBytes(tfAvg.mem || 0)} · 峰值 ${fmtBytes(tfPeak.mem || 0)}
+
- ${tfPts.length ? tfChart(tfPts, tf.unit) : '
暂无流量数据
'} -
下行(蓝实线)上行(橙虚线)
`; $('#view').innerHTML = `
diff --git a/public/style.css b/public/style.css index 305e753..a54ae94 100644 --- a/public/style.css +++ b/public/style.css @@ -289,22 +289,23 @@ table.tb th.num{text-align:right} .empty.sm{padding:16px;font-size:12px;text-align:center;color:#8b95a5} .empty.ok-empty{padding:22px;text-align:center;color:#12a15f;background:#f3fbf6;border:1px dashed #cdebd9;border-radius:12px;font-size:13px} -/* 流量卡片 */ -.traffic-box{background:var(--panel);border:1px solid var(--line);border-radius:12px;box-shadow:var(--shadow);margin-bottom:14px;padding:12px 14px 8px} -.traffic-head{display:flex;align-items:center;gap:10px;flex-wrap:wrap} -.traffic-head b{font-size:13.5px;color:#26303f} -.tf-now{font-size:12px;color:#3d4859;font-weight:600;font-variant-numeric:tabular-nums} +/* 性能趋势(三图一行) */ +.trend-box{background:var(--panel);border:1px solid var(--line);border-radius:12px;box-shadow:var(--shadow);margin-bottom:14px;padding:11px 13px 9px} +.trend-head{display:flex;align-items:center;gap:10px;flex-wrap:wrap;margin-bottom:6px} +.trend-head b{font-size:13.5px;color:#26303f} +.trend-head .muted{font-size:11.5px} .tf-tabs{margin-left:auto;display:flex;align-items:center;gap:5px;flex-wrap:wrap} .tf-tab{font:inherit;font-size:12px;padding:3px 9px;border-radius:7px;border:1px solid var(--line);background:#fbfcfe;color:#5b6a85;cursor:pointer} .tf-tab:hover{background:#f2f6fd} .tf-tab.on{background:#eaf1fd;border-color:#bcd4f7;color:#1a5fd0;font-weight:600} .tf-date{font:inherit;font-size:12px;padding:2px 6px;border:1px solid var(--line);border-radius:7px;color:#5b6a85;background:#fbfcfe} -.tf-sum{display:flex;gap:16px;flex-wrap:wrap;align-items:baseline;margin:8px 0 2px;font-size:12px;color:#78839a} -.tf-sum b{color:#26303f;font-size:13px} -.tf-sum b.down{color:#1a73e8} -.tf-sum b.up{color:#f79009} -.tf-chart{width:100%;height:150px;display:block;margin-top:4px} -.tf-legend{display:flex;align-items:center;gap:6px;font-size:11.5px;color:#78839a;margin:2px 0 4px} -.tf-legend .lg{width:14px;height:3px;border-radius:2px;display:inline-block} -.tf-legend .lg.down{background:#1a73e8} -.tf-legend .lg.up{background:#f79009} +.trend-grid{display:grid;grid-template-columns:repeat(3,minmax(0,1fr));gap:10px} +@media(max-width:980px){.trend-grid{grid-template-columns:1fr}} +.trend-card{border:1px solid var(--line);border-radius:10px;padding:8px 9px 6px;background:#fcfdff;min-width:0} +.tc-head{display:flex;align-items:baseline;justify-content:space-between;gap:8px;margin-bottom:2px} +.tc-title{font-size:12.5px;font-weight:600;color:#3d4859} +.tc-now{font-size:11.5px;color:#1a73e8;font-weight:600;font-variant-numeric:tabular-nums;white-space:nowrap;overflow:hidden;text-overflow:ellipsis} +.trend-card:nth-child(2) .tc-now{color:#12a15f} +.trend-card:nth-child(3) .tc-now{color:#7a5af8} +.tc-foot{font-size:11px;color:#8b95a5;margin-top:2px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis} +.tf-chart{width:100%;height:112px;display:block} diff --git a/server/index.js b/server/index.js index f13a7d8..f522fea 100644 --- a/server/index.js +++ b/server/index.js @@ -105,7 +105,8 @@ const localYmd = (d = new Date(), offsetDays = 0) => { const t = new Date(d.getTime() + 8 * 3600e3 + offsetDays * 86400e3); return t.toISOString().slice(0, 10); }; -const emptyHours = () => Array.from({ length: 24 }, () => ({ rx: 0, tx: 0 })); +const emptyHour = () => ({ rx: 0, tx: 0, cpuSum: 0, cpuN: 0, memSum: 0, memMax: 0, memN: 0 }); +const emptyHours = () => Array.from({ length: 24 }, () => emptyHour()); async function loadTrafficDay(date) { if (trafficDays.has(date)) return trafficDays.get(date); @@ -114,7 +115,14 @@ async function loadTrafficDay(date) { const raw = await fsp.readFile(path.join(TRAFFIC_DIR, `${date}.json`), 'utf8'); const j = JSON.parse(raw); if (j && Array.isArray(j.hours)) { - for (let i = 0; i < 24; i++) data.hours[i] = { rx: j.hours[i]?.rx || 0, tx: j.hours[i]?.tx || 0 }; + for (let i = 0; i < 24; i++) { + const h = j.hours[i] || {}; + data.hours[i] = { + rx: h.rx || 0, tx: h.tx || 0, + cpuSum: h.cpuSum || 0, cpuN: h.cpuN || 0, + memSum: h.memSum || 0, memMax: h.memMax || 0, memN: h.memN || 0, + }; + } } } catch (e) {} if (date !== localYmd()) trafficDays.set(date, data); // 历史天可长缓存 @@ -130,11 +138,13 @@ async function trafficToday() { return trafficDay; } -function addTraffic(rxBytes, txBytes) { +function addTraffic(rxBytes, txBytes, cpuPct, memBytes) { if (!trafficDay) return; const h = new Date().getHours(); const b = trafficDay.hours[h]; b.rx += Math.max(0, rxBytes); b.tx += Math.max(0, txBytes); + if (Number.isFinite(cpuPct)) { b.cpuSum += cpuPct; b.cpuN += 1; } + if (Number.isFinite(memBytes)) { b.memSum += memBytes; b.memMax = Math.max(b.memMax, memBytes); b.memN += 1; } trafficDirty = true; } @@ -200,7 +210,10 @@ async function sampleOnce() { sumRx += st.dRx || 0; sumTx += st.dTx || 0; } - addTraffic(sumRx, sumTx); + const coresAll = os.cpus().length || 1; + let sumCpu = 0, sumMem = 0; + for (const st of stats.values()) { sumCpu += st.cpu || 0; sumMem += st.mem || 0; } + addTraffic(sumRx, sumTx, sumCpu / coresAll, sumMem); const rxNow = [...stats.values()].reduce((a, x) => a + (x.rxBps || 0), 0); const txNow = [...stats.values()].reduce((a, x) => a + (x.txBps || 0), 0); rtTraffic.push({ t: Date.now(), rx: rxNow, tx: txNow }); @@ -527,7 +540,20 @@ app.get('/api/traffic', async (req, res) => { try { const range = String(req.query.range || 'today'); const today = localYmd(); - const sum = (arr, k) => arr.reduce((s, x) => s + (x[k] || 0), 0); + const agg = (rows) => rows.reduce((a, h) => ({ + rx: a.rx + (h.rx || 0), tx: a.tx + (h.tx || 0), + cpuSum: a.cpuSum + (h.cpuSum || 0), cpuN: a.cpuN + (h.cpuN || 0), + memSum: a.memSum + (h.memSum || 0), memN: a.memN + (h.memN || 0), + memMax: Math.max(a.memMax, h.memMax || 0), + }), { rx: 0, tx: 0, cpuSum: 0, cpuN: 0, memSum: 0, memN: 0, memMax: 0 }); + const shape = (a) => ({ + rx: a.rx, tx: a.tx, + cpu: a.cpuN ? a.cpuSum / a.cpuN : 0, + mem: a.memN ? a.memSum / a.memN : 0, + memMax: a.memMax, + }); + const sumOf = (pts, k) => pts.reduce((s, x) => s + (x[k] || 0), 0); + let points = [], label = '', unit = 'hour', from = today, to = today; if (range === 'today' || range === 'yesterday' || range === 'day') { @@ -536,31 +562,30 @@ app.get('/api/traffic', async (req, res) => { : range === 'yesterday' ? localYmd(new Date(), -1) : today; if (!/^\d{4}-\d{2}-\d{2}$/.test(date)) return res.status(400).json({ error: '日期格式应为 YYYY-MM-DD' }); const day = date === today ? await trafficToday() : await loadTrafficDay(date); - points = day.hours.map((b, i) => ({ label: `${String(i).padStart(2, '0')}:00`, rx: b.rx, tx: b.tx })); + points = day.hours.map((h, i) => ({ label: `${String(i).padStart(2, '0')}:00`, ...shape(h) })); label = range === 'today' ? '今天' : range === 'yesterday' ? '昨天' : date; from = to = date; } else if (range === 'month') { for (let i = 29; i >= 0; i--) { const d = localYmd(new Date(), -i); const day = d === today ? await trafficToday() : await loadTrafficDay(d); - points.push({ label: d.slice(5), date: d, rx: sum(day.hours, 'rx'), tx: sum(day.hours, 'tx') }); + points.push({ label: d.slice(5), date: d, ...shape(agg(day.hours)) }); } label = '近 30 天'; unit = 'day'; from = localYmd(new Date(), -29); } else if (range === 'year') { - const now = new Date(); - const base = new Date(now.getTime() + 8 * 3600e3); + const base = new Date(Date.now() + 8 * 3600e3); for (let i = 11; i >= 0; i--) { const m = new Date(Date.UTC(base.getUTCFullYear(), base.getUTCMonth() - i, 1)); const y = m.getUTCFullYear(), mo = m.getUTCMonth() + 1; const daysInMonth = new Date(Date.UTC(y, mo, 0)).getUTCDate(); - let rx = 0, tx = 0; + const rows = []; for (let d = 1; d <= daysInMonth; d++) { const key = `${y}-${String(mo).padStart(2, '0')}-${String(d).padStart(2, '0')}`; if (key > today) break; const day = key === today ? await trafficToday() : await loadTrafficDay(key); - rx += sum(day.hours, 'rx'); tx += sum(day.hours, 'tx'); + rows.push(...day.hours); } - points.push({ label: `${String(mo).padStart(2, '0')}月`, month: `${y}-${String(mo).padStart(2, '0')}`, rx, tx }); + points.push({ label: `${String(mo).padStart(2, '0')}月`, month: `${y}-${String(mo).padStart(2, '0')}`, ...shape(agg(rows)) }); } label = '近 12 个月'; unit = 'month'; from = localYmd(new Date(), -364); } else { @@ -568,12 +593,16 @@ app.get('/api/traffic', async (req, res) => { } const cov = await trafficCoverage(); - const nowRx = rtTraffic.length ? rtTraffic[rtTraffic.length - 1].rx : 0; - const nowTx = rtTraffic.length ? rtTraffic[rtTraffic.length - 1].tx : 0; + const cores = os.cpus().length || 1; + let nowCpu = 0, nowMem = 0, nowRx = 0, nowTx = 0; + for (const st of stats.values()) { nowCpu += st.cpu || 0; nowMem += st.mem || 0; nowRx += st.rxBps || 0; nowTx += st.txBps || 0; } res.json({ range, label, unit, from, to, points, - total: { rx: sum(points, 'rx'), tx: sum(points, 'tx') }, - now: { rx: nowRx, tx: nowTx }, + total: { rx: sumOf(points, 'rx'), tx: sumOf(points, 'tx') }, + avg: { cpu: points.length ? sumOf(points, 'cpu') / points.filter(p => p.cpu).length || 0 : 0, mem: sumOf(points, 'mem') / Math.max(1, points.filter(p => p.mem).length) }, + peak: { mem: Math.max(0, ...points.map(p => p.memMax || 0)), cpu: Math.max(0, ...points.map(p => p.cpu || 0)) }, + now: { rx: nowRx, tx: nowTx, cpu: nowCpu / cores, mem: nowMem }, + cores, realtime: rtTraffic.slice(-120), coverage: { from: cov.from, days: cov.days, today }, });