Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 

1018 строки
30 KiB

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