feat(match): 落地搜索匹配口径四件套与次精确名称护栏,同步字集键入库及测试面板改进

This commit is contained in:
lianjianwei2024
2026-10-10 14:58:13 +08:00
parent a3697ba271
commit 6de1370ce5
12 changed files with 940 additions and 286 deletions

View File

@@ -572,6 +572,18 @@ def _open_db(path):
con.execute("PRAGMA busy_timeout=30000")
return con
def _open_ro(path, timeout=3):
"""只读打开,短超时:库被其它进程(造价审核软件 / 生成子进程)占用时快速失败返回
None,绝不死等。原 stats_of/totals_of 用 15~30s 超时,会导致面板整页卡死。"""
try:
con = sqlite3.connect("file:%s?mode=ro" % path.replace("\\", "/"),
uri=True, timeout=timeout)
con.execute("PRAGMA busy_timeout=%d" % int(timeout * 1000))
return con
except Exception:
return None
# ---------------- 结构级插入(history 与 session 两库共用一份逻辑数据) ----------------
# 逻辑行里的 owner 索引是本地序号,插入时映射为真实 id。
@@ -1189,7 +1201,10 @@ def stats_of(dd):
if not os.path.exists(th):
out["note"] = "测试库尚未生成(%s 不存在),真实库从未被本工具写过" % TEST_H
return out
hc = _open_db(th)
hc = _open_ro(th)
if hc is None:
out["note"] = "测试库被占用(可能正在生成或造价审核软件打开中),稍后点「刷新统计」重试"
return out
q = "FROM batches WHERE is_mock=1 AND Name LIKE '测试%'"
for k in ("project", "market", "info"):
out["b_%s" % k] = hc.execute("SELECT COUNT(*) " + q + " AND kind=?", (k,)).fetchone()[0]
@@ -1214,65 +1229,62 @@ def stats_of(dd):
hc.close()
sp = ts
if os.path.exists(sp):
sc = _open_db(sp)
out["s_sessions"] = sc.execute(
"SELECT COUNT(*) FROM sessions WHERE name LIKE '测试%'").fetchone()[0]
for t in ("s_bills", "s_norms", "s_lmes"):
try:
if t == "s_norms": # s_norms.bill_id 挂在 s_bills 上,要经两层
out[t] = sc.execute(
"SELECT COUNT(*) FROM s_norms WHERE bill_id IN (SELECT id FROM s_bills "
"WHERE session_id IN (SELECT id FROM sessions WHERE name LIKE '测试%'))"
).fetchone()[0]
else:
out[t] = sc.execute(
"SELECT COUNT(*) FROM %s WHERE session_id IN "
"(SELECT id FROM sessions WHERE name LIKE '测试%%')" % t).fetchone()[0]
except Exception:
sc = _open_ro(sp)
if sc is None:
out["s_sessions"] = -1
for t in ("s_bills", "s_norms", "s_lmes"):
out[t] = -1
sc.close()
else:
out["s_sessions"] = sc.execute(
"SELECT COUNT(*) FROM sessions WHERE name LIKE '测试%'").fetchone()[0]
for t in ("s_bills", "s_norms", "s_lmes"):
try:
if t == "s_norms": # s_norms.bill_id 挂在 s_bills 上,要经两层
out[t] = sc.execute(
"SELECT COUNT(*) FROM s_norms WHERE bill_id IN (SELECT id FROM s_bills "
"WHERE session_id IN (SELECT id FROM sessions WHERE name LIKE '测试%'))"
).fetchone()[0]
else:
out[t] = sc.execute(
"SELECT COUNT(*) FROM %s WHERE session_id IN "
"(SELECT id FROM sessions WHERE name LIKE '测试%%')" % t).fetchone()[0]
except Exception:
out[t] = -1
sc.close()
return out
# ---------------- 数据库资产总览(两库全量家底,打开面板即看) ----------------
def totals_of(dd):
"""参考库与测试库的全量资产计数(不区分 mock,只数行数)。
缺文件的库返回 None。全部走只读连接,不碰写入。"""
缺文件 / 被占用的库返回 None。全部走只读短超时连接,库被造价审核软件或生成子进程
占用时快速返回(带 locked 标记),绝不死等——否则 /api/stats 卡 15~30s 会让整页假死。"""
rh, th, rs, ts = db_paths(dd)
def h_totals(p):
def one(p, tables):
if not os.path.exists(p):
return None
c = sqlite3.connect("file:%s?mode=ro" % p.replace("\\", "/"), uri=True,
timeout=15)
c.execute("PRAGMA busy_timeout=15000")
return None, False
c = _open_ro(p, timeout=3)
if c is None:
return None, True # 被占用
out = {}
for t in ("batches", "bills", "norms", "norm_res", "lmes", "lme_lib",
"prices", "attachments"):
for t in tables:
try:
out[t] = c.execute("SELECT COUNT(*) FROM %s" % t).fetchone()[0]
except Exception:
out[t] = -1
c.close()
return out
return out, False
def s_totals(p):
if not os.path.exists(p):
return None
c = sqlite3.connect("file:%s?mode=ro" % p.replace("\\", "/"), uri=True,
timeout=15)
c.execute("PRAGMA busy_timeout=15000")
out = {}
for t in ("sessions", "s_bills", "s_norms", "s_lmes"):
try:
out[t] = c.execute("SELECT COUNT(*) FROM %s" % t).fetchone()[0]
except Exception:
out[t] = -1
c.close()
return out
return {"ref_h": h_totals(rh), "ref_s": s_totals(rs),
"test_h": h_totals(th), "test_s": s_totals(ts)}
H = ("batches", "bills", "norms", "norm_res", "lmes", "lme_lib",
"prices", "attachments")
S = ("sessions", "s_bills", "s_norms", "s_lmes")
rh_d, l1 = one(rh, H); th_d, l2 = one(th, H)
rs_d, l3 = one(rs, S); ts_d, l4 = one(ts, S)
locked = [n for n, f in (("参考history", l1), ("参考session", l3),
("测试history", l2), ("测试session", l4)) if f]
return {"ref_h": rh_d, "ref_s": rs_d, "test_h": th_d, "test_s": ts_d,
"locked": locked}
# ---------------- 分块删除 ----------------