diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f6c766..d37b1d8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,17 @@ # Changelog +## 开发快照 0.1.410(存储页快照状态区分) + +### Fixed + +- 快照超过 15 分钟后不再把目录区写成“元数据快照未接线”:按接口字段区分 + 未配置、缺失、过期、无效、不完整,目录与历史分别标注真实状态。 +- 过期快照不展示旧批次路径、旧目录路径或旧容量预测;目录输入清空并提示等待宿主 + 重新采集;历史区只说明过期原因,不回退冒充“没有备份”。 +- 读取源/采集时间/读回时间按同一快照状态展示:过期仍显示当时的采集时间与年龄, + 读回时间单独取 API 时间,不混用。 +- 增加过期与未配置反例回归,覆盖旧批次/旧容量不得复用。 + ## 开发快照 0.1.409(存储与备份页面显示口径修正) ### Fixed diff --git a/web/src/BackupScheduleSection.test.tsx b/web/src/BackupScheduleSection.test.tsx index 032fa4b..765c7d9 100644 --- a/web/src/BackupScheduleSection.test.tsx +++ b/web/src/BackupScheduleSection.test.tsx @@ -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({ diff --git a/web/src/BackupScheduleSection.tsx b/web/src/BackupScheduleSection.tsx index 490d00a..acc10bf 100644 --- a/web/src/BackupScheduleSection.tsx +++ b/web/src/BackupScheduleSection.tsx @@ -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 ? '平台已接主机凭据' : '平台未接主机凭据'} - {inventoryLoading + {inventoryLoading && !inventory ? '元数据读取中' - : inventoryReady - ? inventory?.metadata.complete - ? '元数据快照有效' - : '元数据快照不完整' - : inventory?.metadata.error - ? '元数据快照不可用' - : '元数据未接线'} + : snapshotState === 'ready' + ? '元数据快照有效' + : snapshotState === 'incomplete' + ? '元数据快照不完整' + : snapshotLabel}