Skip to content
This repository has been archived by the owner on Jun 7, 2024. It is now read-only.

Commit

Permalink
[EN-20691] Added a getAssignmentVariation function (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
simondale00 committed May 9, 2024
1 parent a703254 commit 5af32fc
Showing 1 changed file with 49 additions and 3 deletions.
52 changes: 49 additions & 3 deletions src/main/java/com/eppo/sdk/EppoClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,51 @@ protected Optional<EppoValue> getAssignmentValue(
String flagKey,
EppoAttributes subjectAttributes,
Map<String, EppoAttributes> actionsWithAttributes
) {
Optional<Variation> assignedVariation = getAssignmentVariation(
subjectKey,
flagKey,
subjectAttributes,
actionsWithAttributes
);

if (assignedVariation.isPresent()) {
return Optional.of(assignedVariation.get().getTypedValue());
}

return Optional.empty();
}

/**
* Returns the assigned variation.
*
* @param subjectKey
* @param flagKey
* @param subjectAttributes
* @return
*/
public Optional<Variation> getAssignmentVariation(
String subjectKey,
String flagKey,
EppoAttributes subjectAttributes
) {
return getAssignmentVariation(subjectKey, flagKey, subjectAttributes, null);
}

/**
* Returns the assigned variation.
*
* @param subjectKey
* @param flagKey
* @param subjectAttributes
* @param actionsWithAttributes
* @return
*/
protected Optional<Variation> getAssignmentVariation(
String subjectKey,
String flagKey,
EppoAttributes subjectAttributes,
Map<String, EppoAttributes> actionsWithAttributes
) {
// Validate Input Values
InputValidator.validateNotBlank(subjectKey, "Invalid argument: subjectKey cannot be blank");
Expand All @@ -68,10 +113,11 @@ protected Optional<EppoValue> getAssignmentValue(

if (algorithmType == AlgorithmType.OVERRIDE) {
// Assigned variation was from an override; return its value without logging
return assignmentValue;
return Optional.of(assignedVariation);
} else if (algorithmType == AlgorithmType.CONTEXTUAL_BANDIT) {
// Assigned variation is a bandit; need to use the bandit to determine its value
assignmentValue = this.determineAndLogBanditAction(assignmentResult, actionsWithAttributes);
Optional<EppoValue> banditValue = this.determineAndLogBanditAction(assignmentResult, actionsWithAttributes);
assignedVariation.setTypedValue(banditValue.orElse(null));
}

// Log the assignment
Expand All @@ -88,7 +134,7 @@ protected Optional<EppoValue> getAssignmentValue(
log.warn("Error logging assignment", e);
}

return assignmentValue;
return Optional.of(assignedVariation);
}

private VariationAssignmentResult getAssignedVariation(String flagKey, String subjectKey, EppoAttributes subjectAttributes) {
Expand Down

0 comments on commit 5af32fc

Please sign in to comment.