@@ -20,16 +20,20 @@ package handlers
20
20
import (
21
21
"errors"
22
22
"net/http"
23
+ "strings"
23
24
24
25
"github.com/go-chi/render"
25
26
26
27
"github.com/optimizely/agent/pkg/middleware"
27
28
"github.com/optimizely/go-sdk/v2/pkg/client"
29
+ "github.com/optimizely/go-sdk/v2/pkg/config"
28
30
"github.com/optimizely/go-sdk/v2/pkg/decide"
29
31
"github.com/optimizely/go-sdk/v2/pkg/decision"
30
32
"github.com/optimizely/go-sdk/v2/pkg/odp/segment"
31
33
)
32
34
35
+ const DefaultRolloutPrefix = "default-"
36
+
33
37
// DecideBody defines the request body for decide API
34
38
type DecideBody struct {
35
39
UserID string `json:"userId"`
@@ -50,7 +54,8 @@ type ForcedDecision struct {
50
54
// DecideOut defines the response
51
55
type DecideOut struct {
52
56
client.OptimizelyDecision
53
- Variables map [string ]interface {} `json:"variables,omitempty"`
57
+ Variables map [string ]interface {} `json:"variables,omitempty"`
58
+ IsEveryoneElseVariation bool `json:"isEveryoneElseVariation"`
54
59
}
55
60
56
61
// Decide makes feature decisions for the selected query parameters
@@ -97,6 +102,12 @@ func Decide(w http.ResponseWriter, r *http.Request) {
97
102
keys = r .Form ["keys" ]
98
103
}
99
104
105
+ featureMap := make (map [string ]config.OptimizelyFeature )
106
+ cfg := optlyClient .GetOptimizelyConfig ()
107
+ if cfg != nil {
108
+ featureMap = cfg .FeaturesMap
109
+ }
110
+
100
111
var decides map [string ]client.OptimizelyDecision
101
112
switch len (keys ) {
102
113
case 0 :
@@ -107,7 +118,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {
107
118
key := keys [0 ]
108
119
logger .Debug ().Str ("featureKey" , key ).Msg ("fetching feature decision" )
109
120
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 ) }
111
122
render .JSON (w , r , decideOut )
112
123
return
113
124
default :
@@ -117,7 +128,7 @@ func Decide(w http.ResponseWriter, r *http.Request) {
117
128
118
129
decideOuts := []DecideOut {}
119
130
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 ) }
121
132
decideOuts = append (decideOuts , decideOut )
122
133
logger .Debug ().Msgf ("Feature %q is enabled for user %s? %t" , d .FlagKey , d .UserContext .UserID , d .Enabled )
123
134
}
@@ -137,3 +148,12 @@ func getUserContextWithOptions(r *http.Request) (DecideBody, error) {
137
148
138
149
return body , nil
139
150
}
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