refactor: update import paths to use new types package
This commit is contained in:
@@ -5,7 +5,6 @@ go 1.25.1
|
||||
require (
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/samber/lo v1.53.0
|
||||
github.com/shopspring/decimal v1.4.0
|
||||
github.com/stretchr/testify v1.11.1
|
||||
github.com/tidwall/gjson v1.19.0
|
||||
github.com/tidwall/sjson v1.2.5
|
||||
|
||||
@@ -18,8 +18,6 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
|
||||
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
|
||||
github.com/samber/lo v1.53.0 h1:t975lj2py4kJPQ6haz1QMgtId2gtmfktACxIXArw3HM=
|
||||
github.com/samber/lo v1.53.0/go.mod h1:4+MXEGsJzbKGaUEQFKBq2xtfuznW9oz/WrgyzMzRoM0=
|
||||
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
|
||||
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
|
||||
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/tidwall/gjson v1.14.2/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
|
||||
"github.com/shopspring/decimal"
|
||||
)
|
||||
|
||||
type GroupRatioInfo struct {
|
||||
GroupRatio float64
|
||||
GroupSpecialRatio float64
|
||||
HasSpecialRatio bool
|
||||
}
|
||||
|
||||
type PriceData struct {
|
||||
FreeModel bool
|
||||
ModelPrice float64
|
||||
ModelRatio float64
|
||||
CompletionRatio float64
|
||||
CacheRatio float64
|
||||
CacheCreationRatio float64
|
||||
CacheCreation5mRatio float64
|
||||
CacheCreation1hRatio float64
|
||||
ImageRatio float64
|
||||
AudioRatio float64
|
||||
AudioCompletionRatio float64
|
||||
otherRatios map[string]float64
|
||||
UsePrice bool
|
||||
Quota int // 按次计费的最终额度(MJ / Task)
|
||||
QuotaToPreConsume int // 按量计费的预消耗额度
|
||||
GroupRatioInfo GroupRatioInfo
|
||||
}
|
||||
|
||||
func (p *PriceData) AddOtherRatio(key string, ratio float64) {
|
||||
if !isValidOtherRatio(ratio) {
|
||||
return
|
||||
}
|
||||
if p.otherRatios == nil {
|
||||
p.otherRatios = make(map[string]float64)
|
||||
}
|
||||
p.otherRatios[key] = ratio
|
||||
}
|
||||
|
||||
func (p *PriceData) ReplaceOtherRatios(ratios map[string]float64) bool {
|
||||
p.otherRatios = nil
|
||||
for key, ratio := range ratios {
|
||||
p.AddOtherRatio(key, ratio)
|
||||
}
|
||||
return len(p.otherRatios) > 0
|
||||
}
|
||||
|
||||
func (p *PriceData) HasOtherRatio(key string) bool {
|
||||
ratio, ok := p.otherRatios[key]
|
||||
return ok && isValidOtherRatio(ratio)
|
||||
}
|
||||
|
||||
func (p *PriceData) OtherRatios() map[string]float64 {
|
||||
if len(p.otherRatios) == 0 {
|
||||
return nil
|
||||
}
|
||||
ratios := make(map[string]float64, len(p.otherRatios))
|
||||
for key, ratio := range p.otherRatios {
|
||||
if isValidOtherRatio(ratio) {
|
||||
ratios[key] = ratio
|
||||
}
|
||||
}
|
||||
if len(ratios) == 0 {
|
||||
return nil
|
||||
}
|
||||
return ratios
|
||||
}
|
||||
|
||||
func (p *PriceData) OtherRatioMultiplier() float64 {
|
||||
multiplier := 1.0
|
||||
for _, ratio := range p.otherRatios {
|
||||
if isValidOtherRatio(ratio) && ratio != 1.0 {
|
||||
multiplier *= ratio
|
||||
}
|
||||
}
|
||||
return multiplier
|
||||
}
|
||||
|
||||
func (p *PriceData) ApplyOtherRatiosToFloat(value float64) float64 {
|
||||
return value * p.OtherRatioMultiplier()
|
||||
}
|
||||
|
||||
func (p *PriceData) ApplyOtherRatiosToDecimal(value decimal.Decimal) decimal.Decimal {
|
||||
for _, ratio := range p.otherRatios {
|
||||
if isValidOtherRatio(ratio) && ratio != 1.0 {
|
||||
value = value.Mul(decimal.NewFromFloat(ratio))
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func (p *PriceData) RemoveOtherRatiosFromFloat(value float64) float64 {
|
||||
for _, ratio := range p.otherRatios {
|
||||
if isValidOtherRatio(ratio) && ratio != 1.0 {
|
||||
value /= ratio
|
||||
}
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func isValidOtherRatio(ratio float64) bool {
|
||||
// NaN/Inf would poison every downstream quota multiplication
|
||||
// (int(NaN * quota) wraps to a negative charge).
|
||||
return ratio > 0 && !math.IsInf(ratio, 1)
|
||||
}
|
||||
|
||||
func (p *PriceData) ToSetting() string {
|
||||
return fmt.Sprintf("ModelPrice: %f, ModelRatio: %f, CompletionRatio: %f, CacheRatio: %f, GroupRatio: %f, UsePrice: %t, CacheCreationRatio: %f, CacheCreation5mRatio: %f, CacheCreation1hRatio: %f, QuotaToPreConsume: %d, ImageRatio: %f, AudioRatio: %f, AudioCompletionRatio: %f", p.ModelPrice, p.ModelRatio, p.CompletionRatio, p.CacheRatio, p.GroupRatioInfo.GroupRatio, p.UsePrice, p.CacheCreationRatio, p.CacheCreation5mRatio, p.CacheCreation1hRatio, p.QuotaToPreConsume, p.ImageRatio, p.AudioRatio, p.AudioCompletionRatio)
|
||||
}
|
||||
@@ -1,103 +0,0 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
kitutil "github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil"
|
||||
)
|
||||
|
||||
type RWMap[K comparable, V any] struct {
|
||||
data map[K]V
|
||||
mutex sync.RWMutex
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) UnmarshalJSON(b []byte) error {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.data = make(map[K]V)
|
||||
return kitutil.Unmarshal(b, &m.data)
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) MarshalJSON() ([]byte, error) {
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
return kitutil.Marshal(m.data)
|
||||
}
|
||||
|
||||
func NewRWMap[K comparable, V any]() *RWMap[K, V] {
|
||||
return &RWMap[K, V]{
|
||||
data: make(map[K]V),
|
||||
}
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) Get(key K) (V, bool) {
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
value, exists := m.data[key]
|
||||
return value, exists
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) Set(key K, value V) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.data[key] = value
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) AddAll(other map[K]V) {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
for k, v := range other {
|
||||
m.data[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) Clear() {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.data = make(map[K]V)
|
||||
}
|
||||
|
||||
// ReadAll returns a copy of the entire map.
|
||||
func (m *RWMap[K, V]) ReadAll() map[K]V {
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
copiedMap := make(map[K]V)
|
||||
for k, v := range m.data {
|
||||
copiedMap[k] = v
|
||||
}
|
||||
return copiedMap
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) Len() int {
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
return len(m.data)
|
||||
}
|
||||
|
||||
func LoadFromJsonString[K comparable, V any](m *RWMap[K, V], jsonStr string) error {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.data = make(map[K]V)
|
||||
return kitutil.Unmarshal([]byte(jsonStr), &m.data)
|
||||
}
|
||||
|
||||
// LoadFromJsonStringWithCallback loads a JSON string into the RWMap and calls the callback on success.
|
||||
func LoadFromJsonStringWithCallback[K comparable, V any](m *RWMap[K, V], jsonStr string, onSuccess func()) error {
|
||||
m.mutex.Lock()
|
||||
defer m.mutex.Unlock()
|
||||
m.data = make(map[K]V)
|
||||
err := kitutil.Unmarshal([]byte(jsonStr), &m.data)
|
||||
if err == nil && onSuccess != nil {
|
||||
onSuccess()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
// MarshalJSONString returns the JSON string representation of the RWMap.
|
||||
func (m *RWMap[K, V]) MarshalJSONString() string {
|
||||
bytes, err := m.MarshalJSON()
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
package types
|
||||
|
||||
type Set[T comparable] struct {
|
||||
items map[T]struct{}
|
||||
}
|
||||
|
||||
// NewSet 创建并返回一个新的 Set
|
||||
func NewSet[T comparable]() *Set[T] {
|
||||
return &Set[T]{
|
||||
items: make(map[T]struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Set[T]) Add(item T) {
|
||||
s.items[item] = struct{}{}
|
||||
}
|
||||
|
||||
// Remove 从 Set 中移除一个元素
|
||||
func (s *Set[T]) Remove(item T) {
|
||||
delete(s.items, item)
|
||||
}
|
||||
|
||||
// Contains 检查 Set 是否包含某个元素
|
||||
func (s *Set[T]) Contains(item T) bool {
|
||||
_, exists := s.items[item]
|
||||
return exists
|
||||
}
|
||||
|
||||
// Len 返回 Set 中元素的数量
|
||||
func (s *Set[T]) Len() int {
|
||||
return len(s.items)
|
||||
}
|
||||
|
||||
// Items 返回 Set 中所有元素组成的切片
|
||||
// 注意:由于 map 的无序性,返回的切片元素顺序是随机的
|
||||
func (s *Set[T]) Items() []T {
|
||||
items := make([]T, 0, s.Len())
|
||||
for item := range s.items {
|
||||
items = append(items, item)
|
||||
}
|
||||
return items
|
||||
}
|
||||
Reference in New Issue
Block a user