diff --git a/CHANGELOG.md b/CHANGELOG.md index 1edd7ee..1130f46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # 更新日志 +## 2026-10-26 → ✨ 停止的容器支持批量删除 + +- ✨ 停止的容器页每行加复选框 + 顶部批量条:全选、已选数量、删除选中(按钮显示「删除选中 N 个」) +- ✨ 数量显示为「已选 3 / 共 3」,只删容器不删数据卷(有提示文案) +- 🔧 新增 `POST /api/containers/batch-delete`,逐个校验状态:**运行中的容器自动跳过**并返回原因,单批最多 200 个 +- 🔧 删除后写一条事件(`批量删除容器 N 个`),切换页面自动清空勾选,3 秒轮询刷新也不丢勾选 +- ✅ 实测:造 3 个停止容器 → 勾选 3 个 → 删除 → 全部消失、无残留;对运行中容器调用返回「运行中,已跳过」,未被误删 + ## 2026-10-26 → ✨ 图表鼠标悬停显示数值 - ✨ 三张趋势图(流量/CPU/内存)支持悬停:显示该时间点标签与各序列具体数值(下行/上行/CPU/内存) diff --git a/README.md b/README.md index 5492376..e005607 100644 --- a/README.md +++ b/README.md @@ -108,7 +108,7 @@ bash install.sh | 全部应用 | 完整应用卡片列表(50 个):筛选(全部·有问题·已停止·高占用)、排序、状态胶囊、CPU/内存/网络、端口、已运行时长、可用率、重启次数、卡片内启停与详情;顶部可返回概览 | | 应用详情 | 应用级指标 + 全部容器表格(状态/健康/CPU/内存/网络/重启/运行时长/单容器操作)+ 三个标签页:容器、聚合日志(多容器合并、按容器名加前缀)、事件;顶部可整应用启停重启 | | 容器 | 只列运行中容器,可排序(CPU/内存/网络/磁盘IO/进程数/名称)、搜索;行内启停/重启/删除/详情/打开端口 | -| 停止的容器 | 两个部分:**有镜像但未启动的应用**(compose 项目存在、本地有镜像、但一个容器都没有,可一键 `docker compose up -d`)+ 已停止/退出的容器(只保留启动/删除/详情) | +| 停止的容器 | 两个部分:**有镜像但未启动的应用**(compose 项目存在、本地有镜像、但一个容器都没有,可一键 `docker compose up -d`)+ 已停止/退出的容器(启动/删除/详情,支持**复选框批量删除**,运行中的容器会被跳过) | | 本地镜像 | 按大小排序、删除、清理悬空镜像 | | 镜像仓库 | 拉取镜像(流式进度) | | 网络 | 名称/驱动/子网/网关/容器数 | diff --git a/public/app.js b/public/app.js index 4f70877..4f42810 100644 --- a/public/app.js +++ b/public/app.js @@ -4,7 +4,7 @@ const $ = (s, r = document) => r.querySelector(s); const $$ = (s, r = document) => [...r.querySelectorAll(s)]; let serverBuild = null; -const state = { view: 'apps', filter: 'all', sort: 'severity', order: 'desc', q: '', timer: null, current: null, currentApp: null, tab: 'stat', appTab: 'containers', insightOpen: false, traffic: { range: 'today', date: '' } }; +const state = { view: 'apps', filter: 'all', sort: 'severity', order: 'desc', q: '', timer: null, current: null, currentApp: null, tab: 'stat', appTab: 'containers', insightOpen: false, traffic: { range: 'today', date: '' }, sel: new Set() }; const REFRESH_KEY = 'dc.refreshMs'; state.refreshMs = Math.min(30000, Math.max(1000, Number(localStorage.getItem(REFRESH_KEY)) || 3000)); @@ -99,6 +99,7 @@ const SORTS = { stopped: [['name', '名称'], ['mem', '内存占用']], }; function go(view) { + if (state.view !== view && state.sel) state.sel.clear(); state.view = view; state.currentApp = null; const navView = view === 'apps-all' ? 'apps' : view; @@ -602,6 +603,7 @@ async function renderContainers(silent, mode = 'running') { const ports = c.ports.filter(p => p.public).slice(0, 2); const linkBtn = ports.length ? `${icon('link')}` : ''; return `
+ ${mode === 'stopped' ? `` : ''}
${icon('box')}
${esc(c.name)}
${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}` : ''}
@@ -642,7 +644,14 @@ async function renderContainers(silent, mode = 'running') {
已停止的容器${data.total} 个
` : ''; const label = mode === 'stopped' ? '已停止' : '运行中'; - $('#view').innerHTML = `${pendHtml}${html || `
${mode === 'stopped' ? '没有已停止的容器' : '没有正在运行的容器'}
`} + const batchHtml = (mode === 'stopped' && data.items.length) ? ` +
+ + 已选 ${[...state.sel].filter(id => data.items.some(c => c.id === id)).length} / ${data.items.length} + 只删容器,不删数据卷 + +
` : ''; + $('#view').innerHTML = `${pendHtml}${batchHtml}${html || `
${mode === 'stopped' ? '没有已停止的容器' : '没有正在运行的容器'}
`}
共 ${data.total} 个${label}容器 · 排序:${({ cpu: 'CPU', mem: '内存', net: '网络', io: '磁盘IO', pids: '进程数', name: '名称' })[state.sort] || state.sort} ${state.order === 'desc' ? '降序' : '升序'} · 每 ${state.refreshMs / 1000} 秒刷新${h.disk ? ` · /vol1 可用 ${fmtBytes(h.disk.free)}` : ''}
`; $$('#view [data-pend-cmd]').forEach(b => b.onclick = () => { @@ -659,8 +668,44 @@ async function renderContainers(silent, mode = 'running') { } catch (e) { toast(e.message, true); b.disabled = false; b.textContent = '启动'; } }); + if (mode === 'stopped') { + const ids = data.items.map(c => c.id); + [...state.sel].forEach(id => { if (!ids.includes(id)) state.sel.delete(id); }); + const syncSel = () => { + const n = $('#selCount'); if (n) n.textContent = state.sel.size; + const b = $('#batchDel'); if (b) { b.disabled = state.sel.size === 0; b.textContent = state.sel.size ? `删除选中 ${state.sel.size} 个` : '删除选中'; } + const all = $('#selAll'); if (all) all.checked = state.sel.size > 0 && state.sel.size === ids.length; + }; + $$('#view .rowsel').forEach(cb => { + cb.onclick = (e) => { e.stopPropagation(); if (cb.checked) state.sel.add(cb.dataset.id); else state.sel.delete(cb.dataset.id); syncSel(); }; + cb.parentElement.onclick = (e) => e.stopPropagation(); + }); + const selAll = $('#selAll'); + if (selAll) selAll.onclick = () => { + if (selAll.checked) ids.forEach(i => state.sel.add(i)); else state.sel.clear(); + $$('#view .rowsel').forEach(cb => { cb.checked = selAll.checked; }); + syncSel(); + }; + const bd = $('#batchDel'); + if (bd) bd.onclick = async () => { + const list = [...state.sel]; + if (!list.length) return; + if (!confirm(`确定删除选中的 ${list.length} 个容器?\n\n只删除容器,不删除数据卷。`)) return; + bd.disabled = true; bd.textContent = '删除中…'; + try { + const r = await api('/api/containers/batch-delete', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ ids: list }) }); + const fail = r.results.filter(x => !x.ok); + toast(fail.length ? `已删除 ${r.deleted} 个,失败 ${fail.length} 个` : `已删除 ${r.deleted} 个容器`); + state.sel.clear(); + setTimeout(render, 600); + } catch (e) { toast(e.message, true); bd.disabled = false; bd.textContent = '删除选中'; } + }; + syncSel(); + } + $$('#view .row').forEach(row => { row.addEventListener('click', async (e) => { + if (e.target.closest('.chk')) return; const btn = e.target.closest('[data-act]'); const id = row.dataset.id; if (!btn) return openDrawer(id); e.stopPropagation(); diff --git a/public/style.css b/public/style.css index 4c907d7..cdca5b8 100644 --- a/public/style.css +++ b/public/style.css @@ -323,3 +323,13 @@ table.tb th.num{text-align:right} .chart-tip .ct-row{display:flex;align-items:center;gap:6px;white-space:nowrap} .chart-tip .ct-row b{margin-left:auto;font-variant-numeric:tabular-nums} .chart-tip .ct-dot{width:6px;height:6px;border-radius:50%;display:inline-block} + +/* 批量删除 */ +.batch-bar{display:flex;align-items:center;gap:12px;background:#fff7f7;border:1px solid #f6d5d5;border-radius:10px;padding:8px 12px;margin-bottom:10px;font-size:12.5px;color:#6b4a4a} +.batch-bar .bsel{display:flex;align-items:center;gap:6px;cursor:pointer;font-weight:600;color:#8a3b3b} +.batch-bar .muted{font-size:11.5px;color:#9b7c7c} +.batch-bar b{color:#cf3b3b} +.batch-bar .btn{margin-left:auto} +.batch-bar .btn[disabled]{opacity:.5;cursor:not-allowed} +.chk{display:flex;align-items:center;margin-right:2px;flex-shrink:0} +.chk input{width:15px;height:15px;cursor:pointer;accent-color:#e5484d} diff --git a/server/index.js b/server/index.js index f522fea..a40fb66 100644 --- a/server/index.js +++ b/server/index.js @@ -881,6 +881,29 @@ const SORTERS = { pids: (a, b) => b.stats.pids - a.stats.pids, }; +app.post('/api/containers/batch-delete', async (req, res) => { + try { + const ids = Array.isArray(req.body && req.body.ids) ? req.body.ids.filter(x => typeof x === 'string') : []; + if (!ids.length) return res.status(400).json({ error: '未提供要删除的容器' }); + const results = []; + for (const id of ids.slice(0, 200)) { + try { + const c = docker.getContainer(id); + const info = await c.inspect(); + const name = strip((info.Name || '')); + if (info.State && info.State.Running) { results.push({ id, name, ok: false, error: '运行中,已跳过' }); continue; } + await c.remove({ v: false, force: false }); + results.push({ id, name, ok: true }); + } catch (e) { + results.push({ id, name: '', ok: false, error: e.message }); + } + } + const deleted = results.filter(r => r.ok).length; + pushEvent('info', null, `批量删除容器 ${deleted} 个`, `失败 ${results.length - deleted} 个`); + res.json({ ok: true, deleted, results }); + } catch (e) { res.status(500).json({ error: e.message }); } +}); + app.get('/api/containers', async (req, res) => { try { const all = req.query.all !== '0';