-
Notifications
You must be signed in to change notification settings - Fork 38
bring back getVariable(String,Integer,Double,Boolean) #200
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
470cb0d
bring back getVariable(String,Integer,Double,Boolean)
thomaszurkan-optimizely 694d981
remove throws
thomaszurkan-optimizely 7cf446f
revert
thomaszurkan-optimizely 54a94cf
check for valid input to getVariable calls
thomaszurkan-optimizely c8bb54a
log error on invalid user id or variable key for getVariable
thomaszurkan-optimizely 48a4b35
Merge branch 'master' into fix/getVariableXXX
thomaszurkan-optimizely File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,13 +22,19 @@ | |
|
||
import com.optimizely.ab.Optimizely; | ||
import com.optimizely.ab.UnknownEventTypeException; | ||
import com.optimizely.ab.UnknownLiveVariableException; | ||
import com.optimizely.ab.config.Experiment; | ||
import com.optimizely.ab.config.LiveVariable; | ||
import com.optimizely.ab.config.LiveVariableUsageInstance; | ||
import com.optimizely.ab.config.ProjectConfig; | ||
import com.optimizely.ab.config.Variation; | ||
import com.optimizely.ab.error.NoOpErrorHandler; | ||
import com.optimizely.ab.error.RaiseExceptionErrorHandler; | ||
import com.optimizely.ab.notification.NotificationCenter; | ||
|
||
import org.slf4j.Logger; | ||
|
||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -551,4 +557,226 @@ public NotificationCenter getNotificationCenter() { | |
|
||
return null; | ||
} | ||
|
||
//======== live variable getters ========// | ||
/** | ||
* Helper method to retrieve the {@link LiveVariable} for the given variable key. | ||
* If {@link RaiseExceptionErrorHandler} is provided, either a live variable is returned, or an exception is | ||
* thrown. | ||
* If {@link NoOpErrorHandler} is used, either a live variable or {@code null} is returned. | ||
* | ||
* @param projectConfig the current project config | ||
* @param variableKey the key for the live variable being retrieved from the current project config | ||
* @return the live variable to retrieve for the given variable key | ||
* | ||
*/ | ||
private LiveVariable getLiveVariable(ProjectConfig projectConfig, String variableKey) { | ||
|
||
LiveVariable liveVariable = projectConfig | ||
.getLiveVariableKeyMapping() | ||
.get(variableKey); | ||
|
||
if (liveVariable == null) { | ||
String unknownLiveVariableKeyError = | ||
String.format("Live variable \"%s\" is not in the datafile.", variableKey); | ||
logger.error(unknownLiveVariableKeyError); | ||
return null; | ||
|
||
} | ||
|
||
return liveVariable; | ||
} | ||
|
||
/** | ||
* Get the value of a String live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not | ||
* @return String value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
String getVariableString(@NonNull String variableKey, | ||
@NonNull String userId, | ||
boolean activateExperiment) { | ||
return getVariableString(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); | ||
} | ||
|
||
/** | ||
* Get the value of a String live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param attributes a map of attributes about the user | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not | ||
* @return String value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
String getVariableString(@NonNull String variableKey, | ||
@NonNull String userId, | ||
@NonNull Map<String, String> attributes, | ||
boolean activateExperiment) { | ||
|
||
if (!isValid()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's null-check the |
||
logger.warn("Optimizely is not initialized, could not get live variable {} " + | ||
"for user {}", variableKey, userId); | ||
return null; | ||
} | ||
|
||
|
||
LiveVariable variable = getLiveVariable(optimizely.getProjectConfig(), variableKey); | ||
if (variable == null) { | ||
return null; | ||
} | ||
|
||
List<Experiment> experimentsUsingLiveVariable = | ||
optimizely.getProjectConfig().getLiveVariableIdToExperimentsMapping().get(variable.getId()); | ||
Map<String, Map<String, LiveVariableUsageInstance>> variationToLiveVariableUsageInstanceMapping = | ||
optimizely.getProjectConfig().getVariationToLiveVariableUsageInstanceMapping(); | ||
|
||
if (experimentsUsingLiveVariable == null) { | ||
logger.warn("No experiment is using variable \"{}\".", variable.getKey()); | ||
return variable.getDefaultValue(); | ||
} | ||
|
||
for (Experiment experiment : experimentsUsingLiveVariable) { | ||
Variation variation; | ||
if (activateExperiment) { | ||
variation = activate(experiment.getKey(), userId, attributes); | ||
} else { | ||
variation = getVariation(experiment.getKey(), userId, attributes); | ||
} | ||
|
||
if (variation != null) { | ||
LiveVariableUsageInstance usageInstance = | ||
variationToLiveVariableUsageInstanceMapping.get(variation.getId()).get(variable.getId()); | ||
return usageInstance.getValue(); | ||
} | ||
} | ||
|
||
return variable.getDefaultValue(); | ||
} | ||
|
||
/** | ||
* Get the value of a Boolean live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not | ||
* @return Boolean value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
Boolean getVariableBoolean(@NonNull String variableKey, | ||
@NonNull String userId, | ||
boolean activateExperiment) { | ||
return getVariableBoolean(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); | ||
} | ||
|
||
/** | ||
* Get the value of a Boolean live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param attributes a map of attributes about the user | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not | ||
* @return Boolean value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
Boolean getVariableBoolean(@NonNull String variableKey, | ||
@NonNull String userId, | ||
@NonNull Map<String, String> attributes, | ||
boolean activateExperiment) { | ||
|
||
String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment); | ||
if (variableValueString != null) { | ||
return Boolean.parseBoolean(variableValueString); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Get the value of a Integer live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not | ||
* @return Integer value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
Integer getVariableInteger(@NonNull String variableKey, | ||
@NonNull String userId, | ||
boolean activateExperiment) { | ||
return getVariableInteger(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); | ||
} | ||
|
||
/** | ||
* Get the value of a Integer live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param attributes a map of attributes about the user | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not* @return Integer value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
Integer getVariableInteger(@NonNull String variableKey, | ||
@NonNull String userId, | ||
@NonNull Map<String, String> attributes, | ||
boolean activateExperiment) { | ||
|
||
String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment); | ||
if (variableValueString != null) { | ||
try { | ||
return Integer.parseInt(variableValueString); | ||
} catch (NumberFormatException e) { | ||
logger.error("Variable value \"{}\" for live variable \"{}\" is not an integer.", variableValueString, | ||
variableKey); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Get the value of a Double live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not | ||
* @return Double value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
Double getVariableDouble(@NonNull String variableKey, | ||
@NonNull String userId, | ||
boolean activateExperiment) { | ||
return getVariableDouble(variableKey, userId, Collections.<String, String>emptyMap(), activateExperiment); | ||
} | ||
|
||
/** | ||
* Get the value of a Double live variable | ||
* @param variableKey the String key for the variable | ||
* @param userId the user ID | ||
* @param attributes a map of attributes about the user | ||
* @param activateExperiment the flag denoting whether to activate an experiment or not | ||
* @return Double value of the live variable | ||
*/ | ||
@Deprecated | ||
public @Nullable | ||
Double getVariableDouble(@NonNull String variableKey, | ||
@NonNull String userId, | ||
@NonNull Map<String, String> attributes, | ||
boolean activateExperiment) { | ||
|
||
String variableValueString = getVariableString(variableKey, userId, attributes, activateExperiment); | ||
if (variableValueString != null) { | ||
try { | ||
return Double.parseDouble(variableValueString); | ||
} catch (NumberFormatException e) { | ||
logger.error("Variable value \"{}\" for live variable \"{}\" is not a double.", variableValueString, | ||
variableKey); | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should null-check the
variableKey
since it is a user-provided input or we can do it at a higher level, like the caller of this function