2023-04-22 12:39:27 +00:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
2024-01-21 15:21:42 +00:00
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2024-01-28 11:38:58 +00:00
|
|
|
"github.com/songquanpeng/one-api/common/config"
|
|
|
|
"github.com/songquanpeng/one-api/common/helper"
|
|
|
|
"github.com/songquanpeng/one-api/common/logger"
|
2023-06-16 08:02:00 +00:00
|
|
|
"gorm.io/gorm"
|
2023-04-22 12:39:27 +00:00
|
|
|
)
|
|
|
|
|
2024-04-05 18:03:59 +00:00
|
|
|
const (
|
|
|
|
ChannelStatusUnknown = 0
|
|
|
|
ChannelStatusEnabled = 1 // don't use 0, 0 is the default value!
|
|
|
|
ChannelStatusManuallyDisabled = 2 // also don't use 0
|
|
|
|
ChannelStatusAutoDisabled = 3
|
|
|
|
)
|
|
|
|
|
2023-04-22 13:41:16 +00:00
|
|
|
type Channel struct {
|
2023-05-21 08:09:54 +00:00
|
|
|
Id int `json:"id"`
|
|
|
|
Type int `json:"type" gorm:"default:0"`
|
2024-03-10 15:27:22 +00:00
|
|
|
Key string `json:"key" gorm:"type:text"`
|
2023-05-21 08:09:54 +00:00
|
|
|
Status int `json:"status" gorm:"default:1"`
|
|
|
|
Name string `json:"name" gorm:"index"`
|
2023-09-29 10:13:57 +00:00
|
|
|
Weight *uint `json:"weight" gorm:"default:0"`
|
2023-05-21 08:09:54 +00:00
|
|
|
CreatedTime int64 `json:"created_time" gorm:"bigint"`
|
|
|
|
TestTime int64 `json:"test_time" gorm:"bigint"`
|
|
|
|
ResponseTime int `json:"response_time"` // in milliseconds
|
2023-09-18 14:49:05 +00:00
|
|
|
BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"`
|
2024-06-12 16:07:26 +00:00
|
|
|
Other *string `json:"other"` // DEPRECATED: please save config to field Config
|
2023-05-21 08:09:54 +00:00
|
|
|
Balance float64 `json:"balance"` // in USD
|
|
|
|
BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
|
2023-06-07 15:26:00 +00:00
|
|
|
Models string `json:"models"`
|
2024-04-03 18:58:21 +00:00
|
|
|
Group string `json:"group" gorm:"type:varchar(32);default:'default'"`
|
2023-06-16 08:02:00 +00:00
|
|
|
UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
|
2023-09-18 14:07:17 +00:00
|
|
|
ModelMapping *string `json:"model_mapping" gorm:"type:varchar(1024);default:''"`
|
2023-09-18 13:43:45 +00:00
|
|
|
Priority *int64 `json:"priority" gorm:"bigint;default:0"`
|
2024-02-17 18:22:50 +00:00
|
|
|
Config string `json:"config"`
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|
|
|
|
|
2024-04-26 15:05:48 +00:00
|
|
|
type ChannelConfig struct {
|
|
|
|
Region string `json:"region,omitempty"`
|
|
|
|
SK string `json:"sk,omitempty"`
|
|
|
|
AK string `json:"ak,omitempty"`
|
|
|
|
UserID string `json:"user_id,omitempty"`
|
|
|
|
APIVersion string `json:"api_version,omitempty"`
|
|
|
|
LibraryID string `json:"library_id,omitempty"`
|
|
|
|
Plugin string `json:"plugin,omitempty"`
|
|
|
|
}
|
|
|
|
|
2024-03-10 10:34:57 +00:00
|
|
|
func GetAllChannels(startIdx int, num int, scope string) ([]*Channel, error) {
|
2023-04-22 14:02:59 +00:00
|
|
|
var channels []*Channel
|
2023-04-22 12:39:27 +00:00
|
|
|
var err error
|
2024-03-10 10:34:57 +00:00
|
|
|
switch scope {
|
|
|
|
case "all":
|
2023-05-15 04:36:55 +00:00
|
|
|
err = DB.Order("id desc").Find(&channels).Error
|
2024-03-10 10:34:57 +00:00
|
|
|
case "disabled":
|
2024-04-05 18:03:59 +00:00
|
|
|
err = DB.Order("id desc").Where("status = ? or status = ?", ChannelStatusAutoDisabled, ChannelStatusManuallyDisabled).Find(&channels).Error
|
2024-03-10 10:34:57 +00:00
|
|
|
default:
|
2023-05-15 04:36:55 +00:00
|
|
|
err = DB.Order("id desc").Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
|
|
|
|
}
|
2023-04-22 14:02:59 +00:00
|
|
|
return channels, err
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
func SearchChannels(keyword string) (channels []*Channel, err error) {
|
2024-03-10 15:27:22 +00:00
|
|
|
err = DB.Omit("key").Where("id = ? or name LIKE ?", helper.String2Int(keyword), keyword+"%").Find(&channels).Error
|
2023-04-22 14:02:59 +00:00
|
|
|
return channels, err
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|
|
|
|
|
2023-04-23 10:24:11 +00:00
|
|
|
func GetChannelById(id int, selectAll bool) (*Channel, error) {
|
2023-04-22 14:02:59 +00:00
|
|
|
channel := Channel{Id: id}
|
|
|
|
var err error = nil
|
2023-04-23 10:24:11 +00:00
|
|
|
if selectAll {
|
|
|
|
err = DB.First(&channel, "id = ?", id).Error
|
|
|
|
} else {
|
|
|
|
err = DB.Omit("key").First(&channel, "id = ?", id).Error
|
|
|
|
}
|
|
|
|
return &channel, err
|
2023-04-22 14:02:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 09:08:13 +00:00
|
|
|
func BatchInsertChannels(channels []Channel) error {
|
|
|
|
var err error
|
|
|
|
err = DB.Create(&channels).Error
|
2023-06-07 15:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, channel_ := range channels {
|
|
|
|
err = channel_.AddAbilities()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2023-05-13 09:08:13 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 13:43:45 +00:00
|
|
|
func (channel *Channel) GetPriority() int64 {
|
2023-09-18 14:07:17 +00:00
|
|
|
if channel.Priority == nil {
|
2023-09-18 13:43:45 +00:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return *channel.Priority
|
|
|
|
}
|
|
|
|
|
2023-09-18 14:07:17 +00:00
|
|
|
func (channel *Channel) GetBaseURL() string {
|
|
|
|
if channel.BaseURL == nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
return *channel.BaseURL
|
|
|
|
}
|
|
|
|
|
2024-01-21 15:21:42 +00:00
|
|
|
func (channel *Channel) GetModelMapping() map[string]string {
|
|
|
|
if channel.ModelMapping == nil || *channel.ModelMapping == "" || *channel.ModelMapping == "{}" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
modelMapping := make(map[string]string)
|
|
|
|
err := json.Unmarshal([]byte(*channel.ModelMapping), &modelMapping)
|
|
|
|
if err != nil {
|
|
|
|
logger.SysError(fmt.Sprintf("failed to unmarshal model mapping for channel %d, error: %s", channel.Id, err.Error()))
|
|
|
|
return nil
|
2023-09-18 14:07:17 +00:00
|
|
|
}
|
2024-01-21 15:21:42 +00:00
|
|
|
return modelMapping
|
2023-09-18 14:07:17 +00:00
|
|
|
}
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
func (channel *Channel) Insert() error {
|
2023-04-22 12:39:27 +00:00
|
|
|
var err error
|
2023-04-22 14:02:59 +00:00
|
|
|
err = DB.Create(channel).Error
|
2023-06-07 15:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = channel.AddAbilities()
|
2023-04-22 12:39:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
func (channel *Channel) Update() error {
|
2023-04-22 12:39:27 +00:00
|
|
|
var err error
|
2023-04-22 14:02:59 +00:00
|
|
|
err = DB.Model(channel).Updates(channel).Error
|
2023-06-07 15:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-08 01:26:54 +00:00
|
|
|
DB.Model(channel).First(channel, "id = ?", channel.Id)
|
2023-06-07 15:26:00 +00:00
|
|
|
err = channel.UpdateAbilities()
|
2023-04-22 12:39:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-15 03:35:38 +00:00
|
|
|
func (channel *Channel) UpdateResponseTime(responseTime int64) {
|
|
|
|
err := DB.Model(channel).Select("response_time", "test_time").Updates(Channel{
|
2024-01-21 15:21:42 +00:00
|
|
|
TestTime: helper.GetTimestamp(),
|
2023-05-15 03:35:38 +00:00
|
|
|
ResponseTime: int(responseTime),
|
|
|
|
}).Error
|
|
|
|
if err != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
logger.SysError("failed to update response time: " + err.Error())
|
2023-05-15 03:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-21 08:09:54 +00:00
|
|
|
func (channel *Channel) UpdateBalance(balance float64) {
|
|
|
|
err := DB.Model(channel).Select("balance_updated_time", "balance").Updates(Channel{
|
2024-01-21 15:21:42 +00:00
|
|
|
BalanceUpdatedTime: helper.GetTimestamp(),
|
2023-05-21 08:09:54 +00:00
|
|
|
Balance: balance,
|
|
|
|
}).Error
|
|
|
|
if err != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
logger.SysError("failed to update balance: " + err.Error())
|
2023-05-21 08:09:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-22 14:02:59 +00:00
|
|
|
func (channel *Channel) Delete() error {
|
|
|
|
var err error
|
|
|
|
err = DB.Delete(channel).Error
|
2023-06-07 15:26:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = channel.DeleteAbilities()
|
2023-04-22 14:02:59 +00:00
|
|
|
return err
|
2023-04-22 12:39:27 +00:00
|
|
|
}
|
2023-05-15 09:34:09 +00:00
|
|
|
|
2024-04-26 15:05:48 +00:00
|
|
|
func (channel *Channel) LoadConfig() (ChannelConfig, error) {
|
|
|
|
var cfg ChannelConfig
|
2024-02-17 18:22:50 +00:00
|
|
|
if channel.Config == "" {
|
2024-04-26 15:05:48 +00:00
|
|
|
return cfg, nil
|
2024-02-17 18:22:50 +00:00
|
|
|
}
|
|
|
|
err := json.Unmarshal([]byte(channel.Config), &cfg)
|
|
|
|
if err != nil {
|
2024-04-26 15:05:48 +00:00
|
|
|
return cfg, err
|
2024-02-17 18:22:50 +00:00
|
|
|
}
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2023-05-15 09:34:09 +00:00
|
|
|
func UpdateChannelStatusById(id int, status int) {
|
2024-04-05 18:03:59 +00:00
|
|
|
err := UpdateAbilityStatus(id, status == ChannelStatusEnabled)
|
2023-06-07 15:26:00 +00:00
|
|
|
if err != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
logger.SysError("failed to update ability status: " + err.Error())
|
2023-06-07 15:26:00 +00:00
|
|
|
}
|
|
|
|
err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
|
2023-05-15 09:34:09 +00:00
|
|
|
if err != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
logger.SysError("failed to update channel status: " + err.Error())
|
2023-05-15 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-16 08:02:00 +00:00
|
|
|
|
2024-03-13 12:00:51 +00:00
|
|
|
func UpdateChannelUsedQuota(id int, quota int64) {
|
2024-01-21 15:21:42 +00:00
|
|
|
if config.BatchUpdateEnabled {
|
2023-09-03 06:58:20 +00:00
|
|
|
addNewRecord(BatchUpdateTypeChannelUsedQuota, id, quota)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
updateChannelUsedQuota(id, quota)
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:00:51 +00:00
|
|
|
func updateChannelUsedQuota(id int, quota int64) {
|
2023-08-12 16:51:48 +00:00
|
|
|
err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
|
2023-06-16 08:02:00 +00:00
|
|
|
if err != nil {
|
2024-01-21 15:21:42 +00:00
|
|
|
logger.SysError("failed to update channel used quota: " + err.Error())
|
2023-06-16 08:02:00 +00:00
|
|
|
}
|
|
|
|
}
|
2023-10-02 05:06:27 +00:00
|
|
|
|
|
|
|
func DeleteChannelByStatus(status int64) (int64, error) {
|
|
|
|
result := DB.Where("status = ?", status).Delete(&Channel{})
|
|
|
|
return result.RowsAffected, result.Error
|
|
|
|
}
|
2023-10-14 09:25:48 +00:00
|
|
|
|
|
|
|
func DeleteDisabledChannel() (int64, error) {
|
2024-04-05 18:03:59 +00:00
|
|
|
result := DB.Where("status = ? or status = ?", ChannelStatusAutoDisabled, ChannelStatusManuallyDisabled).Delete(&Channel{})
|
2023-10-14 09:25:48 +00:00
|
|
|
return result.RowsAffected, result.Error
|
|
|
|
}
|