feat: 运行的容器显示已运行时长

This commit is contained in:
jinghui committed 2026-10-26 18:32:33 +08:00
1 parent 2eb235c704
commit c31379c8b0
4 files changed
+25 -2

No files matched your search

+7
View File
@@ -1,5 +1,12 @@
# 更新日志
## 2026-10-26 → ✨ 运行的容器显示已运行时长
- ✨ 容器列表每行副标题改为「项目: xxx · 已运行 1天4小时」(原来是 Docker 的英文状态 `Up 29 hours`)
- ✨ 超过 1 分钟才显示时长,刚启动显示「刚刚启动」,停止的显示「已停止」
- 🔧 时长用紧凑格式(`1天4小时` / `38分钟`)并加宽名称列,61 行里仅 1 行仍因项目名过长省略(有 title 提示)
- 🔧 镜像名是 sha256 摘要时只显示前 12 位,避免撑爆布局
## 2026-10-26 → ✨ 概览新增实时流量图表(今天/昨天/30 天/12 个月/查某天)
- ✨ 按小时记账:每 3 秒采样把各容器网络计数器差量累加到当天小时桶,15 秒落盘 `data/traffic/YYYY-MM-DD.json`,重启不丢
+15 -1
View File
@@ -47,6 +47,20 @@ const fmtDur = (ms) => {
if (m > 0) return `${m} 分钟`;
return '不到 1 分钟';
};
const shortImg = (img) => {
const s = String(img || '');
if (/^sha256:/.test(s)) return s.slice(7, 19);
const last = s.split('/').pop();
return last.length > 22 ? last.slice(0, 22) + '…' : last;
};
const fmtDurCompact = (ms) => {
if (ms === null || ms === undefined || !isFinite(ms) || ms < 0) return '—';
const s = Math.floor(ms / 1000), d = Math.floor(s / 86400), h = Math.floor((s % 86400) / 3600), m = Math.floor((s % 3600) / 60);
if (d > 0) return `${d}天${h ? h + '小时' : ''}`;
if (h > 0) return `${h}小时${m ? m + '分' : ''}`;
if (m > 0) return `${m}分钟`;
return '1分钟内';
};
const ago = (iso) => {
if (!iso) return '-';
const d = (Date.now() - new Date(iso).getTime()) / 1000;
@@ -508,7 +522,7 @@ async function renderContainers(silent, mode = 'running') {
return `<div class="row${c.state === 'running' ? '' : ' off'}" data-id="${c.id}">
<div class="wicon">${icon('box')}<span class="dot ${c.state === 'running' ? 'up' : ''}"></span></div>
<div class="ident" title="${esc(c.name)}"><div class="nm">${esc(c.name)}</div>
<div class="pj" title="${esc((c.project ? '项目: ' + c.project : c.image) + ' · ' + c.status)}">${c.project ? '项目: ' + esc(c.project) : esc(c.image.split('/').pop())} · ${esc(c.status)}${c.restarts ? ` · 重启${c.restarts}` : ''}</div></div>
<div class="pj" title="${esc((c.project ? '项目: ' + c.project : c.image) + ' · ' + c.status)}">${c.project ? '项目: ' + esc(c.project) : esc(shortImg(c.image))} · ${c.state === 'running' ? (c.runMs === null || c.runMs === undefined ? '运行中' : c.runMs < 60000 ? '刚刚启动' : '已运行 ' + fmtDurCompact(c.runMs)) : '已停止'}${c.restarts ? ` · 重启${c.restarts}` : ''}</div></div>
<div class="metrics">
<div class="metric"><div class="k">CPU</div><div class="v">${fmtPct(s.cpu)}</div>
<div class="bar ${cpuCls}"><i style="width:${Math.min(100, s.cpu / Math.max(1, h.cpus))}%"></i></div></div>
+1 -1
View File
@@ -68,7 +68,7 @@ a{color:var(--blue);text-decoration:none;cursor:pointer}
.dot{position:absolute;right:-2px;bottom:-2px;width:9px;height:9px;border-radius:50%;border:2px solid #fff;background:#c3cad6}
.dot.up{background:var(--blue)}
.dot.err{background:var(--red)}
.ident{min-width:170px;flex:0 0 200px;overflow:hidden}
.ident{min-width:190px;flex:0 0 236px;overflow:hidden}
.ident .nm{font-weight:600;font-size:14px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.ident .pj{font-size:12px;color:var(--muted);margin-top:2px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}
.metrics{display:flex;gap:16px;flex:1;flex-wrap:nowrap;align-items:center;overflow:hidden;min-width:0}
+2
View File
@@ -864,6 +864,8 @@ app.get('/api/containers', async (req, res) => {
image: c.Image, state: c.State, status: c.Status, created: c.Created,
project: projectOf(c.Labels), service: svcOf(c.Labels),
health: m.health || '', restarts: m.restarts || 0,
startedAt: m.startedAt || '',
runMs: c.State === 'running' && m.startedAt ? Math.max(0, Date.now() - Date.parse(m.startedAt)) : null,
ports: (c.Ports || []).map(p => ({ ip: p.IP, private: p.PrivatePort, public: p.PublicPort, type: p.Type })),
stats: {
cpu: st.cpu ?? 0, mem: st.mem ?? 0, memLimit: st.memLimit ?? 0, memPct: st.memPct ?? 0,