feat(gateway): GET /me/usage —— 用户自己租户的用量明细(余额+趋势+最近消耗)

面向用户口径(非 admin,受插件按请求 ctx 租户自动隔离):返回积分余额 +
credit_enforce + 按天消耗趋势 + 区间合计 + 最近 10 条消耗明细。复用
UsageTrend/TenantBalance,新增 RecentUsage(按 tenant 取近 N 条 usage_event)。

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Blizzard
2026-07-08 09:31:12 +08:00
parent 511ae199a4
commit f6ea03b34b
3 changed files with 59 additions and 0 deletions
+14
View File
@@ -114,6 +114,20 @@ func (p *Postgres) SaveUsageEvent(ctx context.Context, ev *contract.UsageEvent)
})
}
// RecentUsage 返回某租户最近 n 条用量明细(供用户看"最近消耗")。受租户表→用户 ctx 自动过滤,
// 这里也显式带 tenant_id 双保险。
func (p *Postgres) RecentUsage(ctx context.Context, tenantID string, n int) []UsageEvent {
if p.db == nil || tenantID == "" {
return nil
}
if n <= 0 {
n = 10
}
var out []UsageEvent
p.db.WithContext(ctx).Where("tenant_id = ?", tenantID).Order("ts desc").Limit(n).Find(&out)
return out
}
// pricingForModelName 按模型名查计价(join model 表,pricing 以 model_id 关联)。查不到返回 nil。
func (p *Postgres) pricingForModelName(ctx context.Context, name string) *Pricing {
if p.db == nil || name == "" {