Repository navigation
fix(usage): 按账户筛选时模型与端点分布的实际扣费不再替换为账号成本 - #8806
Merged
Wei-Shaw merged 1 commit intoNov 16, 2026
Merged
Conversation
模型统计和端点统计在仅按 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 口径不变。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
背景
管理后台「使用记录」页只按账户筛选(不带用户、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,用户使用记录页的端点分布隐藏「成本」列。geminiAggregateUsage原先借助上述替换拿到账号成本,现在显式读AccountCost;GetGeminiUsageTotalsBatch的flash_cost/pro_cost同样改为账号成本,两条路径写入同一份GeminiUsageTotals缓存,口径一致。账号用量窗口展示不变。行为变化
仅按账户过滤时,以下接口中的
actual_cost由账号成本改为用户实际扣费,账号成本请看account_cost:GET /admin/dashboard/modelsGET /admin/usage/stats的endpoints/upstream_endpoints/endpoint_pathsGET /admin/accounts/:id/stats的models/endpoints/upstream_endpoints/admin/accounts/:id/stats的history/summary口径不变(前端已分别标注为账号计费与用户扣费)。账号统计弹窗的模型、端点表因此显示为「实际 = 用户扣费、成本 = 账号成本」,与列名一致。测试
actual_cost为SUM(actual_cost)、account_cost为账号成本表达式,扫描结果两列分别正确;geminiAggregateUsage按AccountCost累计。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用例通过。