Kaynağa Gözat

refactor(group): update user group handling to utilize userUsableGroups directly and add GetUserGroupRatio function

CaIon 2 ay önce
ebeveyn
işleme
52d9b8cc78
4 değiştirilmiş dosya ile 15 ekleme ve 3 silme
  1. 2 2
      controller/group.go
  2. 1 1
      controller/playground.go
  3. 1 0
      middleware/distributor.go
  4. 11 0
      service/group.go

+ 2 - 2
controller/group.go

@@ -29,11 +29,11 @@ func GetUserGroups(c *gin.Context) {
 	userId := c.GetInt("id")
 	userGroup, _ = model.GetUserGroup(userId, false)
 	userUsableGroups := service.GetUserUsableGroups(userGroup)
-	for groupName, ratio := range ratio_setting.GetGroupRatioCopy() {
+	for groupName, _ := range ratio_setting.GetGroupRatioCopy() {
 		// UserUsableGroups contains the groups that the user can use
 		if desc, ok := userUsableGroups[groupName]; ok {
 			usableGroups[groupName] = map[string]interface{}{
-				"ratio": ratio,
+				"ratio": service.GetUserGroupRatio(userGroup, groupName),
 				"desc":  desc,
 			}
 		}

+ 1 - 1
controller/playground.go

@@ -31,7 +31,7 @@ func Playground(c *gin.Context) {
 		return
 	}
 
-	group := c.GetString("group")
+	group := common.GetContextKeyString(c, constant.ContextKeyUsingGroup)
 	modelName := c.GetString("original_model")
 
 	userId := c.GetInt("id")

+ 1 - 0
middleware/distributor.go

@@ -94,6 +94,7 @@ func Distribute() func(c *gin.Context) {
 							return
 						}
 						usingGroup = playgroundRequest.Group
+						common.SetContextKey(c, constant.ContextKeyUsingGroup, usingGroup)
 					}
 				}
 				channel, selectGroup, err = service.CacheGetRandomSatisfiedChannel(c, usingGroup, modelRequest.Model, 0)

+ 11 - 0
service/group.go

@@ -52,3 +52,14 @@ func GetUserAutoGroup(userGroup string) []string {
 	}
 	return autoGroups
 }
+
+// GetUserGroupRatio 获取用户使用某个分组的倍率
+// userGroup 用户分组
+// group 需要获取倍率的分组
+func GetUserGroupRatio(userGroup, group string) float64 {
+	ratio, ok := ratio_setting.GetGroupGroupRatio(userGroup, group)
+	if ok {
+		return ratio
+	}
+	return ratio_setting.GetGroupRatio(group)
+}