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
+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 ? (