#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""scan_para_fleet.py —— 全库扫「段落被拍平」规模（无 <p> 的长正文）

判据：content 长度 > 400 且 **完全不含 <p** → 正文没有段落标签，行内 \n 会被折叠
      （search_app 对无 <p>/<table> 的内容按 Markdown 渲染，长纯文本会挤成一坨）
只统计，不改任何数据。逐行迭代，不用 fetchall（生产机内存 1.75G）。
"""
import re
import sqlite3
import time

db = sqlite3.connect("/root/search.db", timeout=60)
db.execute("PRAGMA busy_timeout=60000")

t0 = time.time()
print("=== ① 按站点统计「长正文且完全无 <p>」 ===")
q = ("SELECT site_name, COUNT(*) c, MAX(LENGTH(content)) mx FROM gov_raw "
     "WHERE content IS NOT NULL AND LENGTH(content) > 400 "
     "AND content NOT LIKE '%<p%' AND content NOT LIKE '%<table%' "
     "GROUP BY site_name ORDER BY c DESC")
tot = 0
rows = []
for site, c, mx in db.execute(q):
    tot += c
    rows.append((site, c, mx))
print("命中站点数: %d | 命中行数合计: %d" % (len(rows), tot))
for site, c, mx in rows[:25]:
    print("   %-44s %6d 行  最长 %d" % ((site or "(空)")[:44], c, mx))
print("   （耗时 %.1fs）" % (time.time() - t0))

print()
print("=== ② 对照：全库总量 ===")
print("   gov_raw 总行数:", db.execute("SELECT COUNT(*) FROM gov_raw").fetchone()[0])
print("   含 <p> 的行  :", db.execute("SELECT COUNT(*) FROM gov_raw WHERE content LIKE '%<p%'").fetchone()[0])

print()
print("=== ③ 抽 2 条看实际形态 ===")
for site, c, mx in rows[:2]:
    r = db.execute("SELECT title, content FROM gov_raw WHERE site_name=? AND LENGTH(content)>400 "
                   "AND content NOT LIKE '%<p%' LIMIT 1", (site,)).fetchone()
    if r:
        print("  【%s】%s" % ((site or "")[:40], (r[0] or "")[:40]))
        print("     ", repr((r[1] or "")[:220]))
db.close()
print("\n完成（只读，未改数据）")
