mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-11-16 13:13:41 +08:00
🔖 chore: Rename relay/util to relay/relay_util package and add utils package
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"math/rand"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -105,7 +106,7 @@ func (cc *ChannelsChooser) Next(group, modelName string, filters ...ChannelsFilt
|
||||
|
||||
channelsPriority, ok := cc.Rule[group][modelName]
|
||||
if !ok {
|
||||
matchModel := common.GetModelsWithMatch(&cc.Match, modelName)
|
||||
matchModel := utils.GetModelsWithMatch(&cc.Match, modelName)
|
||||
channelsPriority, ok = cc.Rule[group][matchModel]
|
||||
if !ok {
|
||||
return nil, errors.New("model not found")
|
||||
@@ -199,7 +200,7 @@ func (cc *ChannelsChooser) Load() {
|
||||
// 逗号分割 ability.ChannelId
|
||||
channelIds := strings.Split(ability.ChannelIds, ",")
|
||||
for _, channelId := range channelIds {
|
||||
priorityIds = append(priorityIds, common.String2Int(channelId))
|
||||
priorityIds = append(priorityIds, utils.String2Int(channelId))
|
||||
}
|
||||
|
||||
newGroup[ability.Group][ability.Model] = append(newGroup[ability.Group][ability.Model], priorityIds)
|
||||
|
||||
@@ -2,6 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
|
||||
"gorm.io/datatypes"
|
||||
@@ -235,7 +236,7 @@ func (channel *Channel) UpdateRaw(overwrite bool) error {
|
||||
|
||||
func (channel *Channel) UpdateResponseTime(responseTime int64) {
|
||||
err := DB.Model(channel).Select("response_time", "test_time").Updates(Channel{
|
||||
TestTime: common.GetTimestamp(),
|
||||
TestTime: utils.GetTimestamp(),
|
||||
ResponseTime: int(responseTime),
|
||||
}).Error
|
||||
if err != nil {
|
||||
@@ -245,7 +246,7 @@ func (channel *Channel) UpdateResponseTime(responseTime int64) {
|
||||
|
||||
func (channel *Channel) UpdateBalance(balance float64) {
|
||||
err := DB.Model(channel).Select("balance_updated_time", "balance").Updates(Channel{
|
||||
BalanceUpdatedTime: common.GetTimestamp(),
|
||||
BalanceUpdatedTime: utils.GetTimestamp(),
|
||||
Balance: balance,
|
||||
}).Error
|
||||
if err != nil {
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -41,7 +42,7 @@ func RecordLog(userId int, logType int, content string) {
|
||||
log := &Log{
|
||||
UserId: userId,
|
||||
Username: GetUsernameById(userId),
|
||||
CreatedAt: common.GetTimestamp(),
|
||||
CreatedAt: utils.GetTimestamp(),
|
||||
Type: logType,
|
||||
Content: content,
|
||||
}
|
||||
@@ -59,7 +60,7 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
|
||||
log := &Log{
|
||||
UserId: userId,
|
||||
Username: GetUsernameById(userId),
|
||||
CreatedAt: common.GetTimestamp(),
|
||||
CreatedAt: utils.GetTimestamp(),
|
||||
Type: LogTypeConsume,
|
||||
Content: content,
|
||||
PromptTokens: promptTokens,
|
||||
|
||||
@@ -3,6 +3,7 @@ package model
|
||||
import (
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -26,7 +27,7 @@ func SetupDB() {
|
||||
|
||||
if viper.GetBool("batch_update_enabled") {
|
||||
common.BatchUpdateEnabled = true
|
||||
common.BatchUpdateInterval = common.GetOrDefault("batch_update_interval", 5)
|
||||
common.BatchUpdateInterval = utils.GetOrDefault("batch_update_interval", 5)
|
||||
common.SysLog("batch update enabled with interval " + strconv.Itoa(common.BatchUpdateInterval) + "s")
|
||||
InitBatchUpdater()
|
||||
}
|
||||
@@ -47,7 +48,7 @@ func createRootAccountIfNeed() error {
|
||||
Role: common.RoleRootUser,
|
||||
Status: common.UserStatusEnabled,
|
||||
DisplayName: "Root User",
|
||||
AccessToken: common.GetUUID(),
|
||||
AccessToken: utils.GetUUID(),
|
||||
Quota: 100000000,
|
||||
}
|
||||
DB.Create(&rootUser)
|
||||
@@ -78,7 +79,7 @@ func chooseDB() (*gorm.DB, error) {
|
||||
// Use SQLite
|
||||
common.SysLog("SQL_DSN not set, using SQLite as database")
|
||||
common.UsingSQLite = true
|
||||
config := fmt.Sprintf("?_busy_timeout=%d", common.GetOrDefault("sqlite_busy_timeout", 3000))
|
||||
config := fmt.Sprintf("?_busy_timeout=%d", utils.GetOrDefault("sqlite_busy_timeout", 3000))
|
||||
return gorm.Open(sqlite.Open(viper.GetString("sqlite_path")+config), &gorm.Config{
|
||||
PrepareStmt: true, // precompile SQL
|
||||
})
|
||||
@@ -96,9 +97,9 @@ func InitDB() (err error) {
|
||||
return err
|
||||
}
|
||||
|
||||
sqlDB.SetMaxIdleConns(common.GetOrDefault("SQL_MAX_IDLE_CONNS", 100))
|
||||
sqlDB.SetMaxOpenConns(common.GetOrDefault("SQL_MAX_OPEN_CONNS", 1000))
|
||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetOrDefault("SQL_MAX_LIFETIME", 60)))
|
||||
sqlDB.SetMaxIdleConns(utils.GetOrDefault("SQL_MAX_IDLE_CONNS", 100))
|
||||
sqlDB.SetMaxOpenConns(utils.GetOrDefault("SQL_MAX_OPEN_CONNS", 1000))
|
||||
sqlDB.SetConnMaxLifetime(time.Second * time.Duration(utils.GetOrDefault("SQL_MAX_LIFETIME", 60)))
|
||||
|
||||
if !common.IsMasterNode {
|
||||
return nil
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -33,7 +34,7 @@ func GetRedemptionsList(params *GenericParams) (*DataResult[Redemption], error)
|
||||
var redemptions []*Redemption
|
||||
db := DB
|
||||
if params.Keyword != "" {
|
||||
db = db.Where("id = ? or name LIKE ?", common.String2Int(params.Keyword), params.Keyword+"%")
|
||||
db = db.Where("id = ? or name LIKE ?", utils.String2Int(params.Keyword), params.Keyword+"%")
|
||||
}
|
||||
|
||||
return PaginateAndOrder[Redemption](db, ¶ms.PaginationParams, &redemptions, allowedRedemptionslOrderFields)
|
||||
@@ -75,7 +76,7 @@ func Redeem(key string, userId int) (quota int, err error) {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
redemption.RedeemedTime = common.GetTimestamp()
|
||||
redemption.RedeemedTime = utils.GetTimestamp()
|
||||
redemption.Status = common.RedemptionCodeStatusUsed
|
||||
err = tx.Save(redemption).Error
|
||||
return err
|
||||
|
||||
@@ -2,7 +2,7 @@ package model
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
)
|
||||
|
||||
type TelegramMenu struct {
|
||||
@@ -22,7 +22,7 @@ func GetTelegramMenusList(params *GenericParams) (*DataResult[TelegramMenu], err
|
||||
var menus []*TelegramMenu
|
||||
db := DB
|
||||
if params.Keyword != "" {
|
||||
db = db.Where("id = ? or command LIKE ?", common.String2Int(params.Keyword), params.Keyword+"%")
|
||||
db = db.Where("id = ? or command LIKE ?", utils.String2Int(params.Keyword), params.Keyword+"%")
|
||||
}
|
||||
|
||||
return PaginateAndOrder[TelegramMenu](db, ¶ms.PaginationParams, &menus, allowedTelegramMenusOrderFields)
|
||||
|
||||
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/stmp"
|
||||
"one-api/common/utils"
|
||||
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
@@ -71,7 +72,7 @@ func ValidateUserToken(key string) (token *Token, err error) {
|
||||
if token.Status != common.TokenStatusEnabled {
|
||||
return nil, errors.New("该令牌状态不可用")
|
||||
}
|
||||
if token.ExpiredTime != -1 && token.ExpiredTime < common.GetTimestamp() {
|
||||
if token.ExpiredTime != -1 && token.ExpiredTime < utils.GetTimestamp() {
|
||||
if !common.RedisEnabled {
|
||||
token.Status = common.TokenStatusExpired
|
||||
err := token.SelectUpdate()
|
||||
@@ -188,7 +189,7 @@ func increaseTokenQuota(id int, quota int) (err error) {
|
||||
map[string]interface{}{
|
||||
"remain_quota": gorm.Expr("remain_quota + ?", quota),
|
||||
"used_quota": gorm.Expr("used_quota - ?", quota),
|
||||
"accessed_time": common.GetTimestamp(),
|
||||
"accessed_time": utils.GetTimestamp(),
|
||||
},
|
||||
).Error
|
||||
return err
|
||||
@@ -210,7 +211,7 @@ func decreaseTokenQuota(id int, quota int) (err error) {
|
||||
map[string]interface{}{
|
||||
"remain_quota": gorm.Expr("remain_quota - ?", quota),
|
||||
"used_quota": gorm.Expr("used_quota + ?", quota),
|
||||
"accessed_time": common.GetTimestamp(),
|
||||
"accessed_time": utils.GetTimestamp(),
|
||||
},
|
||||
).Error
|
||||
return err
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"one-api/common"
|
||||
"one-api/common/utils"
|
||||
"strings"
|
||||
|
||||
"gorm.io/gorm"
|
||||
@@ -55,7 +56,7 @@ func GetUsersList(params *GenericParams) (*DataResult[User], error) {
|
||||
var users []*User
|
||||
db := DB.Omit("password")
|
||||
if params.Keyword != "" {
|
||||
db = db.Where("id = ? or username LIKE ? or email LIKE ? or display_name LIKE ?", common.String2Int(params.Keyword), params.Keyword+"%", params.Keyword+"%", params.Keyword+"%")
|
||||
db = db.Where("id = ? or username LIKE ? or email LIKE ? or display_name LIKE ?", utils.String2Int(params.Keyword), params.Keyword+"%", params.Keyword+"%", params.Keyword+"%")
|
||||
}
|
||||
|
||||
return PaginateAndOrder[User](db, ¶ms.PaginationParams, &users, allowedUserOrderFields)
|
||||
@@ -115,9 +116,9 @@ func (user *User) Insert(inviterId int) error {
|
||||
}
|
||||
}
|
||||
user.Quota = common.QuotaForNewUser
|
||||
user.AccessToken = common.GetUUID()
|
||||
user.AffCode = common.GetRandomString(4)
|
||||
user.CreatedTime = common.GetTimestamp()
|
||||
user.AccessToken = utils.GetUUID()
|
||||
user.AffCode = utils.GetRandomString(4)
|
||||
user.CreatedTime = utils.GetTimestamp()
|
||||
result := DB.Create(user)
|
||||
if result.Error != nil {
|
||||
return result.Error
|
||||
@@ -165,7 +166,7 @@ func (user *User) Delete() error {
|
||||
}
|
||||
|
||||
// 不改变当前数据库索引,通过更改用户名来删除用户
|
||||
user.Username = user.Username + "_del_" + common.GetRandomString(6)
|
||||
user.Username = user.Username + "_del_" + utils.GetRandomString(6)
|
||||
err := user.Update(false)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user