@@ -41,9 +41,9 @@ public class DecisionService
41
41
public const string LOGGING_KEY_TYPE_RULE = "rule" ;
42
42
43
43
private Bucketer Bucketer ;
44
- private static IErrorHandler ErrorHandler ;
44
+ private readonly IErrorHandler ErrorHandler ;
45
45
private static UserProfileService UserProfileService ;
46
- private static ILogger Logger ;
46
+ private readonly ILogger Logger ;
47
47
48
48
/// <summary>
49
49
/// Associative array of user IDs to an associative array
@@ -118,7 +118,8 @@ OptimizelyDecideOption[] options
118
118
119
119
if ( UserProfileService != null && ! ignoreUps )
120
120
{
121
- userProfileTracker = new UserProfileTracker ( user . GetUserId ( ) ) ;
121
+ userProfileTracker = new UserProfileTracker ( UserProfileService , user . GetUserId ( ) ,
122
+ Logger , ErrorHandler ) ;
122
123
userProfileTracker . LoadUserProfile ( reasons ) ;
123
124
}
124
125
@@ -690,7 +691,8 @@ public virtual Result<FeatureDecision> GetVariationForFeatureExperiment(
690
691
}
691
692
else
692
693
{
693
- var decisionResponse = GetVariation ( experiment , user , config , options , userProfileTracker ) ;
694
+ var decisionResponse = GetVariation ( experiment , user , config , options ,
695
+ userProfileTracker ) ;
694
696
695
697
reasons += decisionResponse ? . DecisionReasons ;
696
698
decisionVariation = decisionResponse . ResultObject ;
@@ -730,97 +732,6 @@ public virtual Result<FeatureDecision> GetVariationForFeature(FeatureFlag featur
730
732
new OptimizelyDecideOption [ ] { } ) ;
731
733
}
732
734
733
- public class UserProfileTracker
734
- {
735
- public UserProfile UserProfile { get ; private set ; }
736
- public bool ProfileUpdated { get ; private set ; }
737
- private string UserId { get ; }
738
-
739
- public UserProfileTracker ( string userId )
740
- {
741
- UserId = userId ;
742
- ProfileUpdated = false ;
743
- UserProfile = null ;
744
- }
745
-
746
- public void LoadUserProfile ( DecisionReasons reasons )
747
- {
748
- try
749
- {
750
- var userProfileMap = UserProfileService . Lookup ( UserId ) ;
751
- if ( userProfileMap == null )
752
- {
753
- Logger . Log ( LogLevel . INFO ,
754
- reasons . AddInfo (
755
- "We were unable to get a user profile map from the UserProfileService." ) ) ;
756
- }
757
- else if ( UserProfileUtil . IsValidUserProfileMap ( userProfileMap ) )
758
- {
759
- UserProfile = UserProfileUtil . ConvertMapToUserProfile ( userProfileMap ) ;
760
- }
761
- else
762
- {
763
- Logger . Log ( LogLevel . WARN ,
764
- reasons . AddInfo ( "The UserProfileService returned an invalid map." ) ) ;
765
- }
766
- }
767
- catch ( Exception exception )
768
- {
769
- Logger . Log ( LogLevel . ERROR , reasons . AddInfo ( exception . Message ) ) ;
770
- ErrorHandler . HandleError (
771
- new Exceptions . OptimizelyRuntimeException ( exception . Message ) ) ;
772
- }
773
-
774
- if ( UserProfile == null )
775
- {
776
- UserProfile = new UserProfile ( UserId , new Dictionary < string , Decision > ( ) ) ;
777
- }
778
- }
779
-
780
- public void UpdateUserProfile ( Experiment experiment , Variation variation )
781
- {
782
- var experimentId = experiment . Id ;
783
- var variationId = variation . Id ;
784
- Decision decision ;
785
- if ( UserProfile . ExperimentBucketMap . ContainsKey ( experimentId ) )
786
- {
787
- decision = UserProfile . ExperimentBucketMap [ experimentId ] ;
788
- decision . VariationId = variationId ;
789
- }
790
- else
791
- {
792
- decision = new Decision ( variationId ) ;
793
- }
794
-
795
- UserProfile . ExperimentBucketMap [ experimentId ] = decision ;
796
- ProfileUpdated = true ;
797
-
798
- Logger . Log ( LogLevel . INFO ,
799
- $ "Saved variation \" { variationId } \" of experiment \" { experimentId } \" for user \" { UserProfile . UserId } \" .") ;
800
- }
801
-
802
- public void SaveUserProfile ( )
803
- {
804
- if ( ! ProfileUpdated )
805
- {
806
- return ;
807
- }
808
-
809
- try
810
- {
811
- UserProfileService . Save ( UserProfile . ToMap ( ) ) ;
812
- Logger . Log ( LogLevel . INFO ,
813
- $ "Saved user profile of user \" { UserProfile . UserId } \" .") ;
814
- }
815
- catch ( Exception exception )
816
- {
817
- Logger . Log ( LogLevel . WARN ,
818
- $ "Failed to save user profile of user \" { UserProfile . UserId } \" .") ;
819
- ErrorHandler . HandleError ( new Exceptions . OptimizelyRuntimeException ( exception . Message ) ) ;
820
- }
821
- }
822
- }
823
-
824
735
public virtual List < Result < FeatureDecision > > GetVariationsForFeatureList (
825
736
List < FeatureFlag > featureFlags ,
826
737
OptimizelyUserContext user ,
@@ -836,7 +747,8 @@ OptimizelyDecideOption[] options
836
747
837
748
if ( UserProfileService != null && ! ignoreUps )
838
749
{
839
- userProfileTracker = new UserProfileTracker ( user . GetUserId ( ) ) ;
750
+ userProfileTracker = new UserProfileTracker ( UserProfileService , user . GetUserId ( ) ,
751
+ Logger , ErrorHandler ) ;
840
752
userProfileTracker . LoadUserProfile ( upsReasons ) ;
841
753
}
842
754
@@ -883,7 +795,8 @@ OptimizelyDecideOption[] options
883
795
}
884
796
}
885
797
886
- if ( UserProfileService != null && ! ignoreUps && userProfileTracker ? . ProfileUpdated == true )
798
+ if ( UserProfileService != null && ! ignoreUps &&
799
+ userProfileTracker ? . ProfileUpdated == true )
887
800
{
888
801
userProfileTracker . SaveUserProfile ( ) ;
889
802
}
@@ -909,10 +822,11 @@ OptimizelyDecideOption[] options
909
822
)
910
823
{
911
824
return GetVariationsForFeatureList ( new List < FeatureFlag > { featureFlag } ,
912
- user ,
913
- config ,
914
- filteredAttributes ,
915
- options ) . First ( ) ;
825
+ user ,
826
+ config ,
827
+ filteredAttributes ,
828
+ options ) .
829
+ First ( ) ;
916
830
}
917
831
918
832
/// <summary>
0 commit comments