Skip to content

Commit

Permalink
Move anchor update into general activity modify mutation
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelCourtney committed Jan 31, 2025
1 parent 6025fe4 commit f13b669
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public record DatasetIds(DatasetId datasetId, SimulationDatasetId simulationData
* @param gqlStr the graphQL query or mutation to send to aerie
* @return the json response returned by aerie, or an empty optional in case of io errors
*/
protected Optional<JsonObject> postRequest(final String gqlStr) throws IOException, MerlinServiceException {
private Optional<JsonObject> postRequest(final String gqlStr) throws IOException, MerlinServiceException {
try {
//TODO: (mem optimization) use streams here to avoid several copies of strings
final var reqBody = Json.createObjectBuilder().add("query", gqlStr).build();
Expand Down Expand Up @@ -426,7 +426,7 @@ public Map<ActivityDirectiveId, ActivityDirectiveId> updatePlanActivityDirective
for (final var activity : plan.getActivities()) {
if(activity.getParentActivity().isPresent()) continue; // Skip generated activities
if (!activity.isNew()) {
final var actFromInitialPlan = initialPlan.getActivityById(activity.id());
final var actFromInitialPlan = initialPlan.getActivityById(activity.id()).get();
//if act was present in initial plan
final var activityDirectiveFromSchedulingDirective = new ActivityDirective(
activity.startOffset(),
Expand All @@ -435,9 +435,9 @@ public Map<ActivityDirectiveId, ActivityDirectiveId> updatePlanActivityDirective
activity.anchorId(),
activity.anchoredToStart()
);
if (!activityDirectiveFromSchedulingDirective.equals(actFromInitialPlan.get())) {
if (!activityDirectiveFromSchedulingDirective.equals(actFromInitialPlan)) {
final var newState = activityDirectiveFromSchedulingDirective.serializedActivity();
final var oldState = actFromInitialPlan.get().serializedActivity();
final var oldState = actFromInitialPlan.serializedActivity();
if (!Objects.equals(newState.getTypeName(), oldState.getTypeName())) {
throw new IllegalStateException(
"Modified activities cannot change type. Was " + oldState.getTypeName()
Expand Down Expand Up @@ -472,32 +472,6 @@ public Map<ActivityDirectiveId, ActivityDirectiveId> updatePlanActivityDirective
return ids;
}

@Override
public void updatePlanActivityDirectiveAnchors(final PlanId planId, final Plan plan, final Map<ActivityDirectiveId, ActivityDirectiveId> uploadIdMap)
throws MerlinServiceException, IOException
{
final var request = new StringBuilder();
final var acts = plan.getActivities();
request.append("mutation {");
var hasUpdate = false;
for (final SchedulingActivity act: acts) {
if (act.isNew() && act.anchorId() != null) {
hasUpdate = true;
final var id = uploadIdMap.get(act.id()).id();
request.append("""
update_%d: update_activity_directive_by_pk(pk_columns: {id: %d, plan_id: %d}, _set: {anchor_id: %d}) {
id
}
""".formatted(id, id, planId.id(), uploadIdMap.get(act.anchorId()).id())
);
}
}
if (hasUpdate) {
request.append("}");
postRequest(request.toString());
}
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -597,6 +571,10 @@ mutation createAllPlanActivityDirectives($activities: [activity_directive_insert
.add("start_offset", act.startOffset().toString())
.add("anchored_to_start", act.anchoredToStart());

if (act.anchorId() != null) {
insertionObject.add("anchor_id", act.anchorId().toString());
}

if (act.name() != null) insertionObject = insertionObject.add("name", act.name());

//add duration to parameters if controllable
Expand Down Expand Up @@ -676,13 +654,20 @@ private void modifyActivityDirectives(
.add("anchored_to_start", act.anchoredToStart())
.add("name", act.name());

if (act.anchorId() == null) {
activityObject.addNull("anchor_id");
} else {
activityObject.add("anchor_id", act.anchorId().id());
}

final var insertionObjectArguments = Json.createObjectBuilder();
for (final var arg : act.arguments().entrySet()) {
insertionObjectArguments.add(arg.getKey(), serializedValueP.unparse(arg.getValue()));
}
activityObject.add("arguments", insertionObjectArguments.build());
arguments.add("activity_%d".formatted(id), activityObject);
}
request.append("}");
postRequest(request.toString(), arguments.build()).orElseThrow(() -> new NoSuchPlanException(planId));
}

Expand All @@ -694,15 +679,15 @@ private void deleteActivityDirectives(
{
if (ids.isEmpty()) return;
ensurePlanExists(planId);
final var request = new StringBuilder();
request.append("mutation deletePlanActivityDirectives {");
for (final var id : ids) {
request.append("""
delete_%d: delete_activity_directive_by_pk(id: %d, plan_id: %d) {affected_rows}
""".formatted(id.id(), id.id(), planId.id()));
}
request.append("}");
postRequest(request.toString()).orElseThrow(() -> new NoSuchPlanException(planId));
final var idString = ids.stream().map(String::valueOf).collect(Collectors.joining(","));
final var request = """
mutation deletePlanActivityDirectives($planId: Int! = %d, $directiveIds: [Int!]! = [%s]) {
delete_activity_directive(where: {_and: {plan_id: {_eq: $planId}, id: {_in: $directiveIds}}}) {
affected_rows
}
}
""".formatted(planId.id(), idString);
postRequest(request).orElseThrow(() -> new NoSuchPlanException(planId));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,6 @@ Map<ActivityDirectiveId, ActivityDirectiveId> updatePlanActivityDirectives(
)
throws IOException, NoSuchPlanException, MerlinServiceException, NoSuchActivityInstanceException;

/**
* update the list of SchedulingActivityDirectives with anchors, replacing the anchorIds generated by the
* scheduler by the new ids generated by the database
* @throws MerlinServiceException
* @throws IOException
*/
void updatePlanActivityDirectiveAnchors(final PlanId planId, final Plan plan, final Map<ActivityDirectiveId, ActivityDirectiveId> uploadIdMap)
throws MerlinServiceException, IOException;

/**
* delete all the activity instances stored in the target plan container
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ public void schedule(
);
}

merlinDatabaseService.updatePlanActivityDirectiveAnchors(specification.planId(), solutionPlan, uploadIdMap);

//collect results and notify subscribers of success
final var results = collectResults(solutionPlan, uploadIdMap, goals);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ public Map<ActivityDirectiveId, ActivityDirectiveId> updatePlanActivityDirective
return res;
}

@Override
public void updatePlanActivityDirectiveAnchors(final PlanId planId, final Plan plan, final Map<ActivityDirectiveId, ActivityDirectiveId> uploadIdMap)
{}

@Override
public void ensurePlanExists(final PlanId planId) {

Expand Down

0 comments on commit f13b669

Please sign in to comment.