后端
- 新增日桶 ud:/udu:/uds:(键名带日窗 id,TTL 90 天),record 记请求、
add_tokens 记 token,两者写同一批键、字段互补;forget 同步清 udu:
- 新增 GET /api/admin/trend?days=N(默认 14、上限 90),返回图表可直接吃的
{labels,total,users,slots},每条含 requests/in/out 三条等长数组
- util::fmt_day 供 x 轴用(%m-%d)
前端(原 www/admin.html 1100+ 行单文件)
- 拆为 login.html + overview/review/domains/users/upstreams/usage.html
- 抽出 assets/:admin.css + core.js(请求/登录守卫/外壳)+ chart.js(自绘 SVG)
- 每页 <base href="/admin/">;nginx 侧 /admin 仍进总览,/admin/ alias 到 www/
- 图表零第三方依赖(CDN 会让内网部署白屏,本地副本要塞几百 KB 进镜像)
验收
- 回归 227 条全绿(网关 102 + LLM 125)
- agent.sh verify 新增「趋势接口返回 7 天序列」作为镜像换新证据
91 lines
3.1 KiB
JavaScript
91 lines
3.1 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);
|
||
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);
|
||
});
|
||
})();
|