forked from flagship-io/flagship-common
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecision_utils.go
319 lines (284 loc) · 11.1 KB
/
decision_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package decision
import (
"errors"
"sync"
"github.com/flagship-io/flagship-common/internal/utils"
"github.com/flagship-io/flagship-common/targeting"
"github.com/flagship-io/flagship-proto/decision_response"
"google.golang.org/protobuf/types/known/structpb"
"google.golang.org/protobuf/types/known/wrapperspb"
)
type ChosenVariationResult struct {
chosenVariation *Variation
newAssignment *VisitorCache
newAssignmentAnonymous *VisitorCache
}
// isCacheEnabled is true if environments config enables it,
// and if 1vis1test or XP-C is enabled or at least one campaign as multiple variations,
func isCacheEnabled(environmentInfos Environment, variationGroups []*VariationGroup) bool {
hasMultipleVariations := false
for _, vg := range variationGroups {
if len(vg.Variations) > 1 {
hasMultipleVariations = true
break
}
}
// Enable caching if customer package allows it,
// and if 1vis1test or XP-C is enabled or at least one campaign as multiple variations,
return environmentInfos.CacheEnabled && (hasMultipleVariations || environmentInfos.SingleAssignment || environmentInfos.UseReconciliation)
}
// deduplicateCampaigns returns the first campaign that matches the campaign ID
func deduplicateCampaigns(campaigns []*Campaign) []*Campaign {
// Use ID array to sort variation groups by ID to force order of iteration
cArray := []*Campaign{}
cIDs := map[string]bool{}
for _, c := range campaigns {
if _, ok := cIDs[c.ID]; !ok {
cArray = append(cArray, c)
}
cIDs[c.ID] = true
}
return cArray
}
// getVariationGroup returns the first variationGroup that matches the visitorId and context
func getVariationGroup(variationGroups []*VariationGroup, visitorID string, context *targeting.Context) *VariationGroup {
for _, variationGroup := range variationGroups {
match, err := targetingMatch(variationGroup.Targetings, visitorID, context)
if err != nil {
logger.Logf(WarnLevel, "targeting match error variationGroupId %s, user %s: %s", variationGroup.ID, visitorID, err)
}
if match {
return variationGroup
}
}
return nil
}
// getCampaignsVG returns the variation groups that target visitor
func getCampaignsVG(campaigns []*Campaign, visitorID string, context *targeting.Context) []*VariationGroup {
campaignVG := []*VariationGroup{}
existingCampaignVG := make(map[string]bool)
for _, campaign := range campaigns {
_, ok := existingCampaignVG[campaign.ID]
if ok {
// Campaign already handled (maybe because of custom ID)
continue
}
vg := getVariationGroup(campaign.VariationGroups, visitorID, context)
if vg == nil {
continue
}
vg.Campaign = campaign
existingCampaignVG[campaign.ID] = true
campaignVG = append(campaignVG, vg)
}
return campaignVG
}
// getActivatedABVGIds returns previously assigned AB test campaigns for visitor
func getActivatedABVGIds(variationGroups []*VariationGroup, existingVar map[string]*VisitorCache) []string {
previousVisVGsAB := []string{}
alreadyAdded := map[string]bool{}
for _, vg := range variationGroups {
if vg.Campaign.Type != "ab" {
continue
}
existingVariations, ok := existingVar[vg.ID]
_, added := alreadyAdded[vg.ID]
if ok && existingVariations.Activated && !added {
previousVisVGsAB = append(previousVisVGsAB, vg.ID)
alreadyAdded[vg.ID] = true
}
}
return previousVisVGsAB
}
// shouldSkipVG returns true if the current variation should be skiped according to single assignment rule
func shouldSkipVG(environmentInfos Environment, vg *VariationGroup, previousVisVGsAB []string, hasABCampaign bool) bool {
return environmentInfos.SingleAssignment && vg.Campaign.Type == "ab" &&
(len(previousVisVGsAB) > 0 && !utils.IsInStringArray(vg.ID, previousVisVGsAB) || hasABCampaign)
}
// shouldSkipBucketVG returns true if the variation group should be skipped according to bucket allocation rule
func shouldSkipBucketVG(enableBucketAllocation bool, visitorID string, campaign *Campaign) bool {
if enableBucketAllocation {
isInBucket, err := isVisitorInBucket(visitorID, campaign)
if err != nil {
logger.Logf(WarnLevel, "error on bucket allocation for campaign %v: %v", campaign.ID, err)
}
if !isInBucket {
logger.Logf(DebugLevel, "visitor ID %s does not fall into the campaign's buckets. Skipping campaign", visitorID)
return true
}
}
return false
}
// selectNewVariation selects a variation according the visitor ID or decision group, the variation group and decision options
func selectNewVariation(visitorID string, decisionGroup string, vg *VariationGroup, options DecisionOptions) (*Variation, error) {
chosenVariationOrig, err := getRandomAllocation(visitorID, decisionGroup, vg, options.IsCumulativeAlloc)
if err != nil {
if err == VisitorNotTrackedError {
logger.Logf(InfoLevel, err.Error())
} else {
logger.Logf(WarnLevel, "error on new allocation : %v", err)
}
return nil, err
}
modificationsCopy := &decision_response.Modifications{
Type: chosenVariationOrig.Modifications.Type,
Value: &structpb.Struct{},
}
if chosenVariationOrig.Modifications.Value != nil {
modificationsCopy.Value.Fields = make(map[string]*structpb.Value)
for k, v := range chosenVariationOrig.Modifications.Value.Fields {
modificationsCopy.Value.Fields[k] = v
}
}
chosenVariation := &Variation{
ID: chosenVariationOrig.ID,
Name: chosenVariationOrig.Name,
Reference: chosenVariationOrig.Reference,
Modifications: modificationsCopy,
Allocation: chosenVariationOrig.Allocation,
}
return chosenVariation, err
}
func chooseVariation(
visitorID string,
decisionGroup string,
vg *VariationGroup,
allCacheAssignments allVisitorAssignments,
options DecisionOptions) (*ChosenVariationResult, error) {
existingAssignment, ok := allCacheAssignments.Standard.getAssignment(vg.ID)
existingAssignmentAnonymous, okAnonymous := allCacheAssignments.Anonymous.getAssignment(vg.ID)
existingAssignmentDecisionGroup, okDecisionGroup := allCacheAssignments.DecisionGroup.getAssignment(vg.ID)
var existingVariation *Variation
var existingAnonymousVariation *Variation
var existingDecisionGroupVariation *Variation
var newAssignment *VisitorCache
var newAssignmentAnonymous *VisitorCache
if ok || okAnonymous || okDecisionGroup {
for _, v := range vg.Variations {
if ok && v.ID == existingAssignment.VariationID {
existingVariation = v
}
if okAnonymous && v.ID == existingAssignmentAnonymous.VariationID {
existingAnonymousVariation = v
}
if okDecisionGroup && v.ID == existingAssignmentDecisionGroup.VariationID {
existingDecisionGroupVariation = v
}
}
// If decision group variation is found, override the cached variation
if existingDecisionGroupVariation != nil {
existingVariation = existingDecisionGroupVariation
}
// Variation has been deleted
if existingVariation == nil && existingAnonymousVariation == nil {
logger.Logf(DebugLevel, "visitor ID %s was already assigned to deleted variation ID %s", visitorID, existingAssignment.VariationID)
return nil, errors.New("visitor ID assigned to deleted variation")
}
}
var isNew, isNewAnonymous bool
var chosenVariation *Variation
var err error
// If already has variation && assigned variation ID exist, visitor should not be re-assigned
if existingVariation != nil {
logger.Logf(DebugLevel, "visitor already assigned to variation ID %s", existingVariation.ID)
chosenVariation = existingVariation
} else if existingAnonymousVariation != nil {
// If reconciliation is on, find anonymous variation as set vid to that variation ID
logger.Logf(DebugLevel, "anonymous ID already assigned to variation ID %s", existingAnonymousVariation.ID)
chosenVariation = existingAnonymousVariation
isNew = true
} else {
// Else compute new allocation
logger.Logf(DebugLevel, "assigning visitor ID to new variation")
chosenVariation, err = selectNewVariation(visitorID, decisionGroup, vg, options)
if err != nil {
return nil, err
}
logger.Logf(DebugLevel, "visitor ID %s got assigned to variation ID %s", visitorID, chosenVariation.ID)
isNew = true
isNewAnonymous = true
}
// 3.1 If allocation is newly computed and not only 1 variation,
// or if campaign activation not saved and should be
// tag this vg alloc to be saved
if options.TriggerHit && !(ok && existingAssignment.Activated) || isNew {
newAssignment = &VisitorCache{
VariationID: chosenVariation.ID,
Activated: options.TriggerHit,
}
}
// 3.1bis If anonymous allocation is newly computed and not only 1 variation,
// or if campaign activation not saved and should be
// tag this vg alloc to be saved
if options.TriggerHit && !(okAnonymous && existingAssignmentAnonymous.Activated) || isNewAnonymous {
newAssignmentAnonymous = &VisitorCache{
VariationID: chosenVariation.ID,
Activated: options.TriggerHit,
}
}
return &ChosenVariationResult{
chosenVariation: chosenVariation,
newAssignment: newAssignment,
newAssignmentAnonymous: newAssignmentAnonymous,
}, nil
}
// buildCampaignResponse creates a decision campaign response, filling out empty flag keys for each variation if needed
func buildCampaignResponse(vg *VariationGroup, variation *Variation, exposeAllKeys bool) *decision_response.Campaign {
campaignResponse := decision_response.Campaign{
Id: wrapperspb.String(vg.Campaign.ID),
Name: wrapperspb.String(vg.Campaign.Name),
VariationGroupId: wrapperspb.String(vg.ID),
VariationGroupName: wrapperspb.String(vg.Name),
}
if vg.Campaign.Slug != nil {
campaignResponse.Slug = wrapperspb.String(*vg.Campaign.Slug)
}
if exposeAllKeys {
logger.Logf(DebugLevel, "filling non existant keys in variation with null value")
if variation.Modifications == nil {
variation.Modifications = &decision_response.Modifications{}
}
if variation.Modifications.Value == nil {
variation.Modifications.Value = &structpb.Struct{}
}
if variation.Modifications.Value.Fields == nil {
variation.Modifications.Value.Fields = map[string]*structpb.Value{}
}
for _, v := range vg.Variations {
if v.Modifications != nil && v.Modifications.Value != nil && v.Modifications.Value.Fields != nil {
for key := range v.Modifications.Value.Fields {
if _, ok := variation.Modifications.Value.Fields[key]; !ok {
variation.Modifications.Value.Fields[key] = &structpb.Value{Kind: &structpb.Value_NullValue{}}
}
}
}
}
} else {
if variation.Modifications != nil && variation.Modifications.Value != nil {
var keysToDelete []string
for key, val := range variation.Modifications.Value.Fields {
_, okCast := val.GetKind().(*structpb.Value_NullValue)
if okCast {
// Collect keys to delete
keysToDelete = append(keysToDelete, key)
}
}
// Remove nil value keys if shouldFillKeys is false
var mu sync.Mutex
mu.Lock()
for _, key := range keysToDelete {
delete(variation.Modifications.Value.Fields, key)
}
mu.Unlock()
}
}
protoModif := &decision_response.Variation{
Id: wrapperspb.String(variation.ID),
Name: wrapperspb.String(variation.Name),
Modifications: variation.Modifications,
Reference: variation.Reference,
}
campaignResponse.Variation = protoModif
campaignResponse.Type = wrapperspb.String(vg.Campaign.Type)
return &campaignResponse
}