/* 用量统计:趋势(全局 / 按账号 / 按 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 = '