/* 数据总览:卡片 + 趋势图 + 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); }); })();