Skip to content

Commit

Permalink
fix: create new v2 test config. refactor function to take targetBucke…
Browse files Browse the repository at this point in the history
…tingKey as the first param
  • Loading branch information
kaushalkapasi committed Aug 16, 2024
1 parent 49b2110 commit dd0e778
Show file tree
Hide file tree
Showing 6 changed files with 795 additions and 158 deletions.
6 changes: 3 additions & 3 deletions api/model_public_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ type Project struct {
}

type ProjectSettings struct {
EdgeDB EdgeDBSettings `json:"edgeDB"`
OptIn OptInSettings `json:"optIn"`
DisablePassthroughRollouts bool `json:"disablePassthroughRollouts"`
EdgeDB EdgeDBSettings `json:"edgeDB"`
OptIn OptInSettings `json:"optIn"`
DisablePassthroughRollouts bool `json:"disablePassthroughRollouts"`
}

type EdgeDBSettings struct {
Expand Down
14 changes: 7 additions & 7 deletions bucketing/bucketing.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type boundedHash struct {
BucketingHash float64 `json:"bucketingHash"`
}

func generateBoundedHashes(bucketingKeyValue string, targetId string) boundedHash {
func generateBoundedHashes(bucketingKeyValue, targetId string) boundedHash {
var targetHash = murmurhashV3(targetId, baseSeed)
var bhash = boundedHash{
RolloutHash: generateBoundedHash(bucketingKeyValue+"_rollout", targetHash),
Expand All @@ -43,16 +43,16 @@ func generateBoundedHash(input string, hashSeed uint32) float64 {
return float64(mh) / float64(maxHashValue)
}

func determineUserBucketingValue(userId string, mergedCustomData map[string]interface{}, targetBucketingKey string) string {
if (targetBucketingKey == "" || targetBucketingKey == "user_id") {
func determineUserBucketingValueForTarget(targetBucketingKey, userId string, mergedCustomData map[string]interface{}) string {
if targetBucketingKey == "" || targetBucketingKey == "user_id" {
return userId
}

if customDataValue, keyExists := mergedCustomData[targetBucketingKey]; keyExists {
if (customDataValue == nil) {
if customDataValue == nil {
return defaultBucketingValue
}

switch v := customDataValue.(type) {
case int:
return strconv.Itoa(v)
Expand Down Expand Up @@ -137,7 +137,7 @@ func evaluateSegmentationForFeature(config *configBody, feature *ConfigFeature,
passthroughEnabled := !config.Project.Settings.DisablePassthroughRollouts
doesUserPassthrough := true
if target.Rollout != nil && passthroughEnabled {
var bucketingValue = determineUserBucketingValue(user.UserId, mergedCustomData, target.BucketingKey)
var bucketingValue = determineUserBucketingValueForTarget(target.BucketingKey, user.UserId, mergedCustomData)

boundedHash := generateBoundedHashes(bucketingValue, target.Id)
rolloutHash := boundedHash.RolloutHash
Expand All @@ -163,7 +163,7 @@ func doesUserQualifyForFeature(config *configBody, feature *ConfigFeature, user
}

var mergedCustomData = user.CombinedCustomData()
var bucketingValue = determineUserBucketingValue(user.UserId, mergedCustomData, target.BucketingKey)
var bucketingValue = determineUserBucketingValueForTarget(target.BucketingKey, user.UserId, mergedCustomData)

boundedHashes := generateBoundedHashes(bucketingValue, target.Id)
rolloutHash := boundedHashes.RolloutHash
Expand Down
17 changes: 10 additions & 7 deletions bucketing/bucketing_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var (
//go:embed testdata/fixture_test_config.json
test_config []byte

//go:embed testdata/fixture_test_v2_config.json
test_v2_config []byte

//go:embed testdata/fixture_test_broken_config.json
test_broken_config []byte

Expand Down Expand Up @@ -746,13 +749,13 @@ func TestBucketing_Deterministic_AlternateKeyRandomDistribution(t *testing.T) {
user4 := api.User{
UserId: "client_test_3",
CustomData: map[string]interface{}{
"favouriteNull": nil,
"favouriteNull": nil,
},
}.GetPopulatedUser(&api.PlatformData{
PlatformVersion: "1.1.2",
})

err := SetConfig(test_config, "test", "", "", "")
err := SetConfig(test_v2_config, "test", "", "", "")
require.NoError(t, err)

// Check if users with the same alternate bucketing key get the same variation
Expand All @@ -777,7 +780,7 @@ func TestBucketing_Deterministic_AlternateKeyRollout(t *testing.T) {
CustomData: map[string]interface{}{
"favouriteFood": "cake",
"favouriteNull": nil,
"numericId": 123,
"numericId": 123,
},
}.GetPopulatedUser(&api.PlatformData{
PlatformVersion: "1.1.2",
Expand All @@ -787,7 +790,7 @@ func TestBucketing_Deterministic_AlternateKeyRollout(t *testing.T) {
CustomData: map[string]interface{}{
"favouriteFood": "cake",
"favouriteNull": nil,
"numericId": 123,
"numericId": 123,
},
}.GetPopulatedUser(&api.PlatformData{
PlatformVersion: "1.1.2",
Expand All @@ -797,21 +800,21 @@ func TestBucketing_Deterministic_AlternateKeyRollout(t *testing.T) {
CustomData: map[string]interface{}{
"favouriteFood": nil,
"favouriteNull": nil,
"numericId": nil,
"numericId": nil,
},
}.GetPopulatedUser(&api.PlatformData{
PlatformVersion: "1.1.2",
})
user4 := api.User{
UserId: "client_test_3",
CustomData: map[string]interface{}{
"favouriteNull": nil,
"favouriteNull": nil,
},
}.GetPopulatedUser(&api.PlatformData{
PlatformVersion: "1.1.2",
})

err := SetConfig(test_config, "test", "", "", "")
err := SetConfig(test_v2_config, "test", "", "", "")
require.NoError(t, err)

// Check if users with the same alternate bucketing key get the same variation
Expand Down
2 changes: 1 addition & 1 deletion bucketing/model_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Target struct {
Audience *Audience `json:"_audience"`
Rollout *Rollout `json:"rollout"`
Distribution []TargetDistribution `json:"distribution"`
BucketingKey string `json:"bucketingKey"`
BucketingKey string `json:"bucketingKey"`
}

func (t *Target) DecideTargetVariation(boundedHash float64) (string, error) {
Expand Down
140 changes: 0 additions & 140 deletions bucketing/testdata/fixture_test_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -570,136 +570,6 @@
]
}
]
},
{
"_id": "614ef8aa475928459060721d",
"type": "experiment",
"key": "header-copy",
"configuration": {
"_id": "61536f62502d80fff97ed641",
"targets": [
{
"_id": "61536f468fd67f0091982532",
"_audience": {
"filters": {
"filters": [
{
"type": "all",
"subType": "",
"comparator": "",
"values": []
}
],
"operator": "and"
}
},
"distribution": [
{
"_variation": "615382338424cb11646d9669",
"percentage": 0.5
},
{
"_variation": "615382338424cb11646d9670",
"percentage": 0.5
}
],
"bucketingKey": "favouriteFood"
}
]
},
"variations": [
{
"_id": "615382338424cb11646d9669",
"name": "New Copy",
"key": "new-copy",
"variables": [
{
"_var": "61538937b0a70b58ae6af71x",
"value": "New!"
}
]
},
{
"_id": "615382338424cb11646d9670",
"name": "Old Copy",
"key": "old-copy",
"variables": [
{
"_var": "61538937b0a70b58ae6af71x",
"value": "default header"
}
]
}
]
},
{
"_id": "614ef8aa475928459060721e",
"type": "permission",
"key": "feature_access",
"configuration": {
"_id": "61536f62502d80fff97ed642",
"targets": [
{
"_id": "61536f468fd67f0091982533",
"_audience": {
"filters": {
"filters": [
{
"type": "all",
"subType": "",
"comparator": "",
"values": []
}
],
"operator": "and"
}
},
"distribution": [
{
"_variation": "615382338424cb11646d9671",
"percentage": 1
}
],
"rollout": {
"type": "gradual",
"startPercentage": 0,
"startDate": "2024-04-05T17:58:32.318Z",
"stages": [
{
"type": "linear",
"percentage": 1,
"date": "2034-04-07T17:58:32.319Z"
}
]
},
"bucketingKey": "numericId"
}
]
},
"variations": [
{
"_id": "615382338424cb11646d9671",
"name": "Has Access",
"key": "has-access",
"variables": [
{
"_var": "61538937b0a70b58ae6af71g",
"value": true
}
]
},
{
"_id": "615382338424cb11646d9672",
"name": "No Access",
"key": "no-access",
"variables": [
{
"_var": "61538937b0a70b58ae6af71g",
"value": false
}
]
}
]
}
],
"variables": [
Expand Down Expand Up @@ -752,16 +622,6 @@
"_id": "61538937b0a70b58ae6af71f",
"type": "String",
"key": "feature4Var"
},
{
"_id": "61538937b0a70b58ae6af71x",
"type": "String",
"key": "experiment_var"
},
{
"_id": "61538937b0a70b58ae6af71g",
"type": "Boolean",
"key": "new_feature"
}
],
"variableHashes": {
Expand Down
Loading

0 comments on commit dd0e778

Please sign in to comment.