Skip to content

Commit

Permalink
Fix/867 request behavior blocks when it does not recieve an answer (#868
Browse files Browse the repository at this point in the history
)

* Remove the state system from the request behavior and deduplicate request radiograms

* Update test scenarios accordingly

* Fix test scenarios

* Change wording in frontend

* Also pass events that notify when a request has been fulfilled

* Fix ids in export according to #874

* Always tell the request target that we are not requesting resources anymore.

* Invalidate requestedResources after receiving vehicles

* Fix linter

* apply suggestions
  • Loading branch information
Greenscreen23 authored Apr 19, 2023
1 parent f17d69f commit a89914c
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,13 @@
<h5>Anfragenziel</h5>

<ng-container *ngIf="{ time: nextTimeoutIn$ | async } as nextTimeoutIn">
<span *ngIf="(waitingForAnswer$ | async) === true" class="text-muted"
>Wartet auf die Antwort einer Anfrage von</span
>
<span
*ngIf="
nextTimeoutIn.time !== undefined &&
(waitingForAnswer$ | async) === false
nextTimeoutIn.time !== null && nextTimeoutIn.time !== undefined
"
class="text-muted"
>Nächste Anfrage potentiell in
{{ nextTimeoutIn.time | date : 'mm:ss' }} an</span
>Nächste potentielle Anfrage in
{{ nextTimeoutIn.time | formatDuration }}</span
>
</ng-container>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type {
RequestBehaviorState,
} from 'digital-fuesim-manv-shared';
import {
isWaitingForAnswer,
UUID,
SimulatedRegionRequestTargetConfiguration,
TraineesRequestTargetConfiguration,
Expand Down Expand Up @@ -42,8 +41,6 @@ export class RequestVehiclesComponent implements OnChanges {
[key in RequestTargetOption]: string;
}>;

waitingForAnswer$!: Observable<boolean>;

nextTimeoutIn$!: Observable<number | undefined>;

selectedRequestTarget$!: Observable<RequestTargetOption>;
Expand Down Expand Up @@ -92,12 +89,6 @@ export class RequestVehiclesComponent implements OnChanges {
})
);

this.waitingForAnswer$ = this.requestBehaviorState$.pipe(
map((requestBehaviorState) =>
isWaitingForAnswer(requestBehaviorState)
)
);

const activities$ = this.store.select(
createSelectActivityStates(this.simulatedRegionId)
);
Expand Down
61 changes: 50 additions & 11 deletions shared/src/models/utils/request-target/trainees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ import { RadiogramUnpublishedStatus } from '../../../models/radiogram/status/rad
import { publishRadiogram } from '../../../models/radiogram/radiogram-helpers-mutable';
import { nextUUID } from '../../../simulation/utils/randomness';
import { ResourceRequestRadiogram } from '../../radiogram/resource-request-radiogram';
import type { Mutable } from '../../../utils/immutability';
import { isDone, isUnread } from '../../radiogram/radiogram-helpers';
import { StrictObject } from '../../../utils/strict-object';
import { isEmptyResource } from '../rescue-resource';
import type {
RequestTarget,
RequestTargetConfiguration,
Expand All @@ -29,17 +33,52 @@ export const traineesRequestTarget: RequestTarget<TraineesRequestTargetConfigura
requestedResource,
key
) => {
publishRadiogram(
draftState,
cloneDeepMutable(
ResourceRequestRadiogram.create(
nextUUID(draftState),
requestingSimulatedRegionId,
RadiogramUnpublishedStatus.create(),
requestedResource,
key
const unreadRadiogram = StrictObject.values(
draftState.radiograms
).find(
(radiogram) =>
radiogram.type === 'resourceRequestRadiogram' &&
isUnread(radiogram) &&
radiogram.key === key
) as Mutable<ResourceRequestRadiogram> | undefined;
if (unreadRadiogram) {
if (isEmptyResource(requestedResource)) {
delete draftState.radiograms[unreadRadiogram.id];
} else {
unreadRadiogram.requiredResource = requestedResource;
}
return;
}

if (
StrictObject.values(draftState.radiograms)
.filter(
(radiogram) =>
radiogram.type === 'resourceRequestRadiogram' &&
radiogram.key === key
)
.every(isDone) &&
!isEmptyResource(requestedResource)
) {
publishRadiogram(
draftState,
cloneDeepMutable(
ResourceRequestRadiogram.create(
nextUUID(draftState),
requestingSimulatedRegionId,
RadiogramUnpublishedStatus.create(),
requestedResource,
key
)
)
)
);
);
// eslint-disable-next-line no-useless-return
return;
}

/**
* There is a radiogram that is currently accepted,
* we therefore wait for an answer and don't send another request
*/
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,6 @@ export const providePersonnelFromVehiclesActivity: SimulationActivity<ProvidePer
);
const missingPersonnel = activityState.requiredPersonnelCounts;

if (
greaterEqualResourceDescription(
availablePersonnel,
missingPersonnel
)
) {
terminate();
return;
}

const vehiclePriorities = activityState.vehiclePriorities
.map((id) => personnelInVehicleTemplate(draftState, id))
.filter(
Expand Down Expand Up @@ -146,19 +136,13 @@ export const providePersonnelFromVehiclesActivity: SimulationActivity<ProvidePer
);
}

