chore: update implementation

This commit is contained in:
JustSong
2023-09-17 19:12:58 +08:00
parent 85a87af2e9
commit 1017c15758
10 changed files with 56 additions and 59 deletions

View File

@@ -10,7 +10,7 @@ type Ability struct {
Model string `json:"model" gorm:"primaryKey;autoIncrement:false"`
ChannelId int `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
Enabled bool `json:"enabled"`
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
Priority int64 `json:"priority" gorm:"bigint;default:0"`
}
func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {

View File

@@ -160,7 +160,7 @@ func InitChannelCache() {
for group, model2channels := range newGroup2model2channels {
for model, channels := range model2channels {
sort.Slice(channels, func(i, j int) bool {
return channels[i].GetPriority() > channels[j].GetPriority()
return channels[i].Priority > channels[j].Priority
})
newGroup2model2channels[group][model] = channels
}
@@ -192,7 +192,7 @@ func CacheGetRandomSatisfiedChannel(group string, model string) (*Channel, error
}
// choose by priority
firstChannel := channels[0]
if firstChannel.GetPriority() > 0 {
if firstChannel.Priority > 0 {
return firstChannel, nil
}
idx := rand.Intn(len(channels))

View File

@@ -23,14 +23,7 @@ type Channel struct {
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
ModelMapping string `json:"model_mapping" gorm:"type:varchar(1024);default:''"`
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
}
func (c *Channel) GetPriority() int64 {
if c.Priority == nil {
return 0
}
return *c.Priority
Priority int64 `json:"priority" gorm:"bigint;default:0"`
}
func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {

View File

@@ -45,7 +45,7 @@ func RecordLog(userId int, logType int, content string) {
}
}
func RecordConsumeLog(userId int, channelID, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string) {
func RecordConsumeLog(userId int, channelId int, promptTokens int, completionTokens int, modelName string, tokenName string, quota int, content string) {
if !common.LogConsumeEnabled {
return
}
@@ -60,7 +60,7 @@ func RecordConsumeLog(userId int, channelID, promptTokens int, completionTokens
TokenName: tokenName,
ModelName: modelName,
Quota: quota,
Channel: channelID,
Channel: channelId,
}
err := DB.Create(log).Error
if err != nil {
@@ -97,7 +97,7 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName
return logs, err
}
func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int, channel int) (logs []*Log, err error) {
func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int64, modelName string, tokenName string, startIdx int, num int) (logs []*Log, err error) {
var tx *gorm.DB
if logType == LogTypeUnknown {
tx = DB.Where("user_id = ?", userId)
@@ -116,9 +116,6 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
if endTimestamp != 0 {
tx = tx.Where("created_at <= ?", endTimestamp)
}
if channel != 0 {
tx = tx.Where("channel = ?", channel)
}
err = tx.Order("id desc").Limit(num).Offset(startIdx).Omit("id").Find(&logs).Error
return logs, err
}