feat: 金额 decimal 精确计费、failover 按失败类型封顶、渠道小时桶预聚合、分层守护测试

- 计费:19 个金额字段 float64→decimal.Decimal,DB 列保持 numeric(20,8) 零迁移;
  API 金额仍为 JSON number(MarshalJSONWithoutQuotes),前端零改动
- failover:保留时间预算,新增 OT_PROXY_FAILOVER_CAPS_NET/SERVER/RATE/QUOTA
  按失败类型封顶换渠道次数(429/配额类瞬时失败时间预算形同虚设)
- 预聚合:新增 usage_channel_hourlies(schema v10),记账事务内同步 upsert,
  长窗口渠道统计走桶不再扫明细,spent_last_30d 改扫 usage_daily,v10 一次性回填幂等
- 守护:internal/arch_test.go 用 go/parser 强制分层依赖(fail-closed)
This commit is contained in:
Sakurasan committed 2026-11-09 00:41:48 +08:00
1 parent 5d6e5bdb04
commit b2e500ff51
25 files changed
+841 -253

No files matched your search

+8 -4
View File
@@ -7,6 +7,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/openteam/server/internal/pkg/resp"
"github.com/openteam/server/internal/store"
"github.com/shopspring/decimal"
)
// UserProfile GET /api/v1/user/profile
@@ -16,11 +17,14 @@ func (h *Handler) UserProfile(c *gin.Context) {
}
// UserBalance GET /api/v1/user/balance — 余额 + 近 30 日消耗。
// 消耗扫 usage_daily(预聚合)而非 usage_logs:语义等价(usage_daily 只含 cost>0 的
// 计费行,含 canceled——canceled 请求 token 已实际计费),且不受明细保留期清理影响。
func (h *Handler) UserBalance(c *gin.Context) {
u, _ := userFromContext(c)
var spent float64
h.a.DB.Model(&store.UsageLog{}).
Where("user_id = ? AND status = ? AND created_at >= ?", u.ID, store.UsageStatusSuccess, time.Now().Add(-30*24*time.Hour)).
cutoff := time.Now().UTC().AddDate(0, 0, -30).Format("2006-01-02")
var spent decimal.Decimal
h.a.DB.Model(&store.UsageDaily{}).
Where("user_id = ? AND date >= ?", u.ID, cutoff).
Select("COALESCE(SUM(cost),0)").Scan(&spent)
resp.OK(c, gin.H{
"balance": u.Balance,
@@ -39,7 +43,7 @@ func (h *Handler) availableModelCount() int64 {
func (h *Handler) todayUsage(c *gin.Context, userID uint64) gin.H {
var requests int64
var tokens int64
var cost float64
var cost decimal.Decimal
today := time.Now().UTC().Format("2006-01-02")
h.a.DB.Model(&store.UsageDaily{}).
Where("user_id = ? AND date = ?", userID, today).