diff --git a/CostReview/app/core/session_job.py b/CostReview/app/core/session_job.py index 7eec67e..8336995 100644 --- a/CostReview/app/core/session_job.py +++ b/CostReview/app/core/session_job.py @@ -189,6 +189,31 @@ def dedupe_lme_res(con=None): con.close() +def drop_orphan_rows(): + """启动自愈:s_ 表里 session_id 已不存在(会话早被删)的孤儿行清掉,幂等。 + + 2026-10-11 起因:s_lme_res 残留幽灵 sid(32/39/60~67,漏表时代老账)—— + dedupe 只去重不删孤儿,会话没了它们的组成行还躺在库里。口径与 wipe 一致: + 动态发现带 session_id 列的 s_ 表、跳过 WIPE_KEEP(s_remarks/s_settings 本就没有 + session_id 列,天然不沾)。返回删除行数。""" + con = connect() + try: + n = 0 + for (t,) in con.execute("SELECT name FROM sqlite_master WHERE type='table'").fetchall(): + if not t.startswith("s_") or t in WIPE_KEEP: + continue + if "session_id" in {x[1] for x in con.execute("PRAGMA table_info(%s)" % t)}: + cur = con.execute("DELETE FROM %s WHERE session_id NOT IN (SELECT id FROM sessions)" % t) + n += cur.rowcount or 0 + con.commit() + return n + except Exception: + con.rollback() + return -1 + finally: + con.close() + + def drop(con, sid): """连会话行一起删(台账里「删除」用)""" wipe(con, sid) diff --git a/CostReview/app/server/api_server.py b/CostReview/app/server/api_server.py index bf0e8b5..f0d8758 100644 --- a/CostReview/app/server/api_server.py +++ b/CostReview/app/server/api_server.py @@ -1317,6 +1317,26 @@ def _purge_upload_files(names): return gone +def _purge_orphan_uploads(): + """启动自愈:uploads 里**零引用**的源文件副本全量对账回收(幂等)。 + + 2026-10-11 起因:删除路径只清被删会话当时引用的文件,模拟数据工具等渠道留下的 + 孤儿会一直积压(实测 1799 个)。引用口径与 _purge_upload_files 完全一致 + (sessions.src_name 分段 + batches.x_src_hash),复用它逐个判+删,返回删除个数。""" + if not os.path.isdir(UPLOAD_DIR): + return 0 + gone = 0 + for sub in os.listdir(UPLOAD_DIR): + d = os.path.join(UPLOAD_DIR, sub) + if not os.path.isdir(d) or len(sub) != 2: + continue + for f in os.listdir(d): + # 只认 _<原名> 且桶号=sha 前两位的规矩文件,别的一律不碰 + if f.startswith(sub) and "_" in f and os.path.isfile(os.path.join(d, f)): + gone += _purge_upload_files([f]) + return gone + + def api_sessions(): """最近审核列表:按「项目名称」去重——同名工程只留最近一条(2026-10-07 主人定) @@ -3830,6 +3850,14 @@ def run(): license_gate.init(DATA_DIR) # 授权门禁:读配置/状态,配了凭据则起心跳线程(内部吞异常) cleanup_sessions() # 启动时清掉超过保留天数的会话(只碰 session.db) sess.dedupe_lme_res() # 2026-10-10:s_lme_res 存量去重自愈(wipe 曾漏这张表,台班组成重复) + _n = sess.drop_orphan_rows() # 2026-10-11:s_ 表孤儿行自愈(已删会话的幽灵 sid 残留) + if _n > 0: + print("[启动自愈] 清掉已删会话的孤儿数据 %d 行" % _n) + elif _n < 0: + print("[启动自愈] 孤儿数据清理出错(已回滚,不影响启动)") + _g = _purge_orphan_uploads() # 2026-10-11:uploads 零引用源文件副本对账回收(幂等) + if _g: + print("[启动自愈] 回收零引用源文件副本 %d 个" % _g) # 2026-10-11:启动「数据过期」打印已取消(主人定)——打开即自动升级(决策 34)后「需重导」口径不再成立; # stale_batches() 保留:/api/admin/summary 的 stale 字段仍返回,供后续一键批量升级(方案 B)复用。 srv = ThreadingHTTPServer(("127.0.0.1", PORT), Handler) diff --git a/CostReview/app/ui/admin.html b/CostReview/app/ui/admin.html index c5c8e58..d8c261b 100644 --- a/CostReview/app/ui/admin.html +++ b/CostReview/app/ui/admin.html @@ -475,7 +475,7 @@ a:hover{text-decoration:underline} - + + +