Major rewrite of the Go backend to support:
- Three API format imports: openai, anthropic, compatible
- Three protocol conversions: Chat Completions, Responses, Messages
- Hub-and-spoke architecture with Chat as intermediate format
Deleted:
- opencat.go (old entry)
- store/, team/, pkg/team/, pkg/store/ (old data layer)
- internal/model/, internal/consts/ (old types)
- internal/service/team/, internal/controller/team/ (old handlers)
- llm/ (removed LLM client library, pure proxy mode)
- dist/, assets/ (old build artifacts)
Added:
- internal/store/ — 9 GORM models + multi-DB support
- internal/pkg/ — crypto (AES-GCM), apikey, jwt, ratelimit, resp, tokenizer
- internal/channel/ — channel selection, weighted LB, health checks
- internal/proxy/convert/ — 6 protocol conversion functions + SSE streaming
- internal/proxy/ — gateway with request dispatch and upstream selection
- internal/usage/ — async usage recorder with batch writes
- internal/api/ — management API (auth, users, keys, channels, models)
- Makefile for build/test/deploy
Fixed API to match frontend expectations:
- Login response wraps token in { data: { token } }
- GET /api/profile route added
- Profile response wraps user in { code, data }
- Role returned as number (10=admin, 1=user)
100 lines
2.8 KiB
Go
100 lines
2.8 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
|
|
}
|
|
|
|
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.AssignmentColumns([]string{"requests", "input_tokens", "output_tokens", "cache_read_tokens", "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
|
|
}
|