Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
- `POST/GET /api/api-keys` + `PATCH/DELETE /api/api-keys/:id` — 分发 key 生成 / 列表 / 改名与启停(PATCH 的 `status: active|paused`)/ 撤销(软删,不可逆)
- `GET /api/models` — 模型列表(含可用 key 与价格)
- `POST /v1/chat/completions` / `/v1/responses` / `/anthropic/v1/messages` — 网关(非流式 + SSE)
- `GET /api/wallet` / `/api/transactions` / `/api/dashboard` — 钱包 / 交易(summary + 明细)/ 仪表盘
- `GET /api/wallet` / `/api/transactions` / `/api/dashboard` — 钱包 / 交易(summary + 按 api-key 的 by_key + 明细)/ 仪表盘
- `POST/GET /api/sharings` + `PATCH /api/sharings/:id` — key 上架 / 列表 / 编辑(PATCH 部分更新:暂停 · 恢复 · 下线,以及上架时的全部设置)
- `POST /api/admin/credits` / `GET /api/admin/users` + `PATCH /api/admin/users/:id`(仅改部门) / `usage` / `models` CRUD — 管理(role=admin)
- `GET/POST /api/admin/departments` + `PATCH/DELETE /api/admin/departments/:id` — 部门管理(role=admin)
Expand Down
12 changes: 6 additions & 6 deletions src/i18n_pack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,12 @@ const EN_END: &str = "\n };";
///
/// ⚠️ `T_LITERAL_COUNT` 是 `T("…")` **调用点**总数,不是键数,也不是去重后的键数 ——
/// 三个集合各不相同(坑 99);说「这个数不该变」之前先确认它在数哪个集合。
const ZH_KEY_COUNT: usize = 811;
const EN_KEY_COUNT: usize = 811;
const STATIC_ATTR_COUNT: usize = 336;
const STATIC_ATTR_DISTINCT: usize = 310;
const T_LITERAL_COUNT: usize = 556;
const T_LITERAL_DISTINCT: usize = 438;
const ZH_KEY_COUNT: usize = 817;
const EN_KEY_COUNT: usize = 817;
const STATIC_ATTR_COUNT: usize = 338;
const STATIC_ATTR_DISTINCT: usize = 312;
const T_LITERAL_COUNT: usize = 565;
const T_LITERAL_DISTINCT: usize = 442;

