Skip to content

Commit 3181043

Browse files
committed
move class
1 parent 36f3d6f commit 3181043

File tree

2 files changed

+116
-76
lines changed

2 files changed

+116
-76
lines changed

core-api/src/main/java/com/optimizely/ab/bucketing/DecisionService.java

+7-76
Original file line numberDiff line numberDiff line change
@@ -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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/****************************************************************************
2+
* Copyright 2017-2022, 2024, Optimizely, Inc. and contributors *
3+
* *
4+
* Licensed under the Apache License, Version 2.0 (the "License"); *
5+
* you may not use this file except in compliance with the License. *
6+
* You may obtain a copy of the License at *
7+
* *
8+
* http://www.apache.org/licenses/LICENSE-2.0 *
9+
* *
10+
* Unless required by applicable law or agreed to in writing, software *
11+
* distributed under the License is distributed on an "AS IS" BASIS, *
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13+
* See the License for the specific language governing permissions and *
14+
* limitations under the License. *
15+
***************************************************************************/
16+
package com.optimizely.ab.bucketing;
17+
18+
import com.optimizely.ab.OptimizelyRuntimeException;
19+
import com.optimizely.ab.config.Experiment;
20+
import com.optimizely.ab.config.Variation;
21+
import com.optimizely.ab.error.ErrorHandler;
22+
import com.optimizely.ab.optimizelydecision.DecisionReasons;
23+
24+
import javax.annotation.Nonnull;
25+
import java.util.HashMap;
26+
import java.util.Map;
27+
28+
import org.slf4j.Logger;
29+
30+
class UserProfileTracker {
31+
private UserProfileService userProfileService;
32+
private Logger logger;
33+
private UserProfile userProfile;
34+
private boolean profileUpdated;
35+
private String userId;
36+
37+
UserProfileTracker(
38+
@Nonnull String userId,
39+
@Nonnull UserProfileService userProfileService,
40+
@Nonnull Logger logger
41+
) {
42+
this.userId = userId;
43+
this.userProfileService = userProfileService;
44+
this.logger = logger;
45+
this.profileUpdated = false;
46+
this.userProfile = null;
47+
}
48+
49+
public UserProfile getUserProfile() {
50+
return userProfile;
51+
}
52+
53+
public void loadUserProfile(DecisionReasons reasons, ErrorHandler errorHandler) {
54+
try {
55+
Map<String, Object> userProfileMap = userProfileService.lookup(userId);
56+
if (userProfileMap == null) {
57+
String message = reasons.addInfo("We were unable to get a user profile map from the UserProfileService.");
58+
logger.info(message);
59+
} else if (UserProfileUtils.isValidUserProfileMap(userProfileMap)) {
60+
userProfile = UserProfileUtils.convertMapToUserProfile(userProfileMap);
61+
} else {
62+
String message = reasons.addInfo("The UserProfileService returned an invalid map.");
63+
logger.warn(message);
64+
}
65+
} catch (Exception exception) {
66+
String message = reasons.addInfo(exception.getMessage());
67+
logger.error(message);
68+
errorHandler.handleError(new OptimizelyRuntimeException(exception));
69+
}
70+
71+
if (userProfile == null) {
72+
userProfile = new UserProfile(userId, new HashMap<String, Decision>());
73+
}
74+
}
75+
76+
public void updateUserProfile(@Nonnull Experiment experiment,
77+
@Nonnull Variation variation) {
78+
String experimentId = experiment.getId();
79+
String variationId = variation.getId();
80+
Decision decision;
81+
if (userProfile.experimentBucketMap.containsKey(experimentId)) {
82+
decision = userProfile.experimentBucketMap.get(experimentId);
83+
decision.variationId = variationId;
84+
} else {
85+
decision = new Decision(variationId);
86+
}
87+
userProfile.experimentBucketMap.put(experimentId, decision);
88+
profileUpdated = true;
89+
logger.info("Updated variation \"{}\" of experiment \"{}\" for user \"{}\".",
90+
variationId, experimentId, userProfile.userId);
91+
}
92+
93+
public void saveUserProfile(ErrorHandler errorHandler) {
94+
// if there were no updates, no need to save
95+
if (!this.profileUpdated) {
96+
return;
97+
}
98+
99+
try {
100+
userProfileService.save(userProfile.toMap());
101+
logger.info("Saved user profile of user \"{}\".",
102+
userProfile.userId);
103+
} catch (Exception exception) {
104+
logger.warn("Failed to save user profile of user \"{}\".",
105+
userProfile.userId);
106+
errorHandler.handleError(new OptimizelyRuntimeException(exception));
107+
}
108+
}
109+
}

0 commit comments

Comments
 (0)