channel.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. package model
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "math/rand"
  8. "one-api/common"
  9. "one-api/constant"
  10. "one-api/dto"
  11. "one-api/types"
  12. "strings"
  13. "sync"
  14. "gorm.io/gorm"
  15. )
  16. type Channel struct {
  17. Id int `json:"id"`
  18. Type int `json:"type" gorm:"default:0"`
  19. Key string `json:"key" gorm:"not null"`
  20. OpenAIOrganization *string `json:"openai_organization"`
  21. TestModel *string `json:"test_model"`
  22. Status int `json:"status" gorm:"default:1"`
  23. Name string `json:"name" gorm:"index"`
  24. Weight *uint `json:"weight" gorm:"default:0"`
  25. CreatedTime int64 `json:"created_time" gorm:"bigint"`
  26. TestTime int64 `json:"test_time" gorm:"bigint"`
  27. ResponseTime int `json:"response_time"` // in milliseconds
  28. BaseURL *string `json:"base_url" gorm:"column:base_url;default:''"`
  29. Other string `json:"other"`
  30. Balance float64 `json:"balance"` // in USD
  31. BalanceUpdatedTime int64 `json:"balance_updated_time" gorm:"bigint"`
  32. Models string `json:"models"`
  33. Group string `json:"group" gorm:"type:varchar(64);default:'default'"`
  34. UsedQuota int64 `json:"used_quota" gorm:"bigint;default:0"`
  35. ModelMapping *string `json:"model_mapping" gorm:"type:text"`
  36. //MaxInputTokens *int `json:"max_input_tokens" gorm:"default:0"`
  37. StatusCodeMapping *string `json:"status_code_mapping" gorm:"type:varchar(1024);default:''"`
  38. Priority *int64 `json:"priority" gorm:"bigint;default:0"`
  39. AutoBan *int `json:"auto_ban" gorm:"default:1"`
  40. OtherInfo string `json:"other_info"`
  41. Tag *string `json:"tag" gorm:"index"`
  42. Setting *string `json:"setting" gorm:"type:text"` // 渠道额外设置
  43. ParamOverride *string `json:"param_override" gorm:"type:text"`
  44. // add after v0.8.5
  45. ChannelInfo ChannelInfo `json:"channel_info" gorm:"type:json"`
  46. }
  47. type ChannelInfo struct {
  48. IsMultiKey bool `json:"is_multi_key"` // 是否多Key模式
  49. MultiKeySize int `json:"multi_key_size"` // 多Key模式下的Key数量
  50. MultiKeyStatusList map[int]int `json:"multi_key_status_list"` // key状态列表,key index -> status
  51. MultiKeyPollingIndex int `json:"multi_key_polling_index"` // 多Key模式下轮询的key索引
  52. MultiKeyMode constant.MultiKeyMode `json:"multi_key_mode"`
  53. }
  54. // Value implements driver.Valuer interface
  55. func (c ChannelInfo) Value() (driver.Value, error) {
  56. return common.Marshal(&c)
  57. }
  58. // Scan implements sql.Scanner interface
  59. func (c *ChannelInfo) Scan(value interface{}) error {
  60. bytesValue, _ := value.([]byte)
  61. return common.Unmarshal(bytesValue, c)
  62. }
  63. func (channel *Channel) getKeys() []string {
  64. if channel.Key == "" {
  65. return []string{}
  66. }
  67. // use \n to split keys
  68. keys := strings.Split(strings.Trim(channel.Key, "\n"), "\n")
  69. return keys
  70. }
  71. func (channel *Channel) GetNextEnabledKey() (string, int, *types.NewAPIError) {
  72. // If not in multi-key mode, return the original key string directly.
  73. if !channel.ChannelInfo.IsMultiKey {
  74. return channel.Key, 0, nil
  75. }
  76. // Obtain all keys (split by \n)
  77. keys := channel.getKeys()
  78. if len(keys) == 0 {
  79. // No keys available, return error, should disable the channel
  80. return "", 0, types.NewError(errors.New("no keys available"), types.ErrorCodeChannelNoAvailableKey)
  81. }
  82. statusList := channel.ChannelInfo.MultiKeyStatusList
  83. // helper to get key status, default to enabled when missing
  84. getStatus := func(idx int) int {
  85. if statusList == nil {
  86. return common.ChannelStatusEnabled
  87. }
  88. if status, ok := statusList[idx]; ok {
  89. return status
  90. }
  91. return common.ChannelStatusEnabled
  92. }
  93. // Collect indexes of enabled keys
  94. enabledIdx := make([]int, 0, len(keys))
  95. for i := range keys {
  96. if getStatus(i) == common.ChannelStatusEnabled {
  97. enabledIdx = append(enabledIdx, i)
  98. }
  99. }
  100. // If no specific status list or none enabled, fall back to first key
  101. if len(enabledIdx) == 0 {
  102. return keys[0], 0, nil
  103. }
  104. switch channel.ChannelInfo.MultiKeyMode {
  105. case constant.MultiKeyModeRandom:
  106. // Randomly pick one enabled key
  107. selectedIdx := enabledIdx[rand.Intn(len(enabledIdx))]
  108. return keys[selectedIdx], selectedIdx, nil
  109. case constant.MultiKeyModePolling:
  110. // Use channel-specific lock to ensure thread-safe polling
  111. lock := getChannelPollingLock(channel.Id)
  112. lock.Lock()
  113. defer lock.Unlock()
  114. channelInfo, err := CacheGetChannelInfo(channel.Id)
  115. if err != nil {
  116. return "", 0, types.NewError(err, types.ErrorCodeGetChannelFailed)
  117. }
  118. //println("before polling index:", channel.ChannelInfo.MultiKeyPollingIndex)
  119. defer func() {
  120. if common.DebugEnabled {
  121. println(fmt.Sprintf("channel %d polling index: %d", channel.Id, channel.ChannelInfo.MultiKeyPollingIndex))
  122. }
  123. if !common.MemoryCacheEnabled {
  124. _ = channel.SaveChannelInfo()
  125. } else {
  126. // CacheUpdateChannel(channel)
  127. }
  128. }()
  129. // Start from the saved polling index and look for the next enabled key
  130. start := channelInfo.MultiKeyPollingIndex
  131. if start < 0 || start >= len(keys) {
  132. start = 0
  133. }
  134. for i := 0; i < len(keys); i++ {
  135. idx := (start + i) % len(keys)
  136. if getStatus(idx) == common.ChannelStatusEnabled {
  137. // update polling index for next call (point to the next position)
  138. channel.ChannelInfo.MultiKeyPollingIndex = (idx + 1) % len(keys)
  139. return keys[idx], idx, nil
  140. }
  141. }
  142. // Fallback – should not happen, but return first enabled key
  143. return keys[enabledIdx[0]], enabledIdx[0], nil
  144. default:
  145. // Unknown mode, default to first enabled key (or original key string)
  146. return keys[enabledIdx[0]], enabledIdx[0], nil
  147. }
  148. }
  149. func (channel *Channel) SaveChannelInfo() error {
  150. return DB.Model(channel).Update("channel_info", channel.ChannelInfo).Error
  151. }
  152. func (channel *Channel) GetModels() []string {
  153. if channel.Models == "" {
  154. return []string{}
  155. }
  156. return strings.Split(strings.Trim(channel.Models, ","), ",")
  157. }
  158. func (channel *Channel) GetGroups() []string {
  159. if channel.Group == "" {
  160. return []string{}
  161. }
  162. groups := strings.Split(strings.Trim(channel.Group, ","), ",")
  163. for i, group := range groups {
  164. groups[i] = strings.TrimSpace(group)
  165. }
  166. return groups
  167. }
  168. func (channel *Channel) GetOtherInfo() map[string]interface{} {
  169. otherInfo := make(map[string]interface{})
  170. if channel.OtherInfo != "" {
  171. err := json.Unmarshal([]byte(channel.OtherInfo), &otherInfo)
  172. if err != nil {
  173. common.SysError("failed to unmarshal other info: " + err.Error())
  174. }
  175. }
  176. return otherInfo
  177. }
  178. func (channel *Channel) SetOtherInfo(otherInfo map[string]interface{}) {
  179. otherInfoBytes, err := json.Marshal(otherInfo)
  180. if err != nil {
  181. common.SysError("failed to marshal other info: " + err.Error())
  182. return
  183. }
  184. channel.OtherInfo = string(otherInfoBytes)
  185. }
  186. func (channel *Channel) GetTag() string {
  187. if channel.Tag == nil {
  188. return ""
  189. }
  190. return *channel.Tag
  191. }
  192. func (channel *Channel) SetTag(tag string) {
  193. channel.Tag = &tag
  194. }
  195. func (channel *Channel) GetAutoBan() bool {
  196. if channel.AutoBan == nil {
  197. return false
  198. }
  199. return *channel.AutoBan == 1
  200. }
  201. func (channel *Channel) Save() error {
  202. return DB.Save(channel).Error
  203. }
  204. func GetAllChannels(startIdx int, num int, selectAll bool, idSort bool) ([]*Channel, error) {
  205. var channels []*Channel
  206. var err error
  207. order := "priority desc"
  208. if idSort {
  209. order = "id desc"
  210. }
  211. if selectAll {
  212. err = DB.Order(order).Find(&channels).Error
  213. } else {
  214. err = DB.Order(order).Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
  215. }
  216. return channels, err
  217. }
  218. func GetChannelsByTag(tag string, idSort bool) ([]*Channel, error) {
  219. var channels []*Channel
  220. order := "priority desc"
  221. if idSort {
  222. order = "id desc"
  223. }
  224. err := DB.Where("tag = ?", tag).Order(order).Find(&channels).Error
  225. return channels, err
  226. }
  227. func SearchChannels(keyword string, group string, model string, idSort bool) ([]*Channel, error) {
  228. var channels []*Channel
  229. modelsCol := "`models`"
  230. // 如果是 PostgreSQL,使用双引号
  231. if common.UsingPostgreSQL {
  232. modelsCol = `"models"`
  233. }
  234. baseURLCol := "`base_url`"
  235. // 如果是 PostgreSQL,使用双引号
  236. if common.UsingPostgreSQL {
  237. baseURLCol = `"base_url"`
  238. }
  239. order := "priority desc"
  240. if idSort {
  241. order = "id desc"
  242. }
  243. // 构造基础查询
  244. baseQuery := DB.Model(&Channel{}).Omit("key")
  245. // 构造WHERE子句
  246. var whereClause string
  247. var args []interface{}
  248. if group != "" && group != "null" {
  249. var groupCondition string
  250. if common.UsingMySQL {
  251. groupCondition = `CONCAT(',', ` + commonGroupCol + `, ',') LIKE ?`
  252. } else {
  253. // sqlite, PostgreSQL
  254. groupCondition = `(',' || ` + commonGroupCol + ` || ',') LIKE ?`
  255. }
  256. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition
  257. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%", "%,"+group+",%")
  258. } else {
  259. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + " LIKE ?"
  260. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%")
  261. }
  262. // 执行查询
  263. err := baseQuery.Where(whereClause, args...).Order(order).Find(&channels).Error
  264. if err != nil {
  265. return nil, err
  266. }
  267. return channels, nil
  268. }
  269. func GetChannelById(id int, selectAll bool) (*Channel, error) {
  270. channel := &Channel{Id: id}
  271. var err error = nil
  272. if selectAll {
  273. err = DB.First(channel, "id = ?", id).Error
  274. } else {
  275. err = DB.Omit("key").First(channel, "id = ?", id).Error
  276. }
  277. if err != nil {
  278. return nil, err
  279. }
  280. if channel == nil {
  281. return nil, errors.New("channel not found")
  282. }
  283. return channel, nil
  284. }
  285. func BatchInsertChannels(channels []Channel) error {
  286. var err error
  287. err = DB.Create(&channels).Error
  288. if err != nil {
  289. return err
  290. }
  291. for _, channel_ := range channels {
  292. err = channel_.AddAbilities()
  293. if err != nil {
  294. return err
  295. }
  296. }
  297. return nil
  298. }
  299. func BatchDeleteChannels(ids []int) error {
  300. //使用事务 删除channel表和channel_ability表
  301. tx := DB.Begin()
  302. err := tx.Where("id in (?)", ids).Delete(&Channel{}).Error
  303. if err != nil {
  304. // 回滚事务
  305. tx.Rollback()
  306. return err
  307. }
  308. err = tx.Where("channel_id in (?)", ids).Delete(&Ability{}).Error
  309. if err != nil {
  310. // 回滚事务
  311. tx.Rollback()
  312. return err
  313. }
  314. // 提交事务
  315. tx.Commit()
  316. return err
  317. }
  318. func (channel *Channel) GetPriority() int64 {
  319. if channel.Priority == nil {
  320. return 0
  321. }
  322. return *channel.Priority
  323. }
  324. func (channel *Channel) GetWeight() int {
  325. if channel.Weight == nil {
  326. return 0
  327. }
  328. return int(*channel.Weight)
  329. }
  330. func (channel *Channel) GetBaseURL() string {
  331. if channel.BaseURL == nil {
  332. return ""
  333. }
  334. return *channel.BaseURL
  335. }
  336. func (channel *Channel) GetModelMapping() string {
  337. if channel.ModelMapping == nil {
  338. return ""
  339. }
  340. return *channel.ModelMapping
  341. }
  342. func (channel *Channel) GetStatusCodeMapping() string {
  343. if channel.StatusCodeMapping == nil {
  344. return ""
  345. }
  346. return *channel.StatusCodeMapping
  347. }
  348. func (channel *Channel) Insert() error {
  349. var err error
  350. err = DB.Create(channel).Error
  351. if err != nil {
  352. return err
  353. }
  354. err = channel.AddAbilities()
  355. return err
  356. }
  357. func (channel *Channel) Update() error {
  358. // 如果是多密钥渠道,则根据当前 key 列表重新计算 MultiKeySize,避免编辑密钥后数量未同步
  359. if channel.ChannelInfo.IsMultiKey {
  360. var keyStr string
  361. if channel.Key != "" {
  362. keyStr = channel.Key
  363. } else {
  364. // 如果当前未提供 key,读取数据库中的现有 key
  365. if existing, err := GetChannelById(channel.Id, true); err == nil {
  366. keyStr = existing.Key
  367. }
  368. }
  369. keys := []string{}
  370. if keyStr != "" {
  371. keys = strings.Split(strings.Trim(keyStr, "\n"), "\n")
  372. }
  373. channel.ChannelInfo.MultiKeySize = len(keys)
  374. // 清理超过新 key 数量范围的状态数据,防止索引越界
  375. if channel.ChannelInfo.MultiKeyStatusList != nil {
  376. for idx := range channel.ChannelInfo.MultiKeyStatusList {
  377. if idx >= channel.ChannelInfo.MultiKeySize {
  378. delete(channel.ChannelInfo.MultiKeyStatusList, idx)
  379. }
  380. }
  381. }
  382. }
  383. var err error
  384. err = DB.Model(channel).Updates(channel).Error
  385. if err != nil {
  386. return err
  387. }
  388. DB.Model(channel).First(channel, "id = ?", channel.Id)
  389. err = channel.UpdateAbilities(nil)
  390. return err
  391. }
  392. func (channel *Channel) UpdateResponseTime(responseTime int64) {
  393. err := DB.Model(channel).Select("response_time", "test_time").Updates(Channel{
  394. TestTime: common.GetTimestamp(),
  395. ResponseTime: int(responseTime),
  396. }).Error
  397. if err != nil {
  398. common.SysError("failed to update response time: " + err.Error())
  399. }
  400. }
  401. func (channel *Channel) UpdateBalance(balance float64) {
  402. err := DB.Model(channel).Select("balance_updated_time", "balance").Updates(Channel{
  403. BalanceUpdatedTime: common.GetTimestamp(),
  404. Balance: balance,
  405. }).Error
  406. if err != nil {
  407. common.SysError("failed to update balance: " + err.Error())
  408. }
  409. }
  410. func (channel *Channel) Delete() error {
  411. var err error
  412. err = DB.Delete(channel).Error
  413. if err != nil {
  414. return err
  415. }
  416. err = channel.DeleteAbilities()
  417. return err
  418. }
  419. var channelStatusLock sync.Mutex
  420. // channelPollingLocks stores locks for each channel.id to ensure thread-safe polling
  421. var channelPollingLocks sync.Map
  422. // getChannelPollingLock returns or creates a mutex for the given channel ID
  423. func getChannelPollingLock(channelId int) *sync.Mutex {
  424. if lock, exists := channelPollingLocks.Load(channelId); exists {
  425. return lock.(*sync.Mutex)
  426. }
  427. // Create new lock for this channel
  428. newLock := &sync.Mutex{}
  429. actual, _ := channelPollingLocks.LoadOrStore(channelId, newLock)
  430. return actual.(*sync.Mutex)
  431. }
  432. // CleanupChannelPollingLocks removes locks for channels that no longer exist
  433. // This is optional and can be called periodically to prevent memory leaks
  434. func CleanupChannelPollingLocks() {
  435. var activeChannelIds []int
  436. DB.Model(&Channel{}).Pluck("id", &activeChannelIds)
  437. activeChannelSet := make(map[int]bool)
  438. for _, id := range activeChannelIds {
  439. activeChannelSet[id] = true
  440. }
  441. channelPollingLocks.Range(func(key, value interface{}) bool {
  442. channelId := key.(int)
  443. if !activeChannelSet[channelId] {
  444. channelPollingLocks.Delete(channelId)
  445. }
  446. return true
  447. })
  448. }
  449. func handlerMultiKeyUpdate(channel *Channel, usingKey string, status int) {
  450. keys := channel.getKeys()
  451. if len(keys) == 0 {
  452. channel.Status = status
  453. } else {
  454. var keyIndex int
  455. for i, key := range keys {
  456. if key == usingKey {
  457. keyIndex = i
  458. break
  459. }
  460. }
  461. if channel.ChannelInfo.MultiKeyStatusList == nil {
  462. channel.ChannelInfo.MultiKeyStatusList = make(map[int]int)
  463. }
  464. if status == common.ChannelStatusEnabled {
  465. delete(channel.ChannelInfo.MultiKeyStatusList, keyIndex)
  466. } else {
  467. channel.ChannelInfo.MultiKeyStatusList[keyIndex] = status
  468. }
  469. if len(channel.ChannelInfo.MultiKeyStatusList) >= channel.ChannelInfo.MultiKeySize {
  470. channel.Status = common.ChannelStatusAutoDisabled
  471. info := channel.GetOtherInfo()
  472. info["status_reason"] = "All keys are disabled"
  473. info["status_time"] = common.GetTimestamp()
  474. channel.SetOtherInfo(info)
  475. }
  476. }
  477. }
  478. func UpdateChannelStatus(channelId int, usingKey string, status int, reason string) bool {
  479. if common.MemoryCacheEnabled {
  480. channelStatusLock.Lock()
  481. defer channelStatusLock.Unlock()
  482. channelCache, _ := CacheGetChannel(channelId)
  483. if channelCache == nil {
  484. return false
  485. }
  486. if channelCache.ChannelInfo.IsMultiKey {
  487. // 如果是多Key模式,更新缓存中的状态
  488. handlerMultiKeyUpdate(channelCache, usingKey, status)
  489. //CacheUpdateChannel(channelCache)
  490. //return true
  491. } else {
  492. // 如果缓存渠道存在,且状态已是目标状态,直接返回
  493. if channelCache.Status == status {
  494. return false
  495. }
  496. // 如果缓存渠道不存在(说明已经被禁用),且要设置的状态不为启用,直接返回
  497. if status != common.ChannelStatusEnabled {
  498. return false
  499. }
  500. CacheUpdateChannelStatus(channelId, status)
  501. }
  502. }
  503. shouldUpdateAbilities := false
  504. defer func() {
  505. if shouldUpdateAbilities {
  506. err := UpdateAbilityStatus(channelId, status == common.ChannelStatusEnabled)
  507. if err != nil {
  508. common.SysError("failed to update ability status: " + err.Error())
  509. }
  510. }
  511. }()
  512. channel, err := GetChannelById(channelId, true)
  513. if err != nil {
  514. return false
  515. } else {
  516. if channel.Status == status {
  517. return false
  518. }
  519. if channel.ChannelInfo.IsMultiKey {
  520. beforeStatus := channel.Status
  521. handlerMultiKeyUpdate(channel, usingKey, status)
  522. if beforeStatus != channel.Status {
  523. shouldUpdateAbilities = true
  524. }
  525. } else {
  526. info := channel.GetOtherInfo()
  527. info["status_reason"] = reason
  528. info["status_time"] = common.GetTimestamp()
  529. channel.SetOtherInfo(info)
  530. channel.Status = status
  531. shouldUpdateAbilities = true
  532. }
  533. err = channel.Save()
  534. if err != nil {
  535. common.SysError("failed to update channel status: " + err.Error())
  536. return false
  537. }
  538. }
  539. return true
  540. }
  541. func EnableChannelByTag(tag string) error {
  542. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusEnabled).Error
  543. if err != nil {
  544. return err
  545. }
  546. err = UpdateAbilityStatusByTag(tag, true)
  547. return err
  548. }
  549. func DisableChannelByTag(tag string) error {
  550. err := DB.Model(&Channel{}).Where("tag = ?", tag).Update("status", common.ChannelStatusManuallyDisabled).Error
  551. if err != nil {
  552. return err
  553. }
  554. err = UpdateAbilityStatusByTag(tag, false)
  555. return err
  556. }
  557. func EditChannelByTag(tag string, newTag *string, modelMapping *string, models *string, group *string, priority *int64, weight *uint) error {
  558. updateData := Channel{}
  559. shouldReCreateAbilities := false
  560. updatedTag := tag
  561. // 如果 newTag 不为空且不等于 tag,则更新 tag
  562. if newTag != nil && *newTag != tag {
  563. updateData.Tag = newTag
  564. updatedTag = *newTag
  565. }
  566. if modelMapping != nil && *modelMapping != "" {
  567. updateData.ModelMapping = modelMapping
  568. }
  569. if models != nil && *models != "" {
  570. shouldReCreateAbilities = true
  571. updateData.Models = *models
  572. }
  573. if group != nil && *group != "" {
  574. shouldReCreateAbilities = true
  575. updateData.Group = *group
  576. }
  577. if priority != nil {
  578. updateData.Priority = priority
  579. }
  580. if weight != nil {
  581. updateData.Weight = weight
  582. }
  583. err := DB.Model(&Channel{}).Where("tag = ?", tag).Updates(updateData).Error
  584. if err != nil {
  585. return err
  586. }
  587. if shouldReCreateAbilities {
  588. channels, err := GetChannelsByTag(updatedTag, false)
  589. if err == nil {
  590. for _, channel := range channels {
  591. err = channel.UpdateAbilities(nil)
  592. if err != nil {
  593. common.SysError("failed to update abilities: " + err.Error())
  594. }
  595. }
  596. }
  597. } else {
  598. err := UpdateAbilityByTag(tag, newTag, priority, weight)
  599. if err != nil {
  600. return err
  601. }
  602. }
  603. return nil
  604. }
  605. func UpdateChannelUsedQuota(id int, quota int) {
  606. if common.BatchUpdateEnabled {
  607. addNewRecord(BatchUpdateTypeChannelUsedQuota, id, quota)
  608. return
  609. }
  610. updateChannelUsedQuota(id, quota)
  611. }
  612. func updateChannelUsedQuota(id int, quota int) {
  613. err := DB.Model(&Channel{}).Where("id = ?", id).Update("used_quota", gorm.Expr("used_quota + ?", quota)).Error
  614. if err != nil {
  615. common.SysError("failed to update channel used quota: " + err.Error())
  616. }
  617. }
  618. func DeleteChannelByStatus(status int64) (int64, error) {
  619. result := DB.Where("status = ?", status).Delete(&Channel{})
  620. return result.RowsAffected, result.Error
  621. }
  622. func DeleteDisabledChannel() (int64, error) {
  623. result := DB.Where("status = ? or status = ?", common.ChannelStatusAutoDisabled, common.ChannelStatusManuallyDisabled).Delete(&Channel{})
  624. return result.RowsAffected, result.Error
  625. }
  626. func GetPaginatedTags(offset int, limit int) ([]*string, error) {
  627. var tags []*string
  628. err := DB.Model(&Channel{}).Select("DISTINCT tag").Where("tag != ''").Offset(offset).Limit(limit).Find(&tags).Error
  629. return tags, err
  630. }
  631. func SearchTags(keyword string, group string, model string, idSort bool) ([]*string, error) {
  632. var tags []*string
  633. modelsCol := "`models`"
  634. // 如果是 PostgreSQL,使用双引号
  635. if common.UsingPostgreSQL {
  636. modelsCol = `"models"`
  637. }
  638. baseURLCol := "`base_url`"
  639. // 如果是 PostgreSQL,使用双引号
  640. if common.UsingPostgreSQL {
  641. baseURLCol = `"base_url"`
  642. }
  643. order := "priority desc"
  644. if idSort {
  645. order = "id desc"
  646. }
  647. // 构造基础查询
  648. baseQuery := DB.Model(&Channel{}).Omit("key")
  649. // 构造WHERE子句
  650. var whereClause string
  651. var args []interface{}
  652. if group != "" && group != "null" {
  653. var groupCondition string
  654. if common.UsingMySQL {
  655. groupCondition = `CONCAT(',', ` + commonGroupCol + `, ',') LIKE ?`
  656. } else {
  657. // sqlite, PostgreSQL
  658. groupCondition = `(',' || ` + commonGroupCol + ` || ',') LIKE ?`
  659. }
  660. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + ` LIKE ? AND ` + groupCondition
  661. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%", "%,"+group+",%")
  662. } else {
  663. whereClause = "(id = ? OR name LIKE ? OR " + commonKeyCol + " = ? OR " + baseURLCol + " LIKE ?) AND " + modelsCol + " LIKE ?"
  664. args = append(args, common.String2Int(keyword), "%"+keyword+"%", keyword, "%"+keyword+"%", "%"+model+"%")
  665. }
  666. subQuery := baseQuery.Where(whereClause, args...).
  667. Select("tag").
  668. Where("tag != ''").
  669. Order(order)
  670. err := DB.Table("(?) as sub", subQuery).
  671. Select("DISTINCT tag").
  672. Find(&tags).Error
  673. if err != nil {
  674. return nil, err
  675. }
  676. return tags, nil
  677. }
  678. func (channel *Channel) ValidateSettings() error {
  679. channelParams := &dto.ChannelSettings{}
  680. if channel.Setting != nil && *channel.Setting != "" {
  681. err := json.Unmarshal([]byte(*channel.Setting), channelParams)
  682. if err != nil {
  683. return err
  684. }
  685. }
  686. return nil
  687. }
  688. func (channel *Channel) GetSetting() dto.ChannelSettings {
  689. setting := dto.ChannelSettings{}
  690. if channel.Setting != nil && *channel.Setting != "" {
  691. err := json.Unmarshal([]byte(*channel.Setting), &setting)
  692. if err != nil {
  693. common.SysError("failed to unmarshal setting: " + err.Error())
  694. channel.Setting = nil // 清空设置以避免后续错误
  695. _ = channel.Save() // 保存修改
  696. }
  697. }
  698. return setting
  699. }
  700. func (channel *Channel) SetSetting(setting dto.ChannelSettings) {
  701. settingBytes, err := json.Marshal(setting)
  702. if err != nil {
  703. common.SysError("failed to marshal setting: " + err.Error())
  704. return
  705. }
  706. channel.Setting = common.GetPointer[string](string(settingBytes))
  707. }
  708. func (channel *Channel) GetParamOverride() map[string]interface{} {
  709. paramOverride := make(map[string]interface{})
  710. if channel.ParamOverride != nil && *channel.ParamOverride != "" {
  711. err := json.Unmarshal([]byte(*channel.ParamOverride), &paramOverride)
  712. if err != nil {
  713. common.SysError("failed to unmarshal param override: " + err.Error())
  714. }
  715. }
  716. return paramOverride
  717. }
  718. func GetChannelsByIds(ids []int) ([]*Channel, error) {
  719. var channels []*Channel
  720. err := DB.Where("id in (?)", ids).Find(&channels).Error
  721. return channels, err
  722. }
  723. func BatchSetChannelTag(ids []int, tag *string) error {
  724. // 开启事务
  725. tx := DB.Begin()
  726. if tx.Error != nil {
  727. return tx.Error
  728. }
  729. // 更新标签
  730. err := tx.Model(&Channel{}).Where("id in (?)", ids).Update("tag", tag).Error
  731. if err != nil {
  732. tx.Rollback()
  733. return err
  734. }
  735. // update ability status
  736. channels, err := GetChannelsByIds(ids)
  737. if err != nil {
  738. tx.Rollback()
  739. return err
  740. }
  741. for _, channel := range channels {
  742. err = channel.UpdateAbilities(tx)
  743. if err != nil {
  744. tx.Rollback()
  745. return err
  746. }
  747. }
  748. // 提交事务
  749. return tx.Commit().Error
  750. }
  751. // CountAllChannels returns total channels in DB
  752. func CountAllChannels() (int64, error) {
  753. var total int64
  754. err := DB.Model(&Channel{}).Count(&total).Error
  755. return total, err
  756. }
  757. // CountAllTags returns number of non-empty distinct tags
  758. func CountAllTags() (int64, error) {
  759. var total int64
  760. err := DB.Model(&Channel{}).Where("tag is not null AND tag != ''").Distinct("tag").Count(&total).Error
  761. return total, err
  762. }
  763. // Get channels of specified type with pagination
  764. func GetChannelsByType(startIdx int, num int, idSort bool, channelType int) ([]*Channel, error) {
  765. var channels []*Channel
  766. order := "priority desc"
  767. if idSort {
  768. order = "id desc"
  769. }
  770. err := DB.Where("type = ?", channelType).Order(order).Limit(num).Offset(startIdx).Omit("key").Find(&channels).Error
  771. return channels, err
  772. }
  773. // Count channels of specific type
  774. func CountChannelsByType(channelType int) (int64, error) {
  775. var count int64
  776. err := DB.Model(&Channel{}).Where("type = ?", channelType).Count(&count).Error
  777. return count, err
  778. }
  779. // Return map[type]count for all channels
  780. func CountChannelsGroupByType() (map[int64]int64, error) {
  781. type result struct {
  782. Type int64 `gorm:"column:type"`
  783. Count int64 `gorm:"column:count"`
  784. }
  785. var results []result
  786. err := DB.Model(&Channel{}).Select("type, count(*) as count").Group("type").Find(&results).Error
  787. if err != nil {
  788. return nil, err
  789. }
  790. counts := make(map[int64]int64)
  791. for _, r := range results {
  792. counts[r.Type] = r.Count
  793. }
  794. return counts, nil
  795. }