Skip to content

Commit

Permalink
Merge pull request #15 from teambition/fix/labelHit
Browse files Browse the repository at this point in the history
Fix when multiple label are hit, one is randomly selected
  • Loading branch information
zensh authored Jan 12, 2021
2 parents 7fdf905 + 081bf7c commit 20e7aaa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/model/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"context"
"fmt"
"math/rand"
"sort"
"strings"
"time"
Expand All @@ -18,6 +19,7 @@ import (

func init() {
util.DigProvide(NewModels)
rand.Seed(time.Now().UnixNano())
}

type dbMode string
Expand Down
5 changes: 5 additions & 0 deletions src/model/label_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package model
import (
"context"
"hash/crc32"
"math/rand"
"time"

"github.com/doug-martin/goqu/v9"
Expand Down Expand Up @@ -77,6 +78,10 @@ func (m *LabelRule) ComputeUserRule(ctx context.Context, userID int64, excludeLa
}

if len(ids) > 0 {
index := rand.Intn(len(ids))
ids = []interface{}{ids[index]}
labelIDs = []int64{labelIDs[index]}

sd := m.DB.Insert(schema.TableUserLabel).Cols("user_id", "label_id", "rls").
FromQuery(goqu.From(goqu.T(schema.TableLabelRule).As("t1")).
Select(goqu.V(userID), goqu.I("t1.label_id"), goqu.I("t1.id")).
Expand Down

0 comments on commit 20e7aaa

Please sign in to comment.