@@ -123,7 +123,7 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
123123 }
124124
125125 if (userProfileTracker != null ) {
126- decisionVariation = getStoredVariation (experiment , userProfileTracker .userProfile , projectConfig );
126+ decisionVariation = getStoredVariation (experiment , userProfileTracker .getUserProfile () , projectConfig );
127127 reasons .merge (decisionVariation .getReasons ());
128128 variation = decisionVariation .getResult ();
129129 // return the stored variation if it exists
@@ -178,14 +178,14 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
178178 UserProfileTracker userProfileTracker = null ;
179179
180180 if (userProfileService != null && !ignoreUPS ) {
181- userProfileTracker = new UserProfileTracker (user .getUserId ());
182- userProfileTracker .loadUserProfile (reasons );
181+ userProfileTracker = new UserProfileTracker (user .getUserId (), userProfileService , logger );
182+ userProfileTracker .loadUserProfile (reasons , errorHandler );
183183 }
184184
185185 DecisionResponse <Variation > response = getVariation (experiment , user , projectConfig , options , userProfileTracker , reasons );
186186
187187 if (userProfileService != null && !ignoreUPS ) {
188- userProfileTracker .saveUserProfile ();
188+ userProfileTracker .saveUserProfile (errorHandler );
189189 }
190190 return response ;
191191 }
@@ -214,75 +214,6 @@ public DecisionResponse<FeatureDecision> getVariationForFeature(@Nonnull Feature
214214 return getVariationsForFeatureList (Arrays .asList (featureFlag ), user , projectConfig , options ).get (0 );
215215 }
216216
217- class UserProfileTracker {
218- private UserProfile userProfile ;
219- private boolean profileUpdated ;
220- private String userId ;
221-
222- UserProfileTracker (String userId ) {
223- this .userId = userId ;
224- this .profileUpdated = false ;
225- this .userProfile = null ;
226- }
227-
228- public void loadUserProfile (DecisionReasons reasons ) {
229- try {
230- Map <String , Object > userProfileMap = userProfileService .lookup (userId );
231- if (userProfileMap == null ) {
232- String message = reasons .addInfo ("We were unable to get a user profile map from the UserProfileService." );
233- logger .info (message );
234- } else if (UserProfileUtils .isValidUserProfileMap (userProfileMap )) {
235- userProfile = UserProfileUtils .convertMapToUserProfile (userProfileMap );
236- } else {
237- String message = reasons .addInfo ("The UserProfileService returned an invalid map." );
238- logger .warn (message );
239- }
240- } catch (Exception exception ) {
241- String message = reasons .addInfo (exception .getMessage ());
242- logger .error (message );
243- errorHandler .handleError (new OptimizelyRuntimeException (exception ));
244- }
245-
246- if (userProfile == null ) {
247- userProfile = new UserProfile (userId , new HashMap <String , Decision >());
248- }
249- }
250-
251- public void updateUserProfile (@ Nonnull Experiment experiment ,
252- @ Nonnull Variation variation ) {
253- String experimentId = experiment .getId ();
254- String variationId = variation .getId ();
255- Decision decision ;
256- if (userProfile .experimentBucketMap .containsKey (experimentId )) {
257- decision = userProfile .experimentBucketMap .get (experimentId );
258- decision .variationId = variationId ;
259- } else {
260- decision = new Decision (variationId );
261- }
262- userProfile .experimentBucketMap .put (experimentId , decision );
263- profileUpdated = true ;
264- logger .info ("Updated variation \" {}\" of experiment \" {}\" for user \" {}\" ." ,
265- variationId , experimentId , userProfile .userId );
266- }
267-
268- public void saveUserProfile () {
269- // if there were no updates, no need to save
270- if (!this .profileUpdated ) {
271- return ;
272- }
273-
274- try {
275- userProfileService .save (userProfile .toMap ());
276- logger .info ("Saved user profile of user \" {}\" ." ,
277- userProfile .userId );
278- } catch (Exception exception ) {
279- logger .warn ("Failed to save user profile of user \" {}\" ." ,
280- userProfile .userId );
281- errorHandler .handleError (new OptimizelyRuntimeException (exception ));
282- }
283- }
284- }
285-
286217 /**
287218 * Get the variations the user is bucketed into for the list of feature flags
288219 *
@@ -303,8 +234,8 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
303234 UserProfileTracker userProfileTracker = null ;
304235
305236 if (userProfileService != null && !ignoreUPS ) {
306- userProfileTracker = new UserProfileTracker (user .getUserId ());
307- userProfileTracker .loadUserProfile (upsReasons );
237+ userProfileTracker = new UserProfileTracker (user .getUserId (), userProfileService , logger );
238+ userProfileTracker .loadUserProfile (upsReasons , errorHandler );
308239 }
309240
310241 List <DecisionResponse <FeatureDecision >> decisions = new ArrayList <>();
@@ -340,7 +271,7 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
340271 }
341272
342273 if (userProfileService != null && !ignoreUPS ) {
343- userProfileTracker .saveUserProfile ();
274+ userProfileTracker .saveUserProfile (errorHandler );
344275 }
345276
346277 return decisions ;
0 commit comments