- 计费: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)
75 lines
2.6 KiB
Go
75 lines
2.6 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"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
|
|
func (h *Handler) UserProfile(c *gin.Context) {
|
|
u, _ := userFromContext(c)
|
|
resp.OK(c, gin.H{"user": h.publicUser(u)})
|
|
}
|
|
|
|
// 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)
|
|
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,
|
|
"spent_last_30d": spent,
|
|
"today": h.todayUsage(c, u.ID),
|
|
"models_available": h.availableModelCount(),
|
|
})
|
|
}
|
|
|
|
func (h *Handler) availableModelCount() int64 {
|
|
var n int64
|
|
h.a.DB.Model(&store.Model{}).Where("enabled = ?", true).Count(&n)
|
|
return n
|
|
}
|
|
|
|
func (h *Handler) todayUsage(c *gin.Context, userID uint64) gin.H {
|
|
var requests int64
|
|
var tokens int64
|
|
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).
|
|
Select("COALESCE(SUM(requests),0), COALESCE(SUM(input_tokens+output_tokens+cache_read_tokens),0), COALESCE(SUM(cost),0)").
|
|
Row().Scan(&requests, &tokens, &cost)
|
|
return gin.H{"requests": requests, "tokens": tokens, "cost": cost}
|
|
}
|
|
|
|
// UserModels GET /api/v1/user/models — 控制台可用模型列表(无需 API Key)。
|
|
// 仅返回启用的模型且至少绑定到一个启用且健康的渠道,与 /v1/models 口径一致。
|
|
func (h *Handler) UserModels(c *gin.Context) {
|
|
var names []string
|
|
if err := h.a.DB.Table("models").
|
|
Joins("JOIN channel_model_bindings ON channel_model_bindings.model_id = models.id").
|
|
Joins("JOIN channels ON channels.id = channel_model_bindings.channel_id").
|
|
Where("models.enabled = ? AND channels.enabled = ? AND channels.health_status = ?",
|
|
true, true, store.ChannelHealthHealthy).
|
|
Distinct("models.name").
|
|
Order("models.sort ASC, models.id ASC").
|
|
Pluck("models.name", &names).Error; err != nil {
|
|
resp.Fail(c, http.StatusInternalServerError, "failed to load models")
|
|
return
|
|
}
|
|
if names == nil {
|
|
names = []string{}
|
|
}
|
|
resp.OK(c, gin.H{"items": names})
|
|
}
|