From e95a50ca3a49804872456d59a2333c457b1172f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9A=E7=AB=A0=E6=B5=A9?= Date: Fri, 9 Oct 2026 14:12:10 +0800 Subject: [PATCH] =?UTF-8?q?fix(backup):=20=E4=BF=9D=E5=AD=98=E5=90=8E?= =?UTF-8?q?=E5=8D=B3=E6=97=B6=E5=8F=8D=E6=98=A0=E4=B8=BB=E6=9C=BA=E7=8A=B6?= =?UTF-8?q?=E6=80=81=EF=BC=9B=E6=9C=80=E8=BF=91=E4=B8=80=E6=AC=A1=E5=A4=87?= =?UTF-8?q?=E4=BB=BD=E4=B8=8D=E5=86=8D=E9=87=8D=E5=A4=8D=E5=90=8C=E4=B8=80?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=88=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端 handleSaveBackupSchedule 改为先下发、再组装投影:此前页面保存成功那几行仍是 下发前的主机计划,看起来像没生效(需等下一次刷新) - 前端 runMoment:清单里的 created_at 就是目录戳,同值不再重复显示(原为 '20261009T060736Z · 20261009T060736Z') - 前端补 runMoment 用例(171 个 vitest 全绿) 面板实测:保存后立即显示 每天 03:30 / 保留 7 天 / 已一致,cron 条目 30 03 * * * --- internal/httpapi/backup_schedule.go | 14 +++++++------- web/src/BackupScheduleSection.test.tsx | 8 +++++++- web/src/BackupScheduleSection.tsx | 9 ++++++++- 3 files changed, 22 insertions(+), 9 deletions(-) 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)}` : '—'}