#!/usr/bin/env python3
"""批量增量测试所有启用的爬虫"""
import json, os, sys, time, subprocess
from datetime import datetime

BASE = os.path.expanduser("~/Nutstore Files/我的坚果云/Crawler/gov_crawler")
os.chdir(BASE)

cfg = json.load(open("daily_crawl_config.json"))["crawlers"]
enabled = [c for c in cfg if c.get('enabled') and c.get('incremental')]

print(f"\n{'='*60}")
print(f"批量增量测试：{len(enabled)} 个爬虫")
print(f"开始时间: {datetime.now().strftime('%H:%M:%S')}")
print(f"{'='*60}\n")

# 本地 search.db 路径，供爬虫通过 os.getenv("SEARCH_DB") 读取
LOCAL_DB = os.path.join(BASE, "search.db")
RUN_ENV = {**os.environ, "PYTHONWARNINGS": "ignore", "SEARCH_DB": LOCAL_DB}

results = []
for i, c in enumerate(enabled, 1):
    name = c['name']
    script = c['script']
    args = c.get('args', '')
    
    # 预检：脚本文件是否存在
    script_path = script
    if script.startswith('node '):
        script_path = script.replace('node ', '')
        script_cmd = script
    else:
        script_cmd = f"python3 {script}"
    
    if not os.path.exists(os.path.join(BASE, script_path)):
        print(f"[{i}/{len(enabled)}] {name}... ❌ 0s | ⏭ 脚本不存在: {script_path}")
        results.append({"name": name, "status": "MISSING", "elapsed": "0s", "error": "脚本不存在"})
        continue
    
    cmd = f"{script_cmd} {args}"
    print(f"[{i}/{len(enabled)}] {name}... ", end="", flush=True)
    t0 = time.time()
    
    try:
        proc = subprocess.run(
            cmd, shell=True, capture_output=True, text=True, timeout=180,
            env=RUN_ENV
        )
        elapsed = time.time() - t0
        
        if proc.returncode == 0:
            # Extract key info from output
            lines = proc.stdout.strip().split('\n')
            info_lines = [l for l in lines if '✅' in l or '❌' in l or '💾' in l or '完成' in l or '耗时' in l]
            summary = ' | '.join(info_lines[-3:]) if info_lines else 'OK'
            print(f"✅ {elapsed:.0f}s | {summary[:80]}")
            results.append({"name": name, "status": "OK", "elapsed": f"{elapsed:.0f}s"})
        else:
            err = proc.stderr.strip()[-100:] if proc.stderr else ""
            print(f"❌ {elapsed:.0f}s | {err}")
            results.append({"name": name, "status": "FAIL", "elapsed": f"{elapsed:.0f}s", "error": err})
    except subprocess.TimeoutExpired:
        print(f"⏰ 超时 (>120s)")
        results.append({"name": name, "status": "TIMEOUT", "elapsed": ">120s"})
    except Exception as e:
        print(f"💥 {str(e)[:60]}")
        results.append({"name": name, "status": "ERROR", "elapsed": "-", "error": str(e)[:60]})

print(f"\n{'='*60}")
print(f"测试完成！{datetime.now().strftime('%H:%M:%S')}")
ok = sum(1 for r in results if r['status'] == 'OK')
fail = sum(1 for r in results if r['status'] != 'OK')
print(f"✅ 成功: {ok}  ❌ 失败: {fail}")
print(f"\n{'='*60}")
for r in results:
    icon = '✅' if r['status'] == 'OK' else '❌'
    duration = r.get('elapsed', '-')
    err = f" | {r.get('error', '')}" if r.get('error') else ''
    print(f"  {icon} [{duration}] {r['name']}{err}")
print(f"{'='*60}")
