fix(backup): 保存后即时反映主机状态;最近一次备份不再重复同一时间戳

- 后端 handleSaveBackupSchedule 改为先下发、再组装投影:此前页面保存成功那几行仍是
  下发前的主机计划,看起来像没生效(需等下一次刷新)
- 前端 runMoment:清单里的 created_at 就是目录戳,同值不再重复显示(原为
  '20261009T060736Z · 20261009T060736Z')
- 前端补 runMoment 用例(171 个 vitest 全绿)

面板实测:保存后立即显示 每天 03:30 / 保留 7 天 / 已一致,cron 条目 30 03 * * *
This commit is contained in:
姚章浩
2026-10-09 14:12:10 +08:00
parent 343a005db1
commit e95a50ca3a
3 changed files with 22 additions and 9 deletions
+7 -7
View File
@@ -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 不改设置,只把库里的计划重新下发一次。
+7 -1
View File
@@ -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')
+8 -1
View File
@@ -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) {
<dl>
<dt>目标主机</dt><dd>{hostLine}</dd>
<dt>主机 cron</dt><dd>{schedule.host_cron_service ? `服务 ${schedule.host_cron_service}` : '—'}{schedule.host_script_present ? '' : ' · 备份脚本缺失'}</dd>
<dt>最近一次备份</dt><dd>{schedule.last_run_stamp || '—'}{schedule.last_run_created_at ? ` · ${formatBackupMoment(schedule.last_run_created_at)}` : ''}</dd>
<dt>最近一次备份</dt><dd>{schedule.last_run_stamp || '—'}{runMoment(schedule) ? ` · ${runMoment(schedule)}` : ''}</dd>
<dt>备份耗时 / 体积</dt><dd>{schedule.last_run_stamp ? `${schedule.last_run_duration_seconds} 秒 · ${formatBackupBytes(schedule.last_run_total_bytes)}` : '—'}</dd>
</dl>
<dl>