@@ -41,8 +41,8 @@ public class DecisionService
41
41
public const string LOGGING_KEY_TYPE_RULE = "rule" ;
42
42
43
43
private Bucketer Bucketer ;
44
- private IErrorHandler ErrorHandler ;
45
- private UserProfileService UserProfileService ;
44
+ private static IErrorHandler ErrorHandler ;
45
+ private static UserProfileService UserProfileService ;
46
46
private static ILogger Logger ;
47
47
48
48
/// <summary>
@@ -118,8 +118,8 @@ OptimizelyDecideOption[] options
118
118
119
119
if ( UserProfileService != null && ! ignoreUps )
120
120
{
121
- var userProfile = GetUserProfile ( user . GetUserId ( ) , reasons ) ;
122
- userProfileTracker = new UserProfileTracker ( userProfile , false ) ;
121
+ userProfileTracker = new UserProfileTracker ( user . GetUserId ( ) ) ;
122
+ userProfileTracker . LoadUserProfile ( reasons ) ;
123
123
}
124
124
125
125
var response = GetVariation ( experiment , user , config , options , userProfileTracker ,
@@ -128,7 +128,7 @@ OptimizelyDecideOption[] options
128
128
if ( UserProfileService != null && ! ignoreUps &&
129
129
userProfileTracker ? . ProfileUpdated == true )
130
130
{
131
- SaveUserProfile ( userProfileTracker . UserProfile ) ;
131
+ userProfileTracker . SaveUserProfile ( ) ;
132
132
}
133
133
134
134
return response ;
@@ -166,7 +166,6 @@ public virtual Result<Variation> GetVariation(Experiment experiment,
166
166
167
167
var userId = user . GetUserId ( ) ;
168
168
169
- // check if a forced variation is set
170
169
var decisionVariationResult = GetForcedVariation ( experiment . Key , userId , config ) ;
171
170
reasons += decisionVariationResult . DecisionReasons ;
172
171
var variation = decisionVariationResult . ResultObject ;
@@ -733,75 +732,52 @@ public virtual Result<FeatureDecision> GetVariationForFeature(FeatureFlag featur
733
732
new OptimizelyDecideOption [ ] { } ) ;
734
733
}
735
734
736
- void SaveUserProfile ( UserProfile userProfile )
735
+ public class UserProfileTracker
737
736
{
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 ; }
742
740
743
- try
741
+ public UserProfileTracker ( string userId )
744
742
{
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 ;
748
746
}
749
- catch ( Exception exception )
747
+
748
+ public void LoadUserProfile ( DecisionReasons reasons )
750
749
{
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
765
751
{
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
+ }
769
768
}
770
- else if ( UserProfileUtil . IsValidUserProfileMap ( userProfileMap ) )
769
+ catch ( Exception exception )
771
770
{
772
- userProfile = UserProfileUtil . ConvertMapToUserProfile ( userProfileMap ) ;
771
+ Logger . Log ( LogLevel . ERROR , reasons . AddInfo ( exception . Message ) ) ;
772
+ ErrorHandler . HandleError (
773
+ new Exceptions . OptimizelyRuntimeException ( exception . Message ) ) ;
773
774
}
774
- else
775
+
776
+ if ( UserProfile == null )
775
777
{
776
- Logger . Log ( LogLevel . WARN ,
777
- reasons . AddInfo ( "The UserProfileService returned an invalid map." ) ) ;
778
+ UserProfile = new UserProfile ( UserId , new Dictionary < string , Decision > ( ) ) ;
778
779
}
779
780
}
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
- }
805
781
806
782
public void UpdateUserProfile ( Experiment experiment , Variation variation )
807
783
{
@@ -824,6 +800,27 @@ public void UpdateUserProfile(Experiment experiment, Variation variation)
824
800
Logger . Log ( LogLevel . INFO ,
825
801
$ "Updated variation \" { variationId } \" of experiment \" { experimentId } \" for user \" { UserProfile . UserId } \" .") ;
826
802
}
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
+ }
827
824
}
828
825
829
826
public virtual List < Result < FeatureDecision > > GetVariationsForFeatureList (
@@ -836,13 +833,13 @@ OptimizelyDecideOption[] options
836
833
{
837
834
var upsReasons = new DecisionReasons ( ) ;
838
835
839
- var ignoreUPS = options . Contains ( OptimizelyDecideOption . IGNORE_USER_PROFILE_SERVICE ) ;
836
+ var ignoreUps = options . Contains ( OptimizelyDecideOption . IGNORE_USER_PROFILE_SERVICE ) ;
840
837
UserProfileTracker userProfileTracker = null ;
841
838
842
- if ( UserProfileService != null && ! ignoreUPS )
839
+ if ( UserProfileService != null && ! ignoreUps )
843
840
{
844
- var userProfile = GetUserProfile ( user . GetUserId ( ) , upsReasons ) ;
845
- userProfileTracker = new UserProfileTracker ( userProfile , false ) ;
841
+ userProfileTracker = new UserProfileTracker ( user . GetUserId ( ) ) ;
842
+ userProfileTracker . LoadUserProfile ( upsReasons ) ;
846
843
}
847
844
848
845
var userId = user . GetUserId ( ) ;
@@ -888,9 +885,9 @@ OptimizelyDecideOption[] options
888
885
}
889
886
}
890
887
891
- if ( UserProfileService != null && ! ignoreUPS && userProfileTracker ? . ProfileUpdated == true )
888
+ if ( UserProfileService != null && ! ignoreUps && userProfileTracker ? . ProfileUpdated == true )
892
889
{
893
- SaveUserProfile ( userProfileTracker . UserProfile ) ;
890
+ userProfileTracker . SaveUserProfile ( ) ;
894
891
}
895
892
896
893
return decisions ;
0 commit comments