Skip to content

Commit

Permalink
java libs for within subjects (#883)
Browse files Browse the repository at this point in the history
* initial changes for array of conditions

* pop array when getting condition

* add uniquifier

* moved rotate to mark

* handle null condition

* Merge branch 'algorithms-to-generate-conditions-queue' of https://github.com/CarnegieLearningWeb/UpGrade into java-libs-for-within-subject-assignment

* resolve conflicts

* move mark to assignment

* move experiment client to constructor

* rename experiment point to decision point

* keep mark in experiment client
  • Loading branch information
jreddig authored Jul 20, 2023
1 parent 0bf4187 commit a4d6db3
Show file tree
Hide file tree
Showing 8 changed files with 400 additions and 372 deletions.
419 changes: 109 additions & 310 deletions backend/packages/Upgrade/package-lock.json

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import static org.upgradeplatform.utils.Utils.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand All @@ -28,18 +30,22 @@
import org.upgradeplatform.requestbeans.SingleMetric;
import org.upgradeplatform.requestbeans.UserAlias;
import org.upgradeplatform.responsebeans.UserAliasResponse;
import org.upgradeplatform.responsebeans.Assignment;
import org.upgradeplatform.responsebeans.Condition;
import org.upgradeplatform.responsebeans.ErrorResponse;
import org.upgradeplatform.responsebeans.ExperimentUser;
import org.upgradeplatform.responsebeans.ExperimentsResponse;
import org.upgradeplatform.responsebeans.Factor;
import org.upgradeplatform.responsebeans.FeatureFlag;
import org.upgradeplatform.responsebeans.InitializeUser;
import org.upgradeplatform.responsebeans.LogEventResponse;
import org.upgradeplatform.responsebeans.MarkExperimentPoint;
import org.upgradeplatform.responsebeans.MarkDecisionPoint;
import org.upgradeplatform.responsebeans.Metric;
import org.upgradeplatform.responsebeans.Variation;
import org.upgradeplatform.utils.APIService;
import org.upgradeplatform.utils.PublishingRetryCallback;
import org.upgradeplatform.utils.Utils.MarkedDecisionPointStatus;
import org.upgradeplatform.utils.Utils.RequestType;

public class ExperimentClient implements AutoCloseable {

Expand Down Expand Up @@ -177,7 +183,7 @@ public void failed(Throwable throwable) {
}));
}

public void getAllExperimentCondition(String context, final ResponseCallback<List<ExperimentsResponse>> callbacks) {
public void getAllExperimentConditions(String context, final ResponseCallback<List<ExperimentsResponse>> callbacks) {
ExperimentRequest experimentRequest = new ExperimentRequest(this.userId, context);
AsyncInvoker invocation = this.apiService.prepareRequest(GET_ALL_EXPERIMENTS);
Entity<ExperimentRequest> requestContent = Entity.json(experimentRequest);
Expand Down Expand Up @@ -207,14 +213,60 @@ public void failed(Throwable throwable) {
}));
}


