fix(me): 24h 图表改滚动窗口(左 23h 前 → 右 现在)

之前 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 是设计如此,
不动。
This commit is contained in:
admin committed 2026-09-16 12:07:47 +08:00
1 parent 354c58e721
commit d56dd52c3e
3 files changed
+27 -10

No files matched your search

+20 -6
View File
@@ -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::<Vec<_>>(),
"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::<Vec<_>>(),
"by_slot": Vec::<Value>::new(),
"last_at": null,
"last_slot": null,
+6 -3
View File
@@ -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 || []);
+1 -1
View File
@@ -52,7 +52,7 @@
<div class="panel-head">
<div>
<h2>24h 调用分布</h2>
<div class="desc">按本地自然小时分桶;含被拒的 401/404 之类——审计全量视角。</div>
<div class="desc">滚动 24 小时(左 23h 前 → 右 现在);含被拒的 401/404 之类——审计全量视角。</div>
</div>
<div class="row-actions">
<button class="btn-sm" id="refreshBtn">刷新</button>