fix(settings): 区分快照过期与未配置

This commit is contained in:
姚章浩
2026-11-20 02:10:07 +08:00
parent 341c3154ac
commit 4b615abe53
3 changed files with 198 additions and 38 deletions
+12
View File
@@ -1,5 +1,17 @@
# Changelog
## 开发快照 0.1.410(存储页快照状态区分)
### Fixed
- 快照超过 15 分钟后不再把目录区写成“元数据快照未接线”:按接口字段区分
未配置、缺失、过期、无效、不完整,目录与历史分别标注真实状态。
- 过期快照不展示旧批次路径、旧目录路径或旧容量预测;目录输入清空并提示等待宿主
重新采集;历史区只说明过期原因,不回退冒充“没有备份”。
- 读取源/采集时间/读回时间按同一快照状态展示:过期仍显示当时的采集时间与年龄,
读回时间单独取 API 时间,不混用。
- 增加过期与未配置反例回归,覆盖旧批次/旧容量不得复用。
## 开发快照 0.1.409(存储与备份页面显示口径修正)
### Fixed
+82 -3
View File
@@ -9,6 +9,7 @@ import {
copyBackupPath,
formatBackupBytes,
inventoryAlerts,
inventorySnapshotState,
inventoryUsage,
runMoment,
storageAlerts,
@@ -249,8 +250,8 @@ describe('BackupScheduleView', () => {
'范围与读写方向',
'备份执行主机 weknora-test 本机直连 RDS',
'最近批次含 2 个数据库',
'weknora-test 本地备份根目录',
'元数据快照未接线或不可用,不在页面猜测',
'元数据快照未读取',
'元数据快照未读取:目录不可验证,不按设置推定',
'整批 · 2 个数据库(weknora_test、dobotai_prod)',
'100.85.111.121:22',
'立即备份一次(整批 2 个数据库)',
@@ -282,7 +283,7 @@ describe('BackupScheduleView', () => {
it('目录/配额/预留没有后端字段:控件禁用并写明原因,不会假装可保存', () => {
const html = view()
expect(html).toContain('备份主机上的目录')
expect(html).toContain('元数据快照未接线')
expect(html).toContain('元数据快照未读取')
expect(html).toContain('检查目录(不可用)')
expect(html).toContain('备份总配额(GiB)')
expect(html).toContain('磁盘预留(GiB)')
@@ -594,6 +595,84 @@ describe('整盘占用与告警', () => {
expect(html).not.toContain('0.0 MiB')
})
it('过期快照:目录与历史标已过期,不显示未接线,也不沿用旧批次/旧容量', () => {
const base = inventoryFixture()
const stale = inventoryFixture({
metadata: {
...base.metadata,
valid: false,
fresh: false,
complete: false,
age_seconds: 1200,
error: '元数据已陈旧:采集于 2026-11-19T03:30:00Z,超过 15m0s',
},
capacity: { measured: false, total_bytes: 0, free_bytes: 0, available_bytes: 0, used_bytes: 0 },
totals: { ...base.totals, apparent_bytes: null, actual_bytes: null, latest_actual_bytes: null, complete: false },
// 即使 API 意外保留旧批次,页面也不得用过期快照的路径/容量。
batches: base.batches,
})
expect(inventorySnapshotState(stale)).toBe('stale')
expect(inventoryUsage(stale)).toBeNull()
const alerts = inventoryAlerts(stale)
expect(alerts.find((item) => item.code === 'disk_low')?.known).toBe(false)
expect(alerts.find((item) => item.code === 'disk_low')?.detail).toContain('陈旧')
const html = view({}, { inventory: stale })
for (const text of [
'元数据快照已过期',
'采集时间',
'年龄 1200 秒 / 上限 900 秒',
'已过期',
'读回时间',
'(API 读回)',
'不展示旧批次',
'元数据快照已过期:目录不可验证,不沿用旧路径',
'等待宿主重新采集',
'旧批次、旧目录与旧容量都不再展示',
])
expect(html).toContain(text)
expect(html).not.toContain('元数据快照未接线')
expect(html).not.toContain('/20261118T193001Z')
expect(html).not.toContain('实际占盘 1.40 GiB')
expect(html).not.toContain('文件系统已用')
})
it('未配置快照:明确未接线,不能冒充过期或展示目录/历史', () => {
const base = inventoryFixture()
const unconfigured = inventoryFixture({
wired: false,
metadata: {
...base.metadata,
configured: false,
present: false,
valid: false,
fresh: false,
complete: false,
error: '元数据快照未接线(BACKUP_HOST_METADATA_PATH 未设置)',
},
source: '',
root: '',
host_root: '',
capacity: { measured: false, total_bytes: 0, free_bytes: 0, available_bytes: 0, used_bytes: 0 },
totals: { ...base.totals, apparent_bytes: null, actual_bytes: null, latest_actual_bytes: null, complete: false },
batches: [],
})
expect(inventorySnapshotState(unconfigured)).toBe('unconfigured')
const html = view({}, { inventory: unconfigured })
expect(html).toContain('元数据快照未接线')
expect(html).toContain('目录不可证明')
expect(html).not.toContain('元数据快照已过期')
expect(html).not.toContain('不展示旧目录路径')
})
it('快照请求失败:与未配置/过期分开,不展示目录或旧批次', () => {
const html = view({}, { inventoryError: 'HTTP 503' })
expect(html).toContain('元数据快照读取失败')
expect(html).toContain('快照请求失败:HTTP 503')
expect(html).toContain('目录不可验证')
expect(html).not.toContain('元数据快照未接线')
expect(html).not.toContain('元数据快照已过期')
})
it('不完整快照:容量图与占盘不使用部分字节', () => {
const base = inventoryFixture()
const inventory = inventoryFixture({
+104 -35
View File
@@ -342,8 +342,43 @@ export async function copyBackupPath(value: string, button?: HTMLButtonElement |
const manifestLabel = (batch: BackupInventoryBatch): string =>
batch.manifest_state === 'ok' ? '清单完整' : batch.manifest_state === 'missing' ? '清单缺失' : '清单损坏'
const inventoryMetadataAvailable = (inventory: BackupInventory): boolean =>
inventory.metadata.present && inventory.metadata.valid && inventory.metadata.fresh
export type InventorySnapshotState =
'loading' | 'unconfigured' | 'missing' | 'stale' | 'invalid' | 'incomplete' | 'ready'
// 快照状态必须由接口字段判定,不能因为 fresh=false 就一律说“未接线”:
// 过期、缺失、无效、未配置是四种不同事实,目录/历史/容量都不能互相代替。
export function inventorySnapshotState(inventory: BackupInventory | null): InventorySnapshotState {
if (!inventory) return 'loading'
if (!inventory.wired || !inventory.metadata.configured) return 'unconfigured'
if (!inventory.metadata.present) return 'missing'
const age = inventory.metadata.age_seconds
const maxAge = inventory.metadata.max_age_seconds
const staleByAge = maxAge > 0 && age > maxAge
if (!inventory.metadata.fresh && (staleByAge || inventory.metadata.error.includes('陈旧'))) return 'stale'
if (!inventory.metadata.valid) return 'invalid'
if (!inventory.metadata.fresh) return 'stale'
if (!inventory.metadata.complete) return 'incomplete'
return 'ready'
}
const inventoryStateLabel = (state: InventorySnapshotState): string => {
switch (state) {
case 'unconfigured':
return '元数据快照未接线'
case 'missing':
return '元数据快照缺失'
case 'stale':
return '元数据快照已过期'
case 'invalid':
return '元数据快照无效'
case 'incomplete':
return '元数据快照不完整'
case 'ready':
return '元数据快照有效'
default:
return '元数据读取中'
}
}
// 备份执行主机上的整库逻辑备份(全部业务库一次 pg_dump)。
// 页面只展示接口真实给出的读数;目录、配额、预留、子库路径、历史批次与恢复
@@ -442,15 +477,20 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
const runFailed = failedCount > 0 || Boolean(schedule.last_run_error)
const usage = disk ? storageUsage(disk) : null
const alerts = disk ? storageAlerts(disk) : inventoryAlerts(inventory)
const latestBatch: BackupInventoryBatch | null = inventory?.batches[0] ?? null
const snapshotState = inventorySnapshotState(inventory)
const snapshotLabel = inventory
? inventoryStateLabel(snapshotState)
: inventoryError
? '元数据快照读取失败'
: inventoryLoading
? '元数据读取中'
: '元数据快照未读取'
const snapshotPathsTrusted = snapshotState === 'ready' || snapshotState === 'incomplete'
const sshFallbackReason = snapshotPathsTrusted ? '元数据快照无批次' : snapshotLabel
const latestBatch: BackupInventoryBatch | null = snapshotPathsTrusted ? (inventory?.batches[0] ?? null) : null
const inventoryFigure = inventoryUsage(inventory)
const inventoryReady = Boolean(
inventory?.wired && inventory.metadata.present && inventory.metadata.valid && inventory.metadata.fresh
)
const readyInventory =
inventory && inventory.wired && inventory.metadata.present && inventory.metadata.valid && inventory.metadata.fresh
? inventory
: null
const inventoryReady = snapshotPathsTrusted
const readyInventory = snapshotPathsTrusted ? inventory : null
const inventoryDatabases = latestBatch?.databases.map((item) => item.name) ?? []
const scopeDatabases = inventoryDatabases.length ? inventoryDatabases : schedule.last_run_databases
const scopeDatabaseCount = scopeDatabases.length
@@ -484,38 +524,44 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
{schedule.host_configured ? '平台已接主机凭据' : '平台未接主机凭据'}
</span>
<span className={`storage-chip ${inventoryReady ? 'ok' : 'off'}`}>
{inventoryLoading
{inventoryLoading && !inventory
? '元数据读取中'
: inventoryReady
? inventory?.metadata.complete
? '元数据快照有效'
: '元数据快照不完整'
: inventory?.metadata.error
? '元数据快照不可用'
: '元数据未接线'}
: snapshotState === 'ready'
? '元数据快照有效'
: snapshotState === 'incomplete'
? '元数据快照不完整'
: snapshotLabel}
</span>
</div>
<dl className="storage-facts">
<dt>读取源</dt>
<dd>
{inventory?.source || `备份执行主机 ${schedule.host_target || '(未配置)'} 本机直连 RDS`}
{inventory?.source
? `${inventory.source}${snapshotState === 'stale' ? '(快照已过期)' : ''}`
: `备份执行主机 ${schedule.host_target || '(未配置)'} 本机直连 RDS`}
{scopeDatabaseCount ? ` · 最近批次含 ${scopeDatabaseCount} 个数据库` : ' · 数据库清单未读到'}
</dd>
<dt>采集时间</dt>
<dd>
{readyInventory
? `${formatBackupMoment(readyInventory.metadata.collected_at)}(快照) · 年龄 ${readyInventory.metadata.age_seconds} 秒 / 上限 ${readyInventory.metadata.max_age_seconds} 秒 · ${
readyInventory.metadata.fresh ? '新鲜' : '已陈旧'
{inventory?.metadata.present
? `${formatBackupMoment(inventory.metadata.collected_at)}(快照) · 年龄 ${inventory.metadata.age_seconds} 秒 / 上限 ${inventory.metadata.max_age_seconds} 秒 · ${
snapshotState === 'stale' ? '已过期' : inventory.metadata.fresh ? '新鲜' : '不可用'
}`
: '—(没有可用快照)'}
: `—(${snapshotLabel})`}
</dd>
<dt>读回时间</dt>
<dd>{readyInventory ? `${formatBackupMoment(readyInventory.observed_at)}(API 读回)` : '—'}</dd>
<dd>{inventory?.observed_at ? `${formatBackupMoment(inventory.observed_at)}(API 读回)` : '—'}</dd>
<dt>写入方向</dt>
<dd>
{inventory?.host_root || `${schedule.host_target || '(未配置)'} 本地备份根目录`}
{snapshotPathsTrusted && inventory?.host_root
? inventory.host_root
: snapshotState === 'stale'
? `—(${snapshotLabel}:不展示旧目录路径)`
: `—(${snapshotLabel})`}
<span className="storage-inline-note">
{inventoryReady ? '宿主采集器快照(只读);平台没有写路径' : '元数据快照未接线或不可用,不在页面猜测'}
{snapshotPathsTrusted
? '宿主采集器快照(只读);平台没有写路径'
: `${snapshotLabel}:目录不可验证,不按设置推定`}
</span>
</dd>
<dt>执行范围</dt>
@@ -556,7 +602,7 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
: `实际占盘 ${formatBackupBytes(latestBatch.actual_bytes)}`
} · ${latestBatch.duration_seconds} 秒`
: hasLastRun
? `SSH 读回最近一次 ${schedule.last_run_stamp} · ${databaseCount} 个数据库 · ${formatBackupBytes(schedule.last_run_total_bytes)}(元数据快照未接线,以下仅 SSH 结果)`
? `SSH 读回最近一次 ${schedule.last_run_stamp} · ${databaseCount} 个数据库 · ${formatBackupBytes(schedule.last_run_total_bytes)}(${sshFallbackReason},以下仅 SSH 读回结果)`
: '还没有从主机读到任何批次。'}
</p>
{schedule.running ? (
@@ -782,10 +828,10 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
备份主机上的目录
<input
type="text"
value={inventory?.host_root || ''}
value={snapshotPathsTrusted ? (inventory?.host_root ?? '') : ''}
readOnly
disabled
placeholder={inventoryReady ? '' : '元数据快照未接线'}
placeholder={snapshotPathsTrusted ? '' : `${snapshotLabel}:目录不可验证`}
aria-label="备份主机上的目录(只读展示)"
/>
</label>
@@ -794,9 +840,19 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
检查目录(不可用)
</button>
<span className="hint">
{inventoryReady
{snapshotPathsTrusted
? '目录来自宿主元数据快照,只读展示;修改目录需部署变更,平台没有写接口。'
: '元数据快照未接线:目录不可证明,不能按设置推定。'}
: snapshotState === 'stale'
? '元数据快照已过期:目录不可验证,不沿用旧路径;等待宿主重新采集。'
: snapshotState === 'missing'
? '元数据快照缺失:目录不可验证,需先在备份主机运行采集器。'
: snapshotState === 'invalid'
? '元数据快照无效:目录不可验证,需检查快照内容。'
: snapshotState === 'loading'
? inventoryError
? '元数据快照读取失败:目录不可验证,请检查 API 到快照挂载的接线。'
: '尚未读取元数据快照:目录不可验证。'
: '元数据快照未接线:目录不可证明,不能按设置推定。'}
</span>
</div>
<label className="storage-checkbox">
@@ -902,7 +958,7 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
<span className="hint">
{readyInventory
? `${readyInventory.totals.batch_count} 批 · 元数据快照 · 实际占盘 ${formatBackupBytes(readyInventory.totals.actual_bytes)}`
: '元数据快照'}
: `${snapshotLabel} · 不展示旧批次`}
</span>
<button
type="button"
@@ -919,15 +975,28 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
快照请求失败:{inventoryError}。不能用 SSH 的“最近一次”代替历史;请检查 API 到快照挂载的接线。
</p>
) : null}
{inventory && !inventory.wired ? (
{inventory && snapshotState === 'unconfigured' ? (
<p className="hint">
元数据快照未接线:{inventory.metadata.error}。部署需在备份主机运行单次采集器并把输出目录只读挂进 API(见
docs/STORAGE_BACKUP_READONLY_API.md);未读到批次不等于没有备份。
</p>
) : null}
{inventory && inventory.wired && !inventoryMetadataAvailable(inventory) ? (
{inventory && snapshotState === 'missing' ? (
<p className="error-message" role="alert">
元数据快照不可用:{inventory.metadata.error || inventory.error || '缺失/陈旧/无效'}
元数据快照缺失:{inventory.metadata.error || inventory.error || '快照文件不存在'}
。未读到批次不等于没有备份; 等待宿主采集器写出快照后重新盘点。
</p>
) : null}
{inventory && snapshotState === 'stale' ? (
<p className="error-message" role="alert">
元数据快照已过期:{inventory.metadata.error || '采集时间超过上限'}。旧批次、旧目录与旧容量都不再展示;
等待宿主重新采集后重新盘点。
</p>
) : null}
{inventory && snapshotState === 'invalid' ? (
<p className="error-message" role="alert">
元数据快照无效:{inventory.metadata.error || inventory.error || '快照无法通过校验'}
。不展示批次、路径或容量。
</p>
) : null}
{readyInventory ? (