channel.go 22 KB

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