后端 - 新增 proxy/convert 三协议(chat/messages/responses)请求、响应与 SSE 流式互转, 以 Chat 为中间模型;usage.go 统一提取三协议 token 用量(含单测) - gateway: 跨协议调度(渠道未声明客户端协议时转为渠道首选格式), streamResponse 按 \n\n 分块逐行转换直通,bufferResponse 转换失败时剥非 JSON 前缀 - gateway: 新增 SetUsageRecorder 注入异步用量记录器 - auth_llm: 修复 key_prefix 查询长度错配([:8] vs 存储的 [:12])导致全部 401; 修复长度 8-11 的 key 切片越界 panic;统一 unauthorized 响应 - usage: 日报表改为增量累加 upsert,避免多次 flush 互相清零;记录协议/错误码/时延等字段 - channel: 新增渠道并发槽 TryAcquire;健康检查支持可配置参数 - api: 新增 admin 渠道/模型/系统配置管理端点(旧端点保留兼容) 前端 - 新增渠道管理、模型管理、系统配置视图与 ChannelModelsDrawer - 新增 ui 基础组件(Button/Badge/Input/Modal)与 protocol.ts - 调整 Toast 样式、密钥页、路由菜单;dev 代理默认指向 3000 端口
109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package dao
|
||
|
||
import (
|
||
"context"
|
||
"opencatd-open/internal/store"
|
||
"time"
|
||
|
||
"gorm.io/gorm"
|
||
"gorm.io/gorm/clause"
|
||
)
|
||
|
||
type UsageDAO struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
type DailyUsageDAO struct {
|
||
db *gorm.DB
|
||
}
|
||
|
||
func NewUsageDAO(db *gorm.DB) *UsageDAO {
|
||
return &UsageDAO{db: db}
|
||
}
|
||
|
||
func NewDailyUsageDAO(db *gorm.DB) *DailyUsageDAO {
|
||
return &DailyUsageDAO{db: db}
|
||
}
|
||
|
||
// UsageLog DAO
|
||
func (d *UsageDAO) Create(ctx context.Context, log *store.UsageLog) error {
|
||
return d.db.WithContext(ctx).Create(log).Error
|
||
}
|
||
|
||
func (d *UsageDAO) BatchCreate(ctx context.Context, logs []*store.UsageLog) error {
|
||
return d.db.WithContext(ctx).Create(logs).Error
|
||
}
|
||
|
||
func (d *UsageDAO) ListByUserID(ctx context.Context, userID uint64, limit, offset int) ([]*store.UsageLog, error) {
|
||
var logs []*store.UsageLog
|
||
err := d.db.WithContext(ctx).
|
||
Where("user_id = ?", userID).
|
||
Order("created_at DESC").
|
||
Limit(limit).
|
||
Offset(offset).
|
||
Find(&logs).Error
|
||
return logs, err
|
||
}
|
||
|
||
func (d *UsageDAO) Delete(ctx context.Context, id uint64) error {
|
||
return d.db.WithContext(ctx).Delete(&store.UsageLog{}, id).Error
|
||
}
|
||
|
||
func (d *UsageDAO) CountByUserID(ctx context.Context, userID uint64) (int64, error) {
|
||
var count int64
|
||
err := d.db.WithContext(ctx).Model(&store.UsageLog{}).Where("user_id = ?", userID).Count(&count).Error
|
||
return count, err
|
||
}
|
||
|
||
// UsageDaily DAO
|
||
func (d *DailyUsageDAO) Create(ctx context.Context, log *store.UsageDaily) error {
|
||
return d.db.WithContext(ctx).Create(log).Error
|
||
}
|
||
|
||
func (d *DailyUsageDAO) ListByUserID(ctx context.Context, userID uint64, limit, offset int) ([]*store.UsageDaily, error) {
|
||
var logs []*store.UsageDaily
|
||
err := d.db.WithContext(ctx).
|
||
Where("user_id = ?", userID).
|
||
Order("date DESC").
|
||
Limit(limit).
|
||
Offset(offset).
|
||
Find(&logs).Error
|
||
return logs, err
|
||
}
|
||
|
||
func (d *DailyUsageDAO) GetByDate(ctx context.Context, userID uint64, date string) (*store.UsageDaily, error) {
|
||
var log store.UsageDaily
|
||
err := d.db.WithContext(ctx).
|
||
Where("user_id = ? AND date = ?", userID, date).
|
||
First(&log).Error
|
||
if err != nil {
|
||
return nil, err
|
||
}
|
||
return &log, nil
|
||
}
|
||
|
||
// UpsertDailyUsage 按 (user_id, model_id, date) 累加式 upsert:
|
||
// 行不存在则插入;存在则在原值基础上增量累加(不能用 AssignmentColumns 覆盖,
|
||
// 否则多次 flush 会互相清零)。非限定列名在 SQLite/MySQL/PG 的 upsert 语义下都指向目标行。
|
||
func (d *DailyUsageDAO) UpsertDailyUsage(ctx context.Context, log *store.UsageDaily) error {
|
||
return d.db.WithContext(ctx).Clauses(clause.OnConflict{
|
||
Columns: []clause.Column{{Name: "user_id"}, {Name: "model_id"}, {Name: "date"}},
|
||
DoUpdates: clause.Assignments(map[string]interface{}{
|
||
"requests": gorm.Expr("requests + ?", log.Requests),
|
||
"input_tokens": gorm.Expr("input_tokens + ?", log.InputTokens),
|
||
"output_tokens": gorm.Expr("output_tokens + ?", log.OutputTokens),
|
||
"cache_read_tokens": gorm.Expr("cache_read_tokens + ?", log.CacheReadTokens),
|
||
"cost": gorm.Expr("cost + ?", log.Cost),
|
||
}),
|
||
}).Create(log).Error
|
||
}
|
||
|
||
func (d *DailyUsageDAO) ListByDateRange(ctx context.Context, userID uint64, start, end time.Time) ([]*store.UsageDaily, error) {
|
||
var logs []*store.UsageDaily
|
||
err := d.db.WithContext(ctx).
|
||
Where("user_id = ? AND date >= ? AND date <= ?", userID, start.Format("2006-01-02"), end.Format("2006-01-02")).
|
||
Order("date DESC").
|
||
Find(&logs).Error
|
||
return logs, err
|
||
}
|