channel.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. package model
  2. import (
  3. "encoding/json"
  4. "one-api/common"
  5. "strings"
  6. "gorm.io/gorm"
  7. )
  8. type Channel struct {
  9. Id int `json:"id"`
  10. Type int `json:"type" gorm:"default:0"`
  11. Key string `json:"key" gorm:"not null"`
  12. OpenAIOrganization *string `json:"openai_organization"`
  13. TestModel *string `json:"test_model"`
  14. Status int `json:"status" gorm:"default:1"`
  15. Name string `json:"name" gorm:"index"`
  16. Weight *uint `json:"weight" gorm:"default:0"`
  17. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  18. TestTime int64 `json:"test_time" gorm:"bigint"`
  19. ResponseTime int `json:"response_time"` // in milliseconds
  20. BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"`
  21. Other string `json:"other"`
  22. Balance float64 `json:"balance"` // in USD
  23. BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
  24. Models string `json:"models"`
  25. Group string `json:"group" gorm:"type:varchar(64);default:'default'"`
  26. UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
  27. ModelMapping *string `json:"model_mapping" gorm:"type:varchar(1024);default:''"`
  28. //MaxInputTokens *int `json:"max_input_tokens" gorm:"default:0"`
  29. StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
  30. Priority *int64 `json:"priority" gorm:"bigint;default:0"`
  31. AutoBan *int `json:"auto_ban" gorm:"default:1"`
  32. OtherInfo string `json:"other_info"`
  33. Tag *string `json:"tag" gorm:"index"`
  34. }
  35. func (channel *Channel) GetModels() []string {
  36. if channel.Models == "" {
  37. return []string{}
  38. }
  39. return strings.Split(strings.Trim(channel.Models, ","), ",")
  40. }
  41. func (channel *Channel) GetOtherInfo() map[string]interface{} {
  42. otherInfo := make(map[string]interface{})
  43. if channel.OtherInfo != "" {
  44. err := json.Unmarshal([]byte(channel.OtherInfo), &otherInfo)
  45. if err != nil {
  46. common.SysError("failed to unmarshal other info: " + err.Error())
  47. }
  48. }
  49. return otherInfo
  50. }
  51. func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
  52. otherInfoBytes, err := json.Marshal(otherInfo)
  53. if err != nil {
  54. common.SysError("failed to marshal other info: " + err.Error())
  55. return
  56. }
  57. channel.OtherInfo = string(otherInfoBytes)
  58. }
  59. func (channel *Channel) GetTag() string {
  60. if channel.Tag == nil {
  61. return ""
  62. }
  63. return *channel.Tag
  64. }
  65. func (channel *Channel) SetTag(tag string) {
  66. channel.Tag = &tag
  67. }
  68. func (channel *Channel) GetAutoBan() bool {
  69. if channel.AutoBan == nil {
  70. return false
  71. }
  72. return *channel.AutoBan == 1
  73. }
  74. func (channel *Channel) Save() error {
  75. return DB.Save(channel).Error
  76. }
  77. func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
  78. var channels []*Channel
  79. var err error
  80. order := "priority desc"
  81. if idSort {
  82. order = "id desc"
  83. }
  84. if selectAll {
  85. err = DB.Order(order).Find(&channels).Error
  86. } else {
  87. err = DB.Order(order).Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
  88. }
  89. return channels, err
  90. }
  91. func GetChannelsByTag(tag string) ([]*Channel, error) {
  92. var channels []*Channel
  93. err := DB.Where("tag = ?", tag).Find(&channels).Error
  94. return channels, err
  95. }
  96. func SearchChannels(keyword string, group string, model string, idSort bool) ([]*Channel, error) {
  97. var channels []*Channel
  98. keyCol := "`key`"
  99. groupCol := "`group`"
  100. modelsCol := "`models`"
  101. // 如果是 PostgreSQL,使用双引号
  102. if common.UsingPostgreSQL {
  103. keyCol = `"key"`
  104. groupCol = `"group"`
  105. modelsCol = `"models"`
  106. }
  107. order := "priority desc"
  108. if idSort {
  109. order = "id desc"
  110. }
  111. // 构造基础查询
  112. baseQuery := DB.Model(&Channel{}).Omit(keyCol)
  113. // 构造WHERE子句
  114. var whereClause string
  115. var args []interface{}
  116. if group != "" && group != "null" {
  117. var groupCondition string
  118. if common.UsingMySQL {
  119. groupCondition = `CONCAT(',', ` + groupCol + `, ',') LIKE ?`
  120. } else {
  121. // sqlite, PostgreSQL
  122. groupCondition = `(',' || ` + groupCol + ` || ',') LIKE ?`
  123. }
  124. whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition
  125. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+model+"%", "%,"+group+",%")
  126. } else {
  127. whereClause = "(id = ? OR name LIKE ? OR " + keyCol + " = ?) AND " + modelsCol + " LIKE ?"
  128. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+model+"%")
  129. }
  130. // 执行查询
  131. err := baseQuery.Where(whereClause, args...).Order(order).Find(&channels).Error
  132. if err != nil {
  133. return nil, err
  134. }
  135. return channels, nil
  136. }
  137. func GetChannelById(id int, selectAll bool) (*Channel, error) {
  138. channel := Channel{Id: id}
  139. var err error = nil
  140. if selectAll {
  141. err = DB.First(&channel, "id = ?", id).Error
  142. } else {
  143. err = DB.Omit("key").First(&channel, "id = ?", id).Error
  144. }
  145. return &channel, err
  146. }
  147. func BatchInsertChannels(channels []Channel) error {
  148. var err error
  149. err = DB.Create(&channels).Error
  150. if err != nil {
  151. return err
  152. }
  153. for _, channel_ := range channels {
  154. err = channel_.AddAbilities()
  155. if err != nil {
  156. return err
  157. }
  158. }
  159. return nil
  160. }
  161. func BatchDeleteChannels(ids []int) error {
  162. //使用事务 删除channel表和channel_ability表
  163. tx := DB.Begin()
  164. err := tx.Where("id in (?)", ids).Delete(&Channel{}).Error
  165. if err != nil {
  166. // 回滚事务
  167. tx.Rollback()
  168. return err
  169. }
  170. err = tx.Where("channel_id in (?)", ids).Delete(&Ability{}).Error
  171. if err != nil {
  172. // 回滚事务
  173. tx.Rollback()
  174. return err
  175. }
  176. // 提交事务
  177. tx.Commit()
  178. return err
  179. }
  180. func (channel *Channel) GetPriority() int64 {
  181. if channel.Priority == nil {
  182. return 0
  183. }
  184. return *channel.Priority
  185. }
  186. func (channel *Channel) GetWeight() int {
  187. if channel.Weight == nil {
  188. return 0
  189. }
  190. return int(*channel.Weight)
  191. }
  192. func (channel *Channel) GetBaseURL() string {
  193. if channel.BaseURL == nil {
  194. return ""
  195. }
  196. return *channel.BaseURL
  197. }
  198. func (channel *Channel) GetModelMapping() string {
  199. if channel.ModelMapping == nil {
  200. return ""
  201. }
  202. return *channel.ModelMapping
  203. }
  204. func (channel *Channel) GetStatusCodeMapping() string {
  205. if channel.StatusCodeMapping == nil {
  206. return ""
  207. }
  208. return *channel.StatusCodeMapping
  209. }
  210. func (channel *Channel) Insert() error {
  211. var err error
  212. err = DB.Create(channel).Error
  213. if err != nil {
  214. return err
  215. }
  216. err = channel.AddAbilities()
  217. return err
  218. }
  219. func (channel *Channel) Update() error {
  220. var err error
  221. err = DB.Model(channel).Updates(channel).Error
  222. if err != nil {
  223. return err
  224. }
  225. DB.Model(channel).First(channel, "id = ?", channel.Id)
  226. err = channel.UpdateAbilities()
  227. return err
  228. }
  229. func (channel *Channel) UpdateResponseTime(responseTime int64) {
  230. err := DB.Model(channel).Select("response_time", "test_time").Updates(Channel{
  231. TestTime: common.GetTimestamp(),
  232. ResponseTime: int(responseTime),
  233. }).Error
  234. if err != nil {
  235. common.SysError("failed to update response time: " + err.Error())
  236. }
  237. }
  238. func (channel *Channel) UpdateBalance(balance float64) {
  239. err := DB.Model(channel).Select("balance_updated_time", "balance").Updates(Channel{
  240. BalanceUpdatedTime: common.GetTimestamp(),
  241. Balance: balance,
  242. }).Error
  243. if err != nil {
  244. common.SysError("failed to update balance: " + err.Error())
  245. }
  246. }
  247. func (channel *Channel) Delete() error {
  248. var err error
  249. err = DB.Delete(channel).Error
  250. if err != nil {
  251. return err
  252. }
  253. err = channel.DeleteAbilities()
  254. return err
  255. }
  256. func UpdateChannelStatusById(id int, status int, reason string) {
  257. err := UpdateAbilityStatus(id, status == common.ChannelStatusEnabled)
  258. if err != nil {
  259. common.SysError("failed to update ability status: " + err.Error())
  260. }
  261. channel, err := GetChannelById(id, true)
  262. if err != nil {
  263. // find channel by id error, directly update status
  264. err = DB.Model(&Channel{}).Where("id = ?", id).Update("status", status).Error
  265. if err != nil {
  266. common.SysError("failed to update channel status: " + err.Error())
  267. }
  268. } else {
  269. // find channel by id success, update status and other info
  270. info := channel.GetOtherInfo()
  271. info["status_reason"] = reason
  272. info["status_time"] = common.GetTimestamp()
  273. channel.SetOtherInfo(info)
  274. channel.Status = status
  275. err = channel.Save()
  276. if err != nil {
  277. common.SysError("failed to update channel status: " + err.Error())
  278. }
  279. }
  280. }
  281. func EnableChannelByTag(tag string) error {
  282. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusEnabled).Error
  283. if err != nil {
  284. return err
  285. }
  286. err = UpdateAbilityStatusByTag(tag, true)
  287. return err
  288. }
  289. func DisableChannelByTag(tag string) error {
  290. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusManuallyDisabled).Error
  291. if err != nil {
  292. return err
  293. }
  294. err = UpdateAbilityStatusByTag(tag, false)
  295. return err
  296. }
  297. func EditChannelByTag(tag string, newTag *string, modelMapping *string, models *string, group *string, priority *int64, weight *uint) error {
  298. updateData := Channel{}
  299. shouldReCreateAbilities := false
  300. updatedTag := tag
  301. // 如果 newTag 不为空且不等于 tag,则更新 tag
  302. if newTag != nil && *newTag != tag {
  303. updateData.Tag = newTag
  304. updatedTag = *newTag
  305. }
  306. if modelMapping != nil && *modelMapping != "" {
  307. updateData.ModelMapping = modelMapping
  308. }
  309. if models != nil && *models != "" {
  310. shouldReCreateAbilities = true
  311. updateData.Models = *models
  312. }
  313. if group != nil && *group != "" {
  314. shouldReCreateAbilities = true
  315. updateData.Group = *group
  316. }
  317. if priority != nil {
  318. updateData.Priority = priority
  319. }
  320. if weight != nil {
  321. updateData.Weight = weight
  322. }
  323. err := DB.Model(&Channel{}).Where("tag = ?", tag).Updates(updateData).Error
  324. if err != nil {
  325. return err
  326. }
  327. if shouldReCreateAbilities {
  328. channels, err := GetChannelsByTag(updatedTag)
  329. if err == nil {
  330. for _, channel := range channels {
  331. err = channel.UpdateAbilities()
  332. if err != nil {
  333. common.SysError("failed to update abilities: " + err.Error())
  334. }
  335. }
  336. }
  337. } else {
  338. err := UpdateAbilityByTag(tag, newTag, priority, weight)
  339. if err != nil {
  340. return err
  341. }
  342. }
  343. return nil
  344. }
  345. func UpdateChannelUsedQuota(id int, quota int) {
  346. if common.BatchUpdateEnabled {
  347. addNewRecord(BatchUpdateTypeChannelUsedQuota, id, quota)
  348. return
  349. }
  350. updateChannelUsedQuota(id, quota)
  351. }
  352. func updateChannelUsedQuota(id int, quota int) {
  353. err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
  354. if err != nil {
  355. common.SysError("failed to update channel used quota: " + err.Error())
  356. }
  357. }
  358. func DeleteChannelByStatus(status int64) (int64, error) {
  359. result := DB.Where("status = ?", status).Delete(&Channel{})
  360. return result.RowsAffected, result.Error
  361. }
  362. func DeleteDisabledChannel() (int64, error) {
  363. result := DB.Where("status = ? or status = ?", common.ChannelStatusAutoDisabled, common.ChannelStatusManuallyDisabled).Delete(&Channel{})
  364. return result.RowsAffected, result.Error
  365. }
  366. func GetPaginatedTags(offset int, limit int) ([]*string, error) {
  367. var tags []*string
  368. err := DB.Model(&Channel{}).Select("DISTINCT tag").Where("tag != ''").Offset(offset).Limit(limit).Find(&tags).Error
  369. return tags, err
  370. }