refactor: update import paths to use new types package
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
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 {
|
||||
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)
|
||||
}
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
package types
|
||||
|
||||
import (
|
||||
"sync"
|
||||
|
||||
"github.com/QuantumNous/new-api/common"
|
||||
)
|
||||
|
||||
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 common.Unmarshal(b, &m.data)
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) MarshalJSON() ([]byte, error) {
|
||||
m.mutex.RLock()
|
||||
defer m.mutex.RUnlock()
|
||||
return common.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)
|
||||
}
|
||||
|
||||
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 common.Unmarshal([]byte(jsonStr), &m.data)
|
||||
}
|
||||
|
||||
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 := common.Unmarshal([]byte(jsonStr), &m.data)
|
||||
if err == nil && onSuccess != nil {
|
||||
onSuccess()
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *RWMap[K, V]) MarshalJSONString() string {
|
||||
bytes, err := m.MarshalJSON()
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(bytes)
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package types
|
||||
|
||||
type Set[T comparable] struct {
|
||||
items map[T]struct{}
|
||||
}
|
||||
|
||||
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{}{}
|
||||
}
|
||||
|
||||
func (s *Set[T]) Remove(item T) {
|
||||
delete(s.items, item)
|
||||
}
|
||||
|
||||
func (s *Set[T]) Contains(item T) bool {
|
||||
_, exists := s.items[item]
|
||||
return exists
|
||||
}
|
||||
|
||||
func (s *Set[T]) Len() int {
|
||||
return len(s.items)
|
||||
}
|
||||
|
||||
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