diff --git a/internal/httpapi/backup_schedule.go b/internal/httpapi/backup_schedule.go index 33bacc6..72e9572 100644 --- a/internal/httpapi/backup_schedule.go +++ b/internal/httpapi/backup_schedule.go @@ -172,20 +172,20 @@ func (config Config) handleSaveBackupSchedule(w http.ResponseWriter, r *http.Req writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not save backup schedule"}) return } + // 先下发、再组装投影:否则页面保存后那几行还是"下发前"的主机状态,看着像没生效。 apply := input.Apply == nil || *input.Apply + problem, applied := "", false + if apply && config.BackupHost.Available() { + problem, applied = config.applyBackupSchedule(r.Context(), next) + } view, err := config.backupSchedulePayload(r.Context(), true) if err != nil { writeJSON(w, http.StatusInternalServerError, map[string]string{"error": "could not load backup schedule"}) return } - if !apply || !view.HostConfigured { - writeJSON(w, http.StatusOK, map[string]any{"data": view, "applied": false}) - return - } - payload, applied := config.applyBackupSchedule(r.Context(), next) view.Applied = applied - view.ApplyError = payload - writeJSON(w, http.StatusOK, map[string]any{"data": view, "applied": applied, "apply_error": payload}) + view.ApplyError = problem + writeJSON(w, http.StatusOK, map[string]any{"data": view, "applied": applied, "apply_error": problem}) } // handleApplyBackupSchedule 不改设置,只把库里的计划重新下发一次。 diff --git a/web/src/BackupScheduleSection.test.tsx b/web/src/BackupScheduleSection.test.tsx index af50cfb..9c92c71 100644 --- a/web/src/BackupScheduleSection.test.tsx +++ b/web/src/BackupScheduleSection.test.tsx @@ -1,7 +1,7 @@ import { renderToStaticMarkup } from 'react-dom/server' import { describe, expect, it, vi } from 'vitest' -import { BackupScheduleView, backupDaysValid, backupMonthsValid, backupTimeValid, formatBackupBytes } from './BackupScheduleSection' +import { BackupScheduleView, backupDaysValid, backupMonthsValid, backupTimeValid, formatBackupBytes, runMoment } from './BackupScheduleSection' import type { BackupSchedule } from './backupSchedule' const base: BackupSchedule = { @@ -89,6 +89,12 @@ describe('BackupScheduleView', () => { expect(html).toContain('清单无法解析') }) + it('最近一次备份不重复显示同一个时间戳', () => { + expect(runMoment({ last_run_stamp: '20261009T060736Z', last_run_created_at: '20261009T060736Z' })).toBe('') + expect(runMoment({ last_run_stamp: '20261009T060736Z', last_run_created_at: '' })).toBe('') + expect(runMoment({ last_run_stamp: '20261009T060736Z', last_run_created_at: '2026-10-09T06:07:36Z' })).not.toBe('') + }) + it('体积格式化:MiB/GiB', () => { expect(formatBackupBytes(0)).toBe('—') expect(formatBackupBytes(1024 * 1024)).toBe('1.0 MiB') diff --git a/web/src/BackupScheduleSection.tsx b/web/src/BackupScheduleSection.tsx index be085cf..d51c410 100644 --- a/web/src/BackupScheduleSection.tsx +++ b/web/src/BackupScheduleSection.tsx @@ -16,6 +16,13 @@ export const formatBackupMoment = (value: string): string => { return Number.isNaN(parsed.getTime()) ? value : parsed.toLocaleString('zh-CN', { hour12: false }) } +// 清单里的 created_at 就是目录戳(如 20261009T060736Z),同值时不再重复显示一遍。 +export const runMoment = (schedule: { last_run_stamp: string; last_run_created_at: string }): string => { + const createdAt = schedule.last_run_created_at.trim() + if (!createdAt || createdAt === schedule.last_run_stamp.trim()) return '' + return formatBackupMoment(createdAt) +} + export const backupTimeValid = (value: string): boolean => /^([01][0-9]|2[0-3]):[0-5][0-9]$/.test(value) export const backupDaysValid = (value: number): boolean => Number.isInteger(value) && value >= 1 && value <= 365 export const backupMonthsValid = (value: number): boolean => Number.isInteger(value) && value >= 0 && value <= 24 @@ -70,7 +77,7 @@ export function BackupScheduleView(props: BackupScheduleViewProps) {
目标主机
{hostLine}
主机 cron
{schedule.host_cron_service ? `服务 ${schedule.host_cron_service}` : '—'}{schedule.host_script_present ? '' : ' · 备份脚本缺失'}
-
最近一次备份
{schedule.last_run_stamp || '—'}{schedule.last_run_created_at ? ` · ${formatBackupMoment(schedule.last_run_created_at)}` : ''}
+
最近一次备份
{schedule.last_run_stamp || '—'}{runMoment(schedule) ? ` · ${runMoment(schedule)}` : ''}
备份耗时 / 体积
{schedule.last_run_stamp ? `${schedule.last_run_duration_seconds} 秒 · ${formatBackupBytes(schedule.last_run_total_bytes)}` : '—'}