/// 切出语言包区段(起点标记 → 终点标记,含起点)。
fn pack_region<'a>(src: &'a str, start_mark: &str, end_mark: &str) -> &'a str {
Expand Down
9 changes: 5 additions & 4 deletions src/perf_gate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ fn violations(src: &str) -> Vec<(usize, String)> {
// ---------------------------------------------------------------------------
// 第二条不变量(rant 2026-09-14T21:15:02 第 3 条):**聚合语句按需 JOIN**。
//
// `wallet.rs` 的 summary / COUNT / trend 三条语句只碰 `t.*`。而只要 SQL 里出现
// `wallet.rs` 的 summary / COUNT / trend / by-key 四条语句只碰 `t.*`(by-key 的 key 名走
// 标量子查询,不是 JOIN)。而只要 SQL 里出现
// `tx_joins()` 那三个 LEFT JOIN,SQLite 就**不再选用覆盖索引**
// `idx_transactions_user_id_time_type_pts_tokens`,退回 `idx_transactions_user_id_id`
// 逐行回表:dev 库(18.6 万行 / NFS)同一条查询 **0.21s → 22.6s(≈100×)**,
Expand Down Expand Up @@ -190,12 +191,12 @@ fn the_transaction_aggregates_never_join_unconditionally() {
body.iter().any(|(_, l)| l.contains("tx_joins(")),
"`tx_joins_if` 需要 JOIN 时仍应复用 `tx_joins`(唯一一份 JOIN 文案)"
);
// ③ 阳性对照:按需拼接确实还在用 —— summary / COUNT / trend 三处。
// ③ 阳性对照:按需拼接确实还在用 —— summary / COUNT / trend / by-key 四处。
// (新增聚合语句时请一并更新此计数:这是有意的摩擦,不是噪音。)
assert_eq!(
calls_of(src, "tx_joins_if("),
3,
"wallet 的三条聚合语句都应经 `tx_joins_if` 拼接 JOIN"
4,
"wallet 的四条聚合语句都应经 `tx_joins_if` 拼接 JOIN"
);
// ④ 阳性对照:判定与拼接共用同一谓词。
assert_eq!(
Expand Down
433 changes: 432 additions & 1 deletion src/routes/wallet.rs

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ ui/
- 工具栏右侧 `#tx-count` 同步显示 `tx.pager.count`(共 N 条),与汇总卡的记录数同源;
- 图表:`#tx-trend` 改用原型 `.trend` 双色柱状(消费 / 收益两柱 + `.legend`),数据源仍是 `/api/transactions/trend`;**x 轴按请求窗口补零,保证左→右时间递增且柱距恒定**(后端 GROUP BY 只返回有交易的桶,缺行会导致柱子左移——原型同款 bug 的根因);
- 窄屏(≤560px)`.stat-grid` 两列、`.trend` 高度收紧。
- **按 API Key 用量**(rant 2026-11-12T14:13:23 验收项 ②):汇总卡下方第二块是 `#tx-by-key`(一张小表:api-key 名 / 消费 / 收益 / Token 合计 / 条数),数据来自 `/api/transactions` 响应的 **`by_key`** 数组 —— 它由服务端用**与 `summary` 同一个 `where_sql`、同一份绑定**算出,所以「各行之和 == 汇总卡」不是靠前端把两份数据对齐出来的,结构上只有一份口径。分组键是**读模型视图的** `api_key_id`(不是汇总表的 —— 汇总表用 `0` 当 NULL 哨兵),条数是 `SUM(row_count)` 而不是 `COUNT(*)`;`api_key_id` 为 `null` 的那行是「没有 api-key 的交易」(供应商侧 earn / topup / expire / withdraw),**显式成组并排在末行**,丢掉它各组之和就对不上汇总卡。默认按消费点数降序,筛选与上方汇总卡完全共享(同一份载荷,不发额外请求)。
- **趋势卡四档**(rant 2026-09-23T21:03:32):`#tx-trend-modes`(`.tabs > .tab`,形状复用 `#tx-tabs`)提供 **总点数(默认)/ 消费 / 收入 / 消费+收入** 四档,四档**共用同一份载荷**(`/api/transactions/trend` 已同时返回 `net`/`income`/`expense`)⇒ 切档只重绘本卡,**档位不进 `txQuerySig()`、不接 `reloadTransactions()`**(否则每次点档多发一次请求、且四档不再同源)。
- `net` 档=**窗口内累计净变化**(自最早桶起逐桶累加 `net`,窗口起点为 0,可为负),故卡片标题/副标题换 `tx.trend.card.net` / `tx.trend.net.sub` **如实写明口径**——加了列筛选后绝对余额不可定义,不得暗示为余额水位;形态用 `sparkline()` **折线**、y 轴按 `|值|` 定标且**包含零基线**;
- 其余三档保持柱状;归一化的 `max` **只按当前显示的系列**取(`txTrendValues()`),`title` tooltip 与 `#tx-trend-legend` **只陈述显示的系列**;
Expand Down
13 changes: 11 additions & 2 deletions ui/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,15 @@ <h2 data-i18n="view.transactions">交易记录</h2>
<div class="card">
<div class="stat-grid" id="tx-summary"></div>
</div>
<!-- 按 API Key 用量对比(rant 2026-11-12T14:13:23 验收项 ②):与上方汇总卡是**同一套筛选**,
只是把粒度换成 api-key —— 汇总卡答「一共多少」,本卡答「哪把 key 花了多少」。
没有 api-key 的交易(供应商侧 earn / topup / expire / withdraw)显式成组、排在末行:
静默丢掉它们,各组之和就与上方的汇总卡对不上。 -->
<div class="card mt16">
<h3 data-i18n="tx.bykey.title">按 API Key 用量</h3>
<p class="card-sub" data-i18n="tx.bykey.sub">单位:点数 · 与上方汇总卡同一筛选,按 api-key 分组</p>
<div class="table-wrap compact" id="tx-by-key"></div>
</div>
<!-- 每日消费 / 收益 / 总点数(.trend:折线或双色柱状,x 轴左→右时间递增) -->
<div class="card mt16">
<h3 id="tx-trend-title" data-i18n="tx.trend.card">每日消费 / 收益</h3>
Expand Down Expand Up @@ -867,7 +876,7 @@ <h3 id="chat-title">使用模型</h3>

<script src="js/api.js?v=20261012-1"></script>
<script src="js/data.js?v=20261017-2"></script>
<script src="js/i18n.js?v=20261112-1"></script>
<script src="js/app.js?v=20261112-1"></script>
<script src="js/i18n.js?v=20261112-2"></script>
<script src="js/app.js?v=20261112-2"></script>
</body>
</html>
39 changes: 39 additions & 0 deletions ui/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,42 @@
stat(T("tx.summary.count"), D.fmt(count), T("tx.summary.count.sub"));
}

// 按 API Key 用量对比(rant 2026-11-12T14:13:23 验收项 ②):载荷是 `/api/transactions` 的
// `by_key` —— 服务端用**同一个** `where_sql` 与同一份绑定算出它与 `summary`,所以「各组之和
// == 上方汇总卡」不是前端把两份请求对齐出来的,结构上只有一份口径。
// 分组键是服务端的 `api_key_id`:`null` 是**显式的一组**(供应商侧 earn / topup / expire /
// withdraw 等没有 api-key 的交易),不是缺失数据 —— 静默丢掉它,各组之和就对不上汇总卡。
function renderTxByKey() {
const el = $("#tx-by-key");
if (!el) return;
const rows = (Live.transactions && Array.isArray(Live.transactions.by_key))
? Live.transactions.by_key
: [];
if (!rows.length) {
el.innerHTML = emptyState(T("tx.bykey.empty"), T("tx.bykey.empty.sub"));
return;
}
// 列头复用汇总卡与明细表的键:这一块说的是同一件事的另一个粒度,换一套词反而让两处
// 看起来像两条不同的序列(`ui/README.md` 的「一条序列一个词」)。
const cKey = T("tx.col.apiKeyName"), cPts = T("tx.summary.expense"), cInc = T("tx.summary.income"),
cTok = T("tx.summary.tokens"), cCnt = T("tx.summary.count");
el.innerHTML = '<table class="table"><thead><tr>' +
"<th>" + esc(cKey) + "</th>" +
'<th class="num">' + esc(cPts) + "</th>" +
'<th class="num">' + esc(cInc) + "</th>" +
'<th class="num">' + esc(cTok) + "</th>" +
'<th class="num">' + esc(cCnt) + "</th></tr></thead><tbody>" +
rows.map((r) => {
const label = r.api_key_id == null ? T("tx.bykey.none") : (r.key_name || T("tx.bykey.unnamed"));
return "<tr><td data-label='" + cKey + "'>" + esc(label) + "</td>" +
'<td class="num" data-label="' + cPts + '">' + D.fmt(Math.abs(r.expense_pts || 0)) + "</td>" +
'<td class="num" data-label="' + cInc + '">' + D.fmt(r.income_pts || 0) + "</td>" +
'<td class="num" data-label="' + cTok + '">' + fmtTokens(r.tokens || 0) + "</td>" +
'<td class="num" data-label="' + cCnt + '">' + D.fmt(r.entries || 0) + "</td></tr>";
}).join("") +
"</tbody></table>";
}

// 每日消费/收益趋势(rant 2026-09-11T16:23:43 第 7 节 + PR4 的 renderDashTrend 先例):
// 改用原型 .trend 双色柱状图(消费 = accent、收益 = ok),柱高按当日 max 归一、最小 2%;
// 数据源仍是 /api/transactions/trend(与列表同 type/时间段/列筛选口径)。
Expand Down Expand Up @@ -1910,6 +1946,7 @@
// 加载失败 → 空态 + 重试;游客不可达(导航拦截)
if (loggedIn() && !Live.transactions) {
renderTxSummary([]);
renderTxByKey();
renderTxTrend();
const c = $("#tx-count");
if (c) c.textContent = "";
Expand All @@ -1927,6 +1964,8 @@
let list = Live.transactions ? txsToView(Live.transactions.items || []) : [];
// 交易汇总卡:与 tab + 列筛选联动,与表格可见行一致(rant 20:39:30 B;记录数走后端 total)
renderTxSummary(filterRows(list, TX_COLUMNS, txTable.filters));
// 同筛选、按 api-key 的另一个粒度(载荷与 `summary` 同源,不发额外请求)
renderTxByKey();
// 记录数(原型 #tx-count):后端 total(真分页下即筛选后的全量条数)
const cntEl = $("#tx-count");
if (cntEl) {
Expand Down
12 changes: 12 additions & 0 deletions ui/js/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,12 @@
"tx.summary.count": "记录数",
"tx.summary.count.sub": "当前筛选",
"tx.summary.tokens": "总 Token",
"tx.bykey.title": "按 API Key 用量",
"tx.bykey.sub": "单位:点数 · 与上方汇总卡同一筛选,按 api-key 分组",
"tx.bykey.none": "未绑定 API Key",
"tx.bykey.unnamed": "未命名",
"tx.bykey.empty": "当前筛选下没有交易",
"tx.bykey.empty.sub": "换个时间段或清掉列筛选再看看",
"tx.trend.empty": "当前时间段暂无趋势数据",
"tx.trend.metric.expense": "消费",
"tx.trend.metric.income": "收入",
Expand Down Expand Up @@ -1217,6 +1223,12 @@
"tx.summary.count": "Records",
"tx.summary.count.sub": "Current filter",
"tx.summary.tokens": "Total",
"tx.bykey.title": "Usage by API key",
"tx.bykey.sub": "Unit: points · same filters as the summary above, grouped by API key",
"tx.bykey.none": "No API key",
"tx.bykey.unnamed": "Unnamed",
"tx.bykey.empty": "No transactions under the current filters",
"tx.bykey.empty.sub": "Try another range or clear the column filters",
"tx.trend.empty": "No trend data in this range",
"tx.trend.metric.expense": "Spend",
"tx.trend.metric.income": "Income",
Expand Down
Loading