1. **/api/v1/me 增量聚合**:
- 新增 shm 前缀 `usum:`(值 JSON),key = `usum:<username>`
- 持久化 `{calls, tin, tout, by_slot, last_ts, last_slot, last_status}`
- 每次 user_recent 只扫 audit delta (`ts > last_ts`),累加进 shm 后写回
- 24h hour / 24h totals / 7d by_slot 是**滚动数据**,每次现算(持久化会过期)
- dump 走 stats_persist 的 PREFIXES 白名单 → 自动落 llm-stats.json → 跨重启不丢
- **冷启动**:`last_ts=0` → 第一次 user_recent 全量扫 audit 重建索引
- 后续请求:`last_ts` 跟上 → 增量扫 audit → 写入持久化
2. **/api/admin/usage 自上线以来累计**:
- 新增 `tokens.all_time` 字段(1.0.32),来自 audit meta.jsonl 全聚合
- 真源聚合,不依赖 shm 日桶,**不受 stats 落盘/恢复影响**
- audit 容量 1GB = 14 年历史,**自服务上线以来覆盖完整**
- overview.html 新增 stat 卡片「累计 token (自上线)」+ since 日期
回归 238/0 全绿。
96 lines
3.5 KiB
JavaScript
96 lines
3.5 KiB
JavaScript
/* 数据总览:卡片 + 趋势图 + Top 排名条形图 */
|
||
(function () {
|
||
"use strict";
|
||
|
||
var C = { req: "#2563eb", in: "#0d9488", out: "#7c3aed" };
|
||
|
||
function num(v) { return Number(v) || 0; }
|
||
|
||
/// 可用模型 = 全部(启用中的)上游登记模型去重后的条数
|
||
function distinctModels(s) {
|
||
var seen = {};
|
||
s.upstreams.forEach(function (u) {
|
||
if (!u.enabled) return;
|
||
IP.arr(u.models).forEach(function (m) { seen[m] = true; });
|
||
});
|
||
return Object.keys(seen).length;
|
||
}
|
||
|
||
function renderCards(s) {
|
||
var active = s.domains.filter(function (d) { return d.status === "approved"; }).length;
|
||
var tot = s.usage.total || {};
|
||
var tok = s.usage.tokens || {};
|
||
|
||
IP.$("statPending").textContent = s.pending;
|
||
IP.$("statDomains").textContent = s.domains.length;
|
||
IP.$("statActive").textContent = active;
|
||
IP.$("statUsers").textContent = s.users.length;
|
||
IP.$("statUpstreams").textContent = s.upstreams.length;
|
||
IP.$("statLlmtoday").textContent = tot.today || 0;
|
||
IP.$("statLlmtotal").textContent = tot.total || 0;
|
||
|
||
var tokToday = num(tok.in_today) + num(tok.out_today);
|
||
var tokTotal = num(tok.in_total) + num(tok.out_total);
|
||
IP.$("statTokToday").textContent = IP.chart.fmtNum(tokToday);
|
||
IP.$("statTokTotal").textContent = IP.chart.fmtNum(tokTotal);
|
||
// 1.0.32:自服务上线以来累计(来自 audit meta.jsonl 真源,不受 stats 落盘/恢复影响)
|
||
var allTime = tok.all_time || {};
|
||
var allTimeTokens = num(allTime.tin) + num(allTime.tout);
|
||
IP.$("statTokAllTime").textContent = IP.chart.fmtNum(allTimeTokens) +
|
||
(allTime.since ? " (" + allTime.since + " 起)" : "");
|
||
IP.$("statModels").textContent = distinctModels(s);
|
||
}
|
||
|
||
function renderTrend(s) {
|
||
var t = s.trend;
|
||
if (!t || !t.labels || !t.labels.length) {
|
||
IP.chart.line(IP.$("chReq"), { labels: [], series: [] });
|
||
IP.chart.line(IP.$("chTok"), { labels: [], series: [] });
|
||
return;
|
||
}
|
||
IP.chart.line(IP.$("chReq"), {
|
||
labels: t.labels,
|
||
series: [{ name: "请求数", values: t.total.requests, color: C.req }]
|
||
});
|
||
IP.chart.line(IP.$("chTok"), {
|
||
labels: t.labels,
|
||
series: [
|
||
{ name: "入 token", values: t.total["in"], color: C["in"] },
|
||
{ name: "出 token", values: t.total.out, color: C.out }
|
||
]
|
||
});
|
||
}
|
||
|
||
/// 取累计调用 Top N,按累计降序;全 0 的不进榜(榜上堆一排 0 没信息量)
|
||
function top(list, key, n) {
|
||
return list
|
||
.filter(function (x) { return num(x[key]) > 0; })
|
||
.sort(function (a, b) { return num(b[key]) - num(a[key]); })
|
||
.slice(0, n);
|
||
}
|
||
|
||
function renderRanks(s) {
|
||
var byUser = top(s.usage.users || [], "total", 8).map(function (u) {
|
||
var owner = (s.users.filter(function (x) { return x.username === u.username; })[0] || {});
|
||
return {
|
||
label: IP.displayName(owner) + "(@" + u.username + ")",
|
||
value: num(u.total),
|
||
sub: "今 " + num(u.today)
|
||
};
|
||
});
|
||
IP.chart.bars(IP.$("chUsers"), { items: byUser, emptyText: "还没有任何 LLM 调用" });
|
||
|
||
var bySlot = top(s.usage.models || [], "total", 8).map(function (m) {
|
||
return { label: m.slot, value: num(m.total), sub: "今 " + num(m.today) };
|
||
});
|
||
IP.chart.bars(IP.$("chSlots"), { items: bySlot, emptyText: "还没有任何 LLM 调用" });
|
||
}
|
||
|
||
IP.boot("overview", function (s) {
|
||
renderCards(s);
|
||
renderTrend(s);
|
||
renderRanks(s);
|
||
if (IP.review) IP.review.render(s);
|
||
});
|
||
})();
|