Skip to content

Commit

Permalink
Update the UI to parse the ConstraintViolation response differently.
Browse files Browse the repository at this point in the history
This will return an object containing all constraints processed by the server, including whether they have compilation errors or ran successfully.
  • Loading branch information
goetzrrGit committed Nov 7, 2023
1 parent 219ea44 commit 3e52246
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 25 deletions.
37 changes: 20 additions & 17 deletions src/stores/constraints.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { keyBy } from 'lodash-es';
import { derived, get, writable, type Readable, type Writable } from 'svelte/store';
import type { Constraint, ConstraintResult } from '../types/constraint';
import type { Constraint, ConstraintResponse, ConstraintResult } from '../types/constraint';
import gql from '../utilities/gql';
import type { Status } from '../utilities/status';
import { modelId, planId, planStartTimeMs } from './plan';
Expand Down Expand Up @@ -34,30 +34,33 @@ export const constraintVisibilityMap: Readable<Record<Constraint['id'], boolean>

export const checkConstraintsStatus: Writable<Status | null> = writable(null);

export const constraintResultsResponse: Writable<ConstraintResult[]> = writable([]);
export const constraintResponse: Writable<ConstraintResponse[]> = writable([]);

export const constraintsColumns: Writable<string> = writable('2fr 3px 1fr');
export const constraintsFormColumns: Writable<string> = writable('1fr 3px 2fr');

/* Derived. */

export const constraintResults: Readable<ConstraintResult[]> = derived(
[constraintResultsResponse, planStartTimeMs],
([$constraintResultsResponse, $planStartTimeMs]) =>
$constraintResultsResponse.reduce((list: ConstraintResult[], constraintResult) => {
list.push({
...constraintResult,
violations: constraintResult.violations.map(violation => ({
...violation,
windows: violation.windows.map(({ end, start }) => ({
end: $planStartTimeMs + end / 1000,
start: $planStartTimeMs + start / 1000,
[constraintResponse, planStartTimeMs],
([$constraintResponse, $planStartTimeMs]) =>
$constraintResponse
.filter(response => response.success)
.map(successfulResponse => successfulResponse.results)
.reduce((list: ConstraintResult[], constraintResult) => {
list.push({
...constraintResult,
violations: constraintResult.violations.map(violation => ({
...violation,
windows: violation.windows.map(({ end, start }) => ({
end: $planStartTimeMs + end / 1000,
start: $planStartTimeMs + start / 1000,
})),
})),
})),
});
});

return list;
}, []),
return list;
}, []),
);

export const visibleConstraintResults: Readable<ConstraintResult[]> = derived(
Expand Down Expand Up @@ -88,5 +91,5 @@ export function setAllConstraintsVisible(visible: boolean) {

export function resetConstraintStores(): void {
checkConstraintsStatus.set(null);
constraintResultsResponse.set([]);
constraintResponse.set([]);
}
36 changes: 28 additions & 8 deletions src/utilities/effects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { CommandDictionary as AmpcsCommandDictionary } from '@nasa-jpl/aeri
import { get } from 'svelte/store';
import { SearchParameters } from '../enums/searchParameters';
import { activityDirectives, activityDirectivesMap, selectedActivityDirectiveId } from '../stores/activities';
import { checkConstraintsStatus, constraintResultsResponse } from '../stores/constraints';
import { checkConstraintsStatus, constraintResponse } from '../stores/constraints';
import { catchError, catchSchedulingError } from '../stores/errors';
import {
createExpansionRuleError,
Expand Down Expand Up @@ -37,7 +37,7 @@ import type {
import type { ActivityMetadata } from '../types/activity-metadata';
import type { BaseUser, User, UserId } from '../types/app';
import type { ReqAuthResponse, ReqSessionResponse } from '../types/auth';
import type { Constraint, ConstraintInsertInput, ConstraintResult } from '../types/constraint';
import type { Constraint, ConstraintInsertInput, ConstraintResponse, ConstraintResult } from '../types/constraint';
import type {
ExpansionRule,
ExpansionRuleInsertInput,
Expand Down Expand Up @@ -285,19 +285,39 @@ const effects = {
checkConstraintsStatus.set(Status.Incomplete);
if (plan !== null) {
const { id: planId } = plan;
const data = await reqHasura<ConstraintResult[]>(
const data = await reqHasura<ConstraintResponse[]>(
gql.CHECK_CONSTRAINTS,
{
planId,
},
user,
);
const { ConstraintResponses } = data;
if (ConstraintResponses) {
constraintResponse.set(ConstraintResponses);

const { constraintResults } = data;
if (constraintResults != null) {
constraintResultsResponse.set(constraintResults);
checkConstraintsStatus.set(Status.Complete);
showSuccessToast('Check Constraints Complete');
// find only the constraints compiled.
const successfulConstraintResults: ConstraintResult[] = ConstraintResponses.filter(
constraintResponse => constraintResponse.success,
).map(constraintResponse => constraintResponse.results);

const failedConstraintResponses = ConstraintResponses.filter(
constraintResponse => !constraintResponse.success,
);

if (successfulConstraintResults.length != ConstraintResponses.length) {
showFailureToast('Partial Constraints Completed');
failedConstraintResponses.forEach(failedConstraint => {
failedConstraint.errors.forEach(error => {
catchError(`${error.message}`, error.stack);
});
});

checkConstraintsStatus.set(successfulConstraintResults.length !== 0 ? Status.Incomplete : Status.Failed);
} else {
showSuccessToast('All Constraints Completed');
checkConstraintsStatus.set(Status.Complete);
}
} else {
throw Error(`Unable to check constraints for plan with ID: "${plan.id}"`);
}
Expand Down

0 comments on commit 3e52246

Please sign in to comment.