Build and Deploy / test-backend (push) Successful in 1m11s
Build and Deploy / lint-frontend (push) Successful in 21s
Build and Deploy / build-backend (push) Successful in 58s
Build and Deploy / deploy (push) Canceled after 0s
Build and Deploy / build-frontend (push) Canceled after 5m38s
137 lines
3.6 KiB
Go
137 lines
3.6 KiB
Go
package service
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"log"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/dusiburg/fancy-api/internal/model"
|
|
"github.com/google/uuid"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
const aiUsageTTL = 45 * 24 * time.Hour
|
|
|
|
type userIDContextKey struct{}
|
|
|
|
func WithUserID(ctx context.Context, id uuid.UUID) context.Context {
|
|
return context.WithValue(ctx, userIDContextKey{}, id)
|
|
}
|
|
|
|
func userIDFromContext(ctx context.Context) (uuid.UUID, bool) {
|
|
id, ok := ctx.Value(userIDContextKey{}).(uuid.UUID)
|
|
return id, ok
|
|
}
|
|
|
|
type UsageTracker struct {
|
|
redis *redis.Client
|
|
}
|
|
|
|
func NewUsageTracker(r *redis.Client) *UsageTracker {
|
|
return &UsageTracker{redis: r}
|
|
}
|
|
|
|
func (t *UsageTracker) RecordAIUsage(ctx context.Context, tokens int64) {
|
|
month := time.Now().UTC().Format("2006-01")
|
|
|
|
pipe := t.redis.Pipeline()
|
|
pipe.Incr(ctx, fmt.Sprintf("usage:ai:requests:%s", month))
|
|
pipe.IncrBy(ctx, fmt.Sprintf("usage:ai:tokens:%s", month), tokens)
|
|
pipe.Expire(ctx, fmt.Sprintf("usage:ai:requests:%s", month), aiUsageTTL)
|
|
pipe.Expire(ctx, fmt.Sprintf("usage:ai:tokens:%s", month), aiUsageTTL)
|
|
|
|
if userID, ok := userIDFromContext(ctx); ok {
|
|
uid := userID.String()
|
|
pipe.Incr(ctx, fmt.Sprintf("usage:ai:requests:%s:%s", uid, month))
|
|
pipe.IncrBy(ctx, fmt.Sprintf("usage:ai:tokens:%s:%s", uid, month), tokens)
|
|
pipe.SAdd(ctx, fmt.Sprintf("usage:ai:users:%s", month), uid)
|
|
pipe.Expire(ctx, fmt.Sprintf("usage:ai:requests:%s:%s", uid, month), aiUsageTTL)
|
|
pipe.Expire(ctx, fmt.Sprintf("usage:ai:tokens:%s:%s", uid, month), aiUsageTTL)
|
|
pipe.Expire(ctx, fmt.Sprintf("usage:ai:users:%s", month), aiUsageTTL)
|
|
}
|
|
|
|
if _, err := pipe.Exec(ctx); err != nil {
|
|
log.Printf("recording ai usage: %v", err)
|
|
}
|
|
}
|
|
|
|
func (t *UsageTracker) AIUsage(ctx context.Context, userID uuid.UUID) (*model.AIUsage, error) {
|
|
month := time.Now().UTC().Format("2006-01")
|
|
uid := userID.String()
|
|
|
|
pipe := t.redis.Pipeline()
|
|
requestsCmd := pipe.Get(ctx, fmt.Sprintf("usage:ai:requests:%s:%s", uid, month))
|
|
tokensCmd := pipe.Get(ctx, fmt.Sprintf("usage:ai:tokens:%s:%s", uid, month))
|
|
if _, err := pipe.Exec(ctx); err != nil && !errors.Is(err, redis.Nil) {
|
|
return nil, fmt.Errorf("getting ai usage keys: %w", err)
|
|
}
|
|
|
|
usage := &model.AIUsage{}
|
|
usage.Requests = atoi64(requestsCmd.Val())
|
|
usage.Tokens = atoi64(tokensCmd.Val())
|
|
|
|
return usage, nil
|
|
}
|
|
|
|
func (t *UsageTracker) ActiveUsers(ctx context.Context) int64 {
|
|
month := time.Now().UTC().Format("2006-01")
|
|
|
|
count, err := t.redis.SCard(ctx, fmt.Sprintf("usage:ai:users:%s", month)).Result()
|
|
if err != nil {
|
|
log.Printf("counting active users: %v", err)
|
|
return 0
|
|
}
|
|
|
|
return count
|
|
}
|
|
|
|
func atoi64(val string) int64 {
|
|
n, err := strconv.ParseInt(val, 10, 64)
|
|
if err != nil {
|
|
return 0
|
|
}
|
|
return n
|
|
}
|
|
|
|
type DeepLUsageReporter interface {
|
|
Usage(ctx context.Context) (*model.DeepLUsage, error)
|
|
}
|
|
|
|
type UsageService struct {
|
|
tracker *UsageTracker
|
|
deepl DeepLUsageReporter
|
|
tokenLimit int64
|
|
}
|
|
|
|
func NewUsageService(tracker *UsageTracker, deepl DeepLUsageReporter, tokenLimit int64) *UsageService {
|
|
return &UsageService{tracker: tracker, deepl: deepl, tokenLimit: tokenLimit}
|
|
}
|
|
|
|
func (s *UsageService) GetUsage(ctx context.Context, userID uuid.UUID) (*model.UsageResult, error) {
|
|
ai, err := s.tracker.AIUsage(ctx, userID)
|
|
if err != nil {
|
|
log.Printf("getting ai usage: %v", err)
|
|
ai = &model.AIUsage{}
|
|
}
|
|
|
|
users := s.tracker.ActiveUsers(ctx)
|
|
if users < 1 {
|
|
users = 1
|
|
}
|
|
ai.TokenLimit = s.tokenLimit / users
|
|
|
|
result := &model.UsageResult{AI: ai}
|
|
|
|
if deepL, err := s.deepl.Usage(ctx); err != nil {
|
|
log.Printf("getting deepl usage: %v", err)
|
|
} else {
|
|
deepL.CharacterLimit = deepL.CharacterLimit / users
|
|
result.DeepL = deepL
|
|
}
|
|
|
|
return result, nil
|
|
}
|