Files
admin c426789734 feat(admin): 用量图表(趋势/token/按人/按 API 组)+ 管理台单页拆分多页
后端
- 新增日桶 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 天序列」作为镜像换新证据
2026-09-13 00:42:13 +08:00

142 lines
6.4 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* ============================================================================
图表:纯 SVG / HTML,**零外部依赖**
为什么不用 Chart.js / ECharts:管理台是网关容器里的一把静态文件,
引 CDN 等于把面板可用性押在公网可达性上(内网部署直接白屏),
引本地副本又要把几百 KB 第三方代码塞进镜像。趋势图只有折线和条形两种,
自己画不到 200 行,且颜色/字体能直接吃页面的 CSS 变量。
========================================================================== */
window.IP = window.IP || {};
IP.chart = (function () {
"use strict";
var VB_W = 720; // viewBox 宽度;实际宽度由 CSS 拉满容器
var COLOR = ["#2563eb", "#0d9488", "#7c3aed", "#ea580c", "#0891b2", "#db2777"];
/// 轴上限取整到 1/2/2.5/5/10 × 10^n,避免 y 轴刻度出现 8734 这种数
function niceMax(v) {
if (!(v > 0)) return 1;
var exp = Math.pow(10, Math.floor(Math.log10(v)));
var f = v / exp;
var n = f <= 1 ? 1 : f <= 2 ? 2 : f <= 2.5 ? 2.5 : f <= 5 ? 5 : 10;
return n * exp;
}
/// 大数简写:12345 → 1.2万。图表刻度里写全串数字会挤成一团
function fmtNum(n) {
n = Number(n) || 0;
if (n >= 100000000) return (n / 100000000).toFixed(1).replace(/\.0$/, "") + "亿";
if (n >= 10000) return (n / 10000).toFixed(n >= 1000000 ? 0 : 1).replace(/\.0$/, "") + "万";
return String(n);
}
function empty(el, text) {
if (el) el.innerHTML = '<div class="chart-empty">' + IP.esc(text || "暂无数据") + "</div>";
}
/**
* 折线(面积)图。
* @param el 容器元素
* @param opts.labels x 轴标签(如 ["09-07", ...])
* @param opts.series [{ name, values, color, fill }]
* @param opts.height 图形高度(px,默认 220)
*/
function line(el, opts) {
if (!el) return;
opts = opts || {};
var labels = opts.labels || [];
var series = (opts.series || []).filter(function (s) { return s && s.values && s.values.length; });
if (!labels.length || !series.length) return empty(el, opts.emptyText || "该区间内还没有数据");
var H = opts.height || 220;
var L = 48, R = 14, T = 16, B = 26;
var n = labels.length;
var max = 0;
series.forEach(function (s) {
s.values.forEach(function (v) { if (v > max) max = v; });
});
max = niceMax(max);
var iw = VB_W - L - R, ih = H - T - B;
var X = function (i) { return n === 1 ? L + iw / 2 : L + (iw * i) / (n - 1); };
var Y = function (v) { return T + ih * (1 - v / max); };
var parts = [];
// 网格 + y 轴刻度(5 条,含 0 与顶)
for (var g = 0; g <= 4; g++) {
var yv = (max * g) / 4, y = Y(yv);
parts.push('<line class="grid" x1="' + L + '" y1="' + y.toFixed(1) + '" x2="' + (VB_W - R) + '" y2="' + y.toFixed(1) + '"/>');
parts.push('<text class="tick" x="' + (L - 8) + '" y="' + (y + 4).toFixed(1) + '" text-anchor="end">' + fmtNum(yv) + "</text>");
}
// x 轴
parts.push('<line class="axis" x1="' + L + '" y1="' + (T + ih) + '" x2="' + (VB_W - R) + '" y2="' + (T + ih) + '"/>');
// x 轴标签:点多时抽稀,最多 8 个,否则 14 天标签会互相叠
var step = Math.ceil(n / 8);
for (var i = 0; i < n; i++) {
if (i % step !== 0 && i !== n - 1) continue;
parts.push('<text class="tick" x="' + X(i).toFixed(1) + '" y="' + (T + ih + 17) + '" text-anchor="middle">' +
IP.esc(labels[i]) + "</text>");
}
series.forEach(function (s, si) {
var color = s.color || COLOR[si % COLOR.length];
var pts = s.values.map(function (v, i) { return X(i).toFixed(1) + "," + Y(v).toFixed(1); }).join(" ");
if (s.fill !== false && series.length === 1) {
parts.push('<polygon fill="' + color + '" fill-opacity=".10" stroke="none" points="' +
X(0).toFixed(1) + "," + (T + ih) + " " + pts + " " + X(n - 1).toFixed(1) + "," + (T + ih) + '"/>');
}
parts.push('<polyline fill="none" stroke="' + color + '" stroke-width="2" stroke-linejoin="round" ' +
'stroke-linecap="round" points="' + pts + '"/>');
// 点:带 <title>,鼠标停上去显示「日期 · 序列名:值」
s.values.forEach(function (v, i) {
parts.push('<circle class="dot" cx="' + X(i).toFixed(1) + '" cy="' + Y(v).toFixed(1) + '" r="2.8" fill="' + color + '">' +
"<title>" + IP.esc(labels[i] + " · " + s.name + ":" + v) + "</title></circle>");
});
});
var legend = series.map(function (s, si) {
var color = s.color || COLOR[si % COLOR.length];
return '<span><i style="background:' + color + '"></i>' + IP.esc(s.name) + "</span>";
}).join("");
el.innerHTML =
(series.length > 1 ? '<div class="chart-legend">' + legend + "</div>" : "") +
// 不设 preserveAspectRatio="none":那会按容器横向拉伸,圆点变椭圆、文字变形。
// 保持默认等比缩放(CSS 里 height:auto),图表随宽度整体放大,文字始终正。
'<svg viewBox="0 0 ' + VB_W + " " + H + '" role="img">' +
parts.join("") + "</svg>";
}
/**
* 横向条形图(HTML 实现:文字对齐与换行交给浏览器,比 SVG text 稳)。
* @param opts.items [{ label, value, sub, color }] 已按业务排序
* @param opts.valueText 自定义右侧数值文本
*/
function bars(el, opts) {
if (!el) return;
opts = opts || {};
var items = opts.items || [];
if (!items.length) return empty(el, opts.emptyText || "该区间内还没有数据");
var max = 0;
items.forEach(function (it) { if (it.value > max) max = it.value; });
if (max <= 0) max = 1;
el.innerHTML = items.map(function (it, i) {
var pct = Math.max(2, Math.round((it.value / max) * 100));
var color = it.color || COLOR[i % COLOR.length];
var right = opts.valueText ? opts.valueText(it) : "<b>" + fmtNum(it.value) + "</b>" +
(it.sub ? ' <span class="muted">' + IP.esc(it.sub) + "</span>" : "");
return '<div class="bar-row">' +
'<div class="lb" title="' + IP.esc(it.label) + '">' + IP.esc(it.label) + "</div>" +
'<div class="bar-track"><div class="bar-fill" style="width:' + pct + "%;background:" + color + '"></div></div>' +
'<div class="vv">' + right + "</div>" +
"</div>";
}).join("");
}
return { line: line, bars: bars, fmtNum: fmtNum, niceMax: niceMax, COLOR: COLOR };
})();