-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1169 from NASA-AMMOS/fix/get-constraints-loads-co…
…rrect-sim Make Check Constraints Load Correct Sim Dataset
- Loading branch information
Showing
14 changed files
with
308 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
108 changes: 108 additions & 0 deletions
108
e2e-tests/src/tests/constraints-load-old-sim-results.test.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import req, { awaitSimulation } from '../utilities/requests.js'; | ||
|
||
test.describe.serial('Check Constraints Against Specific Sim Datasets', () => { | ||
const constraintName = 'fruit_equal_peel'; | ||
const activity_offset_hours = 1; | ||
|
||
const plan_start_timestamp = "2023-01-01T00:00:00+00:00"; | ||
|
||
let mission_model_id: number; | ||
let plan_id: number; | ||
let constraint_id: number; | ||
let violation: Violation; | ||
let activity_id: number; | ||
|
||
test.beforeAll(async ({request}) => { | ||
let rd = Math.random() * 100000; | ||
let jar_id = await req.uploadJarFile(request); | ||
// Add Mission Model | ||
const model: MissionModelInsertInput = { | ||
jar_id, | ||
mission: 'aerie_e2e_tests', | ||
name: 'Banananation (e2e tests)', | ||
version: rd + "", | ||
}; | ||
mission_model_id = await req.createMissionModel(request, model); | ||
|
||
// Add Plan | ||
const plan_input: CreatePlanInput = { | ||
model_id: mission_model_id, | ||
name: 'test_plan' + rd, | ||
start_time: plan_start_timestamp, | ||
duration: "24:00:00" | ||
}; | ||
plan_id = await req.createPlan(request, plan_input); | ||
|
||
// Add Activity | ||
const activityToInsert: ActivityInsertInput = { | ||
arguments: { | ||
biteSize: 2, | ||
}, | ||
plan_id: plan_id, | ||
type: 'BiteBanana', | ||
start_offset: activity_offset_hours + 'h', | ||
}; | ||
activity_id = await req.insertActivity(request, activityToInsert); | ||
|
||
// Add Constraint | ||
const constraint: ConstraintInsertInput = { | ||
name: constraintName, | ||
definition: 'export default (): Constraint => Real.Resource("/fruit").equal(Real.Resource("/peel"))', | ||
description: '', | ||
model_id: null, | ||
plan_id, | ||
}; | ||
constraint_id = await req.insertConstraint(request, constraint); | ||
}); | ||
|
||
test.afterAll(async ({request}) => { | ||
// Delete Constraint | ||
await req.deleteConstraint(request, constraint_id); | ||
|
||
// Deleting Plan and Model cascades the rest of the cleanup | ||
await req.deleteMissionModel(request, mission_model_id); | ||
await req.deletePlan(request, plan_id); | ||
}); | ||
|
||
test('Constraints Loads Specified Outdated Sim Dataset', async ({request}) => { | ||
// Simulate | ||
const oldSimDatasetId = (await awaitSimulation(request, plan_id)).simulationDatasetId; | ||
|
||
// Invalidate Results and Resim | ||
await req.deleteActivity(request, plan_id, activity_id); | ||
const newSimulationDatasetId = (await awaitSimulation(request, plan_id)).simulationDatasetId; | ||
|
||
// Assert No Violations on Newest Results | ||
const newConstraintResults = await req.checkConstraints(request, plan_id, newSimulationDatasetId); | ||
expect(newConstraintResults).toHaveLength(0); | ||
|
||
// Assert One Violation on Old Results | ||
const oldConstraintResults = await req.checkConstraints(request, plan_id, oldSimDatasetId); | ||
expect(oldConstraintResults).toHaveLength(1); | ||
|
||
// Assert the Results to be as expected | ||
const result = oldConstraintResults.pop(); | ||
expect(result.constraintName).toEqual(constraintName); | ||
expect(result.constraintId).toEqual(constraint_id); | ||
|
||
// Resources | ||
expect(result.resourceIds).toHaveLength(2); | ||
expect(result.resourceIds).toContain('/fruit'); | ||
expect(result.resourceIds).toContain('/peel'); | ||
|
||
// Violation | ||
expect(result.violations).toHaveLength(1); | ||
|
||
// Violation Window | ||
const plan_duration_micro = 24 * 60 * 60 * 1000 * 1000; | ||
const activity_offset_micro = activity_offset_hours * 60 * 60 * 1000 * 1000; | ||
|
||
violation = result.violations[0]; | ||
expect(violation.windows[0].start).toEqual(activity_offset_micro); | ||
expect(violation.windows[0].end).toEqual(plan_duration_micro); | ||
|
||
// Gaps | ||
expect(result.gaps).toHaveLength(0); | ||
}); | ||
}); |
15 changes: 15 additions & 0 deletions
15
.../java/gov/nasa/jpl/aerie/merlin/server/exceptions/SimulationDatasetMismatchException.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package gov.nasa.jpl.aerie.merlin.server.exceptions; | ||
|
||
import gov.nasa.jpl.aerie.merlin.server.models.PlanId; | ||
import gov.nasa.jpl.aerie.merlin.server.models.SimulationDatasetId; | ||
|
||
public class SimulationDatasetMismatchException extends Exception { | ||
public final SimulationDatasetId simDatasetId; | ||
public final PlanId planId; | ||
|
||
public SimulationDatasetMismatchException(final PlanId pid, final SimulationDatasetId sid) { | ||
super("Simulation Dataset with id `" + sid.id() + "` does not belong to Plan with id `"+pid.id()+"`"); | ||
this.planId = pid; | ||
this.simDatasetId = sid; | ||
} | ||
} |
This file contains 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 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 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 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
71 changes: 71 additions & 0 deletions
71
...ava/gov/nasa/jpl/aerie/merlin/server/remotes/postgres/GetSimulationDatasetByIdAction.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package gov.nasa.jpl.aerie.merlin.server.remotes.postgres; | ||
|
||
import gov.nasa.jpl.aerie.merlin.server.models.Timestamp; | ||
import org.intellij.lang.annotations.Language; | ||
|
||
import java.sql.Connection; | ||
import java.sql.PreparedStatement; | ||
import java.sql.SQLException; | ||
import java.util.Optional; | ||
|
||
public class GetSimulationDatasetByIdAction implements AutoCloseable { | ||
private static final @Language("Sql") String sql = """ | ||
select | ||
d.simulation_id as simulation_id, | ||
d.status as status, | ||
d.reason as reason, | ||
d.canceled as canceled, | ||
to_char(d.simulation_start_time, 'YYYY-DDD"T"HH24:MI:SS.FF6') as simulation_start_time, | ||
to_char(d.simulation_end_time, 'YYYY-DDD"T"HH24:MI:SS.FF6') as simulation_end_time, | ||
d.dataset_id as dataset_id | ||
from simulation_dataset as d | ||
where | ||
d.id = ? | ||
"""; | ||
|
||
private final PreparedStatement statement; | ||
|
||
public GetSimulationDatasetByIdAction(final Connection connection) throws SQLException { | ||
this.statement = connection.prepareStatement(sql); | ||
} | ||
|
||
public Optional<SimulationDatasetRecord> get( | ||
final long simulationDatasetId | ||
) throws SQLException { | ||
this.statement.setLong(1, simulationDatasetId); | ||
|
||
try (final var results = this.statement.executeQuery()) { | ||
if (!results.next()) return Optional.empty(); | ||
|
||
final SimulationStateRecord.Status status; | ||
try { | ||
status = SimulationStateRecord.Status.fromString(results.getString("status")); | ||
} catch (final SimulationStateRecord.Status.InvalidSimulationStatusException ex) { | ||
throw new Error("Simulation Dataset initialized with invalid state."); | ||
} | ||
|
||
final var simulationId = results.getLong("simulation_id"); | ||
final var reason = PreparedStatements.getFailureReason(results, 3); | ||
final var canceled = results.getBoolean("canceled"); | ||
final var state = new SimulationStateRecord(status, reason); | ||
final var simStartTime = Timestamp.fromString(results.getString("simulation_start_time")); | ||
final var simEndTime = Timestamp.fromString(results.getString("simulation_end_time")); | ||
final var datasetId = results.getLong("dataset_id"); | ||
|
||
return Optional.of( | ||
new SimulationDatasetRecord( | ||
simulationId, | ||
datasetId, | ||
state, | ||
canceled, | ||
simStartTime, | ||
simEndTime, | ||
simulationDatasetId)); | ||
} | ||
} | ||
|
||
@Override | ||
public void close() throws SQLException { | ||
this.statement.close(); | ||
} | ||
} |
This file contains 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 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
Oops, something went wrong.