Summary • Migrated all ratio-related sources into `setting/ratio_setting/` – `model_ratio.go` (renamed from model-ratio.go) – `cache_ratio.go` – `group_ratio.go` • Changed package name to `ratio_setting` and relocated initialization (`ratio_setting.InitRatioSettings()` in main). • Updated every import & call site: – Model / cache / completion / image ratio helpers – Group ratio helpers (`GetGroupRatio*`, `ContainsGroupRatio`, `CheckGroupRatio`, etc.) – JSON-serialization & update helpers (`*Ratio2JSONString`, `Update*RatioByJSONString`) • Adjusted controllers, middleware, relay helpers, services and models to reference the new package. • Removed obsolete `setting` / `operation_setting` imports; added missing `ratio_setting` imports. • Adopted idiomatic map iteration (`for key := range m`) where value is unused. • Ran static checks to ensure clean build. This commit centralises all ratio configuration (model, cache and group) in one cohesive module, simplifying future maintenance and improving code clarity.
123 lines
2.5 KiB
Go
123 lines
2.5 KiB
Go
package ratio_setting
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"one-api/common"
|
|
"sync"
|
|
)
|
|
|
|
var groupRatio = map[string]float64{
|
|
"default": 1,
|
|
"vip": 1,
|
|
"svip": 1,
|
|
}
|
|
var groupRatioMutex sync.RWMutex
|
|
|
|
var (
|
|
GroupGroupRatio = map[string]map[string]float64{
|
|
"vip": {
|
|
"edit_this": 0.9,
|
|
},
|
|
}
|
|
groupGroupRatioMutex sync.RWMutex
|
|
)
|
|
|
|
func GetGroupRatioCopy() map[string]float64 {
|
|
groupRatioMutex.RLock()
|
|
defer groupRatioMutex.RUnlock()
|
|
|
|
groupRatioCopy := make(map[string]float64)
|
|
for k, v := range groupRatio {
|
|
groupRatioCopy[k] = v
|
|
}
|
|
return groupRatioCopy
|
|
}
|
|
|
|
func ContainsGroupRatio(name string) bool {
|
|
groupRatioMutex.RLock()
|
|
defer groupRatioMutex.RUnlock()
|
|
|
|
_, ok := groupRatio[name]
|
|
return ok
|
|
}
|
|
|
|
func GroupRatio2JSONString() string {
|
|
groupRatioMutex.RLock()
|
|
defer groupRatioMutex.RUnlock()
|
|
|
|
jsonBytes, err := json.Marshal(groupRatio)
|
|
if err != nil {
|
|
common.SysError("error marshalling model ratio: " + err.Error())
|
|
}
|
|
return string(jsonBytes)
|
|
}
|
|
|
|
func UpdateGroupRatioByJSONString(jsonStr string) error {
|
|
groupRatioMutex.Lock()
|
|
defer groupRatioMutex.Unlock()
|
|
|
|
groupRatio = make(map[string]float64)
|
|
return json.Unmarshal([]byte(jsonStr), &groupRatio)
|
|
}
|
|
|
|
func GetGroupRatio(name string) float64 {
|
|
groupRatioMutex.RLock()
|
|
defer groupRatioMutex.RUnlock()
|
|
|
|
ratio, ok := groupRatio[name]
|
|
if !ok {
|
|
common.SysError("group ratio not found: " + name)
|
|
return 1
|
|
}
|
|
return ratio
|
|
}
|
|
|
|
func GetGroupGroupRatio(group, name string) (float64, bool) {
|
|
groupGroupRatioMutex.RLock()
|
|
defer groupGroupRatioMutex.RUnlock()
|
|
|
|
gp, ok := GroupGroupRatio[group]
|
|
if !ok {
|
|
return -1, false
|
|
}
|
|
ratio, ok := gp[name]
|
|
if !ok {
|
|
return -1, false
|
|
}
|
|
return ratio, true
|
|
}
|
|
|
|
func GroupGroupRatio2JSONString() string {
|
|
groupGroupRatioMutex.RLock()
|
|
defer groupGroupRatioMutex.RUnlock()
|
|
|
|
jsonBytes, err := json.Marshal(GroupGroupRatio)
|
|
if err != nil {
|
|
common.SysError("error marshalling group-group ratio: " + err.Error())
|
|
}
|
|
return string(jsonBytes)
|
|
}
|
|
|
|
func UpdateGroupGroupRatioByJSONString(jsonStr string) error {
|
|
groupGroupRatioMutex.Lock()
|
|
defer groupGroupRatioMutex.Unlock()
|
|
|
|
GroupGroupRatio = make(map[string]map[string]float64)
|
|
return json.Unmarshal([]byte(jsonStr), &GroupGroupRatio)
|
|
}
|
|
|
|
func CheckGroupRatio(jsonStr string) error {
|
|
checkGroupRatio := make(map[string]float64)
|
|
err := json.Unmarshal([]byte(jsonStr), &checkGroupRatio)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for name, ratio := range checkGroupRatio {
|
|
if ratio < 0 {
|
|
return errors.New("group ratio must be not less than 0: " + name)
|
|
}
|
|
}
|
|
return nil
|
|
}
|