#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""qc_vacuum_runall.py —— 18 个疑似空转脚本全部实跑：取脚本自己报的新增数（后台可跑）"""
import json, re, sqlite3, subprocess, time
D = "/root/gov_crawler/"
d = json.load(open(D + "qc_out/qc_d5_recheck_20260925.json", encoding="utf-8"))
db = sqlite3.connect("file:/root/search.db?mode=ro", uri=True, timeout=30)
tgt = []
for s, n, h in d["no_data"]:
    r = db.execute("SELECT IFNULL(SUM(new_count),0) FROM run_logs WHERE script_name=? AND run_date>=date('now','-30 day')", (s,)).fetchone()
    if r[0] == 0: tgt.append((s, n, h))
print("目标 %d 个" % len(tgt), flush=True)
res = []
for i, (s, n, h) in enumerate(tgt, 1):
    t0 = time.time()
    rec = dict(script=s, 站点=n, host=h)
    try:
        r = subprocess.run(["python3", D + s], capture_output=True, text=True, timeout=300, cwd=D)
        rec["exit"] = r.returncode
        outs = [x.strip() for x in (r.stdout or "").strip().split("\n") if x.strip()]
        rec["out"] = outs[-1][:110] if outs else ""
        ms = re.findall(r"(?:新增|入库|Inserted|new)[^\d]{0,6}(\d+)", r.stdout or "")
        rec["自报新增"] = ms[-1] if ms else "?"
        errs = [x.strip() for x in (r.stderr or "").strip().split("\n") if x.strip()]
        rec["err"] = errs[-1][:130] if errs else ""
    except subprocess.TimeoutExpired:
        rec["exit"] = "TIMEOUT"; rec["out"] = ""; rec["err"] = ">300s"; rec["自报新增"] = "?"
    rec["秒"] = int(time.time() - t0)
    res.append(rec)
    print("[%2d/%d] %-30s exit=%-8s 新增=%-4s %3ds | %s" % (
        i, len(tgt), s[:30], str(rec["exit"]), str(rec["自报新增"]), rec["秒"],
        (rec["out"] or rec["err"])[:60]), flush=True)
json.dump(res, open(D + "qc_out/qc_vacuum_runall.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)
print("DONE 已写 qc_out/qc_vacuum_runall.json", flush=True)
