fix(costreview): 历史清单定额展开支持重导后按批次+哈希重定位,并后台预升级过期批次
This commit is contained in:
@@ -458,7 +458,19 @@ def api_adopt_list(sid=0):
|
||||
finally:
|
||||
c.close()
|
||||
|
||||
_UPGRADE_LOCK = threading.Lock() # 决策 341:采纳内升级与后台预升级互斥——同批次绝不并发重导
|
||||
|
||||
|
||||
def _upgrade_batch_row(hc, row):
|
||||
"""决策 341 薄壳:过期判断(快速路径不占锁)→ 持锁重导。采纳内升级与后台预升级共用,
|
||||
绝不并发重导同一批。"""
|
||||
if row["kind"] != "project" or row["x_parser_ver"] == job.PARSER_VER:
|
||||
return None
|
||||
with _UPGRADE_LOCK:
|
||||
return _upgrade_batch_row_locked(hc, row)
|
||||
|
||||
|
||||
def _upgrade_batch_row_locked(hc, row):
|
||||
"""决策 34 的核心:把一个旧版批次行用**当前解析器**原位重导(row 需含 batches 全列)。
|
||||
|
||||
原位升级口径(区别于手动重导的「删旧插新」——绑定不能丢):
|
||||
@@ -471,8 +483,6 @@ def _upgrade_batch_row(hc, row):
|
||||
护栏:源文件缺失/内容哈希不符/加密/解析失败 → 保留旧数据只打日志,**绝不影响调用方**。
|
||||
返回 None(没升级)或 {"dropped_binds": n};异常由调用方兜(本函数自己 rollback 后转 None)。
|
||||
"""
|
||||
if row["kind"] != "project" or row["x_parser_ver"] == job.PARSER_VER:
|
||||
return None
|
||||
nm = row["x_src_hash"]
|
||||
path = _src_path(nm) if nm else None
|
||||
if not path:
|
||||
@@ -578,6 +588,53 @@ def _lme_lib_map(con, T, oid):
|
||||
return {r["code"]: [r["name"], r["spec"], r["unit"], r["kind"], r["ntp"], r["taxp"]]
|
||||
for r in rows if r["code"]}
|
||||
|
||||
|
||||
def upgrade_stale_batches_once():
|
||||
"""决策 341(2026-11-15 主人拍板「修下」):把解析器过期的工程批次**逐批预升级**一遍——
|
||||
把 279 按需重导的一次性成本从「采纳时卡几分钟」集中到启动后台消化。只升 kind='project';
|
||||
每批独立兜错(失败不挡后续);与采纳内升级共用 _upgrade_batch_row(内置互斥锁,绝不并发重导)。
|
||||
返回 (已升级数, 跳过数, 总数)。测试可同步直调;服务启动走 upgrade_stale_batches_bg()。"""
|
||||
hc = conn()
|
||||
try:
|
||||
rows = hc.execute("""SELECT * FROM batches WHERE kind='project'
|
||||
AND COALESCE(x_parser_ver,'')<>? ORDER BY id""",
|
||||
(job.PARSER_VER,)).fetchall()
|
||||
finally:
|
||||
hc.close()
|
||||
up = skip = 0
|
||||
for i, row in enumerate(rows):
|
||||
try:
|
||||
hc2 = conn()
|
||||
try:
|
||||
r = _upgrade_batch_row(hc2, row)
|
||||
finally:
|
||||
hc2.close()
|
||||
if r:
|
||||
up += 1
|
||||
else:
|
||||
skip += 1
|
||||
print("279 预升级 %d/%d:批次 #%s「%s」%s"
|
||||
% (i + 1, len(rows), row["id"], (row["Name"] or "")[:20],
|
||||
"已升级" if r else "跳过(源件缺失/哈希不符/不可解析)"), flush=True)
|
||||
except Exception as _e:
|
||||
skip += 1
|
||||
print("279 预升级批次 #%s 失败(不挡后续):%s" % (row["id"], _e), flush=True)
|
||||
return (up, skip, len(rows))
|
||||
|
||||
|
||||
def upgrade_stale_batches_bg():
|
||||
"""决策 341:启动后台线程跑 upgrade_stale_batches_once()——先让服务就绪(3s)再动库。"""
|
||||
def _bg():
|
||||
time.sleep(3)
|
||||
try:
|
||||
up, skip, total = upgrade_stale_batches_once()
|
||||
if total:
|
||||
print("279 后台预升级完成:升级 %d / 跳过 %d / 共 %d 批" % (up, skip, total), flush=True)
|
||||
except Exception as _e:
|
||||
print("279 预升级线程异常(不挡服务):%s" % _e, flush=True)
|
||||
threading.Thread(target=_bg, name="upgrade-stale-batches", daemon=True).start()
|
||||
|
||||
|
||||
def _norm_res(con, T, norm_id):
|
||||
"""一条定额下挂的工料机(**两侧唯一实现**):定额 → 消耗量 → 工料机。
|
||||
|
||||
|
||||
@@ -8616,17 +8616,32 @@ def _bill_norms(con, T, bill_id):
|
||||
return {"rows": out}
|
||||
|
||||
|
||||
def api_bill_norms(bill_id):
|
||||
"""批次内:一条清单下挂的定额(批次库)"""
|
||||
def api_bill_norms(bill_id, bhash=None, batch=None):
|
||||
"""批次内:一条清单下挂的定额(批次库)。
|
||||
决策 341(2026-11-15 主人拍板「修下」):279 按需重导会把 bills/norms 的 id 全部换新——
|
||||
对比框/检索行手里攥的旧 id 展开「定额/耗」会空。前端随 ± 把手带 bhash+batch,旧 id
|
||||
查不到清单行时按 **批次+内容哈希** 重定位(279 延伸同款),用新 id 取数;响应带重定位后的 bill_id。"""
|
||||
c = conn()
|
||||
try:
|
||||
d = _bill_norms(c, TBL["batch"], bill_id)
|
||||
_b = c.execute("SELECT batch_id FROM bills WHERE id=?", (bill_id,)).fetchone()
|
||||
if _b is None and bhash and batch:
|
||||
try:
|
||||
batch = int(batch)
|
||||
except (TypeError, ValueError):
|
||||
batch = 0
|
||||
_nb = c.execute("""SELECT id, batch_id FROM bills WHERE batch_id=? AND x_hash_exact=?
|
||||
ORDER BY id DESC LIMIT 1""", (batch, bhash)).fetchone()
|
||||
if _nb is not None:
|
||||
bill_id = _nb["id"]
|
||||
d = _bill_norms(c, TBL["batch"], bill_id)
|
||||
_b = _nb
|
||||
# 决策 289 补三(2026-11-09 主人定「显示哪条,耗就开他来源的工料机」):入库历史行
|
||||
# 耗浮层的**子级组成**要按批次取(/api/admin/lme/res?batch_id=)——响应带上本清单
|
||||
# 所属批次号,前端沿 tgHist→耗钮→浮层→± 传递,检索行/定额行本身都不用改。
|
||||
_b = c.execute("SELECT batch_id FROM bills WHERE id=?", (bill_id,)).fetchone()
|
||||
if _b is not None:
|
||||
d["batch_id"] = _b[0]
|
||||
d["bill_id"] = bill_id # 决策 341:重定位后的新 id
|
||||
return d
|
||||
finally:
|
||||
c.close()
|
||||
@@ -9255,7 +9270,7 @@ class Handler(SimpleHTTPRequestHandler):
|
||||
return self._json(_norm_res(_shared_ro("hist"), TBL["batch"], int(m.group(1))))
|
||||
m = re.match(r"^/api/admin/bill/(\d+)/norms$", p)
|
||||
if m:
|
||||
return self._json(api_bill_norms(int(m.group(1))))
|
||||
return self._json(api_bill_norms(int(m.group(1)), one.get("bhash"), one.get("batch")))
|
||||
except Exception as e:
|
||||
import traceback as _tb
|
||||
_tb.print_exc() # 决策 288:500 带栈落日志(排查基建,不再静默)
|
||||
@@ -9865,6 +9880,7 @@ def run():
|
||||
print("serving on http://127.0.0.1:%d (db=%s) (PID=%d)" % (PORT, DB, os.getpid()), flush=True)
|
||||
# 决策 126:端口已 listening,先让启动器/窗口走;清理回收在后台跑,慢也不再挡住开窗。
|
||||
threading.Thread(target=_startup_selfheal_bg, name="startup-selfheal", daemon=True).start()
|
||||
adopt_api.upgrade_stale_batches_bg() # 决策 341(主人拍板「修下」):后台逐批预升级过期批次——把采纳时的一次性重导卡顿集中到启动后台消化
|
||||
srv.serve_forever()
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user