/**@param site This is matched case-insensitively*/
public void getExperimentCondition(String context, String site, final ResponseCallback<ExperimentsResponse> callbacks) {
public void getExperimentCondition(String context, String site, final ResponseCallback<Assignment> callbacks) {
getExperimentCondition(context, site, null, callbacks);
}

/**@param site This is matched case-insensitively
/**@param site This is matched case-insensitively
* @param target This is matched case-insensitively*/
public void getExperimentCondition(String context, String site, String target,
final ResponseCallback<Assignment> callbacks) {

if (this.allExperiments != null) {
ExperimentsResponse resultExperimentsResponse = findExperimentResponse(site, target, allExperiments);
Map<String, Factor> assignedFactor = resultExperimentsResponse.getAssignedFactor() != null ? resultExperimentsResponse.getAssignedFactor()[0] : null;
Condition assignedCondition = resultExperimentsResponse.getAssignedCondition() != null ? resultExperimentsResponse.getAssignedCondition()[0] : null;
Assignment resultAssignment = new Assignment(this, target, site, resultExperimentsResponse.getExperimentType(), assignedCondition, assignedFactor);

if (callbacks != null) {
callbacks.onSuccess(resultAssignment);
}
} else {
getAllExperimentConditions(context, new ResponseCallback<List<ExperimentsResponse>>() {
@Override
public void onSuccess(@NonNull List<ExperimentsResponse> experiments) {

ExperimentsResponse resultExperimentsResponse = findExperimentResponse(site, target, experiments);
Map<String, Factor> assignedFactor = resultExperimentsResponse.getAssignedFactor() != null ? resultExperimentsResponse.getAssignedFactor()[0] : null;
Condition assignedCondition = resultExperimentsResponse.getAssignedCondition() != null ? resultExperimentsResponse.getAssignedCondition()[0] : null;
Assignment resultAssignment = new Assignment(ExperimentClient.this, target, site, resultExperimentsResponse.getExperimentType(), assignedCondition, assignedFactor);

if (callbacks != null) {
callbacks.onSuccess(resultAssignment);
}
}

@Override
public void onError(@NonNull ErrorResponse error) {
if (callbacks != null)
callbacks.onError(error);

}
});
}

}

/**@param site This is matched case-insensitively*/
public void getAllExperimentConditions(String context, String site, final ResponseCallback<ExperimentsResponse> callbacks) {
getAllExperimentConditions(context, site, null, callbacks);
}

/**@param site This is matched case-insensitively
* @param target This is matched case-insensitively*/
public void getAllExperimentConditions(String context, String site, String target,
final ResponseCallback<ExperimentsResponse> callbacks) {
if (this.allExperiments != null) {

Expand All @@ -224,7 +276,7 @@ public void getExperimentCondition(String context, String site, String target,
callbacks.onSuccess(resultCondition);
}
} else {
getAllExperimentCondition(context, new ResponseCallback<List<ExperimentsResponse>>() {
getAllExperimentConditions(context, new ResponseCallback<List<ExperimentsResponse>>() {
@Override
public void onSuccess(@NonNull List<ExperimentsResponse> experiments) {

Expand Down Expand Up @@ -256,48 +308,74 @@ private ExperimentsResponse findExperimentResponse(String site, String target,
.orElse(new ExperimentsResponse());
}

private static ExperimentsResponse copyExperimentResponse(ExperimentsResponse experimentsResponse) {
Condition assignedCondition = new Condition(experimentsResponse.getAssignedCondition().getId(),
experimentsResponse.getAssignedCondition().getConditionCode(), experimentsResponse.getAssignedCondition().getExperimentId(),
experimentsResponse.getAssignedCondition().getPayload());
private void rotateConditions(String site, String target) {
if (this.allExperiments != null) {
ExperimentsResponse result = this.allExperiments.stream().filter(t -> t.getSite().equalsIgnoreCase(site) &&
(isStringNull(target) ? isStringNull(t.getTarget().toString())
: t.getTarget().toString().equalsIgnoreCase(target))).findFirst().orElse(null);
if (result != null) {
Condition[] rotatedCondition = Arrays.copyOf(result.getAssignedCondition(),
result.getAssignedCondition().length);
List<Condition> rotatedList = Arrays.asList(rotatedCondition);
Collections.rotate(rotatedList, -1);
rotatedCondition = rotatedList.toArray(rotatedCondition);
result.setAssignedCondition(rotatedCondition);
result.setAssignedFactor(result.getAssignedFactor());
}
}
}

private static ExperimentsResponse copyExperimentResponse(ExperimentsResponse experimentsResponse) {
ExperimentsResponse resultCondition = new ExperimentsResponse(experimentsResponse.getTarget().toString(),
experimentsResponse.getSite(), experimentsResponse.getExperimentType(), assignedCondition, experimentsResponse.getAssignedFactor());
experimentsResponse.getSite(), experimentsResponse.getExperimentType(), Arrays.copyOf(experimentsResponse.getAssignedCondition(),
experimentsResponse.getAssignedCondition().length), experimentsResponse.getAssignedFactor());
return resultCondition;
}

public void markExperimentPoint(final String site, String condition, MarkedDecisionPointStatus status,
final ResponseCallback<MarkExperimentPoint> callbacks) {
public void markDecisionPoint(final String site, String condition, MarkedDecisionPointStatus status,
final ResponseCallback<MarkDecisionPoint> callbacks) {
MarkExperimentRequestData markExperimentRequestData = new MarkExperimentRequestData(site, "", new Condition(condition));
markDecisionPoint(status, markExperimentRequestData, "", "", callbacks);
}

public void markDecisionPoint(final String site, String target, String condition, MarkedDecisionPointStatus status,
final ResponseCallback<MarkDecisionPoint> callbacks) {
MarkExperimentRequestData markExperimentRequestData = new MarkExperimentRequestData(site, target, new Condition(condition));
markDecisionPoint(status, markExperimentRequestData, "", "", callbacks);
}

public void markDecisionPoint(final String site, String condition, MarkedDecisionPointStatus status, String clientError,
final ResponseCallback<MarkDecisionPoint> callbacks) {
MarkExperimentRequestData markExperimentRequestData = new MarkExperimentRequestData(site, "", new Condition(condition));
markExperimentPoint(status, markExperimentRequestData, "", callbacks);
markDecisionPoint(status, markExperimentRequestData, clientError, "", callbacks);
}

public void markExperimentPoint(final String site, String target, String condition, MarkedDecisionPointStatus status,
final ResponseCallback<MarkExperimentPoint> callbacks) {
public void markDecisionPoint(final String site, String target, String condition, MarkedDecisionPointStatus status, String clientError,
final ResponseCallback<MarkDecisionPoint> callbacks) {
MarkExperimentRequestData markExperimentRequestData = new MarkExperimentRequestData(site, target, new Condition(condition));
markExperimentPoint(status, markExperimentRequestData, "", callbacks);
markDecisionPoint(status, markExperimentRequestData, clientError, "", callbacks);
}

public void markExperimentPoint(final String site, String condition, MarkedDecisionPointStatus status, String clientError,
final ResponseCallback<MarkExperimentPoint> callbacks) {
public void markDecisionPoint(final String site, String condition, MarkedDecisionPointStatus status, String clientError, String uniquifier,
final ResponseCallback<MarkDecisionPoint> callbacks) {
MarkExperimentRequestData markExperimentRequestData = new MarkExperimentRequestData(site, "", new Condition(condition));
markExperimentPoint(status, markExperimentRequestData, clientError, callbacks);
markDecisionPoint(status, markExperimentRequestData, clientError, uniquifier, callbacks);
}

public void markExperimentPoint(final String site, String target, String condition, MarkedDecisionPointStatus status, String clientError,
final ResponseCallback<MarkExperimentPoint> callbacks) {
public void markDecisionPoint(final String site, String target, String condition, MarkedDecisionPointStatus status, String clientError, String uniquifier,
final ResponseCallback<MarkDecisionPoint> callbacks) {
MarkExperimentRequestData markExperimentRequestData = new MarkExperimentRequestData(site, target, new Condition(condition));
markExperimentPoint(status, markExperimentRequestData, clientError, callbacks);
markDecisionPoint(status, markExperimentRequestData, clientError, uniquifier, callbacks);
}

public void markExperimentPoint(MarkedDecisionPointStatus status, MarkExperimentRequestData data,
final ResponseCallback<MarkExperimentPoint> callbacks) {
markExperimentPoint(status, data, "", callbacks);
public void markDecisionPoint(MarkedDecisionPointStatus status, MarkExperimentRequestData data,
final ResponseCallback<MarkDecisionPoint> callbacks) {
markDecisionPoint(status, data, "", "", callbacks);
}

public void markExperimentPoint(MarkedDecisionPointStatus status, MarkExperimentRequestData data, String clientError,
final ResponseCallback<MarkExperimentPoint> callbacks) {
MarkExperimentRequest markExperimentRequest = new MarkExperimentRequest(this.userId, status, data, clientError);
public void markDecisionPoint(MarkedDecisionPointStatus status, MarkExperimentRequestData data, String clientError, String uniquifier,
final ResponseCallback<MarkDecisionPoint> callbacks) {
MarkExperimentRequest markExperimentRequest = new MarkExperimentRequest(this.userId, status, data, clientError, uniquifier);
AsyncInvoker invocation = this.apiService.prepareRequest(MARK_EXPERIMENT_POINT);

Entity<MarkExperimentRequest> requestContent = Entity.json(markExperimentRequest);
Expand All @@ -310,7 +388,8 @@ public void markExperimentPoint(MarkedDecisionPointStatus status, MarkExperiment
public void completed(Response response) {
if (response.getStatus() == Response.Status.OK.getStatusCode()) {

readResponseToCallback(response, callbacks, MarkExperimentPoint.class);
readResponseToCallback(response, callbacks, MarkDecisionPoint.class);
rotateConditions(data.getSite(), data.getTarget());
} else {
String status = Response.Status.fromStatusCode(response.getStatus()).toString();
ErrorResponse error = new ErrorResponse(response.getStatus(), response.readEntity( String.class ), status );
Expand Down
17 changes: 10 additions & 7 deletions clientlibs/java/src/main/java/org/upgradeplatform/client/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -11,12 +12,12 @@

import org.eclipse.jdt.annotation.NonNull;
import org.upgradeplatform.interfaces.ResponseCallback;
import org.upgradeplatform.responsebeans.Assignment;
import org.upgradeplatform.responsebeans.Condition;
import org.upgradeplatform.responsebeans.ErrorResponse;
import org.upgradeplatform.responsebeans.ExperimentUser;
import org.upgradeplatform.responsebeans.ExperimentsResponse;
import org.upgradeplatform.responsebeans.InitializeUser;
import org.upgradeplatform.responsebeans.MarkExperimentPoint;
import org.upgradeplatform.responsebeans.MarkDecisionPoint;
import org.upgradeplatform.responsebeans.UserAliasResponse;
import org.upgradeplatform.utils.Utils.MarkedDecisionPointStatus;

Expand All @@ -27,7 +28,7 @@ public static void main(String[] args) throws InterruptedException, ExecutionExc
final String userId = UUID.randomUUID().toString();
final String site = "SelectSection";

String target = args.length > 0 ? args[0] : "volume_surface_area_cone_vol";
String target = args.length > 0 ? args[0] : "absolute_value_plot_equality";

try(ExperimentClient experimentClient = new ExperimentClient(userId, "BearerToken", baseUrl, Collections.emptyMap())){
CompletableFuture<String> result = new CompletableFuture<>();
Expand Down Expand Up @@ -62,16 +63,18 @@ public void onSuccess(@NonNull ExperimentUser expResult){
public void onSuccess(@NonNull UserAliasResponse uar) {
System.out.println(prefix() + "success updating user aliases; getting conditions");

experimentClient.getExperimentCondition("assign-prog", site, target, new ResponseCallback<ExperimentsResponse>(){
experimentClient.getExperimentCondition("assign-prog", site, target, new ResponseCallback<Assignment>(){
@Override
public void onSuccess(@NonNull ExperimentsResponse expResult){
public void onSuccess(@NonNull Assignment expResult){
System.out.println(prefix() + "success getting condition; marking");

Condition condition = expResult.getAssignedCondition();
String code = condition == null ? null : condition.getConditionCode();
experimentClient.markExperimentPoint(site, target, code, MarkedDecisionPointStatus.CONDITION_APPLIED, new ResponseCallback<MarkExperimentPoint>(){
System.out.println(condition);
System.out.println(code);
expResult.markDecisionPoint(MarkedDecisionPointStatus.CONDITION_APPLIED, new Date().toString(), new ResponseCallback<MarkDecisionPoint>(){
@Override
public void onSuccess(@NonNull MarkExperimentPoint markResult){
public void onSuccess(@NonNull MarkDecisionPoint markResult){
result.complete("marked " + code + ": " + markResult.toString());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class MarkExperimentRequest {
private MarkedDecisionPointStatus status;
private MarkExperimentRequestData data;
private String clientError;
private String uniquifier;

public MarkExperimentRequest() {}

Expand All @@ -27,6 +28,15 @@ public MarkExperimentRequest(String userId, MarkedDecisionPointStatus status, Ma
this.clientError = clientError;
}

public MarkExperimentRequest(String userId, MarkedDecisionPointStatus status, MarkExperimentRequestData data,
String clientError, String uniquifier) {
this.userId = userId;
this.status = status;
this.data = data;
this.clientError = clientError;
this.uniquifier = uniquifier;
}

public String getUserId() {
return userId;
}
Expand Down Expand Up @@ -58,5 +68,13 @@ public String getClientError() {
public void setClientError(String clientError) {
this.clientError = clientError;
}

public String getUniquifier() {
return uniquifier;
}

public void setUniquifier(String uniquifier) {
this.uniquifier = uniquifier;
}

}
Loading

0 comments on commit a4d6db3

Please sign in to comment.