Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: pulak-opti <[email protected]>
  • Loading branch information
pulak-opti committed Oct 22, 2024
1 parent 85f2395 commit 0b21b60
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
33 changes: 13 additions & 20 deletions pkg/client/optimizely_user_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,35 +144,18 @@ func (o *OptimizelyUserContext) DecideAll(options []decide.OptimizelyDecideOptio
userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.getForcedDecisionService(), o.GetQualifiedSegments())
decideOptions := convertDecideOptions(options)
decisionMap := o.optimizely.decideAll(userContextCopy, decideOptions)
allOptions := o.optimizely.getAllOptions(decideOptions)
enabledFlagsOnly := allOptions.EnabledFlagsOnly

filteredDecision := make(map[string]OptimizelyDecision)
for key, decision := range decisionMap {
if !enabledFlagsOnly || decision.Enabled {
filteredDecision[key] = decision
}
}
return filteredDecision
return filteredDecision(decisionMap, o.optimizely.getAllOptions(decideOptions).EnabledFlagsOnly)
}

// DecideForKeys returns a key-map of decision results for multiple flag keys and options.
func (o *OptimizelyUserContext) DecideForKeys(keys []string, options []decide.OptimizelyDecideOptions) map[string]OptimizelyDecision {
// use a copy of the user context so that any changes to the original context are not reflected inside the decision
userContextCopy := newOptimizelyUserContext(o.GetOptimizely(), o.GetUserID(), o.GetUserAttributes(), o.getForcedDecisionService(), o.GetQualifiedSegments())
decisionMap := o.optimizely.decideForKeys(userContextCopy, keys, convertDecideOptions(options))

decideOptions := convertDecideOptions(options)
allOptions := o.optimizely.getAllOptions(decideOptions)
enabledFlagsOnly := allOptions.EnabledFlagsOnly
decisionMap := o.optimizely.decideForKeys(userContextCopy, keys, decideOptions)

filteredDecision := make(map[string]OptimizelyDecision)
for key, decision := range decisionMap {
if !enabledFlagsOnly || decision.Enabled {
filteredDecision[key] = decision
}
}
return filteredDecision
return filteredDecision(decisionMap, o.optimizely.getAllOptions(decideOptions).EnabledFlagsOnly)
}

// TrackEvent generates a conversion event with the given event key if it exists and queues it up to be sent to the Optimizely
Expand Down Expand Up @@ -236,3 +219,13 @@ func copyQualifiedSegments(qualifiedSegments []string) (qualifiedSegmentsCopy []
copy(qualifiedSegmentsCopy, qualifiedSegments)
return
}

func filteredDecision(decisionMap map[string]OptimizelyDecision, enabledFlagsOnly bool) map[string]OptimizelyDecision {
filteredDecision := make(map[string]OptimizelyDecision)
for key, decision := range decisionMap {
if !enabledFlagsOnly || decision.Enabled {
filteredDecision[key] = decision
}
}
return filteredDecision
}
21 changes: 12 additions & 9 deletions pkg/decision/persisting_experiment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (p PersistingExperimentService) GetDecision(decisionContext ExperimentDecis
return p.experimentBucketedService.GetDecision(decisionContext, userContext, options)
}

var userProfile *UserProfile
var userProfile UserProfile
var decisionReasons decide.DecisionReasons
// check to see if there is a saved decision for the user
experimentDecision, userProfile, decisionReasons = p.getSavedDecision(decisionContext, userContext, options)
Expand All @@ -72,13 +72,14 @@ func (p PersistingExperimentService) GetDecision(decisionContext ExperimentDecis
return experimentDecision, reasons, err
}

func (p PersistingExperimentService) getSavedDecision(decisionContext ExperimentDecisionContext, userContext entities.UserContext, options *decide.Options) (ExperimentDecision, *UserProfile, decide.DecisionReasons) {
func (p PersistingExperimentService) getSavedDecision(decisionContext ExperimentDecisionContext, userContext entities.UserContext, options *decide.Options) (ExperimentDecision, UserProfile, decide.DecisionReasons) {
reasons := decide.NewDecisionReasons(options)
experimentDecision := ExperimentDecision{}
userProfile := decisionContext.UserProfile
if userProfile == nil {
up := p.userProfileService.Lookup(userContext.ID)
userProfile = &up
var userProfile UserProfile
if decisionContext.UserProfile == nil {
userProfile = p.userProfileService.Lookup(userContext.ID)
} else {
userProfile = *decisionContext.UserProfile
}

// look up experiment decision from user profile
Expand All @@ -101,15 +102,17 @@ func (p PersistingExperimentService) getSavedDecision(decisionContext Experiment
return experimentDecision, userProfile, reasons
}

func (p PersistingExperimentService) saveDecision(userProfile *UserProfile, decisionContext ExperimentDecisionContext, decision ExperimentDecision) {
func (p PersistingExperimentService) saveDecision(userProfile UserProfile, decisionContext ExperimentDecisionContext, decision ExperimentDecision) {
if p.userProfileService != nil {
decisionKey := NewUserDecisionKey(decisionContext.Experiment.ID)
if userProfile.ExperimentBucketMap == nil {
userProfile.ExperimentBucketMap = map[UserDecisionKey]string{}
}
userProfile.ExperimentBucketMap[decisionKey] = decision.Variation.ID
if decisionContext.UserProfile == nil {
p.userProfileService.Save(*userProfile)
userProfile.ExperimentBucketMap[decisionKey] = decision.Variation.ID
p.userProfileService.Save(userProfile)
} else {
decisionContext.UserProfile.ExperimentBucketMap[decisionKey] = decision.Variation.ID
}
p.logger.Debug(fmt.Sprintf(`Decision saved for user %q.`, userProfile.ID))
}
Expand Down

0 comments on commit 0b21b60

Please sign in to comment.