import { newOperationID } from './operationKey' // 「系统设置 → 存储与备份」:整库逻辑备份的定时计划(时间 + 储存天数)与主机回执。 export interface BackupSchedule { enabled: boolean schedule_time: string retention_days: number retention_months: number updated_by: string updated_at: string last_applied_at: string last_applied_summary: string host_configured: boolean host_target: string host_reachable: boolean host_error: string host_schedule_enabled: boolean host_schedule_time: string host_keep_days: number host_keep_months: number host_cron_lines: string[] host_cron_service: string host_script_present: boolean last_run_stamp: string last_run_created_at: string last_run_duration_seconds: number last_run_total_bytes: number last_run_databases: string[] last_run_failed_databases: string[] last_run_error: string in_sync: boolean applied: boolean apply_error: string running: boolean running_started_at: string run_last_error: string } export interface BackupRunStart { started: boolean running: boolean } const record = (value: unknown): value is Record => Boolean(value) && typeof value === 'object' && !Array.isArray(value) const asString = (value: unknown): string => (typeof value === 'string' ? value : '') const asNumber = (value: unknown): number => (typeof value === 'number' && Number.isFinite(value) ? value : 0) const asStringList = (value: unknown): string[] => Array.isArray(value) ? value.filter((item): item is string => typeof item === 'string') : [] // 严格解析:计划三要素(时间/天数/月数)必须是正确类型,缺一个就整块报错, // 而不是让界面显示成"默认 03:30 / 7 天"这种假状态。 function scheduleProjection(item: Record): BackupSchedule { if (typeof item.schedule_time !== 'string' || typeof item.retention_days !== 'number' || typeof item.retention_months !== 'number') { throw new Error('存储与备份响应缺少计划字段(schedule_time / retention_days / retention_months)') } return { enabled: item.enabled === true, schedule_time: item.schedule_time, retention_days: item.retention_days, retention_months: item.retention_months, updated_by: asString(item.updated_by), updated_at: asString(item.updated_at), last_applied_at: asString(item.last_applied_at), last_applied_summary: asString(item.last_applied_summary), host_configured: item.host_configured === true, host_target: asString(item.host_target), host_reachable: item.host_reachable === true, host_error: asString(item.host_error), host_schedule_enabled: item.host_schedule_enabled === true, host_schedule_time: asString(item.host_schedule_time), host_keep_days: asNumber(item.host_keep_days), host_keep_months: asNumber(item.host_keep_months), host_cron_lines: asStringList(item.host_cron_lines), host_cron_service: asString(item.host_cron_service), host_script_present: item.host_script_present === true, last_run_stamp: asString(item.last_run_stamp), last_run_created_at: asString(item.last_run_created_at), last_run_duration_seconds: asNumber(item.last_run_duration_seconds), last_run_total_bytes: asNumber(item.last_run_total_bytes), last_run_databases: asStringList(item.last_run_databases), last_run_failed_databases: asStringList(item.last_run_failed_databases), last_run_error: asString(item.last_run_error), in_sync: item.in_sync === true, applied: item.applied === true, apply_error: asString(item.apply_error), running: item.running === true, running_started_at: asString(item.running_started_at), run_last_error: asString(item.run_last_error), } } async function readSchedule(path: string, failure: string, signal?: AbortSignal): Promise { const response = await fetch(path, { signal, headers: { Accept: 'application/json' } }) if (!response.ok) throw new Error(`${failure}(HTTP ${response.status})`) const value = await response.json() as unknown if (!record(value) || !record(value.data)) throw new Error('存储与备份响应无效') return scheduleProjection(value.data) } export function fetchBackupSchedule(signal?: AbortSignal): Promise { return readSchedule('/api/v1/backup-schedule', '读取备份计划失败', signal) } // host 端点会 SSH 探测目标主机(计划 + 最近一次结果),单独调用以免拖慢首屏。 export function fetchBackupScheduleHost(signal?: AbortSignal): Promise { return readSchedule('/api/v1/backup-schedule/host', '读取目标主机上的备份状态失败', signal) } async function writeSchedule(path: string, method: string, body: unknown, failure: string, signal?: AbortSignal): Promise { const response = await fetch(path, { method, signal, headers: { 'Content-Type': 'application/json', Accept: 'application/json', 'Idempotency-Key': newOperationID() }, body: body === undefined ? undefined : JSON.stringify(body), }) if (!response.ok) { let reason = `${failure}(HTTP ${response.status})` try { const payload = await response.json() as unknown if (record(payload) && typeof payload.error === 'string') reason = payload.error } catch { /* keep the status text */ } throw new Error(reason) } const value = await response.json() as unknown if (!record(value) || !record(value.data)) throw new Error('存储与备份响应无效') return scheduleProjection(value.data) } export interface BackupScheduleInput { enabled: boolean schedule_time: string retention_days: number retention_months: number // 默认 true:保存后立刻下发到目标主机(页面按钮"保存并下发")。 apply?: boolean } export function saveBackupSchedule(input: BackupScheduleInput, signal?: AbortSignal): Promise { return writeSchedule('/api/v1/backup-schedule', 'PUT', input, '保存备份计划失败', signal) } export function applyBackupSchedule(signal?: AbortSignal): Promise { return writeSchedule('/api/v1/backup-schedule/apply', 'POST', undefined, '下发备份计划失败', signal) } export async function runBackupNow(signal?: AbortSignal): Promise { const response = await fetch('/api/v1/backup-schedule/run', { method: 'POST', signal, headers: { Accept: 'application/json', 'Idempotency-Key': newOperationID() }, }) if (!response.ok) { let reason = `触发备份失败(HTTP ${response.status})` try { const payload = await response.json() as unknown if (record(payload) && typeof payload.error === 'string') reason = payload.error } catch { /* keep the status text */ } throw new Error(reason) } const value = await response.json() as unknown if (!record(value) || !record(value.data)) throw new Error('触发备份响应无效') return { started: value.data.started === true, running: value.data.running === true } }