@@ -123,7 +123,7 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
123
123
}
124
124
125
125
if (userProfileTracker != null ) {
126
- decisionVariation = getStoredVariation (experiment , userProfileTracker .userProfile , projectConfig );
126
+ decisionVariation = getStoredVariation (experiment , userProfileTracker .getUserProfile () , projectConfig );
127
127
reasons .merge (decisionVariation .getReasons ());
128
128
variation = decisionVariation .getResult ();
129
129
// return the stored variation if it exists
@@ -178,14 +178,14 @@ public DecisionResponse<Variation> getVariation(@Nonnull Experiment experiment,
178
178
UserProfileTracker userProfileTracker = null ;
179
179
180
180
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 );
183
183
}
184
184
185
185
DecisionResponse <Variation > response = getVariation (experiment , user , projectConfig , options , userProfileTracker , reasons );
186
186
187
187
if (userProfileService != null && !ignoreUPS ) {
188
- userProfileTracker .saveUserProfile ();
188
+ userProfileTracker .saveUserProfile (errorHandler );
189
189
}
190
190
return response ;
191
191
}
@@ -214,75 +214,6 @@ public DecisionResponse<FeatureDecision> getVariationForFeature(@Nonnull Feature
214
214
return getVariationsForFeatureList (Arrays .asList (featureFlag ), user , projectConfig , options ).get (0 );
215
215
}
216
216
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
-
286
217
/**
287
218
* Get the variations the user is bucketed into for the list of feature flags
288
219
*
@@ -303,8 +234,8 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
303
234
UserProfileTracker userProfileTracker = null ;
304
235
305
236
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 );
308
239
}
309
240
310
241
List <DecisionResponse <FeatureDecision >> decisions = new ArrayList <>();
@@ -340,7 +271,7 @@ public List<DecisionResponse<FeatureDecision>> getVariationsForFeatureList(@Non
340
271
}
341
272
342
273
if (userProfileService != null && !ignoreUPS ) {
343
- userProfileTracker .saveUserProfile ();
274
+ userProfileTracker .saveUserProfile (errorHandler );
344
275
}
345
276
346
277
return decisions ;
0 commit comments