Documentation
¶
Index ¶
- func Migrate(logger *zap.Logger, db *gorm.DB) error
- func Open(logger *zap.Logger, path string) (*gorm.DB, error)
- type CostFunc
- type GlobalStats
- type Group
- type GroupRepo
- type Peer
- type RefreshToken
- type RefreshTokenRepo
- func (r *RefreshTokenRepo) Create(rt *RefreshToken) error
- func (r *RefreshTokenRepo) DeleteExpired() (int64, error)
- func (r *RefreshTokenRepo) GetByJTI(jti string) (*RefreshToken, error)
- func (r *RefreshTokenRepo) Revoke(jti string) error
- func (r *RefreshTokenRepo) RevokeAllForUser(userID string) error
- type UsageFilter
- type UsageLog
- type UsageRecord
- type UsageRepo
- func (r *UsageRepo) GlobalSumTokens(from, to time.Time) (GlobalStats, error)
- func (r *UsageRepo) ListUnsynced(limit int) ([]UsageLog, error)
- func (r *UsageRepo) MarkSynced(requestIDs []string) error
- func (r *UsageRepo) Query(filter UsageFilter) ([]UsageLog, error)
- func (r *UsageRepo) SumCostUSD(from, to time.Time) (float64, error)
- func (r *UsageRepo) SumTokens(userID string, from, to time.Time) (inputSum, outputSum int64, err error)
- func (r *UsageRepo) UserStats(from, to time.Time, limit int) ([]UserStatRow, error)
- type UsageWriter
- type User
- type UserRepo
- func (r *UserRepo) Create(u *User) error
- func (r *UserRepo) GetByID(id string) (*User, error)
- func (r *UserRepo) GetByUsername(username string) (*User, error)
- func (r *UserRepo) ListByGroup(groupID string) ([]User, error)
- func (r *UserRepo) SetActive(id string, active bool) error
- func (r *UserRepo) UpdateLastLogin(id string, at time.Time) error
- func (r *UserRepo) UpdatePassword(id string, hash string) error
- type UserStatRow
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type GlobalStats ¶
type GlobalStats struct {
TotalInput int64
TotalOutput int64
TotalTokens int64 // = TotalInput + TotalOutput
RequestCount int64
ErrorCount int64
}
GlobalStats 全局用量统计
type Group ¶
type Group struct {
ID string `gorm:"primarykey"`
Name string `gorm:"uniqueIndex;not null"`
DailyTokenLimit *int64 // NULL = 无限制
MonthlyTokenLimit *int64 // NULL = 无限制
RequestsPerMinute *int // NULL = 无限制(每分钟请求数 RPM)
CreatedAt time.Time
}
Group 用户分组(配额管理)
type GroupRepo ¶
type GroupRepo struct {
// contains filtered or unexported fields
}
GroupRepo 分组仓库
func NewGroupRepo ¶
NewGroupRepo 创建 GroupRepo
type Peer ¶
type Peer struct {
ID string `gorm:"primarykey"` // e.g. "sp-2"
Addr string `gorm:"uniqueIndex;not null"`
Weight int `gorm:"default:50"`
IsActive bool `gorm:"default:true"`
RegisteredAt time.Time
LastSeen *time.Time
}
Peer 集群中已注册的 s-proxy 节点(sp-1 专用)
type RefreshToken ¶
type RefreshToken struct {
JTI string `gorm:"primarykey"`
UserID string `gorm:"not null;index"`
ExpiresAt time.Time `gorm:"not null"`
Revoked bool `gorm:"default:false"`
CreatedAt time.Time
}
RefreshToken 刷新令牌(用于撤销)
func (RefreshToken) TableName ¶
func (RefreshToken) TableName() string
type RefreshTokenRepo ¶
type RefreshTokenRepo struct {
// contains filtered or unexported fields
}
RefreshTokenRepo 管理刷新令牌的持久化存储
func NewRefreshTokenRepo ¶
func NewRefreshTokenRepo(db *gorm.DB, logger *zap.Logger) *RefreshTokenRepo
NewRefreshTokenRepo 创建 RefreshTokenRepo
func (*RefreshTokenRepo) Create ¶
func (r *RefreshTokenRepo) Create(rt *RefreshToken) error
Create 持久化一条新的刷新令牌
func (*RefreshTokenRepo) DeleteExpired ¶
func (r *RefreshTokenRepo) DeleteExpired() (int64, error)
DeleteExpired 删除所有已过期的令牌(定期清理用)
func (*RefreshTokenRepo) GetByJTI ¶
func (r *RefreshTokenRepo) GetByJTI(jti string) (*RefreshToken, error)
GetByJTI 按 JTI 查询(不存在返回 nil, nil)
func (*RefreshTokenRepo) Revoke ¶
func (r *RefreshTokenRepo) Revoke(jti string) error
Revoke 将指定 JTI 的刷新令牌标记为已撤销
func (*RefreshTokenRepo) RevokeAllForUser ¶
func (r *RefreshTokenRepo) RevokeAllForUser(userID string) error
RevokeAllForUser 撤销指定用户的所有刷新令牌(强制下线)
type UsageFilter ¶
type UsageFilter struct {
UserID string
GroupID string
From *time.Time
To *time.Time
Model string
Limit int
Offset int
}
UsageFilter 用量查询条件
type UsageLog ¶
type UsageLog struct {
ID uint `gorm:"primarykey;autoIncrement"`
RequestID string `gorm:"uniqueIndex;not null"` // 幂等防重
UserID string `gorm:"not null;index"`
Model string
InputTokens int `gorm:"default:0"`
OutputTokens int `gorm:"default:0"`
TotalTokens int `gorm:"default:0"`
IsStreaming bool `gorm:"default:false"`
UpstreamURL string
StatusCode int
DurationMs int64
CostUSD float64 `gorm:"default:0"` // 估算费用(USD)
SourceNode string `gorm:"default:'local'"` // 数据来源节点 ID
Synced bool `gorm:"default:false;index"` // sp-2+ 用:是否已上报给 sp-1
CreatedAt time.Time `gorm:"index"`
}
UsageLog 用量日志(核心统计表)
type UsageRecord ¶
type UsageRecord struct {
RequestID string
UserID string
Model string
InputTokens int
OutputTokens int
IsStreaming bool
UpstreamURL string
StatusCode int
DurationMs int64
SourceNode string
CreatedAt time.Time
}
UsageRecord 用量写入数据(通过 channel 传递,不直接操作 DB)
func (*UsageRecord) TotalTokens ¶
func (r *UsageRecord) TotalTokens() int
TotalTokens 计算总 token 数(辅助方法,放在 UsageRecord 上)
type UsageRepo ¶
type UsageRepo struct {
// contains filtered or unexported fields
}
UsageRepo 提供用量日志的查询接口
func NewUsageRepo ¶
NewUsageRepo 创建 UsageRepo
func (*UsageRepo) GlobalSumTokens ¶
func (r *UsageRepo) GlobalSumTokens(from, to time.Time) (GlobalStats, error)
GlobalSumTokens 计算时间段内所有用户的汇总统计
func (*UsageRepo) ListUnsynced ¶
ListUnsynced 查询未上报给 sp-1 的记录(sp-2+ 使用)
func (*UsageRepo) MarkSynced ¶
MarkSynced 将指定 request_id 列表标记为已上报
func (*UsageRepo) Query ¶
func (r *UsageRepo) Query(filter UsageFilter) ([]UsageLog, error)
Query 查询用量日志(支持多条件过滤)
func (*UsageRepo) SumCostUSD ¶
SumCostUSD 计算时间段内的总估算费用(USD)
type UsageWriter ¶
type UsageWriter struct {
// contains filtered or unexported fields
}
UsageWriter 异步批量写入用量日志
func NewUsageWriter ¶
func NewUsageWriter(db *gorm.DB, logger *zap.Logger, bufferSize int, interval time.Duration) *UsageWriter
NewUsageWriter 创建 UsageWriter bufferSize: channel 容量,也是批量写入的最大条数 interval: 强制 flush 间隔
func (*UsageWriter) Flush ¶
func (w *UsageWriter) Flush()
Flush 阻塞等待当前 channel 中所有记录写入完成(graceful shutdown 用)
func (*UsageWriter) Record ¶
func (w *UsageWriter) Record(r UsageRecord)
Record 非阻塞写入一条用量记录到 channel 如果 channel 已满,丢弃记录并记录警告(保证代理主路不阻塞)
func (*UsageWriter) SetCostFunc ¶
func (w *UsageWriter) SetCostFunc(fn CostFunc)
SetCostFunc 设置费用计算函数(可选;nil 时不计算费用)
func (*UsageWriter) Start ¶
func (w *UsageWriter) Start(ctx context.Context)
Start 启动后台写入 goroutine(ctx 取消时停止)
func (*UsageWriter) Wait ¶
func (w *UsageWriter) Wait()
Wait 阻塞直到后台 goroutine 退出(用于测试和 graceful shutdown)
type User ¶
type User struct {
ID string `gorm:"primarykey"`
Username string `gorm:"uniqueIndex;not null"`
PasswordHash string `gorm:"not null"`
GroupID *string `gorm:"index"` // NULL 表示未分配分组
Group Group `gorm:"foreignKey:GroupID"`
IsActive bool `gorm:"default:true"`
CreatedAt time.Time
LastLoginAt *time.Time
}
User 系统用户
type UserRepo ¶
type UserRepo struct {
// contains filtered or unexported fields
}
UserRepo 用户仓库
func NewUserRepo ¶
NewUserRepo 创建 UserRepo
func (*UserRepo) GetByUsername ¶
GetByUsername 按用户名查询(含关联 Group)
func (*UserRepo) ListByGroup ¶
ListByGroup 按分组列出用户(groupID="" 表示列出所有用户)
func (*UserRepo) UpdateLastLogin ¶
UpdateLastLogin 更新最后登录时间