feat(library): 存储管理真实化——容量门禁、依赖保护与清理预览
build / build (push) Successful in 1m15s

为什么:存储页原来只有占用数字,保留/依赖保护/清理预览写"未接线";这些都不需要生产写权限,
按"未设置就不做决定"的口径实现成只读计算。

- internal/libraryretention:按保留数量/天数生成预览;生产生效、每库最近封存、每次生产预备份、
  在飞作业引用一律受保护;回收预览只算独占字节,共享 blob 不计。
- internal/libraryretention.CheckCapacity:预算未设置即不放行(无法证明装得下),超预算阻断;
  已并入 POST /library/syncs 的 gate 缺项。
- 新增 GET /api/v1/library/storage:真实归档用量 + 三类版本保护依据 + 策略 + 预览 + 容量,只读。
- 系统设置→存储管理改为读该接口:策略未设置时明确"全部保留、不生成候选",自动清理恒关闭。
- 配置项 LIBRARY_CAPACITY_BUDGET_BYTES / LIBRARY_RETENTION_VERSIONS / LIBRARY_RETENTION_DAYS(未设=0)。
This commit is contained in:
姚章浩
2026-10-28 15:04:44 +08:00
parent 12bb839d8a
commit f91359f881
13 changed files with 756 additions and 49 deletions
+128 -39
View File
@@ -1,29 +1,33 @@
import { useEffect, useState } from 'react'
import { fetchLibrarySnapshots, formatBytes, type LibrarySnapshotList } from './librarySnapshots'
import { fetchLibraryStorage, formatBytes, type LibraryStorage } from './librarySnapshots'
import './libraryStorage.css'
// 系统设置 → 存储管理:只读展示整库归档的真实口径。
// 保留策略、依赖保护、清理预览都没有实现,自动清理保持关闭——页面不能暗示可以删。
// 保留策略、依赖保护、清理预览都只是"如果清理会怎样",没有任何删除动作;
// 自动清理恒为关闭,策略未设置时不生成候选。
export function LibraryStorageSection() {
const [page, setPage] = useState<LibrarySnapshotList | null>(null)
const [data, setData] = useState<LibraryStorage | null>(null)
const [error, setError] = useState<string | null>(null)
const [loading, setLoading] = useState(true)
useEffect(() => {
const controller = new AbortController()
fetchLibrarySnapshots(controller.signal)
fetchLibraryStorage(controller.signal)
.then((value) => {
setPage(value)
setData(value)
setError(null)
})
.catch((loadError) => {
if ((loadError as Error).name === 'AbortError') return
setError(loadError instanceof Error ? loadError.message : '读取归档用量失败')
setError(loadError instanceof Error ? loadError.message : '读取存储用量失败')
})
.finally(() => setLoading(false))
return () => controller.abort()
}, [])
const plan = data?.plan
const policyConfigured = Boolean(plan?.policy_configured)
return (
<section className="library-storage" aria-label="存储管理">
<header>
@@ -37,43 +41,128 @@ export function LibraryStorageSection() {
<p>{error}</p>
</div>
) : null}
{page && !page.available ? <p className="library-storage-hint">{page.reason || '归档未接线'}</p> : null}
{page?.available ? (
<dl className="library-storage-grid">
<div>
<dt>版本数</dt>
<dd>{page.versions}</dd>
{data && !data.available ? <p className="library-storage-hint">{data.reason || '归档未接线'}</p> : null}
{data?.available ? (
<>
<dl className="library-storage-grid">
<div>
<dt>版本数</dt>
<dd>{data.versions.length}</dd>
</div>
<div>
<dt>被版本引用的去重内容</dt>
<dd>{formatBytes(data.content_bytes)}</dd>
</div>
<div>
<dt>blob 文件(表观)</dt>
<dd>{formatBytes(data.blob_file_bytes)}</dd>
</div>
<div>
<dt>blob 实际分配</dt>
<dd>{formatBytes(data.blob_allocated_bytes)}</dd>
</div>
<div>
<dt>未被任何版本引用</dt>
<dd>
{data.unreferenced_blobs} 个 / {formatBytes(data.unreferenced_bytes)}
</dd>
</div>
<div>
<dt>清单文件</dt>
<dd>{formatBytes(data.manifest_file_bytes)}</dd>
</div>
</dl>
<div className="library-storage-panel">
<h4>容量门禁</h4>
{data.capacity.configured ? (
<p>
预算 {formatBytes(data.capacity.budget_bytes)} · 当前 {formatBytes(data.capacity.current_bytes)} ·
目标版本约 {formatBytes(data.capacity.required_bytes)} →{' '}
{data.capacity.ok ? '可写入' : `阻断:${data.capacity.reason}`}
</p>
) : (
<p className="library-storage-warn">
容量预算未设置(LIBRARY_CAPACITY_BUDGET_BYTES):无法证明装得下,生产写入按阻断处理。
</p>
)}
</div>
<div>
<dt>被版本引用的去重内容</dt>
<dd>{formatBytes(page.content_bytes)}</dd>
<div className="library-storage-panel">
<h4>保留与依赖保护</h4>
<p>
{policyConfigured
? `策略:${[
plan && plan.policy.keep_versions > 0 ? `保留最近 ${plan.policy.keep_versions} 个版本` : '',
plan && plan.policy.keep_days > 0 ? `保留 ${plan.policy.keep_days} 天` : '',
]
.filter(Boolean)
.join(' + ')}`
: '策略:未设置(不生成清理候选)'}
</p>
<p>
受保护版本 {plan?.protected.length ?? 0} 个:生产生效、每库最近封存、每次生产预备份、在飞作业引用,
不论多久都不会进入候选。
</p>
{data.versions.filter((item) => item.protected).length > 0 ? (
<ul className="library-storage-versions">
{data.versions
.filter((item) => item.protected)
.map((item) => (
<li key={item.id}>
<code>{item.id.slice(0, 12)}</code> {item.kind} · {(item.protected_by ?? []).join('、')}
</li>
))}
</ul>
) : null}
</div>
<div>
<dt>blob 文件(表观)</dt>
<dd>{formatBytes(page.blob_file_bytes)}</dd>
<div className="library-storage-panel">
<h4>清理预览</h4>
{policyConfigured ? (
plan && plan.candidates.length > 0 ? (
<>
<p>
按策略可考虑清理 {plan.candidates.length} 个版本,独占字节上限{' '}
{formatBytes(plan.reclaim_preview_bytes)}(共享 blob 不会因此释放,这不是可回收空间)。
</p>
<ul className="library-storage-versions">
{plan.candidates.map((item) => (
<li key={item.id}>
<code>{item.id.slice(0, 12)}</code> {item.kind} · {item.reason.join(';')} ·{' '}
{formatBytes(item.exclusive_bytes)}
</li>
))}
</ul>
</>
) : (
<p>按当前策略没有可清理的候选版本。</p>
)
) : (
<p className="library-storage-warn">
保留数量与天数都未设置,按「全部保留」处理:不生成候选,也不提供默认值。
</p>
)}
{plan?.blocked.length ? (
<ul className="library-storage-blocked-list">
{plan.blocked.map((item) => (
<li key={item}>{item}</li>
))}
</ul>
) : null}
</div>
<div>
<dt>blob 实际分配</dt>
<dd>{formatBytes(page.blob_allocated_bytes)}</dd>
</div>
<div>
<dt>未被任何版本引用</dt>
<dd>
{page.unreferenced_blobs} 个 / {formatBytes(page.unreferenced_bytes)}
</dd>
</div>
<div>
<dt>清单文件</dt>
<dd>{formatBytes(page.manifest_file_bytes)}</dd>
</div>
</dl>
<ul className="library-storage-limits">
<li>自动清理:{data.automatic_cleanup ? '开启' : '保持关闭'}(页面不提供删除动作)</li>
<li>实际删除需要单独授权,本页只给预览</li>
<li>以上数字都不是「可回收空间」,不能用来估算清理收益</li>
{data.notes.map((note) => (
<li key={note}>{note}</li>
))}
</ul>
</>
) : null}
<ul className="library-storage-limits">
<li>保留策略:未接线(不提供保留几版/多少天的设置)</li>
<li>依赖保护与清理预览:未接线</li>
<li>自动清理:保持关闭,页面不提供删除动作</li>
<li>以上数字都不是“可回收空间”,不能用来估算清理收益</li>
</ul>
</section>
)
}
+76
View File
@@ -311,3 +311,79 @@ export async function fetchLibraryOverview(scope: string, signal?: AbortSignal):
if (!record(value)) return null
return value as unknown as LibraryOverview
}
// ---- 存储管理:容量、保留策略、依赖保护与清理预览(只读) ----
export interface LibraryStorageVersion {
id: string
kind: string
scope: string
created_at: string
protected: boolean
protected_by?: string[]
exclusive_bytes: number
shared_bytes: number
}
export interface LibraryRetentionPolicy {
keep_versions: number
keep_days: number
}
export interface LibraryCleanupCandidate {
id: string
kind: string
reason: string[]
exclusive_bytes: number
}
export interface LibraryRetentionPlan {
policy: LibraryRetentionPolicy
policy_configured: boolean
automatic_cleanup: boolean
version_count: number
total_bytes: number
protected: string[]
keep: string[]
candidates: LibraryCleanupCandidate[]
reclaim_preview_bytes: number
blocked: string[]
now: string
}
export interface LibraryCapacity {
budget_bytes: number
current_bytes: number
required_bytes: number
projected_bytes: number
configured: boolean
ok: boolean
reason?: string
}
export interface LibraryStorage {
available: boolean
reason?: string
archive_root?: string
versions: LibraryStorageVersion[]
content_bytes: number
blob_file_bytes: number
blob_allocated_bytes: number
unreferenced_blobs: number
unreferenced_bytes: number
manifest_file_bytes: number
policy: LibraryRetentionPolicy
plan?: LibraryRetentionPlan
capacity: LibraryCapacity
automatic_cleanup: boolean
notes: string[]
}
export async function fetchLibraryStorage(signal?: AbortSignal): Promise<LibraryStorage | null> {
const response = await fetch('/api/v1/library/storage', {
headers: { Accept: 'application/json' },
signal,
})
const value = (await response.json().catch(() => null)) as unknown
if (!record(value)) return null
return value as unknown as LibraryStorage
}
+33
View File
@@ -64,3 +64,36 @@
font-size: 12px;
color: #7c2d12;
}
.library-storage-panel {
border: 1px solid #e6edf2;
border-radius: 8px;
padding: 10px 12px;
margin-bottom: 10px;
}
.library-storage-panel h4 {
margin: 0 0 6px;
font-size: 13px;
}
.library-storage-panel p {
margin: 0 0 6px;
font-size: 12px;
line-height: 1.7;
color: #3f5566;
}
.library-storage-warn {
color: #986b24 !important;
}
.library-storage-versions {
margin: 4px 0 0;
padding-left: 18px;
font-size: 12px;
line-height: 1.8;
color: #3f5566;
}
.library-storage-blocked-list {
margin: 4px 0 0;
padding-left: 18px;
font-size: 12px;
color: #a03a1e;
}