/* 用量统计:趋势(全局 / 按账号 / 按 API 组)+ 按人 / 按 API 组 的表格与排名图 */ (function () { "use strict"; var $ = IP.$; var C = { req: "#2563eb", in: "#0d9488", out: "#7c3aed" }; function num(v) { return Number(v) || 0; } function tok(o) { return o || {}; } // ---------------- 概览卡片 ---------------- function renderCards(s) { var t = s.trend; var sum = function (arr) { return (arr || []).reduce(function (a, b) { return a + num(b); }, 0); }; IP.$("uReq").textContent = t ? sum(t.total.requests) : 0; IP.$("uTin").textContent = t ? IP.chart.fmtNum(sum(t.total["in"])) : 0; IP.$("uTout").textContent = t ? IP.chart.fmtNum(sum(t.total.out)) : 0; IP.$("uToday").textContent = num((s.usage.total || {}).today); IP.$("uTotal").textContent = num((s.usage.total || {}).total); var tk = tok(s.usage.tokens); IP.$("uTokTotal").textContent = IP.chart.fmtNum(num(tk.in_total) + num(tk.out_total)); } // ---------------- 趋势 ---------------- function renderTrend(s) { var t = s.trend; if (!t || !t.labels || !t.labels.length) { IP.chart.line($("chReq"), { labels: [], series: [] }); IP.chart.line($("chTok"), { labels: [], series: [] }); return; } var view = $("trendView").value; if (view === "total") { IP.chart.line($("chReq"), { labels: t.labels, series: [{ name: "请求数", values: t.total.requests, color: C.req }] }); IP.chart.line($("chTok"), { labels: t.labels, series: [ { name: "入 token", values: t.total["in"], color: C["in"] }, { name: "出 token", values: t.total.out, color: C.out } ] }); return; } // 分视角:每个账号 / 每个上游一条线。全 0 的(`empty`)不画, // 否则一个从没调过的账号会变成贴着轴的死线,把图例挤爆 var list = (view === "users" ? t.users : t.slots) || []; var live = list.filter(function (x) { return !x.empty; }); IP.chart.line($("chReq"), { labels: t.labels, series: live.map(function (x) { return { name: view === "users" ? x.username : x.slot, values: x.requests }; }), emptyText: "该区间内还没有数据" }); IP.chart.line($("chTok"), { labels: t.labels, series: live.map(function (x) { return { name: view === "users" ? x.username : x.slot, values: (x["in"] || []).map(function (v, i) { return num(v) + num((x.out || [])[i]); }) }; }), emptyText: "该区间内还没有数据" }); } // ---------------- 排名图 ---------------- /// 按选中指标排序取 Top N function rank(list, metric, nameOf, n) { var pick = function (x) { var tk = tok(x.tokens); if (metric === "in") return num(tk.in_total); if (metric === "out") return num(tk.out_total); if (metric === "tok") return num(tk.in_total) + num(tk.out_total); return num(x.total); }; return list .filter(function (x) { return pick(x) > 0; }) .sort(function (a, b) { return pick(b) - pick(a); }) .slice(0, n) .map(function (x) { return { label: nameOf(x), value: pick(x) }; }); } function renderRanks(s) { IP.chart.bars($("chUsers"), { items: rank(s.usage.users || [], $("metricUser").value, function (u) { var owner = (s.users.filter(function (x) { return x.username === u.username; })[0] || {}); return IP.displayName(owner) + "(@" + u.username + ")"; }, 10), emptyText: "还没有任何用量" }); IP.chart.bars($("chSlots"), { items: rank(s.usage.models || [], $("metricSlot").value, function (m) { return m.slot; }, 10), emptyText: "还没有任何用量" }); } // ---------------- 表格 ---------------- function renderTables(s) { var ub = $("usageUsersBody"); var rows = IP.arr(s.usage.users); if (!rows.length) { ub.innerHTML = '还没有任何 LLM 调用'; } else { ub.innerHTML = rows.map(function (u) { var tk = tok(u.tokens); var byModel = IP.arr(u.models).length ? IP.arr(u.models).map(function (m) { return "
" + IP.esc(m.slot) + ":" + (m.today || 0) + " / " + (m.total || 0) + "
"; }).join("") : "-"; return "" + "" + IP.esc(u.username) + "" + "" + (u.today || 0) + "" + "" + (u.total || 0) + "" + "" + IP.chart.fmtNum(tk.in_total) + "" + "" + IP.chart.fmtNum(tk.out_total) + "" + "" + IP.esc(u.last_ts_text || "-") + "" + "" + byModel + "" + ""; }).join(""); } var mb = $("usageModelsBody"); var mrows = IP.arr(s.usage.models); if (!mrows.length) { mb.innerHTML = '还没有任何 LLM 调用'; return; } mb.innerHTML = mrows.map(function (m) { var tk = tok(m.tokens); var users = IP.arr(m.users).length ? IP.arr(m.users).map(function (x) { return "
" + IP.esc(x.username) + ":" + (x.today || 0) + " / " + (x.total || 0) + "
"; }).join("") : "-"; return "" + "" + IP.esc(m.slot) + "" + "" + (m.today || 0) + "" + "" + (m.total || 0) + "" + "" + IP.chart.fmtNum(tk.in_total) + "" + "" + IP.chart.fmtNum(tk.out_total) + "" + "" + IP.esc(m.last_ts_text || "-") + "" + "" + users + "" + ""; }).join(""); } function syncDayButtons() { var btns = $("dayBtns").querySelectorAll("button[data-days]"); Array.prototype.forEach.call(btns, function (b) { var on = Number(b.getAttribute("data-days")) === IP.state.trendDays; b.className = "btn-sm" + (on ? " btn-primary" : ""); }); } // ---------------- 事件 ---------------- $("dayBtns").addEventListener("click", function (e) { var b = e.target.closest("button[data-days]"); if (!b) return; IP.state.trendDays = Number(b.getAttribute("data-days")); syncDayButtons(); IP.reload(); }); $("trendView").addEventListener("change", function () { renderTrend(IP.state); }); $("metricUser").addEventListener("change", function () { renderRanks(IP.state); }); $("metricSlot").addEventListener("change", function () { renderRanks(IP.state); }); syncDayButtons(); IP.boot("usage", function (s) { renderCards(s); renderTrend(s); renderRanks(s); renderTables(s); }); })();