From d56dd52c3e6f701eb928f60875f2513be14b1263 Mon Sep 17 00:00:00 2001 From: admin Date: Wed, 16 Sep 2026 12:07:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(me):=2024h=20=E5=9B=BE=E8=A1=A8=E6=94=B9?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8=E7=AA=97=E5=8F=A3=EF=BC=88=E5=B7=A6=2023h=20?= =?UTF-8?q?=E5=89=8D=20=E2=86=92=20=E5=8F=B3=20=E7=8E=B0=E5=9C=A8=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前 hour_24 数组按 hour-of-day 桶索引(h = (now_h - age_h + 24) % 24), 导致 11 点打开面板看到「今天 0-11 + 昨天 12-23」混在一起,x 轴 「0-23」语义跟时间顺序对不上,看着像「显示的是昨天的 24 小时」。 改 llm_audit.rs: * 索引语义换成「hours ago」:hour_24[0] = 23h 前,hour_24[23] = 现在 * 24h 边界(age_h == 24)clamp 到 0 桶;老逻辑会把边界调用错归到 当前小时桶,是同一个 bug 的另一种表现 * 响应里加 hour_24h_labels 数组,前端不用再算时间 改 www/assets/me.js: * renderHourly 接收 server 返回的 labels,老 server 缺字段时降级到索引 改 www/me.html: * 描述文字「按本地自然小时分桶」→「滚动 24 小时(左 23h 前 → 右 现在)」 ——避免后续用户再问 hourly.html 的热力图(slot × 24 小时)按 hour-of-day 是设计如此, 不动。 --- rust/crates/plugin-llm/src/llm_audit.rs | 26 +++++++++++++++++++------ www/assets/me.js | 9 ++++++--- www/me.html | 2 +- 3 files changed, 27 insertions(+), 10 deletions(-) diff --git a/rust/crates/plugin-llm/src/llm_audit.rs b/rust/crates/plugin-llm/src/llm_audit.rs index 61fb8be..d1b29b2 100644 --- a/rust/crates/plugin-llm/src/llm_audit.rs +++ b/rust/crates/plugin-llm/src/llm_audit.rs @@ -454,7 +454,6 @@ pub fn user_recent(username: &str, days: u32) -> Value { return empty_recent(); } let now = util::now(); - let now_h = ((now % 86400) / 3600) as i64; let cutoff_24h = now - 86400; let cutoff_7d = now - (days as i64) * 86400; @@ -525,16 +524,18 @@ pub fn user_recent(username: &str, days: u32) -> Value { let tin = r.get("tin").and_then(|v| v.as_u64()).unwrap_or(0); let tout = r.get("tout").and_then(|v| v.as_u64()).unwrap_or(0); - // 24h 窗口 + // 24h 滚动窗口(**左老右新**:hour_24[0] = 23h 前,hour_24[23] = 当前小时) + // age = now - ts,age_h = age / 3600 ∈ [0, 24] + // age_h = 0 → 现在 + // age_h = 23 → 23h 前(窗口里最老的整小时桶) + // age_h = 24 → 正好 24h 前(cutoff 边界,clamp 到 0 桶) if ts >= cutoff_24h { total_24h += 1; total_tin_24h += tin; total_tout_24h += tout; let age_h = ((now - ts) / 3600) as i64; - let h = (now_h - age_h + 24) % 24; - if (0..24).contains(&h) { - hour_24[h as usize] += 1; - } + let idx = (23 - age_h).max(0) as usize; + hour_24[idx] += 1; } // 7d by_slot:last_status 必须用「最大 ts 的那次调用」 // —— list_days() 是倒序(今天 → 昨天),如果无脑 entry.last_status = status @@ -581,6 +582,13 @@ pub fn user_recent(username: &str, days: u32) -> Value { "tin_7d": total_tin_7d, "tout_7d": total_tout_7d, "hour_24h": hour_24.to_vec(), + // hour_24h_labels[i] = 「i 小时前」语义:i=0 → "23h前"(最老),i=23 → "现在" + "hour_24h_labels": (0..24).map(|i| { + let age = 23 - i; + if age == 0 { "现在".to_string() } + else if age == 1 { "1h前".to_string() } + else { format!("{}h前", age) } + }).collect::>(), "by_slot": slot_vec.iter().map(|(s, st)| json!({ "slot": s, "calls_24h": st.calls_24h, @@ -665,6 +673,12 @@ fn empty_recent() -> Value { "tin_7d": 0, "tout_7d": 0, "hour_24h": vec![0u64; 24], + "hour_24h_labels": (0..24).map(|i| { + let age = 23 - i; + if age == 0 { "现在".to_string() } + else if age == 1 { "1h前".to_string() } + else { format!("{}h前", age) } + }).collect::>(), "by_slot": Vec::::new(), "last_at": null, "last_slot": null, diff --git a/www/assets/me.js b/www/assets/me.js index d960e56..09efcf0 100644 --- a/www/assets/me.js +++ b/www/assets/me.js @@ -67,10 +67,13 @@ } } - function renderHourly(hour24) { + function renderHourly(hour24, labels) { var el = $("dHourChart"); + // server 返回 hour_24h_labels("23h前"…"现在"),老 server 没这个字段时降级到索引 + var lbls = (labels && labels.length === hour24.length) ? labels + : hour24.map(function (_, h) { return h; }); IP.chart.line(el, { - labels: hour24.map(function (_, h) { return h; }), + labels: lbls, series: [{ name: "调用数", values: hour24.map(function (v) { return Number(v) || 0; }) }], height: 90, emptyText: "过去 24 小时没有调用记录" @@ -209,7 +212,7 @@ $("dLast").textContent = r.last_at ? esc(r.last_at + " · " + (r.last_slot || "-")) : "—"; - renderHourly(r.hour_24h || []); + renderHourly(r.hour_24h || [], r.hour_24h_labels || []); renderSlots(r.by_slot || []); renderUpstreams(me.upstreams || []); renderApix(me.apix || []); diff --git a/www/me.html b/www/me.html index 12dace6..5f5e30a 100644 --- a/www/me.html +++ b/www/me.html @@ -52,7 +52,7 @@

24h 调用分布

-
按本地自然小时分桶;含被拒的 401/404 之类——审计全量视角。
+
滚动 24 小时(左 23h 前 → 右 现在);含被拒的 401/404 之类——审计全量视角。