# -*- coding: utf-8 -*-
"""详细分析 v8 日志，提取所有任务状态。"""
import json
import re
from pathlib import Path
from dotenv import load_dotenv

load_dotenv(Path(__file__).resolve().parents[2] / ".env")
from _env_paths import get_output_path

log_dir = get_output_path("SYSTEM_LOG_ROOT")
raw = json.loads((log_dir / "2026-02-21__etl_run_raw_v8.json").read_text("utf-8"))

error_log = raw.get("error_log", "")
output_log = raw.get("output_log", "")
full = output_log + "\n" + error_log

# 更宽泛的匹配
success_pat = re.compile(r"任务\s+(\S+)\s+执行成功")
fail_pat = re.compile(r"任务\s+(\S+)\s+失败")
complete_pat = re.compile(r"(\S+)\s*(?:完成|成功|✅)")

# 查找所有提到的任务
task_pat = re.compile(r"(ODS_FETCH|DWD_LOAD_FROM_ODS|DWS_\w+)")
all_tasks = set(task_pat.findall(full))

print("=== 日志中出现的所有任务 ===")
for t in sorted(all_tasks):
    print(f"  {t}")

print(f"\n=== 成功匹配 ===")
for m in success_pat.finditer(full):
    print(f"  ✅ {m.group(1)}")

print(f"\n=== 失败匹配 ===")
for m in fail_pat.finditer(full):
    print(f"  ❌ {m.group(1)}")

# 查找 DWD 装载详情
dwd_pat = re.compile(r"DWD 装载(成功|失败)：(\S+?)，用时 ([\d.]+)s(?:，err=(.*))?")
print(f"\n=== DWD 装载详情 ===")
dwd_success = 0
dwd_fail = 0
for m in dwd_pat.finditer(full):
    status, table, dur, err = m.groups()
    icon = "✅" if status == "成功" else "❌"
    if status == "成功":
        dwd_success += 1
    else:
        dwd_fail += 1
    line = f"  {icon} {table} ({dur}s)"
    if err:
        line += f" — {err[:80]}"
    print(line)
print(f"  合计: {dwd_success} 成功, {dwd_fail} 失败")

# 查找 year -1 相关上下文
print(f"\n=== 'year -1' 相关行 ===")
for line in full.split("\n"):
    if "year" in line.lower() and ("-1" in line or "out of range" in line):
        print(f"  {line.strip()[:200]}")
