feat: 启动自愈清理孤儿数据与零引用上传文件,表格列宽记忆优化
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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):
|
||||
# 只认 <sha256>_<原名> 且桶号=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)
|
||||
|
||||
@@ -475,7 +475,7 @@ a:hover{text-decoration:underline}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="common.js?v=20261011b"></script>
|
||||
<script src="common.js?v=20261011c"></script>
|
||||
<script src="period.js"></script>
|
||||
<script>
|
||||
const $=id=>document.getElementById(id);
|
||||
@@ -660,7 +660,8 @@ async function load(p){
|
||||
||`<tr><td colspan="${cols.length}" class="tip" style="text-align:center;padding:16px">没有数据</td></tr>`;
|
||||
const tw=document.querySelector('.tw');
|
||||
if(tw) tw.style.maxHeight=availHeight()+'px'; // 容器高与行数同源:不空白、不出竖向滚动条
|
||||
requestAnimationFrame(()=>fitFlexByContent(cols)); // 伸缩列按内容定宽(期次不再过宽、名称不再截断)
|
||||
// 伸缩列定宽:用户拖过的比例记忆优先(2026-10-11 主人定),无记忆才按内容定宽(期次不再过宽、名称不再截断)
|
||||
requestAnimationFrame(()=>{ if(!flexMemApply($('tb'),cols)) fitFlexByContent(cols); });
|
||||
// 行高校准(只自校正一次,防循环):首行实测高度回填 ROWH;仍溢出就按实测减行数重取本页
|
||||
requestAnimationFrame(()=>{
|
||||
const rows=tb.tBodies[0].rows;
|
||||
|
||||
@@ -122,6 +122,9 @@ td.gtitle{text-align:left;white-space:nowrap;overflow:hidden;text-overflow:ellip
|
||||
.tg{border-radius:3px;padding:1px 6px;font-size:11px;font-weight:700}
|
||||
.tg-e{background:#e8f5ec;color:#1e8e3e}.tg-s{background:#fff4e5;color:#a86300}.tg-f{background:#e7f0fb;color:#1e4e8c}
|
||||
td.tg{text-align:center;padding-left:4px;padding-right:4px}
|
||||
/* 组/清单两级把手层级(2026-10-11 主人定):组把手贴左,清单把手再缩进一层 */
|
||||
tr[data-grp]>td.tg{text-align:left;padding-left:5px}
|
||||
tr[data-bid]>td.tg{text-align:left;padding-left:21px}
|
||||
.tog{display:inline-block;width:17px;height:17px;line-height:15px;text-align:center;border:1px solid #8ba3c2;
|
||||
border-radius:3px;background:#fff;color:#1f5fae;cursor:pointer;font-weight:700;font-size:12px;user-select:none}
|
||||
.tog:hover{background:#e7eefb}
|
||||
|
||||
@@ -93,7 +93,7 @@ const COLS = {
|
||||
// 类似清单 / 组价不一致清单 共用(12 列)
|
||||
// ⚠️ 与后端 BILLROW() 数组下标一一对应:
|
||||
// 0=id 1=编码 2=名称 3=项目特征 4=单位 5=工程量 6=单价 7=合价 8=序号 9=单位工程名称
|
||||
bill: [['', 30, 'c', 'tg'], ['序号', 52, 'c'], ['编码', 120, 'c'], ['类别', 52, 'c'],
|
||||
bill: [['', 44, 'c', 'tg'], ['序号', 52, 'c'], ['编码', 120, 'c'], ['类别', 52, 'c'],
|
||||
['单位工程名称', 150, 'l'], ['名称', 210, 'l'], ['项目特征', 260, 'l', 'attr'],
|
||||
['单位', 56, 'c'], ['工程量', 90, 'r'], ['单价', 100, 'r'], ['合价', 110, 'r'],
|
||||
['备注', 150, 'l']],
|
||||
@@ -274,14 +274,16 @@ function billSortDOM(tb) {
|
||||
按**表头文字指纹**为每张表存一份列宽到 localStorage,重建后自动套用。
|
||||
指纹跨重画稳定:同一套列即使表元素是新建的也得到同一个键;
|
||||
排序标记 ▲▼ 不参与指纹(同一张表排序前后是同一个键)。 */
|
||||
function _colk(tb) {
|
||||
const ths = tb.querySelectorAll('thead th');
|
||||
function _colkFromTexts(texts) {
|
||||
let s = '';
|
||||
ths.forEach(t => { s += (t.textContent || '').trim().replace(/[▲▼]\s*$/, '') + '|'; });
|
||||
(texts || []).forEach(t => { s += String(t == null ? '' : t).trim().replace(/[▲▼]\s*$/, '') + '|'; });
|
||||
let h = 0;
|
||||
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) & 0x7fffffff;
|
||||
return 'h' + h.toString(36);
|
||||
}
|
||||
function _colk(tb) {
|
||||
return _colkFromTexts([...tb.querySelectorAll('thead th')].map(t => t.textContent || ''));
|
||||
}
|
||||
function _colwKey(tb) { return 'colw|' + location.pathname + '|' + _colk(tb); }
|
||||
function colwSave(tb) {
|
||||
const cg = tb.querySelector('colgroup');
|
||||
@@ -289,7 +291,10 @@ function colwSave(tb) {
|
||||
const w = Array.prototype.map.call(cg.children, c => parseFloat(c.style.width) || 0);
|
||||
// 补宽列(项目特征):显示宽是动态算的,不入库,只存基础宽 —— 否则列宽记忆会越滚越大
|
||||
if (tb._attrCol != null && tb._attrBase != null) w[tb._attrCol] = tb._attrBase;
|
||||
if (!w.every(x => x > 0)) return; // 含自动宽列的表不记
|
||||
// 2026-10-11 主人定「全部列宽都记」:混合宽表(名称/规格等 auto 弹性列)原来整表不存,
|
||||
// 拖了就丢 —— 改为**按列存**:px 列记实宽、弹性列记 0(colwApply/rawTbl 见 0 跳过,
|
||||
// 弹性列照旧吸收余宽,撑满设计不破)。
|
||||
if (!w.some(x => x > 0)) return;
|
||||
try { localStorage.setItem(_colwKey(tb), JSON.stringify(w)); } catch (e) {}
|
||||
}
|
||||
function colwApply(tb, deferred) {
|
||||
@@ -337,10 +342,15 @@ function flexFit(tb, cols) {
|
||||
const flexIdx = cols.map((c, i) => c[3] === 'F' ? -1 : i).filter(i => i >= 0);
|
||||
if (!flexIdx.length) return;
|
||||
const avail = Math.max(360, tb.parentNode.clientWidth - 23);
|
||||
const wsum = flexIdx.reduce((s, i) => s + cols[i][1], 0);
|
||||
const rest = avail - fixedSum;
|
||||
const w = {};
|
||||
flexIdx.forEach(i => { w[i] = Math.max(90, Math.round(rest * cols[i][1] / wsum)); });
|
||||
const rat = flexMemLoad(cols); // 用户拖过的比例记忆优先(2026-10-11 主人定)
|
||||
if (rat) {
|
||||
flexIdx.forEach((i, k) => { w[i] = Math.max(30, Math.round(rest * rat[k])); });
|
||||
} else {
|
||||
const wsum = flexIdx.reduce((s, i) => s + cols[i][1], 0);
|
||||
flexIdx.forEach(i => { w[i] = Math.max(90, Math.round(rest * cols[i][1] / wsum)); });
|
||||
}
|
||||
// 精确贴合:min 钳位/取整的差额补到最大伸缩列,不挤出横向滚动条
|
||||
const diff = avail - fixedSum - flexIdx.reduce((s, i) => s + w[i], 0);
|
||||
if (diff !== 0) { const big = flexIdx.reduce((a, b) => w[a] >= w[b] ? a : b); w[big] += diff; }
|
||||
@@ -362,6 +372,50 @@ function flexRedistribute(tb, idx) {
|
||||
const rest = Math.max(90 * others.length, avail - fixedSum - cur);
|
||||
const each = Math.floor(rest / Math.max(1, others.length));
|
||||
others.forEach(i => cg.children[i].style.width = each + 'px');
|
||||
flexMemSave(tb); // 拖完存比例记忆(2026-10-11 主人定:撑满表也记住列宽)
|
||||
}
|
||||
|
||||
/* ---- 伸缩表的比例记忆(2026-10-11 主人定:项目工料机这类「撑满」表也要记住列宽)----
|
||||
绝对宽度与撑满设计冲突(窗口一变就错位),改记**各伸缩列占剩余宽度的比例**:
|
||||
拖完(flexRedistribute)存比例,重画/换项目/窗口缩放(flexFit)按当前容器宽换算回放。
|
||||
键按列定义标题指纹(不含项目、不含窗口尺寸)——打开什么项目都是同一份,与 colw 记忆同口径。 */
|
||||
function _flexKeyOf(cols) {
|
||||
return 'flexr|' + location.pathname + '|' + _colkFromTexts(cols.map(c => c[0]));
|
||||
}
|
||||
function flexMemLoad(cols) {
|
||||
try {
|
||||
const r = JSON.parse(localStorage.getItem(_flexKeyOf(cols)) || 'null');
|
||||
const n = cols.filter(c => c[3] !== 'F').length;
|
||||
if (Array.isArray(r) && r.length === n && r.every(x => x > 0.005)) return r;
|
||||
} catch (e) {}
|
||||
return null;
|
||||
}
|
||||
function flexMemSave(tb) {
|
||||
if (!tb || !tb._cols) return;
|
||||
const cg = tb.querySelector('colgroup');
|
||||
if (!cg) return;
|
||||
const cols = tb._cols;
|
||||
const flexIdx = cols.map((c, i) => c[3] === 'F' ? -1 : i).filter(i => i >= 0);
|
||||
if (!flexIdx.length) return;
|
||||
const ws = flexIdx.map(i => parseFloat(cg.children[i].style.width) || 0);
|
||||
const tot = ws.reduce((a, b) => a + b, 0);
|
||||
if (tot <= 0) return;
|
||||
try { localStorage.setItem(_flexKeyOf(cols), JSON.stringify(ws.map(w => w / tot))); } catch (e) {}
|
||||
}
|
||||
/* 按比例回放到现成 colgroup(admin #tb 的内容定宽通道用:有记忆则记忆优先)。
|
||||
返回是否套上了。 */
|
||||
function flexMemApply(tb, cols) {
|
||||
const rat = flexMemLoad(cols);
|
||||
if (!rat) return false;
|
||||
const cg = tb.querySelector('colgroup');
|
||||
if (!cg || cg.children.length !== cols.length) return false;
|
||||
const host = tb.parentNode;
|
||||
if (!host || !host.clientWidth) return false;
|
||||
const fixedSum = cols.reduce((s, c) => s + (c[3] === 'F' ? c[1] : 0), 0);
|
||||
const flexIdx = cols.map((c, i) => c[3] === 'F' ? -1 : i).filter(i => i >= 0);
|
||||
const rest = Math.max(360, host.clientWidth - 23) - fixedSum;
|
||||
flexIdx.forEach((i, k) => { cg.children[i].style.width = Math.max(30, Math.round(rest * rat[k])) + 'px'; });
|
||||
return true;
|
||||
}
|
||||
// 窗口尺寸变化:撑满屏幕的表重新贴合(防抖 200ms;只认 data-flexfit,
|
||||
// 不碰后台 #tb 那套内容定宽伸缩逻辑)
|
||||
@@ -695,7 +749,18 @@ let RVOPEN = null, RVROOT = true;
|
||||
点击落在拖动手柄 .rs 上时不触发排序。 */
|
||||
function rawTbl(cols, head, body, al, sort) {
|
||||
const keep = cols.map((w, i) => i).filter(i => cols[i] !== 0);
|
||||
const cg = keep.map(i => '<col style="width:' + (typeof cols[i] === 'number' ? cols[i] + 'px' : (cols[i] || 'auto')) + '">').join('');
|
||||
// 列宽记忆回套(2026-10-11 主人定:原数据表拖完也要记住):构建期按表头指纹取记忆宽,
|
||||
// px 列替换默认宽;记忆里的 0 = 弹性列(见 colwSave 按列存),保持 auto 吸收余宽。
|
||||
let saved = null;
|
||||
try { saved = JSON.parse(localStorage.getItem('colw|' + location.pathname + '|' + _colkFromTexts(keep.map(i => Array.isArray(head[i]) ? head[i][0] : head[i]))) || 'null'); } catch (e) {}
|
||||
if (!saved || saved.length !== keep.length) saved = null;
|
||||
const cg = keep.map((i, k) => {
|
||||
let w;
|
||||
if (saved && saved[k] > 0) w = saved[k] + 'px';
|
||||
else if (typeof cols[i] === 'number') w = cols[i] + 'px';
|
||||
else w = cols[i] || 'auto';
|
||||
return '<col style="width:' + w + '">';
|
||||
}).join('');
|
||||
const th = keep.map((i) => {
|
||||
const h = head[i];
|
||||
const t = Array.isArray(h) ? h[0] : h;
|
||||
@@ -710,7 +775,8 @@ function rawTbl(cols, head, body, al, sort) {
|
||||
}).join('');
|
||||
let attr = '';
|
||||
if (keep.every(i => typeof cols[i] === 'number')) {
|
||||
attr = ' data-sumw="1" style="width:' + keep.reduce((s, i) => s + cols[i], 0) + 'px"';
|
||||
const wsum = keep.reduce((s, i, k) => s + (saved && saved[k] > 0 ? saved[k] : cols[i]), 0);
|
||||
attr = ' data-sumw="1" style="width:' + wsum + 'px"';
|
||||
}
|
||||
return '<table' + attr + '><colgroup>' + cg + '</colgroup><thead><tr>' + th + '</tr></thead><tbody>' + body + '</tbody></table>';
|
||||
}
|
||||
|
||||
@@ -60,7 +60,7 @@ a:hover{text-decoration:underline}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="common.js?v=20261011b"></script>
|
||||
<script src="common.js?v=20261011c"></script>
|
||||
<script>
|
||||
/* 会话管理页:照后台「项目界面」的版式,管的是**还没入库**的工作台会话。
|
||||
会话库不自动清(SESSION_KEEP_DAYS=0),要删只能在这里手动删。
|
||||
|
||||
@@ -459,7 +459,7 @@ body.grptree-dragging{cursor:col-resize !important;user-select:none}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<script src="common.js?v=20261011b"></script>
|
||||
<script src="common.js?v=20261011c"></script>
|
||||
<script src="period.js"></script>
|
||||
<script>
|
||||
let SID=0, CUR=null, CURTYPE='lme', HROWS=[];
|
||||
|
||||
Reference in New Issue
Block a user