#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""自动清理 60 天前的复盘网页报告 /root/web_reports/report_*.html
由系统 crontab 每日 08:00 调用 (0 8 * * * cd /root/web_reports && /usr/bin/python3 cleanup.py >> cleanup.log 2>&1)
只删 report_*.html, 不动其它文件; 保留 60 天内的历史链接有效。"""
import pathlib
import time

WEB_DIR = pathlib.Path("/root/web_reports")
MAX_AGE_DAYS = 60

now = time.time()
deleted = []
for f in sorted(WEB_DIR.glob("report_*.html")):
    try:
        age_days = (now - f.stat().st_mtime) / 86400
    except FileNotFoundError:
        continue
    if age_days > MAX_AGE_DAYS:
        f.unlink(missing_ok=True)
        deleted.append(f.name)

remaining = len(list(WEB_DIR.glob("report_*.html")))
if deleted:
    print(f"[cleanup {time.strftime('%Y-%m-%d %H:%M')}] 删除 {len(deleted)} 个超过 {MAX_AGE_DAYS} 天的 HTML: {', '.join(deleted)}; 剩余 {remaining}")
else:
    print(f"[cleanup {time.strftime('%Y-%m-%d %H:%M')}] 无超过 {MAX_AGE_DAYS} 天的 HTML (当前 {remaining} 个), 无需清理")