Skip to content

Commit

Permalink
update to provide custom seed and target key in single argument
Browse files Browse the repository at this point in the history
Signed-off-by: Cole Bailey <[email protected]>
  • Loading branch information
colebaileygit committed Apr 1, 2024
1 parent ce8faab commit 9f687dc
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 67 deletions.
13 changes: 5 additions & 8 deletions core/pkg/evaluator/fractional.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,25 +56,20 @@ func parseFractionalEvaluationData(values, data any) (string, []fractionalEvalua
if ok {
valuesArray = valuesArray[1:]
} else {
bucketBy, ok = dataMap[targetingKeyKey].(string)
targetingKey, ok := dataMap[targetingKeyKey].(string)
if !ok {
return "", nil, errors.New("bucketing value not supplied and no targetingKey in context")
}
}

seed, ok := valuesArray[0].(string)
if ok {
valuesArray = valuesArray[1:]
} else {
seed = properties.FlagKey
bucketBy = fmt.Sprintf("%s%s", properties.FlagKey, targetingKey)
}

feDistributions, err := parseFractionalEvaluationDistributions(valuesArray)
if err != nil {
return "", nil, err
}

return fmt.Sprintf("%s%s", seed, bucketBy), feDistributions, nil
return bucketBy, feDistributions, nil
}

func parseFractionalEvaluationDistributions(values []any) ([]fractionalEvaluationDistribution, error) {
Expand Down Expand Up @@ -121,6 +116,8 @@ func distributeValue(value string, feDistribution []fractionalEvaluationDistribu
hashRatio := math.Abs(float64(hashValue)) / math.MaxInt32
bucket := int(hashRatio * 100) // in range [0, 100]

fmt.Println(value, feDistribution)

rangeEnd := 0
for _, dist := range feDistribution {
rangeEnd += dist.percentage
Expand Down
59 changes: 2 additions & 57 deletions core/pkg/evaluator/fractional_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ func TestFractionalEvaluation(t *testing.T) {
},
{
"fractional": [
{"var": "email"},
{"var": "$flagd.flagKey"},
{"cat": [{"var": "$flagd.flagKey"}, {"var": "email"}]},
[
"red",
25
Expand Down Expand Up @@ -70,8 +69,7 @@ func TestFractionalEvaluation(t *testing.T) {
},
{
"fractional": [
{"var": "email"},
"my-seed",
{"cat": ["my-seed", {"var": "email"}]},
["red",25],
["blue",25],
["green",25],
Expand Down Expand Up @@ -226,59 +224,6 @@ func TestFractionalEvaluation(t *testing.T) {
expectedValue: "#FF0000",
expectedReason: model.TargetingMatchReason,
},
"[email protected] with default seed": {
flags: Flags{
Flags: map[string]model.Flag{
"headerColor": {
State: "ENABLED",
DefaultVariant: "red",
Variants: map[string]any{
"red": "#FF0000",
"blue": "#0000FF",
"green": "#00FF00",
"yellow": "#FFFF00",
},
Targeting: []byte(`{
"if": [
{
"in": ["@faas.com", {
"var": ["email"]
}]
},
{
"fractional": [
{"var": "email"},
[
"red",
25
],
[
"blue",
25
],
[
"green",
25
],
[
"yellow",
25
]
]
}, null
]
}`),
},
},
},
flagKey: "headerColor",
context: map[string]any{
"email": "[email protected]",
},
expectedVariant: "green",
expectedValue: "#00FF00",
expectedReason: model.TargetingMatchReason,
},
"non even split": {
flags: Flags{
Flags: map[string]model.Flag{
Expand Down
2 changes: 1 addition & 1 deletion schemas
2 changes: 1 addition & 1 deletion test-harness

0 comments on commit 9f687dc

Please sign in to comment.