Skip to content
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

[FSSDK-10553] add IsEveryoneElseVariation marker in decide API #422

Merged
merged 8 commits into from
Aug 23, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 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-rollout-"

// 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,17 @@ func Decide(w http.ResponseWriter, r *http.Request) {
keys = r.Form["keys"]
}

cfg := optlyClient.GetOptimizelyConfig()
if cfg == nil {
RenderError(errors.New("optimizely config not found"), http.StatusInternalServerError, w, r)
return
}
featureMap := cfg.FeaturesMap
if featureMap == nil {
RenderError(errors.New("features not found in config"), http.StatusInternalServerError, w, r)
return
}

var decides map[string]client.OptimizelyDecision
switch len(keys) {
case 0:
Expand All @@ -107,7 +123,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 +133,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 +153,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
Loading