if (
Object.values(missingVehicleCounts).some(
(vehicleCount) => vehicleCount > 0
)
) {
const event = ResourceRequiredEvent.create(
nextUUID(draftState),
simulatedRegion.id,
VehicleResource.create(missingVehicleCounts),
activityState.key
);
sendSimulationEvent(simulatedRegion, event);
}
const event = ResourceRequiredEvent.create(
nextUUID(draftState),
simulatedRegion.id,
VehicleResource.create(missingVehicleCounts),
activityState.key
);
sendSimulationEvent(simulatedRegion, event);

terminate();
},
Expand Down
21 changes: 9 additions & 12 deletions shared/src/simulation/activities/reassign-treatments.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IsInt, IsOptional, IsUUID, Min } from 'class-validator';
import type { PatientStatus, PersonnelType } from '../../models/utils';
import {
isEmptyResource,
PersonnelResource,
getCreate,
isInSpecificSimulatedRegion,
Expand Down Expand Up @@ -206,17 +205,15 @@ export const reassignTreatmentsActivity: SimulationActivity<ReassignTreatmentsAc
const missingResource = PersonnelResource.create(
ceilResourceDescription(missingPersonnel)
);
if (!isEmptyResource(missingResource)) {
sendSimulationEvent(
simulatedRegion,
ResourceRequiredEvent.create(
nextUUID(draftState),
simulatedRegion.id,
missingResource,
'reassignTreatmentsActivity'
)
);
}
sendSimulationEvent(
simulatedRegion,
ResourceRequiredEvent.create(
nextUUID(draftState),
simulatedRegion.id,
missingResource,
'reassignTreatmentsActivity'
)
);
}

if (allowTerminate) {
Expand Down
54 changes: 21 additions & 33 deletions shared/src/simulation/activities/transfer-vehicles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,38 +130,27 @@ export const transferVehiclesActivity: SimulationActivity<TransferVehiclesActivi
(vehicle) => vehicle.vehicleType
);

if (
Object.entries(
activityState.vehiclesToBeTransferred.vehicleCounts
).some(
([vehicleType, vehicleCount]) =>
(groupedVehicles[vehicleType]?.length ?? 0) <
vehicleCount
const missingVehicles: { [key: string]: number } = {};
Object.entries(
activityState.vehiclesToBeTransferred.vehicleCounts
).forEach(([vehicleType, vehicleCount]) => {
if (
(groupedVehicles[vehicleType]?.length ?? 0) < vehicleCount
) {
missingVehicles[vehicleType] =
vehicleCount -
(groupedVehicles[vehicleType]?.length ?? 0);
}
});
sendSimulationEvent(
simulatedRegion,
ResourceRequiredEvent.create(
nextUUID(draftState),
simulatedRegion.id,
VehicleResource.create(missingVehicles),
activityState.key
)
) {
const missingVehicles: { [key: string]: number } = {};
Object.entries(
activityState.vehiclesToBeTransferred.vehicleCounts
).forEach(([vehicleType, vehicleCount]) => {
if (
(groupedVehicles[vehicleType]?.length ?? 0) <
vehicleCount
) {
missingVehicles[vehicleType] =
vehicleCount -
(groupedVehicles[vehicleType]?.length ?? 0);
}
});
sendSimulationEvent(
simulatedRegion,
ResourceRequiredEvent.create(
nextUUID(draftState),
simulatedRegion.id,
VehicleResource.create(missingVehicles),
activityState.key
)
);
}
);

const sentVehicles: { [key: string]: number } = {};
Object.entries(
Expand Down Expand Up @@ -221,8 +210,7 @@ export const transferVehiclesActivity: SimulationActivity<TransferVehiclesActivi
),
VehiclesSentEvent.create(
nextUUID(draftState),
VehicleResource.create(sentVehicles),
activityState.key
VehicleResource.create(sentVehicles)
)
);
}
Expand Down
Loading

0 comments on commit a89914c

Please sign in to comment.