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 23, 2024
1 parent 62eb35e commit 36a9604
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions pkg/decision/persisting_experiment_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ func (p PersistingExperimentService) GetDecision(decisionContext ExperimentDecis
return p.experimentBucketedService.GetDecision(decisionContext, userContext, options)
}

isUserProfileNil := decisionContext.UserProfile == nil

var userProfile UserProfile
var decisionReasons decide.DecisionReasons
// check to see if there is a saved decision for the user
Expand All @@ -66,7 +68,15 @@ func (p PersistingExperimentService) GetDecision(decisionContext ExperimentDecis
if experimentDecision.Variation != nil {
// save decision if a user profile service is provided
userProfile.ID = userContext.ID
p.saveDecision(userProfile, decisionContext, experimentDecision)
decisionKey := NewUserDecisionKey(decisionContext.Experiment.ID)
if isUserProfileNil {
p.saveDecision(userProfile, decisionKey, experimentDecision)
} else {
if decisionContext.UserProfile.ExperimentBucketMap == nil {
decisionContext.UserProfile.ExperimentBucketMap = make(map[UserDecisionKey]string)
}
decisionContext.UserProfile.ExperimentBucketMap[decisionKey] = experimentDecision.Variation.ID
}
}

return experimentDecision, reasons, err
Expand Down Expand Up @@ -102,22 +112,13 @@ 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, decisionKey UserDecisionKey, decision ExperimentDecision) {
if p.userProfileService != nil {
decisionKey := NewUserDecisionKey(decisionContext.Experiment.ID)
if userProfile.ExperimentBucketMap == nil {
userProfile.ExperimentBucketMap = map[UserDecisionKey]string{}
}
if decisionContext.UserProfile == nil {
userProfile.ExperimentBucketMap[decisionKey] = decision.Variation.ID
p.userProfileService.Save(userProfile)
} else {
if decisionContext.UserProfile.ExperimentBucketMap == nil {
decisionContext.UserProfile.ExperimentBucketMap = make(map[UserDecisionKey]string)
}
decisionContext.UserProfile.ExperimentBucketMap[decisionKey] = decision.Variation.ID
p.userProfileService.Save(*decisionContext.UserProfile)
}
userProfile.ExperimentBucketMap[decisionKey] = decision.Variation.ID
p.userProfileService.Save(userProfile)
p.logger.Debug(fmt.Sprintf(`Decision saved for user %q.`, userProfile.ID))
}
}

0 comments on commit 36a9604

Please sign in to comment.