-
Notifications
You must be signed in to change notification settings - Fork 97
feat: support relative weighting for fractional evaluation #1313
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
toddbaert
merged 9 commits into
open-feature:main
from
bacherfl:feat/1282/fractional-evaluation-weights
Jun 27, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d048f48
feat: support relative weighting for fractional evaluation
bacherfl a9b582e
adapt docs for fractional evaluation
bacherfl 7eb74fa
additional unit test
bacherfl cf61f46
fix linting
bacherfl 46d1159
make distribution weight optional
bacherfl 48422d3
update docs and spec for fractional evaluation
bacherfl cb0807f
Update docs/reference/specifications/custom-operations/fractional-ope…
toddbaert da26955
Update docs/reference/specifications/custom-operations/fractional-ope…
toddbaert 2c5d23a
Update docs/reference/custom-operations/fractional-operation.md
toddbaert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -7,6 +7,7 @@ import ( | |
| "github.com/open-feature/flagd/core/pkg/logger" | ||
| "github.com/open-feature/flagd/core/pkg/model" | ||
| "github.com/open-feature/flagd/core/pkg/store" | ||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestFractionalEvaluation(t *testing.T) { | ||
|
|
@@ -318,7 +319,7 @@ func TestFractionalEvaluation(t *testing.T) { | |
| expectedValue: "#FF0000", | ||
| expectedReason: model.DefaultReason, | ||
| }, | ||
| "fallback to default variant if percentages don't sum to 100": { | ||
| "get variant for non-percentage weight values": { | ||
| flags: Flags{ | ||
| Flags: map[string]model.Flag{ | ||
| "headerColor": { | ||
|
|
@@ -352,7 +353,41 @@ func TestFractionalEvaluation(t *testing.T) { | |
| }, | ||
| expectedVariant: "red", | ||
| expectedValue: "#FF0000", | ||
| expectedReason: model.DefaultReason, | ||
| expectedReason: model.TargetingMatchReason, | ||
| }, | ||
| "get variant for non-specified weight values": { | ||
| 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(`{ | ||
| "fractional": [ | ||
| {"var": "email"}, | ||
| [ | ||
| "red" | ||
| ], | ||
| [ | ||
| "blue" | ||
| ] | ||
| ] | ||
| }`), | ||
| }, | ||
| }, | ||
| }, | ||
| flagKey: "headerColor", | ||
| context: map[string]any{ | ||
| "email": "[email protected]", | ||
| }, | ||
| expectedVariant: "red", | ||
| expectedValue: "#FF0000", | ||
| expectedReason: model.TargetingMatchReason, | ||
| }, | ||
| "default to targetingKey if no bucket key provided": { | ||
| flags: Flags{ | ||
|
|
@@ -579,3 +614,49 @@ func BenchmarkFractionalEvaluation(b *testing.B) { | |
| }) | ||
| } | ||
| } | ||
|
|
||
| func Test_fractionalEvaluationVariant_getPercentage(t *testing.T) { | ||
| type fields struct { | ||
| variant string | ||
| weight int | ||
| } | ||
| type args struct { | ||
| totalWeight int | ||
| } | ||
| tests := []struct { | ||
| name string | ||
| fields fields | ||
| args args | ||
| want float64 | ||
| }{ | ||
| { | ||
| name: "get percentage", | ||
| fields: fields{ | ||
| weight: 10, | ||
| }, | ||
| args: args{ | ||
| totalWeight: 20, | ||
| }, | ||
| want: 50, | ||
| }, | ||
| { | ||
| name: "total weight 0", | ||
| fields: fields{ | ||
| weight: 10, | ||
| }, | ||
| args: args{ | ||
| totalWeight: 0, | ||
| }, | ||
| want: 0, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| v := fractionalEvaluationVariant{ | ||
| variant: tt.fields.variant, | ||
| weight: tt.fields.weight, | ||
| } | ||
| assert.Equalf(t, tt.want, v.getPercentage(tt.args.totalWeight), "getPercentage(%v)", tt.args.totalWeight) | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.