Files
new-api/model/pricing.go

74 lines
1.8 KiB
Go
Raw Normal View History

2024-05-13 23:02:35 +08:00
package model
import (
"one-api/common"
"sync"
"time"
)
2024-06-27 19:30:17 +08:00
type Pricing struct {
Available bool `json:"available"`
ModelName string `json:"model_name"`
QuotaType int `json:"quota_type"`
ModelRatio float64 `json:"model_ratio"`
ModelPrice float64 `json:"model_price"`
OwnerBy string `json:"owner_by"`
CompletionRatio float64 `json:"completion_ratio"`
EnableGroup []string `json:"enable_group,omitempty"`
}
2024-05-13 23:02:35 +08:00
var (
2024-06-27 19:30:17 +08:00
pricingMap []Pricing
2024-05-13 23:02:35 +08:00
lastGetPricingTime time.Time
updatePricingLock sync.Mutex
)
2024-06-27 19:30:17 +08:00
func GetPricing(group string) []Pricing {
2024-05-13 23:02:35 +08:00
updatePricingLock.Lock()
defer updatePricingLock.Unlock()
if time.Since(lastGetPricingTime) > time.Minute*1 || len(pricingMap) == 0 {
2024-05-15 23:56:26 +08:00
updatePricing()
2024-05-13 23:02:35 +08:00
}
2024-05-15 23:56:26 +08:00
if group != "" {
2024-06-27 19:30:17 +08:00
userPricingMap := make([]Pricing, 0)
2024-05-15 23:56:26 +08:00
models := GetGroupModels(group)
2024-05-13 23:02:35 +08:00
for _, pricing := range pricingMap {
if !common.StringsContains(models, pricing.ModelName) {
pricing.Available = false
}
userPricingMap = append(userPricingMap, pricing)
}
return userPricingMap
}
return pricingMap
}
2024-05-15 23:56:26 +08:00
func updatePricing() {
2024-05-15 20:17:27 +08:00
//modelRatios := common.GetModelRatios()
2024-05-13 23:02:35 +08:00
enabledModels := GetEnabledModels()
2024-05-15 20:17:27 +08:00
allModels := make(map[string]int)
for i, model := range enabledModels {
allModels[model] = i
2024-05-13 23:02:35 +08:00
}
2024-05-15 20:17:27 +08:00
2024-06-27 19:30:17 +08:00
pricingMap = make([]Pricing, 0)
2024-05-15 20:17:27 +08:00
for model, _ := range allModels {
2024-06-27 19:30:17 +08:00
pricing := Pricing{
2024-05-13 23:02:35 +08:00
Available: true,
ModelName: model,
}
modelPrice, findPrice := common.GetModelPrice(model, false)
if findPrice {
pricing.ModelPrice = modelPrice
pricing.QuotaType = 1
} else {
pricing.ModelRatio = common.GetModelRatio(model)
pricing.CompletionRatio = common.GetCompletionRatio(model)
pricing.QuotaType = 0
}
pricingMap = append(pricingMap, pricing)
}
lastGetPricingTime = time.Now()
}