"""重跑 DWS_FINANCE_INCOME_STRUCTURE 验证 pay_time JOIN 修复"""
import os, sys, subprocess, time
from pathlib import Path
from datetime import datetime
from dotenv import load_dotenv
from zoneinfo import ZoneInfo

load_dotenv(Path(__file__).resolve().parents[2] / ".env")
TZ = ZoneInfo("Asia/Shanghai")
ETL_CWD = Path(__file__).resolve().parents[2] / "apps" / "etl" / "connectors" / "feiqiu"

cmd = [
    "uv", "run", "--package", "etl-feiqiu",
    "python", "-m", "cli.main",
    "--layers", "DWS",
    "--tasks", "DWS_FINANCE_INCOME_STRUCTURE",
    "--window-start", "2025-11-01 00:00:00",
    "--window-end", "2026-02-27 00:00:00",
    "--window-split-days", "30",
    "--force-full",
]

now = datetime.now(TZ)
print(f"FINANCE_INCOME 重跑 | {now.isoformat()}")
start = time.time()
proc = subprocess.run(
    cmd, cwd=str(ETL_CWD),
    capture_output=True, text=True,
    encoding="utf-8", errors="replace",
    timeout=300,
)
elapsed = time.time() - start
output = proc.stdout + proc.stderr
has_error = any(
    kw in line for line in output.splitlines()
    for kw in ("ERROR", "CRITICAL", "Traceback")
)
ok = proc.returncode == 0 and not has_error
icon = "OK" if ok else "FAIL"
print(f"结果: {icon} ({elapsed:.0f}s)")
if not ok:
    for line in output.splitlines()[-30:]:
        print(f"  | {line}")

log_root = os.environ.get("SYSTEM_LOG_ROOT")
if not log_root:
    raise RuntimeError("SYSTEM_LOG_ROOT 未设置")
log_dir = Path(log_root)
log_dir.mkdir(parents=True, exist_ok=True)
log_file = log_dir / f"{now.strftime('%Y%m%d')}_rerun_finance_v6.log"
with open(log_file, "w", encoding="utf-8") as f:
    f.write(f"[{icon}] DWS_FINANCE_INCOME_STRUCTURE ({elapsed:.0f}s)\n")
    f.write(output)
print(f"日志: {log_file}")
sys.exit(0 if ok else 1)
