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 之类——审计全量视角。