Skip to content

Commit

Permalink
[FSSDK-10553] add IsEveryoneElseVariation marker in decide API (#422)
Browse files Browse the repository at this point in the history
* add IsEveryoneElseVariation marker in decide API

* update acceptance tests

* update unit test

* update tests

* update tests

* update tests

* update test
  • Loading branch information
pulak-opti authored Aug 23, 2024
1 parent 6c4d3f8 commit 114f7c0
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 89 deletions.
26 changes: 23 additions & 3 deletions pkg/handlers/decide.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,20 @@ package handlers
import (
"errors"
"net/http"
"strings"

"github.com/go-chi/render"

"github.com/optimizely/agent/pkg/middleware"
"github.com/optimizely/go-sdk/v2/pkg/client"
"github.com/optimizely/go-sdk/v2/pkg/config"
"github.com/optimizely/go-sdk/v2/pkg/decide"
"github.com/optimizely/go-sdk/v2/pkg/decision"
"github.com/optimizely/go-sdk/v2/pkg/odp/segment"
)

const DefaultRolloutPrefix = "default-"

// DecideBody defines the request body for decide API
type DecideBody struct {
UserID string `json:"userId"`
Expand All @@ -50,7 +54,8 @@ type ForcedDecision struct {
// DecideOut defines the response
type DecideOut struct {
client.OptimizelyDecision
Variables map[string]interface{} `json:"variables,omitempty"`
Variables map[string]interface{} `json:"variables,omitempty"`
IsEveryoneElseVariation bool `json:"isEveryoneElseVariation"`
}

// Decide makes feature decisions for the selected query parameters
Expand Down Expand Up @@ -97,6 +102,12 @@ func Decide(w http.ResponseWriter, r *http.Request) {
keys = r.Form["keys"]
}

featureMap := make(map[string]config.OptimizelyFeature)
cfg := optlyClient.GetOptimizelyConfig()
if cfg != nil {
featureMap = cfg.FeaturesMap
}

var decides map[string]client.OptimizelyDecision
switch len(keys) {
case 0:
Expand All @@ -107,7 +118,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {
key := keys[0]
logger.Debug().Str("featureKey", key).Msg("fetching feature decision")
d := optimizelyUserContext.Decide(key, decideOptions)
decideOut := DecideOut{d, d.Variables.ToMap()}
decideOut := DecideOut{d, d.Variables.ToMap(), isEveryoneElseVariation(featureMap[d.FlagKey].DeliveryRules, d.RuleKey)}
render.JSON(w, r, decideOut)
return
default:
Expand All @@ -117,7 +128,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {

decideOuts := []DecideOut{}
for _, d := range decides {
decideOut := DecideOut{d, d.Variables.ToMap()}
decideOut := DecideOut{d, d.Variables.ToMap(), isEveryoneElseVariation(featureMap[d.FlagKey].DeliveryRules, d.RuleKey)}
decideOuts = append(decideOuts, decideOut)
logger.Debug().Msgf("Feature %q is enabled for user %s? %t", d.FlagKey, d.UserContext.UserID, d.Enabled)
}
Expand All @@ -137,3 +148,12 @@ func getUserContextWithOptions(r *http.Request) (DecideBody, error) {

return body, nil
}

func isEveryoneElseVariation(rules []config.OptimizelyExperiment, ruleKey string) bool {
for _, r := range rules {
if r.Key == ruleKey {
return r.Key == r.ID && strings.HasPrefix(r.Key, DefaultRolloutPrefix)
}
}
return false
}
Loading

0 comments on commit 114f7c0

Please sign in to comment.