| |
| package model |
|
|
| import ( |
| "crypto/sha256" |
| "encoding/hex" |
| "encoding/json" |
| "strings" |
| "time" |
|
|
| "ccLoad/internal/util" |
| ) |
|
|
| |
| |
| type AuthToken struct { |
| ID int64 `json:"id"` |
| Token string `json:"token"` |
| Description string `json:"description"` |
| CreatedAt time.Time `json:"created_at"` |
| ExpiresAt *int64 `json:"expires_at,omitempty"` |
| LastUsedAt *int64 `json:"last_used_at,omitempty"` |
| IsActive bool `json:"is_active"` |
|
|
| |
| SuccessCount int64 `json:"success_count"` |
| FailureCount int64 `json:"failure_count"` |
| StreamAvgTTFB float64 `json:"stream_avg_ttfb"` |
| NonStreamAvgRT float64 `json:"non_stream_avg_rt"` |
| StreamCount int64 `json:"stream_count"` |
| NonStreamCount int64 `json:"non_stream_count"` |
|
|
| |
| PromptTokensTotal int64 `json:"prompt_tokens_total"` |
| CompletionTokensTotal int64 `json:"completion_tokens_total"` |
| CacheReadTokensTotal int64 `json:"cache_read_tokens_total"` |
| CacheCreationTokensTotal int64 `json:"cache_creation_tokens_total"` |
| TotalCostUSD float64 `json:"total_cost_usd"` |
| EffectiveCostUSD float64 `json:"effective_cost_usd"` |
|
|
| |
| |
| |
| CostUsedMicroUSD int64 `json:"-"` |
| CostLimitMicroUSD int64 `json:"-"` |
|
|
| |
| PeakRPM float64 `json:"peak_rpm,omitempty"` |
| AvgRPM float64 `json:"avg_rpm,omitempty"` |
| RecentRPM float64 `json:"recent_rpm,omitempty"` |
|
|
| |
| AllowedModels []string `json:"allowed_models,omitempty"` |
|
|
| |
| AllowedChannelIDs []int64 `json:"allowed_channel_ids,omitempty"` |
|
|
| |
| MaxConcurrency int `json:"max_concurrency"` |
| } |
|
|
| |
| type AuthTokenRangeStats struct { |
| SuccessCount int64 `json:"success_count"` |
| FailureCount int64 `json:"failure_count"` |
| PromptTokens int64 `json:"prompt_tokens"` |
| CompletionTokens int64 `json:"completion_tokens"` |
| CacheReadTokens int64 `json:"cache_read_tokens"` |
| CacheCreationTokens int64 `json:"cache_creation_tokens"` |
| TotalCost float64 `json:"total_cost"` |
| EffectiveCost float64 `json:"effective_cost"` |
| StreamAvgTTFB float64 `json:"stream_avg_ttfb"` |
| NonStreamAvgRT float64 `json:"non_stream_avg_rt"` |
| StreamCount int64 `json:"stream_count"` |
| NonStreamCount int64 `json:"non_stream_count"` |
| |
| PeakRPM float64 `json:"peak_rpm"` |
| AvgRPM float64 `json:"avg_rpm"` |
| RecentRPM float64 `json:"recent_rpm"` |
| } |
|
|
| |
| |
| func HashToken(token string) string { |
| hash := sha256.Sum256([]byte(token)) |
| return hex.EncodeToString(hash[:]) |
| } |
|
|
| |
| func (t *AuthToken) IsExpired() bool { |
| if t.ExpiresAt == nil || *t.ExpiresAt == 0 { |
| return false |
| } |
| return time.Now().UnixMilli() > *t.ExpiresAt |
| } |
|
|
| |
| func (t *AuthToken) IsValid() bool { |
| return t.IsActive && !t.IsExpired() |
| } |
|
|
| |
| |
| func MaskToken(token string) string { |
| if len(token) <= 8 { |
| return "****" |
| } |
| return token[:4] + "****" + token[len(token)-4:] |
| } |
|
|
| |
| func (t *AuthToken) UpdateLastUsed() { |
| now := time.Now().UnixMilli() |
| if t.LastUsedAt != nil && now <= *t.LastUsedAt { |
| now = *t.LastUsedAt + 1 |
| } |
| t.LastUsedAt = &now |
| } |
|
|
| |
| |
| func (t *AuthToken) IsModelAllowed(model string) bool { |
| if len(t.AllowedModels) == 0 { |
| return true |
| } |
| for _, m := range t.AllowedModels { |
| if strings.EqualFold(m, model) { |
| return true |
| } |
| } |
| return false |
| } |
|
|
| |
| |
| func (t *AuthToken) IsChannelAllowed(channelID int64) bool { |
| if len(t.AllowedChannelIDs) == 0 { |
| return true |
| } |
| for _, id := range t.AllowedChannelIDs { |
| if id == channelID { |
| return true |
| } |
| } |
| return false |
| } |
|
|
| |
| func (t *AuthToken) CostUsedUSD() float64 { |
| return util.MicroUSDToUSD(t.CostUsedMicroUSD) |
| } |
|
|
| |
| func (t *AuthToken) CostLimitUSD() float64 { |
| return util.MicroUSDToUSD(t.CostLimitMicroUSD) |
| } |
|
|
| |
| func (t *AuthToken) SetCostLimitUSD(usd float64) { |
| if usd <= 0 { |
| t.CostLimitMicroUSD = 0 |
| return |
| } |
| t.CostLimitMicroUSD = util.USDToMicroUSD(usd) |
| } |
|
|
| |
| type authTokenJSON struct { |
| ID int64 `json:"id"` |
| Token string `json:"token"` |
| Description string `json:"description"` |
| CreatedAt time.Time `json:"created_at"` |
| ExpiresAt *int64 `json:"expires_at,omitempty"` |
| LastUsedAt *int64 `json:"last_used_at,omitempty"` |
| IsActive bool `json:"is_active"` |
| SuccessCount int64 `json:"success_count"` |
| FailureCount int64 `json:"failure_count"` |
| StreamAvgTTFB float64 `json:"stream_avg_ttfb"` |
| NonStreamAvgRT float64 `json:"non_stream_avg_rt"` |
| StreamCount int64 `json:"stream_count"` |
| NonStreamCount int64 `json:"non_stream_count"` |
| PromptTokensTotal int64 `json:"prompt_tokens_total"` |
| CompletionTokensTotal int64 `json:"completion_tokens_total"` |
| CacheReadTokensTotal int64 `json:"cache_read_tokens_total"` |
| CacheCreationTokensTotal int64 `json:"cache_creation_tokens_total"` |
| TotalCostUSD float64 `json:"total_cost_usd"` |
| EffectiveCostUSD float64 `json:"effective_cost_usd"` |
| CostUsedUSD float64 `json:"cost_used_usd"` |
| CostLimitUSD float64 `json:"cost_limit_usd"` |
| PeakRPM float64 `json:"peak_rpm,omitempty"` |
| AvgRPM float64 `json:"avg_rpm,omitempty"` |
| RecentRPM float64 `json:"recent_rpm,omitempty"` |
| AllowedModels []string `json:"allowed_models,omitempty"` |
| AllowedChannelIDs []int64 `json:"allowed_channel_ids,omitempty"` |
| MaxConcurrency int `json:"max_concurrency"` |
| } |
|
|
| |
| func (t AuthToken) MarshalJSON() ([]byte, error) { |
| return json.Marshal(authTokenJSON{ |
| ID: t.ID, |
| Token: t.Token, |
| Description: t.Description, |
| CreatedAt: t.CreatedAt, |
| ExpiresAt: t.ExpiresAt, |
| LastUsedAt: t.LastUsedAt, |
| IsActive: t.IsActive, |
| SuccessCount: t.SuccessCount, |
| FailureCount: t.FailureCount, |
| StreamAvgTTFB: t.StreamAvgTTFB, |
| NonStreamAvgRT: t.NonStreamAvgRT, |
| StreamCount: t.StreamCount, |
| NonStreamCount: t.NonStreamCount, |
| PromptTokensTotal: t.PromptTokensTotal, |
| CompletionTokensTotal: t.CompletionTokensTotal, |
| CacheReadTokensTotal: t.CacheReadTokensTotal, |
| CacheCreationTokensTotal: t.CacheCreationTokensTotal, |
| TotalCostUSD: t.TotalCostUSD, |
| EffectiveCostUSD: t.EffectiveCostUSD, |
| CostUsedUSD: t.CostUsedUSD(), |
| CostLimitUSD: t.CostLimitUSD(), |
| PeakRPM: t.PeakRPM, |
| AvgRPM: t.AvgRPM, |
| RecentRPM: t.RecentRPM, |
| AllowedModels: t.AllowedModels, |
| AllowedChannelIDs: t.AllowedChannelIDs, |
| MaxConcurrency: t.MaxConcurrency, |
| }) |
| } |
|
|