ability.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. package model
  2. import (
  3. "one-api/common"
  4. "strings"
  5. )
  6. type Ability struct {
  7. Group string `json:"group" gorm:"type:varchar(255);primaryKey;autoIncrement:false"`
  8. Model string `json:"model" gorm:"primaryKey;autoIncrement:false"`
  9. ChannelId int `json:"channel_id" gorm:"primaryKey;autoIncrement:false;index"`
  10. Enabled bool `json:"enabled"`
  11. Priority *int64 `json:"priority" gorm:"bigint;default:0;index"`
  12. Weight uint `json:"weight" gorm:"default:0;index"`
  13. }
  14. func GetGroupModels(group string) []string {
  15. var models []string
  16. // Find distinct models
  17. groupCol := "`group`"
  18. if common.UsingPostgreSQL {
  19. groupCol = `"group"`
  20. }
  21. DB.Table("abilities").Where(groupCol+" = ? and enabled = ?", group, true).Distinct("model").Pluck("model", &models)
  22. return models
  23. }
  24. func GetRandomSatisfiedChannel(group string, model string) (*Channel, error) {
  25. var abilities []Ability
  26. groupCol := "`group`"
  27. trueVal := "1"
  28. if common.UsingPostgreSQL {
  29. groupCol = `"group"`
  30. trueVal = "true"
  31. }
  32. var err error = nil
  33. maxPrioritySubQuery := DB.Model(&Ability{}).Select("MAX(priority)").Where(groupCol+" = ? and model = ? and enabled = "+trueVal, group, model)
  34. channelQuery := DB.Where(groupCol+" = ? and model = ? and enabled = "+trueVal+" and priority = (?)", group, model, maxPrioritySubQuery)
  35. if common.UsingSQLite || common.UsingPostgreSQL {
  36. err = channelQuery.Order("weight DESC").Find(&abilities).Error
  37. } else {
  38. err = channelQuery.Order("weight DESC").Find(&abilities).Error
  39. }
  40. if err != nil {
  41. return nil, err
  42. }
  43. channel := Channel{}
  44. if len(abilities) > 0 {
  45. // Randomly choose one
  46. weightSum := uint(0)
  47. for _, ability_ := range abilities {
  48. weightSum += ability_.Weight
  49. }
  50. if weightSum == 0 {
  51. // All weight is 0, randomly choose one
  52. channel.Id = abilities[common.GetRandomInt(len(abilities))].ChannelId
  53. } else {
  54. // Randomly choose one
  55. weight := common.GetRandomInt(int(weightSum))
  56. for _, ability_ := range abilities {
  57. weight -= int(ability_.Weight)
  58. //log.Printf("weight: %d, ability weight: %d", weight, *ability_.Weight)
  59. if weight <= 0 {
  60. channel.Id = ability_.ChannelId
  61. break
  62. }
  63. }
  64. }
  65. } else {
  66. return nil, nil
  67. }
  68. err = DB.First(&channel, "id = ?", channel.Id).Error
  69. return &channel, err
  70. }
  71. func (channel *Channel) AddAbilities() error {
  72. models_ := strings.Split(channel.Models, ",")
  73. groups_ := strings.Split(channel.Group, ",")
  74. abilities := make([]Ability, 0, len(models_))
  75. for _, model := range models_ {
  76. for _, group := range groups_ {
  77. ability := Ability{
  78. Group: group,
  79. Model: model,
  80. ChannelId: channel.Id,
  81. Enabled: channel.Status == common.ChannelStatusEnabled,
  82. Priority: channel.Priority,
  83. Weight: uint(channel.GetWeight()),
  84. }
  85. abilities = append(abilities, ability)
  86. }
  87. }
  88. return DB.Create(&abilities).Error
  89. }
  90. func (channel *Channel) DeleteAbilities() error {
  91. return DB.Where("channel_id = ?", channel.Id).Delete(&Ability{}).Error
  92. }
  93. // UpdateAbilities updates abilities of this channel.
  94. // Make sure the channel is completed before calling this function.
  95. func (channel *Channel) UpdateAbilities() error {
  96. // A quick and dirty way to update abilities
  97. // First delete all abilities of this channel
  98. err := channel.DeleteAbilities()
  99. if err != nil {
  100. return err
  101. }
  102. // Then add new abilities
  103. err = channel.AddAbilities()
  104. if err != nil {
  105. return err
  106. }
  107. return nil
  108. }
  109. func UpdateAbilityStatus(channelId int, status bool) error {
  110. return DB.Model(&Ability{}).Where("channel_id = ?", channelId).Select("enabled").Update("enabled", status).Error
  111. }