mirror of
https://github.com/woodchen-ink/aimodels-prices.git
synced 2025-07-18 05:32:00 +08:00
优化价格倍率计算逻辑,使用map存储模型及其对应的最高倍率,确保不区分大小写的比较,提升性能和准确性。
This commit is contained in:
parent
fe84b2c9c9
commit
50866158c6
@ -2,6 +2,7 @@ package rates
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
@ -40,8 +41,8 @@ func GetPriceRates(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 预分配rates切片,减少内存分配
|
// 创建map用于存储模型及其对应的最高倍率
|
||||||
rates := make([]PriceRate, 0, len(prices))
|
modelRateMap := make(map[string]PriceRate)
|
||||||
|
|
||||||
// 计算倍率
|
// 计算倍率
|
||||||
for _, price := range prices {
|
for _, price := range prices {
|
||||||
@ -58,13 +59,39 @@ func GetPriceRates(c *gin.Context) {
|
|||||||
outputRate = price.OutputPrice / 14
|
outputRate = price.OutputPrice / 14
|
||||||
}
|
}
|
||||||
|
|
||||||
rates = append(rates, PriceRate{
|
// 创建当前价格的PriceRate
|
||||||
|
currentRate := PriceRate{
|
||||||
Model: price.Model,
|
Model: price.Model,
|
||||||
Type: price.BillingType,
|
Type: price.BillingType,
|
||||||
ChannelType: price.ChannelType,
|
ChannelType: price.ChannelType,
|
||||||
Input: inputRate,
|
Input: inputRate,
|
||||||
Output: outputRate,
|
Output: outputRate,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
// 转换为小写以实现不区分大小写比较
|
||||||
|
modelLower := strings.ToLower(price.Model)
|
||||||
|
|
||||||
|
// 检查是否已存在相同模型名称(不区分大小写)
|
||||||
|
if existingRate, exists := modelRateMap[modelLower]; exists {
|
||||||
|
// 比较倍率,保留较高的那个
|
||||||
|
// 这里我们以输入和输出倍率的总和作为比较标准
|
||||||
|
existingTotal := existingRate.Input + existingRate.Output
|
||||||
|
currentTotal := inputRate + outputRate
|
||||||
|
|
||||||
|
if currentTotal > existingTotal {
|
||||||
|
// 当前倍率更高,替换已存在的
|
||||||
|
modelRateMap[modelLower] = currentRate
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// 不存在相同模型名称,直接添加
|
||||||
|
modelRateMap[modelLower] = currentRate
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 从map中提取结果到slice
|
||||||
|
rates := make([]PriceRate, 0, len(modelRateMap))
|
||||||
|
for _, rate := range modelRateMap {
|
||||||
|
rates = append(rates, rate)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 存入缓存,有效期24小时
|
// 存入缓存,有效期24小时
|
||||||
|
Loading…
x
Reference in New Issue
Block a user