feat(preflight): 报告保留最近 N 份(默认 6,配置可调)

- [Compatibility] report_keep,<=0 表示不清理
- 只清理 report_dir 下匹配 preflight-*.xlsx/csv 的文件,按 mtime 保留最新
This commit is contained in:
ali
2026-11-19 02:25:50 +08:00
parent 50886ac764
commit 17b24fb5ce
2 changed files with 24 additions and 0 deletions
+21
View File
@@ -322,6 +322,25 @@ class PreflightService:
]))
return rows
@staticmethod
def _prune_reports() -> None:
"""只保留最近 N 份报告([Compatibility] report_keep,<=0 表示不清理)。"""
keep = config.PREFLIGHT_REPORT_KEEP
if keep <= 0:
return
try:
files = [p for p in config.PREFLIGHT_REPORT_DIR.glob("preflight-*.*")
if p.is_file() and p.suffix.lower() in {".xlsx", ".csv"}]
files.sort(key=lambda p: p.stat().st_mtime, reverse=True)
except OSError:
return
for old in files[keep:]:
try:
old.unlink()
log.debug(f"🧹 清理旧报告: {old.name}")
except OSError as e:
log.warning(f"⚠️ 清理旧报告失败: {old.name} -> {e}")
@staticmethod
def _report_path(ext: str) -> Path:
"""集中目录 + 时间戳:<report_dir>/preflight-YYYYmmdd-HHMMSS.<ext>"""
@@ -402,6 +421,7 @@ class PreflightService:
report = cls._report_path("xlsx")
wb.save(report)
cls._prune_reports()
log.info(f"📄 兼容性报告: {report}")
return report
@@ -412,6 +432,7 @@ class PreflightService:
for _, _, values in rows:
lines.append(",".join('"' + str(v).replace('"', '""') + '"' for v in values) + "\n")
report.write_text("".join(lines), "utf-8")
cls._prune_reports()
log.info(f"📄 兼容性报告(CSV): {report}")
return report