@@ -20,16 +20,20 @@ package handlers
2020import (
2121 "errors"
2222 "net/http"
23+ "strings"
2324
2425 "github.com/go-chi/render"
2526
2627 "github.com/optimizely/agent/pkg/middleware"
2728 "github.com/optimizely/go-sdk/v2/pkg/client"
29+ "github.com/optimizely/go-sdk/v2/pkg/config"
2830 "github.com/optimizely/go-sdk/v2/pkg/decide"
2931 "github.com/optimizely/go-sdk/v2/pkg/decision"
3032 "github.com/optimizely/go-sdk/v2/pkg/odp/segment"
3133)
3234
35+ const DefaultRolloutPrefix = "default-"
36+
3337// DecideBody defines the request body for decide API
3438type DecideBody struct {
3539 UserID string `json:"userId"`
@@ -50,7 +54,8 @@ type ForcedDecision struct {
5054// DecideOut defines the response
5155type DecideOut struct {
5256 client.OptimizelyDecision
53- Variables map [string ]interface {} `json:"variables,omitempty"`
57+ Variables map [string ]interface {} `json:"variables,omitempty"`
58+ IsEveryoneElseVariation bool `json:"isEveryoneElseVariation"`
5459}
5560
5661// Decide makes feature decisions for the selected query parameters
@@ -97,6 +102,12 @@ func Decide(w http.ResponseWriter, r *http.Request) {
97102 keys = r .Form ["keys" ]
98103 }
99104
105+ featureMap := make (map [string ]config.OptimizelyFeature )
106+ cfg := optlyClient .GetOptimizelyConfig ()
107+ if cfg != nil {
108+ featureMap = cfg .FeaturesMap
109+ }
110+
100111 var decides map [string ]client.OptimizelyDecision
101112 switch len (keys ) {
102113 case 0 :
@@ -107,7 +118,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {
107118 key := keys [0 ]
108119 logger .Debug ().Str ("featureKey" , key ).Msg ("fetching feature decision" )
109120 d := optimizelyUserContext .Decide (key , decideOptions )
110- decideOut := DecideOut {d , d .Variables .ToMap ()}
121+ decideOut := DecideOut {d , d .Variables .ToMap (), isEveryoneElseVariation ( featureMap [ d . FlagKey ]. DeliveryRules , d . RuleKey ) }
111122 render .JSON (w , r , decideOut )
112123 return
113124 default :
@@ -117,7 +128,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {
117128
118129 decideOuts := []DecideOut {}
119130 for _ , d := range decides {
120- decideOut := DecideOut {d , d .Variables .ToMap ()}
131+ decideOut := DecideOut {d , d .Variables .ToMap (), isEveryoneElseVariation ( featureMap [ d . FlagKey ]. DeliveryRules , d . RuleKey ) }
121132 decideOuts = append (decideOuts , decideOut )
122133 logger .Debug ().Msgf ("Feature %q is enabled for user %s? %t" , d .FlagKey , d .UserContext .UserID , d .Enabled )
123134 }
@@ -137,3 +148,12 @@ func getUserContextWithOptions(r *http.Request) (DecideBody, error) {
137148
138149 return body , nil
139150}
151+
152+ func isEveryoneElseVariation (rules []config.OptimizelyExperiment , ruleKey string ) bool {
153+ for _ , r := range rules {
154+ if r .Key == ruleKey {
155+ return r .Key == r .ID && strings .HasPrefix (r .Key , DefaultRolloutPrefix )
156+ }
157+ }
158+ return false
159+ }
0 commit comments