feat(match): 工料机过滤统一到 lme_list_where 并用于检索口径

- 将「项目工料机」过滤规则唯一实现挪到 match.lme_list_where(),支持列前缀参数,工作台与后台统一引用
- 工料机检索(hist_search_lme 工程分支)同用该过滤,修复检索能搜出已被屏蔽单位(如台班)的问题
- 测试数据生成器支持独立参考库目录(REF_DIR),面板展示参考/测试库文件与存在状态
- 清理遗留的审查报告汇总与旧界面.html
This commit is contained in:
lianjianwei2024
2026-10-09 21:42:13 +08:00
parent 22acb0dfe2
commit eee1fe9029
10 changed files with 228 additions and 1626 deletions

View File

@@ -68,10 +68,15 @@ def _has_anchor(d):
or os.path.exists(os.path.join(d, TEST_H)))
REF_DIR = None # 参考库目录(None = 与数据目录同目录)
def db_paths(dd):
"""返回 (真实history, 测试history, 真实session, 测试session) 四个绝对路径。"""
return (os.path.join(dd, REAL_H), os.path.join(dd, TEST_H),
os.path.join(dd, "session.db"), os.path.join(dd, TEST_S))
"""返回 (真实history, 测试history, 真实session, 测试session) 四个绝对路径。
参考库可在独立目录(REF_DIR);测试库与附件一律落在数据目录 dd。"""
rd = REF_DIR or dd
return (os.path.join(rd, REAL_H), os.path.join(dd, TEST_H),
os.path.join(rd, "session.db"), os.path.join(dd, TEST_S))
def ensure_test_dbs(dd, log=print):
@@ -80,7 +85,9 @@ def ensure_test_dbs(dd, log=print):
rh, th, rs, ts = db_paths(dd)
if not os.path.exists(th):
if not os.path.exists(rh):
raise SystemExit("!! 数据目录里既没有 history.db 也没有 history-test.db,无法建测试库")
raise SystemExit("!! 参考目录里没有 %s(参考目录:%s),无法建测试库"
% (REAL_H, _disp(os.path.dirname(rh) or dd)))
os.makedirs(dd, exist_ok=True) # 测试库独立目录可能还不存在
s = sqlite3.connect(rh)
d = sqlite3.connect(th)
s.backup(d)
@@ -102,7 +109,7 @@ def find_data_dir():
try:
with open(CFG_PATH, encoding="utf-8") as f:
p = (json.load(f) or {}).get("data_dir") or ""
if p and os.path.exists(os.path.join(p, "history.db")):
if p and _has_anchor(p):
return os.path.normpath(p)
except Exception:
pass
@@ -122,9 +129,21 @@ def find_data_dir():
return None
def set_data_dir(p):
with open(CFG_PATH, "w", encoding="utf-8") as f:
json.dump({"data_dir": p}, f, ensure_ascii=False, indent=2)
def set_data_dir(p, ref_dir=None):
cfg = {}
if os.path.exists(CFG_PATH):
try:
with open(CFG_PATH, encoding="utf-8") as f:
cfg = json.load(f) or {}
except Exception:
cfg = {}
cfg["data_dir"] = p
if ref_dir is not None:
cfg["ref_dir"] = ref_dir
tmp = CFG_PATH + ".tmp"
with open(tmp, "w", encoding="utf-8") as f:
json.dump(cfg, f, ensure_ascii=False, indent=2)
os.replace(tmp, CFG_PATH)
# ---------------- 归一化 / 哈希(复刻 core/import_job.py 口径) ----------------
@@ -521,18 +540,26 @@ def _parser_vers(dd):
"""从母体 app 源码读当前解析器版本(工程/价格两条线)。
stale_batches() 按 kind 分开比,生成器必须同样分开盖版本号,否则全部误报过期。"""
proj, price = PARSER_VER_FALLBACK, PARSER_VER_PRICE_FALLBACK
core = os.path.join(os.path.dirname(dd), "app", "core")
for fname, defv in (("import_job.py", "proj"), ("price_html.py", "price")):
try:
txt = open(os.path.join(core, fname), encoding="utf-8").read()
m = re.search(r"PARSER_VER\s*=\s*['\"]([^'\"]+)", txt)
if m:
if defv == "proj":
proj = m.group(1)
else:
price = m.group(1)
except Exception:
pass
bases = [os.path.dirname(dd)]
if REF_DIR:
rb = os.path.dirname(REF_DIR)
if rb not in bases:
bases.append(rb) # 测试库在自定义目录时,参考库旁才是母体
for base in bases:
core = os.path.join(base, "app", "core")
for fname, defv in (("import_job.py", "proj"), ("price_html.py", "price")):
try:
txt = open(os.path.join(core, fname), encoding="utf-8").read()
m = re.search(r"PARSER_VER\s*=\s*['\"]([^'\"]+)", txt)
if m:
if defv == "proj":
proj = m.group(1)
else:
price = m.group(1)
except Exception:
pass
if proj != PARSER_VER_FALLBACK and price != PARSER_VER_PRICE_FALLBACK:
break
return proj, price
@@ -1267,6 +1294,8 @@ def delete_testdata(dd, keep_files=False, log=print):
# ---------------- 编排入口 ----------------
def run(cfg, log=print):
global REF_DIR
REF_DIR = (cfg.get("ref_dir") or "").strip() or None
dd = cfg.get("data_dir") or find_data_dir()
if not dd:
log("!! 找不到数据目录(history.db)。请在控制台里手动指定数据目录。")
@@ -1276,7 +1305,8 @@ def run(cfg, log=print):
sc = _open_db(ts) if os.path.exists(ts) else None
rng = random.Random() # 每次运行真随机(不固定种子)
words = Words(hc)
log("数据目录:%s" % _disp(dd))
log("参考库目录:%s" % _disp(os.path.dirname(db_paths(dd)[0])))
log("测试库目录:%s" % _disp(dd))
log("词料:项目名 %d / 定额 %d / 工料机 %d / 建设单位 %d" % (
len(words.stems), len(words.norms), len(words.lmes), len(words.builders)))
t0 = time.time()

