#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""qc_noimport.py —— 「跑了但没入库」专项：脚本产出 JSON/成功运行，但库内 0 行（只读）"""
import json, os, re, sqlite3
from collections import Counter
D = "/root/gov_crawler/"
db = sqlite3.connect("file:/root/search.db?mode=ro", uri=True, timeout=30)
db.execute("PRAGMA busy_timeout=30000")
CFG = json.load(open(D + "daily_crawl_config.json", encoding="utf-8"))
cfg = CFG if isinstance(CFG, list) else CFG.get("tasks", CFG.get("scripts", []))
rows = []
for c in cfg:
    s = (c.get("script") or "").strip()
    if not s or not s.endswith(".py"):
        continue
    rows.append((s, c.get("name") or "", c.get("enabled", True)))
# 每脚本：库内行数（按 script_name 精确）+ run_logs 最近成功
out = []
for s, name, en in rows:
    if not en:
        continue
    n = db.execute("SELECT COUNT(*) FROM gov_raw WHERE script_name=?", (s,)).fetchone()[0]
    if n:
        continue  # 有数据，跳过
    ok = db.execute("SELECT COUNT(*) FROM run_logs WHERE script_name=? AND status='成功'", (s,)).fetchone()[0]
    run = db.execute("SELECT COUNT(*) FROM run_logs WHERE script_name=?", (s,)).fetchone()[0]
    last = db.execute("SELECT MAX(run_date) FROM run_logs WHERE script_name=?", (s,)).fetchone()[0] or ""
    p = D + s
    src = open(p, encoding="utf-8", errors="ignore").read() if os.path.exists(p) else ""
    jsonish = bool(re.search(r"JSON_OUTPUT|json\.dump|--json|jsonl|to_json|OUT_JSON", src))
    has_insert = bool(re.search(r"INSERT\s+(OR\s+\w+\s+)?INTO\s+gov_raw", src, re.I))
    out.append(dict(script=s, 站点=name[:22], 成功次数=ok, 运行次数=run, 末次=last,
                    JSON输出=jsonish, 有INSERT=has_insert, 文件="有" if src else "无"))
print("config 启用脚本数: %d | 其中库内 0 行: %d" % (sum(1 for r in rows if r[2]), len(out)))
print("\n=== 分类 ===")
print("  A 跑了但没入库（有 run_logs 成功 + 脚本含 INSERT）: %d" % sum(1 for x in out if x["成功次数"] and x["有INSERT"]))
print("  B JSON 导出型（有成功 + JSON输出 + 无 INSERT）    : %d" % sum(1 for x in out if x["成功次数"] and x["JSON输出"] and not x["有INSERT"]))
print("  C 从未成功（真跑不动）                            : %d" % sum(1 for x in out if not x["成功次数"]))
print("  D 无 run_logs 记录                                : %d" % sum(1 for x in out if not x["运行次数"]))
print("\n=== B 类样例（JSON 导出型，最多 12）===")
for x in [y for y in out if y["成功次数"] and y["JSON输出"] and not y["有INSERT"]][:12]:
    print("  %-34s %-20s 成功%3d 末次%s" % (x["script"][:34], x["站点"], x["成功次数"], x["末次"]))
print("\n=== A 类样例（有 INSERT 但 0 行，最多 12）===")
for x in [y for y in out if y["成功次数"] and y["有INSERT"]][:12]:
    print("  %-34s %-20s 成功%3d/%d 末次%s" % (x["script"][:34], x["站点"], x["成功次数"], x["运行次数"], x["末次"]))
json.dump(out, open(D + "qc_out/qc_noimport_20260925.json", "w", encoding="utf-8"), ensure_ascii=False, indent=1)
print("\n已写 qc_out/qc_noimport_20260925.json")
db.close()
