ability.go 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. package model
  2. import (
  3. "errors"
  4. "fmt"
  5. "one-api/common"
  6. "strings"
  7. "github.com/samber/lo"
  8. "gorm.io/gorm"
  9. "gorm.io/gorm/clause"
  10. )
  11. type Ability struct {
  12. Group string `json:"group" gorm:"type:varchar(64);primaryKey;autoIncrement:false"`
  13. Model string `json:"model" gorm:"type:varchar(255);primaryKey;autoIncrement:false"`
  14. ChannelId int `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
  15. Enabled bool `json:"enabled"`
  16. Priority *int64 `json:"priority" gorm:"bigint;default:0;index"`
  17. Weight uint `json:"weight" gorm:"default:0;index"`
  18. Tag *string `json:"tag" gorm:"index"`
  19. }
  20. type AbilityWithChannel struct {
  21. Ability
  22. ChannelType int `json:"channel_type"`
  23. }
  24. func GetAllEnableAbilityWithChannels() ([]AbilityWithChannel, error) {
  25. var abilities []AbilityWithChannel
  26. err := DB.Table("abilities").
  27. Select("abilities.*, channels.type as channel_type").
  28. Joins("left join channels on abilities.channel_id = channels.id").
  29. Where("abilities.enabled = ?", true).
  30. Scan(&abilities).Error
  31. return abilities, err
  32. }
  33. func GetGroupEnabledModels(group string) []string {
  34. var models []string
  35. // Find distinct models
  36. DB.Table("abilities").Where(commonGroupCol+" = ? and enabled = ?", group, true).Distinct("model").Pluck("model", &models)
  37. return models
  38. }
  39. func GetEnabledModels() []string {
  40. var models []string
  41. // Find distinct models
  42. DB.Table("abilities").Where("enabled = ?", true).Distinct("model").Pluck("model", &models)
  43. return models
  44. }
  45. func GetAllEnableAbilities() []Ability {
  46. var abilities []Ability
  47. DB.Find(&abilities, "enabled = ?", true)
  48. return abilities
  49. }
  50. func getPriority(group string, model string, retry int) (int, error) {
  51. var priorities []int
  52. err := DB.Model(&Ability{}).
  53. Select("DISTINCT(priority)").
  54. Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, true).
  55. Order("priority DESC"). // 按优先级降序排序
  56. Pluck("priority", &priorities).Error // Pluck用于将查询的结果直接扫描到一个切片中
  57. if err != nil {
  58. // 处理错误
  59. return 0, err
  60. }
  61. if len(priorities) == 0 {
  62. // 如果没有查询到优先级,则返回错误
  63. return 0, errors.New("数据库一致性被破坏")
  64. }
  65. // 确定要使用的优先级
  66. var priorityToUse int
  67. if retry >= len(priorities) {
  68. // 如果重试次数大于优先级数,则使用最小的优先级
  69. priorityToUse = priorities[len(priorities)-1]
  70. } else {
  71. priorityToUse = priorities[retry]
  72. }
  73. return priorityToUse, nil
  74. }
  75. func getChannelQuery(group string, model string, retry int) *gorm.DB {
  76. maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(commonGroupCol+" = ? and model = ? and enabled = ?", group, model, true)
  77. channelQuery := DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = (?)", group, model, true, maxPrioritySubQuery)
  78. if retry != 0 {
  79. priority, err := getPriority(group, model, retry)
  80. if err != nil {
  81. common.SysError(fmt.Sprintf("Get priority failed: %s", err.Error()))
  82. } else {
  83. channelQuery = DB.Where(commonGroupCol+" = ? and model = ? and enabled = ? and priority = ?", group, model, true, priority)
  84. }
  85. }
  86. return channelQuery
  87. }
  88. func GetRandomSatisfiedChannel(group string, model string, retry int) (*Channel, error) {
  89. var abilities []Ability
  90. var err error = nil
  91. channelQuery := getChannelQuery(group, model, retry)
  92. if common.UsingSQLite || common.UsingPostgreSQL {
  93. err = channelQuery.Order("weight DESC").Find(&abilities).Error
  94. } else {
  95. err = channelQuery.Order("weight DESC").Find(&abilities).Error
  96. }
  97. if err != nil {
  98. return nil, err
  99. }
  100. channel := Channel{}
  101. if len(abilities) > 0 {
  102. // Randomly choose one
  103. weightSum := uint(0)
  104. for _, ability_ := range abilities {
  105. weightSum += ability_.Weight + 10
  106. }
  107. // Randomly choose one
  108. weight := common.GetRandomInt(int(weightSum))
  109. for _, ability_ := range abilities {
  110. weight -= int(ability_.Weight) + 10
  111. //log.Printf("weight: %d, ability weight: %d", weight, *ability_.Weight)
  112. if weight <= 0 {
  113. channel.Id = ability_.ChannelId
  114. break
  115. }
  116. }
  117. } else {
  118. return nil, errors.New("channel not found")
  119. }
  120. err = DB.First(&channel, "id = ?", channel.Id).Error
  121. return &channel, err
  122. }
  123. func (channel *Channel) AddAbilities() error {
  124. models_ := strings.Split(channel.Models, ",")
  125. groups_ := strings.Split(channel.Group, ",")
  126. abilitySet := make(map[string]struct{})
  127. abilities := make([]Ability, 0, len(models_))
  128. for _, model := range models_ {
  129. for _, group := range groups_ {
  130. key := group + "|" + model
  131. if _, exists := abilitySet[key]; exists {
  132. continue
  133. }
  134. abilitySet[key] = struct{}{}
  135. ability := Ability{
  136. Group: group,
  137. Model: model,
  138. ChannelId: channel.Id,
  139. Enabled: channel.Status == common.ChannelStatusEnabled,
  140. Priority: channel.Priority,
  141. Weight: uint(channel.GetWeight()),
  142. Tag: channel.Tag,
  143. }
  144. abilities = append(abilities, ability)
  145. }
  146. }
  147. if len(abilities) == 0 {
  148. return nil
  149. }
  150. for _, chunk := range lo.Chunk(abilities, 50) {
  151. err := DB.Clauses(clause.OnConflict{DoNothing: true}).Create(&chunk).Error
  152. if err != nil {
  153. return err
  154. }
  155. }
  156. return nil
  157. }
  158. func (channel *Channel) DeleteAbilities() error {
  159. return DB.Where("channel_id = ?", channel.Id).Delete(&Ability{}).Error
  160. }
  161. // UpdateAbilities updates abilities of this channel.
  162. // Make sure the channel is completed before calling this function.
  163. func (channel *Channel) UpdateAbilities(tx *gorm.DB) error {
  164. isNewTx := false
  165. // 如果没有传入事务,创建新的事务
  166. if tx == nil {
  167. tx = DB.Begin()
  168. if tx.Error != nil {
  169. return tx.Error
  170. }
  171. isNewTx = true
  172. defer func() {
  173. if r := recover(); r != nil {
  174. tx.Rollback()
  175. }
  176. }()
  177. }
  178. // First delete all abilities of this channel
  179. err := tx.Where("channel_id = ?", channel.Id).Delete(&Ability{}).Error
  180. if err != nil {
  181. if isNewTx {
  182. tx.Rollback()
  183. }
  184. return err
  185. }
  186. // Then add new abilities
  187. models_ := strings.Split(channel.Models, ",")
  188. groups_ := strings.Split(channel.Group, ",")
  189. abilitySet := make(map[string]struct{})
  190. abilities := make([]Ability, 0, len(models_))
  191. for _, model := range models_ {
  192. for _, group := range groups_ {
  193. key := group + "|" + model
  194. if _, exists := abilitySet[key]; exists {
  195. continue
  196. }
  197. abilitySet[key] = struct{}{}
  198. ability := Ability{
  199. Group: group,
  200. Model: model,
  201. ChannelId: channel.Id,
  202. Enabled: channel.Status == common.ChannelStatusEnabled,
  203. Priority: channel.Priority,
  204. Weight: uint(channel.GetWeight()),
  205. Tag: channel.Tag,
  206. }
  207. abilities = append(abilities, ability)
  208. }
  209. }
  210. if len(abilities) > 0 {
  211. for _, chunk := range lo.Chunk(abilities, 50) {
  212. err = tx.Clauses(clause.OnConflict{DoNothing: true}).Create(&chunk).Error
  213. if err != nil {
  214. if isNewTx {
  215. tx.Rollback()
  216. }
  217. return err
  218. }
  219. }
  220. }
  221. // 如果是新创建的事务,需要提交
  222. if isNewTx {
  223. return tx.Commit().Error
  224. }
  225. return nil
  226. }
  227. func UpdateAbilityStatus(channelId int, status bool) error {
  228. return DB.Model(&Ability{}).Where("channel_id = ?", channelId).Select("enabled").Update("enabled", status).Error
  229. }
  230. func UpdateAbilityStatusByTag(tag string, status bool) error {
  231. return DB.Model(&Ability{}).Where("tag = ?", tag).Select("enabled").Update("enabled", status).Error
  232. }
  233. func UpdateAbilityByTag(tag string, newTag *string, priority *int64, weight *uint) error {
  234. ability := Ability{}
  235. if newTag != nil {
  236. ability.Tag = newTag
  237. }
  238. if priority != nil {
  239. ability.Priority = priority
  240. }
  241. if weight != nil {
  242. ability.Weight = *weight
  243. }
  244. return DB.Model(&Ability{}).Where("tag = ?", tag).Updates(ability).Error
  245. }
  246. func FixAbility() (int, error) {
  247. var channelIds []int
  248. count := 0
  249. // Find all channel ids from channel table
  250. err := DB.Model(&Channel{}).Pluck("id", &channelIds).Error
  251. if err != nil {
  252. common.SysError(fmt.Sprintf("Get channel ids from channel table failed: %s", err.Error()))
  253. return 0, err
  254. }
  255. // Delete abilities of channels that are not in channel table - in batches to avoid too many placeholders
  256. if len(channelIds) > 0 {
  257. // Process deletion in chunks to avoid "too many placeholders" error
  258. for _, chunk := range lo.Chunk(channelIds, 100) {
  259. err = DB.Where("channel_id NOT IN (?)", chunk).Delete(&Ability{}).Error
  260. if err != nil {
  261. common.SysError(fmt.Sprintf("Delete abilities of channels (batch) that are not in channel table failed: %s", err.Error()))
  262. return 0, err
  263. }
  264. }
  265. } else {
  266. // If no channels exist, delete all abilities
  267. err = DB.Delete(&Ability{}).Error
  268. if err != nil {
  269. common.SysError(fmt.Sprintf("Delete all abilities failed: %s", err.Error()))
  270. return 0, err
  271. }
  272. common.SysLog("Delete all abilities successfully")
  273. return 0, nil
  274. }
  275. common.SysLog(fmt.Sprintf("Delete abilities of channels that are not in channel table successfully, ids: %v", channelIds))
  276. count += len(channelIds)
  277. // Use channelIds to find channel not in abilities table
  278. var abilityChannelIds []int
  279. err = DB.Table("abilities").Distinct("channel_id").Pluck("channel_id", &abilityChannelIds).Error
  280. if err != nil {
  281. common.SysError(fmt.Sprintf("Get channel ids from abilities table failed: %s", err.Error()))
  282. return count, err
  283. }
  284. var channels []Channel
  285. if len(abilityChannelIds) == 0 {
  286. err = DB.Find(&channels).Error
  287. } else {
  288. // Process query in chunks to avoid "too many placeholders" error
  289. err = nil
  290. for _, chunk := range lo.Chunk(abilityChannelIds, 100) {
  291. var channelsChunk []Channel
  292. err = DB.Where("id NOT IN (?)", chunk).Find(&channelsChunk).Error
  293. if err != nil {
  294. common.SysError(fmt.Sprintf("Find channels not in abilities table failed: %s", err.Error()))
  295. return count, err
  296. }
  297. channels = append(channels, channelsChunk...)
  298. }
  299. }
  300. for _, channel := range channels {
  301. err := channel.UpdateAbilities(nil)
  302. if err != nil {
  303. common.SysError(fmt.Sprintf("Update abilities of channel %d failed: %s", channel.Id, err.Error()))
  304. } else {
  305. common.SysLog(fmt.Sprintf("Update abilities of channel %d successfully", channel.Id))
  306. count++
  307. }
  308. }
  309. InitChannelCache()
  310. return count, nil
  311. }