View File

@@ -37,6 +37,15 @@ def log(msg):
pass
def _cfg():
"""读 config.json(找不到/坏档返回空 dict)。"""
try:
with open(G.CFG_PATH, encoding="utf-8") as f:
return json.load(f) or {}
except Exception:
return {}
def running():
p = _state["proc"]
return p is not None and p.poll() is None
@@ -111,6 +120,16 @@ class H(BaseHTTPRequestHandler):
s = G.stats_of(dd) if dd else {"data_dir": None}
except Exception as e:
s = {"data_dir": dd, "error": str(e)}
rd = _cfg().get("ref_dir", "")
G.REF_DIR = rd or None
if dd:
rh, th, rs, ts = G.db_paths(dd)
s["ref_dir"] = os.path.dirname(rh)
s["test_dir"] = dd
s["ref_names"] = [os.path.basename(rh), os.path.basename(rs)]
s["test_names"] = [os.path.basename(th), os.path.basename(ts)]
s["ref_ok"] = [os.path.exists(rh), os.path.exists(rs)]
s["test_ok"] = [os.path.exists(th), os.path.exists(ts)]
s["running"] = running()
s["mode"] = _state["mode"] if running() else None
self._send(200, s)
@@ -126,21 +145,35 @@ class H(BaseHTTPRequestHandler):
except Exception:
body = {}
if self.path == "/api/dir":
p = (body.get("data_dir") or "").strip()
if p and (os.path.exists(os.path.join(p, "history.db"))
or os.path.exists(os.path.join(p, G.TEST_H))):
G.set_data_dir(p)
self._send(200, {"ok": True, "data_dir": p})
def _normdir(v):
p = (v or "").strip()
if not p:
return ""
p = os.path.normpath(p)
if os.path.isfile(p): # 粘贴了 .db 文件路径 → 取其目录
p = os.path.dirname(p)
return p if os.path.isdir(p) else ""
c0 = _cfg()
t_raw, r_raw = body.get("test_dir"), body.get("ref_dir")
td = _normdir(t_raw) if t_raw is not None else c0.get("data_dir", "")
rd = (_normdir(r_raw) if r_raw else "") if r_raw is not None else c0.get("ref_dir", "")
if not td:
self._send(200, {"ok": False, "msg": "测试库目录无效(目录不存在)"})
elif rd and not (os.path.exists(os.path.join(rd, G.REAL_H))
or os.path.exists(os.path.join(rd, G.TEST_H))):
self._send(200, {"ok": False, "msg": "参考目录里没有 history.db"})
else:
self._send(200, {"ok": False,
"msg": "目录里没有 history.db / history-test.db"})
G.set_data_dir(td, rd or None)
self._send(200, {"ok": True, "test_dir": td, "ref_dir": rd})
elif self.path == "/api/run":
cfg = {k: G.DEFAULTS.get(k) for k in G.DEFAULTS}
cfg.update({k: v for k, v in body.items() if k in G.DEFAULTS})
cfg["data_dir"] = G.find_data_dir()
cfg["ref_dir"] = _cfg().get("ref_dir", "")
ok, msg = start_sub("run", cfg)
self._send(200, {"ok": ok, "msg": msg})
elif self.path == "/api/align":
G.REF_DIR = _cfg().get("ref_dir", "") or None
dd = G.find_data_dir()
if not dd:
self._send(200, {"ok": False, "msg": "找不到数据目录"})
@@ -198,6 +231,14 @@ height:300px;overflow-y:auto;padding:12px;border-radius:6px;white-space:pre-wrap
.chk{display:flex;align-items:center;gap:6px;font-size:13px;color:var(--txt)}
.sec-h{font-size:12px;color:var(--sub);margin:10px 0 6px;font-weight:bold}
.err{color:var(--warn)}
.dblab{display:inline-block;width:84px;text-align:center;border-radius:4px;
padding:2px 0;font-size:12px;color:#fff;flex:none}
.dblab.ref{background:#646a73}.dblab.test{background:var(--pri)}
.dbin{flex:1;min-width:260px}
.dbname{font:12px Consolas,monospace}
.badge{font-size:12px;padding:1px 8px;border-radius:10px;flex:none}
.badge.ok{color:var(--ok);background:#eaf6e8}
.badge.no{color:var(--sub);background:#eef0f3}
@media(max-width:860px){.grid{grid-template-columns:repeat(2,1fr)}}
</style></head><body>
<h1>testdata · 测试数据生成器</h1>
@@ -205,11 +246,20 @@ height:300px;overflow-y:auto;padding:12px;border-radius:6px;white-space:pre-wrap
history-test.db / session-test.db(首次自动从真实库克隆底子),<b>真实库零污染</b>——
对接软件时把测试库改名换上即可;换到别的母体后点一次「版本对齐」,删除只是兜底手段。</div>
<div class="card"><h2>数据目录</h2>
<div class="card"><h2>数据库</h2>
<div class="row" style="margin-top:0">
<span class="dblab ref">参考数据库</span>
<input type="text" id="refdir" class="dbin" placeholder="留空 = 与测试库同目录">
<button class="gray" onclick="saveDir('ref')">保存</button>
<span id="refok" class="badge"></span><span id="refmsg" class="sub"></span></div>
<div class="row">
<input type="text" id="dd" placeholder="自动探测……">
<button class="gray" onclick="saveDir()">保存</button>
<span id="ddmsg" class="sub"></span></div></div>
<span class="dblab test">测试数据库</span>
<input type="text" id="testdir" class="dbin" placeholder="自动探测……">
<button class="gray" onclick="saveDir('test')">保存</button>
<span id="testok" class="badge"></span><span id="testmsg" class="sub"></span></div>
<div class="sub" style="margin-top:8px">生成只写<b>测试数据库</b>(history-test.db / session-test.db,
附件也落这个目录,对接软件时整个目录拿走换名即可);<b>参考数据库</b>(history.db / session.db)
只读,仅作克隆底子。两框均可直接粘贴目录或 .db 文件路径(自动取其目录)。</div></div>
<div class="card"><h2>一、后台数据(history-test.db)</h2>
<div class="sec-h">入库项目</div>
@@ -293,11 +343,25 @@ async function doStop(){await post("/api/stop",{});}
async function doDel(){if(!confirm("确认真删除全部「测试」前缀的模拟数据?此操作不可恢复!"))return;
await post("/api/delete",{});poll();}
async function doAlign(){const r=await post("/api/align",{});alert(r.msg||"完成");}
async function saveDir(){const r=await post("/api/dir",{data_dir:document.getElementById("dd").value});
document.getElementById("ddmsg").textContent=r.ok?"已保存":(r.msg||"失败");loadStats();}
async function saveDir(which){
const b={};
if(which==="ref")b.ref_dir=document.getElementById("refdir").value;
else b.test_dir=document.getElementById("testdir").value;
const r=await post("/api/dir",b);
const m=document.getElementById(which+"msg");
if(m)m.textContent=r.ok?"已保存":(r.msg||"失败");
loadStats();}
function kpi(t,v,cls){return '<div class="kpi">'+t+' <b'+(cls?' class="'+cls+'"':'')+'>'+v+'</b></div>';}
async function loadStats(){const s=await(await fetch("/api/stats")).json();
if(s.data_dir)document.getElementById("dd").value=s.data_dir;
const rf=document.getElementById("refdir"),tf=document.getElementById("testdir");
if(document.activeElement!==rf)rf.value=s.ref_dir||"";
if(document.activeElement!==tf)tf.value=s.test_dir||s.data_dir||"";
const ro=(s.ref_ok||[]).length?s.ref_ok.every(x=>x):false;
const re=document.getElementById("refok");
re.textContent=ro?"在":"缺";re.className="badge "+(ro?"ok":"no");
const to=(s.test_ok||[]).length?s.test_ok.every(x=>x):false;
const te=document.getElementById("testok");
te.textContent=to?"已生成":"尚未生成";te.className="badge "+(to?"ok":"no");
let h="";
h+=kpi("后台项目",s.b_project??0);h+=kpi("市场价",s.b_market??0);h+=kpi("信息价",s.b_info??0);
h+=kpi("清单",s.bills??0);h+=kpi("定额",s.norms??0);h+=kpi("定额消耗",s.norm_res??0);