Skip to content

Commit c0ce71f

Browse files
fix: match updates to java PR
1 parent 344f451 commit c0ce71f

File tree

2 files changed

+76
-98
lines changed

2 files changed

+76
-98
lines changed

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 66 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public class DecisionService
4141
public const string LOGGING_KEY_TYPE_RULE = "rule";
4242

4343
private Bucketer Bucketer;
44-
private IErrorHandler ErrorHandler;
45-
private UserProfileService UserProfileService;
44+
private static IErrorHandler ErrorHandler;
45+
private static UserProfileService UserProfileService;
4646
private static ILogger Logger;
4747

4848
/// <summary>
@@ -118,8 +118,8 @@ OptimizelyDecideOption[] options
118118

119119
if (UserProfileService != null && !ignoreUps)
120120
{
121-
var userProfile = GetUserProfile(user.GetUserId(), reasons);
122-
userProfileTracker = new UserProfileTracker(userProfile, false);
121+
userProfileTracker = new UserProfileTracker(user.GetUserId());
122+
userProfileTracker.LoadUserProfile(reasons);
123123
}
124124

125125
var response = GetVariation(experiment, user, config, options, userProfileTracker,
@@ -128,7 +128,7 @@ OptimizelyDecideOption[] options
128128
if (UserProfileService != null && !ignoreUps &&
129129
userProfileTracker?.ProfileUpdated == true)
130130
{
131-
SaveUserProfile(userProfileTracker.UserProfile);
131+
userProfileTracker.SaveUserProfile();
132132
}
133133

134134
return response;
@@ -166,7 +166,6 @@ public virtual Result<Variation> GetVariation(Experiment experiment,
166166

167167
var userId = user.GetUserId();
168168

169-
// check if a forced variation is set
170169
var decisionVariationResult = GetForcedVariation(experiment.Key, userId, config);
171170
reasons += decisionVariationResult.DecisionReasons;
172171
var variation = decisionVariationResult.ResultObject;
@@ -733,75 +732,52 @@ public virtual Result<FeatureDecision> GetVariationForFeature(FeatureFlag featur
733732
new OptimizelyDecideOption[] { });
734733
}
735734

736-
void SaveUserProfile(UserProfile userProfile)
735+
public class UserProfileTracker
737736
{
738-
if (UserProfileService == null)
739-
{
740-
return;
741-
}
737+
public UserProfile UserProfile { get; private set; }
738+
public bool ProfileUpdated { get; private set; }
739+
private string UserId { get; set; }
742740

743-
try
741+
public UserProfileTracker(string userId)
744742
{
745-
UserProfileService.Save(userProfile.ToMap());
746-
Logger.Log(LogLevel.INFO,
747-
$"Saved user profile of user \"{userProfile.UserId}\".");
743+
UserId = userId;
744+
ProfileUpdated = false;
745+
UserProfile = null;
748746
}
749-
catch (Exception exception)
747+
748+
public void LoadUserProfile(DecisionReasons reasons)
750749
{
751-
Logger.Log(LogLevel.WARN,
752-
$"Failed to save user profile of user \"{userProfile.UserId}\".");
753-
ErrorHandler.HandleError(new Exceptions.OptimizelyRuntimeException(exception.Message));
754-
}
755-
}
756-
757-
private UserProfile GetUserProfile(String userId, DecisionReasons reasons)
758-
{
759-
UserProfile userProfile = null;
760-
761-
try
762-
{
763-
var userProfileMap = UserProfileService.Lookup(userId);
764-
if (userProfileMap == null)
750+
try
765751
{
766-
Logger.Log(LogLevel.INFO,
767-
reasons.AddInfo(
768-
"We were unable to get a user profile map from the UserProfileService."));
752+
var userProfileMap = UserProfileService.Lookup(UserId);
753+
if (userProfileMap == null)
754+
{
755+
Logger.Log(LogLevel.INFO,
756+
reasons.AddInfo(
757+
"We were unable to get a user profile map from the UserProfileService."));
758+
}
759+
else if (UserProfileUtil.IsValidUserProfileMap(userProfileMap))
760+
{
761+
UserProfile = UserProfileUtil.ConvertMapToUserProfile(userProfileMap);
762+
}
763+
else
764+
{
765+
Logger.Log(LogLevel.WARN,
766+
reasons.AddInfo("The UserProfileService returned an invalid map."));
767+
}
769768
}
770-
else if (UserProfileUtil.IsValidUserProfileMap(userProfileMap))
769+
catch (Exception exception)
771770
{
772-
userProfile = UserProfileUtil.ConvertMapToUserProfile(userProfileMap);
771+
Logger.Log(LogLevel.ERROR, reasons.AddInfo(exception.Message));
772+
ErrorHandler.HandleError(
773+
new Exceptions.OptimizelyRuntimeException(exception.Message));
773774
}
774-
else
775+
776+
if (UserProfile == null)
775777
{
776-
Logger.Log(LogLevel.WARN,
777-
reasons.AddInfo("The UserProfileService returned an invalid map."));
778+
UserProfile = new UserProfile(UserId, new Dictionary<string, Decision>());
778779
}
779780
}
780-
catch (Exception exception)
781-
{
782-
Logger.Log(LogLevel.ERROR, reasons.AddInfo(exception.Message));
783-
ErrorHandler.HandleError(
784-
new Exceptions.OptimizelyRuntimeException(exception.Message));
785-
}
786-
787-
if (userProfile == null)
788-
{
789-
userProfile = new UserProfile(userId, new Dictionary<string, Decision>());
790-
}
791-
792-
return userProfile;
793-
}
794-
795-
public class UserProfileTracker
796-
{
797-
public UserProfile UserProfile { get; set; }
798-
public bool ProfileUpdated { get; set; }
799-
800-
public UserProfileTracker(UserProfile userProfile, bool profileUpdated)
801-
{
802-
UserProfile = userProfile;
803-
ProfileUpdated = profileUpdated;
804-
}
805781

806782
public void UpdateUserProfile(Experiment experiment, Variation variation)
807783
{
@@ -824,6 +800,27 @@ public void UpdateUserProfile(Experiment experiment, Variation variation)
824800
Logger.Log(LogLevel.INFO,
825801
$"Updated variation \"{variationId}\" of experiment \"{experimentId}\" for user \"{UserProfile.UserId}\".");
826802
}
803+
804+
public void SaveUserProfile()
805+
{
806+
if (!ProfileUpdated)
807+
{
808+
return;
809+
}
810+
811+
try
812+
{
813+
UserProfileService.Save(UserProfile.ToMap());
814+
Logger.Log(LogLevel.INFO,
815+
$"Saved user profile of user \"{UserProfile.UserId}\".");
816+
}
817+
catch (Exception exception)
818+
{
819+
Logger.Log(LogLevel.WARN,
820+
$"Failed to save user profile of user \"{UserProfile.UserId}\".");
821+
ErrorHandler.HandleError(new Exceptions.OptimizelyRuntimeException(exception.Message));
822+
}
823+
}
827824
}
828825

829826
public virtual List<Result<FeatureDecision>> GetVariationsForFeatureList(
@@ -836,13 +833,13 @@ OptimizelyDecideOption[] options
836833
{
837834
var upsReasons = new DecisionReasons();
838835

839-
var ignoreUPS = options.Contains(OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE);
836+
var ignoreUps = options.Contains(OptimizelyDecideOption.IGNORE_USER_PROFILE_SERVICE);
840837
UserProfileTracker userProfileTracker = null;
841838

842-
if (UserProfileService != null && !ignoreUPS)
839+
if (UserProfileService != null && !ignoreUps)
843840
{
844-
var userProfile = GetUserProfile(user.GetUserId(), upsReasons);
845-
userProfileTracker = new UserProfileTracker(userProfile, false);
841+
userProfileTracker = new UserProfileTracker(user.GetUserId());
842+
userProfileTracker.LoadUserProfile(upsReasons);
846843
}
847844

848845
var userId = user.GetUserId();
@@ -888,9 +885,9 @@ OptimizelyDecideOption[] options
888885
}
889886
}
890887

891-
if (UserProfileService != null && !ignoreUPS && userProfileTracker?.ProfileUpdated == true)
888+
if (UserProfileService != null && !ignoreUps && userProfileTracker?.ProfileUpdated == true)
892889
{
893-
SaveUserProfile(userProfileTracker.UserProfile);
890+
userProfileTracker.SaveUserProfile();
894891
}
895892

896893
return decisions;

OptimizelySDK/Optimizely.cs

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -855,31 +855,6 @@ private OptimizelyUserContext CreateUserContextCopy(string userId,
855855
);
856856
}
857857

