mirror of
https://github.com/songquanpeng/one-api.git
synced 2025-09-30 15:16:39 +08:00
230 lines
6.4 KiB
Go
230 lines
6.4 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"one-api/common"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Channel struct {
|
|
Id int `json:"id"`
|
|
Type int `json:"type" gorm:"default:0"`
|
|
Key string `json:"key" gorm:"not null;index"`
|
|
Status int `json:"status" gorm:"default:1"`
|
|
Name string `json:"name" gorm:"index"`
|
|
Weight *uint `json:"weight" gorm:"default:0"`
|
|
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
|
TestTime int64 `json:"test_time" gorm:"bigint"`
|
|
ResponseTime int `json:"response_time"` // in milliseconds
|
|
BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"`
|
|
Other string `json:"other"`
|
|
Balance float64 `json:"balance"` // in USD
|
|
BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
|
|
Models string `json:"models"`
|
|
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:''"`
|
|
WeightMapping *string `json:"weight_mapping" gorm:"type:varchar(1024);default:''"`
|
|
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
|
|
}
|
|
|
|
func GetAllChannels(startIdx int, num int, selectAll bool) ([]*Channel, error) {
|
|
var channels []*Channel
|
|
var err error
|
|
if selectAll {
|
|
err = DB.Order("id desc").Find(&channels).Error
|
|
} else {
|
|
err = DB.Order("id desc").Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
|
|
}
|
|
return channels, err
|
|
}
|
|
|
|
func SearchChannels(keyword string) (channels []*Channel, err error) {
|
|
keyCol := "`key`"
|
|
if common.UsingPostgreSQL {
|
|
keyCol = `"key"`
|
|
}
|
|
err = DB.Omit("key").Where("id = ? or name LIKE ? or "+keyCol+" = ?", common.String2Int(keyword), keyword+"%", keyword).Find(&channels).Error
|
|
return channels, err
|
|
}
|
|
|
|
func GetChannelById(id int, selectAll bool) (*Channel, error) {
|
|
channel := Channel{Id: id}
|
|
var err error = nil
|
|
if selectAll {
|
|
err = DB.First(&channel, "id = ?", id).Error
|
|
} else {
|
|
err = DB.Omit("key").First(&channel, "id = ?", id).Error
|
|
}
|
|
return &channel, err
|
|
}
|
|
|
|
func BatchInsertChannels(channels []Channel) error {
|
|
var err error
|
|
err = DB.Create(&channels).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for _, channel_ := range channels {
|
|
err = channel_.AddAbilities()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (channel *Channel) GetWeightMapping() (weightMapping map[string]int) {
|
|
if channel.WeightMapping == nil || *channel.WeightMapping == "" {
|
|
return
|
|
}
|
|
err := json.Unmarshal([]byte(*channel.WeightMapping), &weightMapping)
|
|
if err != nil {
|
|
common.SysError("failed to unmarshal weight mapping: " + err.Error())
|
|
}
|
|
return
|
|
}
|
|
|
|
func (channel *Channel) FixWeightMapping() {
|
|
var weightMapping map[string]int
|
|
if channel.WeightMapping == nil || *channel.WeightMapping == "" {
|
|
weightMapping = make(map[string]int)
|
|
} else {
|
|
err := json.Unmarshal([]byte(*channel.WeightMapping), &weightMapping)
|
|
if err != nil {
|
|
common.SysError("failed to marshal weight mapping: " + err.Error())
|
|
}
|
|
}
|
|
|
|
models := channel.GetModels()
|
|
for _, model := range models {
|
|
if _, ok := weightMapping[model]; !ok {
|
|
weightMapping[model] = common.DefaultWeight
|
|
}
|
|
}
|
|
|
|
jsonStr, err := json.Marshal(weightMapping)
|
|
if err != nil {
|
|
common.SysError("failed to marshal weight mapping: " + err.Error())
|
|
}
|
|
var result = string(jsonStr)
|
|
channel.WeightMapping = &result
|
|
}
|
|
|
|
func (channel *Channel) GetModels() []string {
|
|
return common.SplitDistinct(channel.Models, ",")
|
|
}
|
|
|
|
func (channel *Channel) GetGroups() []string {
|
|
return common.SplitDistinct(channel.Group, ",")
|
|
}
|
|
|
|
func (channel *Channel) GetPriority() int64 {
|
|
if channel.Priority == nil {
|
|
return 0
|
|
}
|
|
return *channel.Priority
|
|
}
|
|
|
|
func (channel *Channel) GetBaseURL() string {
|
|
if channel.BaseURL == nil {
|
|
return ""
|
|
}
|
|
return *channel.BaseURL
|
|
}
|
|
|
|
func (channel *Channel) GetModelMapping() string {
|
|
if channel.ModelMapping == nil {
|
|
return ""
|
|
}
|
|
return *channel.ModelMapping
|
|
}
|
|
|
|
func (channel *Channel) Insert() error {
|
|
var err error
|
|
err = DB.Create(channel).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = channel.AddAbilities()
|
|
return err
|
|
}
|
|
|
|
func (channel *Channel) Update() error {
|
|
var err error
|
|
err = DB.Model(channel).Updates(channel).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
DB.Model(channel).First(channel, "id = ?", channel.Id)
|
|
err = channel.UpdateAbilities()
|
|
return err
|
|
}
|
|
|
|
func (channel *Channel) UpdateResponseTime(responseTime int64) {
|
|
err := DB.Model(channel).Select("response_time", "test_time").Updates(Channel{
|
|
TestTime: common.GetTimestamp(),
|
|
ResponseTime: int(responseTime),
|
|
}).Error
|
|
if err != nil {
|
|
common.SysError("failed to update response time: " + err.Error())
|
|
}
|
|
}
|
|
|
|
func (channel *Channel) UpdateBalance(balance float64) {
|
|
err := DB.Model(channel).Select("balance_updated_time", "balance").Updates(Channel{
|
|
BalanceUpdatedTime: common.GetTimestamp(),
|
|
Balance: balance,
|
|
}).Error
|
|
if err != nil {
|
|
common.SysError("failed to update balance: " + err.Error())
|
|
}
|
|
}
|
|
|
|
func (channel *Channel) Delete() error {
|
|
var err error
|
|
err = DB.Delete(channel).Error
|
|
if err != nil {
|
|
return err
|
|
}
|
|
err = channel.DeleteAbilities()
|
|
return err
|
|
}
|
|
|
|
func UpdateChannelStatusById(id int, status int) {
|
|
err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
|
|
if err != nil {
|
|
common.SysError("failed to update ability status: " + err.Error())
|
|
}
|
|
err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
|
|
if err != nil {
|
|
common.SysError("failed to update channel status: " + err.Error())
|
|
}
|
|
}
|
|
|
|
func UpdateChannelUsedQuota(id int, quota int) {
|
|
if common.BatchUpdateEnabled {
|
|
addNewRecord(BatchUpdateTypeChannelUsedQuota, id, quota)
|
|
return
|
|
}
|
|
updateChannelUsedQuota(id, quota)
|
|
}
|
|
|
|
func updateChannelUsedQuota(id int, quota int) {
|
|
err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
|
|
if err != nil {
|
|
common.SysError("failed to update channel used quota: " + err.Error())
|
|
}
|
|
}
|
|
|
|
func DeleteChannelByStatus(status int64) (int64, error) {
|
|
result := DB.Where("status = ?", status).Delete(&Channel{})
|
|
return result.RowsAffected, result.Error
|
|
}
|
|
|
|
func DeleteDisabledChannel() (int64, error) {
|
|
result := DB.Where("status = ? or status = ?", common.ChannelStatusAutoDisabled, common.ChannelStatusManuallyDisabled).Delete(&Channel{})
|
|
return result.RowsAffected, result.Error
|
|
}
|