Repository: nasa-ammos/aerie
Issue: Error when saving scheduling simulation results with child activities

Aerie version: 49bfe3f7ed4ccc1826727fd972d9036fb8e7edd0

To reproduce: create a plan with banananation, add a scheduling goal of any sort, e.g.

```typescript
export default (): Goal => {
  return Goal.ActivityRecurrenceGoal({
    interval: Temporal.Duration.from({hours: 48}),
    activityTemplate: ActivityTemplates.BiteBanana({
      biteSize: 1
    })
  })
}

```

Add a `child` activity to the plan - which will decompose to add a grandchild activity.

Run scheduling:
```
java.util.NoSuchElementException: No value present
	at java.base/java.util.Optional.get(Unknown Source)
	at gov.nasa.jpl.aerie.scheduler.server.services.GraphQLMerlinService.lambda$postActivities$10(GraphQLMerlinService.java:1107)
	at java.base/java.util.stream.Collectors.lambda$uniqKeysMapAccumulator$1(Unknown Source)
	at java.base/java.util.stream.ReduceOps$3ReducingSink.accept(Unknown Source)
	at java.base/java.util.HashMap$EntrySpliterator.forEachRemaining(Unknown Source)
	at java.base/java.util.stream.AbstractPipeline.copyInto(Unknown Source)
	at java.base/java.util.stream.AbstractPipeline.wrapAndCopyInto(Unknown Source)
	at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(Unknown Source)
	at java.base/java.util.stream.AbstractPipeline.evaluate(Unknown Source)
	at java.base/java.util.stream.ReferencePipeline.collect(Unknown Source)
	at gov.nasa.jpl.aerie.scheduler.server.services.GraphQLMerlinService.postActivities(GraphQLMerlinService.java:1106)
	at gov.nasa.jpl.aerie.scheduler.server.services.GraphQLMerlinService.storeSimulationResults(GraphQLMerlinService.java:737)
	at gov.nasa.jpl.aerie.scheduler.worker.services.SynchronousSchedulerAgent.storeSimulationResults(SynchronousSchedulerAgent.java:287)
	at gov.nasa.jpl.aerie.scheduler.worker.services.SynchronousSchedulerAgent.schedule(SynchronousSchedulerAgent.java:229)
	at gov.nasa.jpl.aerie.scheduler.worker.SchedulerWorkerAppDriver.main(SchedulerWorkerAppDriver.java:88)
```

This line of code assumes that every simulated activity has a non-empty directiveId:
https://github.com/NASA-AMMOS/aerie/blob/49bfe3f7ed4ccc1826727fd972d9036fb8e7edd0/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinService.java#L1095

Changed files:
scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinService.java

diff --git a/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinService.java b/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinService.java
index 94a1f92a4..2f609da81 100644
--- a/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinService.java
+++ b/scheduler-server/src/main/java/gov/nasa/jpl/aerie/scheduler/server/services/GraphQLMerlinService.java
@@ -1116,11 +1116,11 @@ public SimulationId getSimulationId(PlanId planId) throws PlanServiceException,
   {
       final var simulatedActivityRecords = simulatedActivities.entrySet().stream()
                                                               .collect(Collectors.toMap(
-                                                                  e -> simulationActivityDirectiveIdToMerlinActivityDirectiveId.get(e.getValue().directiveId().get()).id(),
+                                                                  e -> e.getKey().id(),
                                                                   e -> simulatedActivityToRecord(e.getValue(), simulationActivityDirectiveIdToMerlinActivityDirectiveId)));
       final var allActivityRecords = unfinishedActivities.entrySet().stream()
                                                          .collect(Collectors.toMap(
-                                                             e -> simulationActivityDirectiveIdToMerlinActivityDirectiveId.get(e.getValue().directiveId().get()).id(),
+                                                             e -> e.getKey().id(),
                                                              e -> unfinishedActivityToRecord(e.getValue(), simulationActivityDirectiveIdToMerlinActivityDirectiveId)));
       allActivityRecords.putAll(simulatedActivityRecords);
       final var simIdToPgId = postSpans(
@@ -1166,10 +1166,18 @@ public SimulationId getSimulationId(PlanId planId) throws PlanServiceException,
 
   final JsonObject response;
   response = postRequest(req, arguments).get();
-  final var affected_rows = response.getJsonObject("data").getJsonObject("update_span_many").getInt("affected_rows");
-  if(affected_rows != updateCounter) {
-    throw new PlanServiceException("not the same size");
-  }
+    final var jsonValue = response.getJsonObject("data").get("update_span_many");
+    var affected_rows = 0;
+    if (jsonValue.getValueType() == JsonValue.ValueType.ARRAY) {
+      for (final var jsonObject : response.getJsonObject("data").getJsonArray("update_span_many").getValuesAs(JsonObject.class)) {
+        affected_rows += jsonObject.getInt("affected_rows");
+      }
+    } else {
+      affected_rows = response.getJsonObject("data").getJsonObject("update_span_many").getInt("affected_rows");
+    }
+    if(affected_rows != updateCounter) {
+      throw new PlanServiceException("not the same size");
+    }
 }
 
   private static SpanRecord simulatedActivityToRecord(final SimulatedActivity activity,