858-
/// <summary>
859-
/// Get forced decision for the given flag key.
860-
/// </summary>
861-
/// <param name="flagKey">Flag key</param>
862-
/// <param name="decisionReasons">A collection of decision reasons</param>
863-
/// <param name="projectConfig">The project config</param>
864-
/// <param name="user">The user context</param>
865-
/// <returns>Feature decision</returns>
866-
public FeatureDecision GetForcedDecision(string flagKey, DecisionReasons decisionReasons,
867-
ProjectConfig projectConfig, OptimizelyUserContext user
868-
)
869-
{
870-
var context = new OptimizelyDecisionContext(flagKey);
871-
var forcedDecisionVariation =
872-
DecisionService.ValidatedForcedDecision(context, projectConfig, user);
873-
decisionReasons += forcedDecisionVariation.DecisionReasons;
874-
if (forcedDecisionVariation.ResultObject != null)
875-
{
876-
return new FeatureDecision(null, forcedDecisionVariation.ResultObject,
877-
FeatureDecision.DECISION_SOURCE_FEATURE_TEST);
878-
}
879-
880-
return null;
881-
}
882-
883858
/// <summary>
884859
/// Returns a decision result ({@link OptimizelyDecision}) for a given flag key and a user context, which contains all data required to deliver the flag.
885860
/// <ul>
@@ -975,12 +950,18 @@ internal Dictionary<string, OptimizelyDecision> DecideForKeys(OptimizelyUserCont
975950
validKeys.Add(key);
976951

977952
var decisionReasons = new DecisionReasons();
978-
var forcedDecision = GetForcedDecision(key, decisionReasons, projectConfig, user);
979953
decisionReasonsMap.Add(key, decisionReasons);
980-
981-
if (forcedDecision != null)
954+
955+
var optimizelyDecisionContext = new OptimizelyDecisionContext(key);
956+
var forcedDecisionVariation =
957+
DecisionService.ValidatedForcedDecision(optimizelyDecisionContext, projectConfig, user);
958+
decisionReasons += forcedDecisionVariation.DecisionReasons;
959+
960+
if (forcedDecisionVariation.ResultObject != null)
982961
{
983-
flagDecisions.Add(key, forcedDecision);
962+
flagDecisions.Add(key, new FeatureDecision(null,
963+
forcedDecisionVariation.ResultObject,
964+
FeatureDecision.DECISION_SOURCE_FEATURE_TEST));
984965
}
985966
else
986967
{

0 commit comments

Comments
 (0)