Skip to content

Commit

Permalink
Merge pull request #9 from teambition/feature/multiGray
Browse files Browse the repository at this point in the history
support label in label
  • Loading branch information
zensh authored Sep 16, 2020
2 parents fdf735e + 01fd7bf commit c163a1a
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
70 changes: 70 additions & 0 deletions src/bll/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,73 @@ func TestUsers(t *testing.T) {
assert.Nil(err)
})
}

func TestChildLabelUserPercen(t *testing.T) {

user := &User{ms: model.NewModels(service.NewDB())}
product := &Product{ms: model.NewModels(service.NewDB())}

t.Run("childLabelUserPercen should work with apply rule", func(t *testing.T) {
assert := assert.New(t)
ctx := context.Background()

uid1 := tpl.RandUID()
user.BatchAdd(ctx, []string{uid1})
userObj, err := user.ms.User.Acquire(ctx, uid1)
assert.Nil(err)

productName := tpl.RandName()
productRes, err := product.Create(ctx, productName, productName)

label := &schema.Label{
ProductID: productRes.Result.ID,
Name: tpl.RandName(),
}
err = user.ms.Label.Create(ctx, label)
assert.Nil(err)
labelRes, err := user.ms.Label.Acquire(ctx, productRes.Result.ID, label.Name)
assert.Nil(err)

labelRule := &schema.LabelRule{
ProductID: productRes.Result.ID,
LabelID: labelRes.ID,
Kind: schema.RuleUserPercent,
Rule: `{"value": 100 }`,
}
assert.Equal(100, labelRule.ToPercent())
err = user.ms.LabelRule.Create(ctx, labelRule)
assert.Nil(err)

label2 := &schema.Label{
ProductID: productRes.Result.ID,
Name: labelRes.Name + "-gray",
}
err = user.ms.Label.Create(ctx, label2)
assert.Nil(err)
labelRes2, err := user.ms.Label.Acquire(ctx, productRes.Result.ID, label2.Name)
assert.Nil(err)

labelRule2 := &schema.LabelRule{
ProductID: productRes.Result.ID,
LabelID: labelRes2.ID,
Kind: schema.RuleChildLabelUserPercent,
Rule: `{"value": 100 }`,
}
assert.Equal(100, labelRule.ToPercent())
err = user.ms.LabelRule.Create(ctx, labelRule2)
assert.Nil(err)

userRes, err := user.ms.ApplyLabelRulesAndRefreshUserLabels(ctx, productRes.Result.ID, productName, userObj.ID, time.Now().UTC(), true)
assert.Nil(err)
userLabels := userRes.GetLabels(productName)
assert.True(len(userLabels) == 1)
assert.Equal(labelRes.Name, userLabels[0].Label)

userRes, err = user.ms.ApplyLabelRulesAndRefreshUserLabels(ctx, productRes.Result.ID, productName, userObj.ID, time.Now().UTC(), true)
assert.Nil(err)
userLabels = userRes.GetLabels(productName)
assert.True(len(userLabels) == 2)
assert.Equal(labelRes2.Name, userLabels[0].Label)
assert.Equal(labelRes.Name, userLabels[1].Label)
})
}
26 changes: 26 additions & 0 deletions src/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@ package model
import (
"context"
"fmt"
"sort"
"strings"
"time"

"github.com/doug-martin/goqu/v9"
"github.com/teambition/gear"
"github.com/teambition/urbs-setting/src/logging"
"github.com/teambition/urbs-setting/src/schema"
"github.com/teambition/urbs-setting/src/service"
"github.com/teambition/urbs-setting/src/tpl"
"github.com/teambition/urbs-setting/src/util"
)

Expand Down Expand Up @@ -77,6 +80,29 @@ func (ms *Models) ApplyLabelRulesAndRefreshUserLabels(ctx context.Context, produ
// refresh label again
user, labelIDs, ok, err = ms.User.RefreshLabels(ctx, userID, now.Unix(), true)
}
} else if len(userProductLables) > 0 {
pg := tpl.Pagination{PageSize: 200}
pg.Q = userProductLables[0].Label + "%"
labels, _, err := ms.Label.Find(ctx, productID, pg)
if err != nil {
return nil, err
}
sort.SliceStable(labels, func(i, j int) bool {
return len(labels[i].Name) < len(labels[j].Name)
})
for _, item := range labels {
if !strings.HasPrefix(item.Name, userProductLables[0].Label+"-") {
continue
}
hit, err := ms.LabelRule.ApplyRule(ctx, productID, userID, item.ID, schema.RuleChildLabelUserPercent)
if err != nil {
return nil, err
}
if hit > 0 {
user, labelIDs, ok, err = ms.User.RefreshLabels(ctx, userID, now.Unix(), true)
}
break
}
}

if elapsed := time.Now().UTC().Sub(now) / time.Millisecond; elapsed > 200 {
Expand Down
31 changes: 30 additions & 1 deletion src/model/label_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ type LabelRule struct {
// ApplyRules ...
func (m *LabelRule) ApplyRules(ctx context.Context, productID int64, userID int64, excludeLabels []int64, kind string) (int, error) {
rules := []schema.LabelRule{}
// 不把 excludeLabels 放入查询条件,从而尽量复用查询缓存
exps := []exp.Expression{goqu.C("kind").Eq(kind)}
if productID > 0 {
exps = append(exps, goqu.C("product_id").Eq(productID))
Expand All @@ -31,6 +30,36 @@ func (m *LabelRule) ApplyRules(ctx context.Context, productID int64, userID int6
if err != nil {
return 0, err
}
// 不把 excludeLabels 放入查询条件,从而尽量复用查询缓存
res, err := m.ComputeUserRule(ctx, userID, excludeLabels, rules)
if err != nil {
return 0, err
}
return res, nil
}

// ApplyRule ...
func (m *LabelRule) ApplyRule(ctx context.Context, productID int64, userID int64, labelID int64, kind string) (int, error) {
rules := []schema.LabelRule{}
exps := []exp.Expression{
goqu.C("kind").Eq(kind),
goqu.C("label_id").Eq(labelID),
goqu.C("product_id").Eq(productID),
}
sd := m.RdDB.From(schema.TableLabelRule).Where(exps...).Order(goqu.C("updated_at").Desc()).Limit(200)
err := sd.Executor().ScanStructsContext(ctx, &rules)
if err != nil {
return 0, err
}
res, err := m.ComputeUserRule(ctx, userID, []int64{}, rules)
if err != nil {
return 0, err
}
return res, nil
}

// ComputeUserRule ...
func (m *LabelRule) ComputeUserRule(ctx context.Context, userID int64, excludeLabels []int64, rules []schema.LabelRule) (int, error) {
ids := make([]interface{}, 0)
labelIDs := make([]int64, 0)
for _, rule := range rules {
Expand Down
4 changes: 3 additions & 1 deletion src/schema/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ const (
RuleUserPercent = "userPercent"
// RuleNewUserPercent ...
RuleNewUserPercent = "newUserPercent"
// RuleChildLabelUserPercent parent-child relationship label
RuleChildLabelUserPercent = "childLabelUserPercent"
)

var (
// RuleKinds ...
RuleKinds = []string{RuleUserPercent, RuleNewUserPercent}
RuleKinds = []string{RuleUserPercent, RuleNewUserPercent, RuleChildLabelUserPercent}
)

// PercentRule ...
Expand Down

0 comments on commit c163a1a

Please sign in to comment.