Skip to content

fix(usage): 按账户筛选时模型与端点分布的实际扣费不再替换为账号成本 - #8806

Merged
Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
feeeei:fix/usage-account-filter-actual-cost
Nov 16, 2026
Merged

Wei-Shaw merged 1 commit into
Wei-Shaw:mainfrom
feeeei:fix/usage-account-filter-actual-cost

Conversation

@feeeei

@feeeei feeeei commented Nov 14, 2026

Copy link
Copy Markdown
Contributor

背景

管理后台「使用记录」页只按账户筛选(不带用户、API 密钥)时,模型分布表里某个模型行的「实际」会比展开后其下单个用户行的「实际」还小,且与同一行的「成本」完全相同。

原因是模型统计 getModelStatsWithFiltersBySource 和端点统计(getEndpointStatsByColumnWithFilters、GetStatsWithFilters 里的端点分组)在 accountID > 0 && userID == 0 && apiKeyID == 0 时,把 actual_cost 列替换成了账号成本 SUM(COALESCE(account_stats_cost, total_cost) * COALESCE(account_rate_multiplier, 1))。而同一页面上:

  • 展开模型或端点行的用户明细(GetUserBreakdownStats)用的是真实 SUM(actual_cost);
  • 分组分布、统计卡片的 total_actual_cost 用的也是真实 SUM(actual_cost);
  • 模型统计本身已经单独返回 account_cost,表格已有「成本」列。

所以「实际」在模型、端点两张表里表示账号成本,在其余地方表示用户实际扣费,同一页面口径不一致。

改动

  • 模型统计、端点统计的 actual_cost 统一为 SUM(actual_cost)(用户实际扣费),账号成本由 account_cost 承载。
  • usagestats.EndpointStat 新增 account_cost(omitempty),端点分布表 EndpointDistributionChart 增加「成本」列(showAccountCost,默认显示,与模型、分组分布表一致)。
  • 用户侧 /api/v1/usage/stats 清零端点的 account_cost,用户使用记录页的端点分布隐藏「成本」列。
  • Gemini 配额用量 geminiAggregateUsage 原先借助上述替换拿到账号成本,现在显式读 AccountCost;GetGeminiUsageTotalsBatch 的 flash_cost/pro_cost 同样改为账号成本,两条路径写入同一份 GeminiUsageTotals 缓存,口径一致。账号用量窗口展示不变。

行为变化

仅按账户过滤时,以下接口中的 actual_cost 由账号成本改为用户实际扣费,账号成本请看 account_cost:

  • GET /admin/dashboard/models
  • GET /admin/usage/stats 的 endpoints / upstream_endpoints / endpoint_paths
  • GET /admin/accounts/:id/stats 的 models / endpoints / upstream_endpoints

/admin/accounts/:id/stats 的 history / summary 口径不变(前端已分别标注为账号计费与用户扣费)。账号统计弹窗的模型、端点表因此显示为「实际 = 用户扣费、成本 = 账号成本」,与列名一致。

测试

  • unit(sqlmock):仅按账户过滤时模型统计、端点统计查询的 actual_cost 为 SUM(actual_cost)、account_cost 为账号成本表达式,扫描结果两列分别正确;geminiAggregateUsage 按 AccountCost 累计。
  • integration:TestGetModelStatsWithFilters 加入带账号倍率的记录,断言按账户过滤时 ActualCost 与 AccountCost 各自的值。
  • go test ./internal/repository/ ./internal/handler/... ./internal/pkg/usagestats/、go test -tags unit ./internal/service/ ./internal/handler/... ./internal/repository/ ./internal/server/、golangci-lint run(相关包)、go test -tags integration -run TestUsageLogRepoSuite ./internal/repository/ 均通过;TestInflightEstimate_AccountMappingNoDBAndBoundedMemory 在整包运行时偶发超出堆增量上限,单跑通过,与本改动无关。
  • 前端 vue-tsc --noEmit、eslint(改动文件)、vitest run src/components/charts 及管理端/用户端 UsageView 用例通过。

模型统计和端点统计在仅按 account_id 过滤(无 user_id/api_key_id)时,
把 actual_cost 换成了账号成本 SUM(COALESCE(account_stats_cost, total_cost)
* COALESCE(account_rate_multiplier, 1));展开后的用户明细与分组分布用的是
真实 SUM(actual_cost),于是模型行「实际」会小于其下单个用户行的「实际」,
且与同页「成本」列数值相同。

- actual_cost 统一为用户实际扣费,账号成本由 account_cost 承载;
  EndpointStat 新增 account_cost,端点分布表增加「成本」列,用户侧
  /api/v1/usage/stats 清零该字段、用户页隐藏该列
- Gemini 配额用量(geminiAggregateUsage、GetGeminiUsageTotalsBatch)
  显式按账号成本累计,账号用量窗口展示不变

接口口径变化:仅按账户过滤时,/admin/dashboard/models、/admin/usage/stats
的 endpoints/upstream_endpoints/endpoint_paths、/admin/accounts/:id/stats
的 models/endpoints 中 actual_cost 由账号成本改为用户实际扣费,账号成本改看
account_cost;/admin/accounts/:id/stats 的 history/summary 口径不变。
@Wei-Shaw
Wei-Shaw merged commit 568db32 into Wei-Shaw:main Nov 16, 2026
8 of 9 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 16, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants