Skip to content

Commit

Permalink
adjust shift efficiency tracker as well
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuehnel committed Jun 23, 2024
1 parent ddd6914 commit b739633
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public DrtShiftEfficiencyModeModule(DrtConfigGroup drtConfigGroup) {
@Override
public void install() {
bindModal(ShiftEfficiencyTracker.class).toProvider(modalProvider(getter ->
new ShiftEfficiencyTracker())).asEagerSingleton();
new ShiftEfficiencyTracker(getMode()))).asEagerSingleton();
addEventHandlerBinding().to(modalKey(ShiftEfficiencyTracker.class));
bindModal(ShiftEfficiencyAnalysisControlerListener.class).toProvider(modalProvider(getter ->
new ShiftEfficiencyAnalysisControlerListener(drtConfigGroup,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
public final class ShiftEfficiencyTracker implements PersonMoneyEventHandler,
PassengerDroppedOffEventHandler, DrtShiftStartedEventHandler, DrtShiftEndedEventHandler {

private final String mode;
private Map<Id<DrtShift>, Double> revenueByShift;
private Map<Id<Request>, Id<DrtShift>> shiftByRequest;

Expand All @@ -63,44 +64,53 @@ public Map<Id<DrtShift>, Id<DvrpVehicle>> getFinishedShifts() {
}
}

public ShiftEfficiencyTracker() {
this.revenueByShift = new HashMap<>();
public ShiftEfficiencyTracker(String mode) {
this.mode = mode;
this.revenueByShift = new HashMap<>();
this.shiftByRequest = new HashMap<>();
this.finishedShifts = new HashMap<>();
this.currentRecord = new Record(revenueByShift, shiftByRequest, finishedShifts);
}

@Override
public void handleEvent(PersonMoneyEvent personMoneyEvent) {
if (DrtFareHandler.PERSON_MONEY_EVENT_PURPOSE_DRT_FARE.equals(personMoneyEvent.getPurpose())) {
Id<DrtShift> key = shiftByRequest.get(Id.create(personMoneyEvent.getReference(), DrtRequest.class));
if(key != null) {
revenueByShift.merge(key, -personMoneyEvent.getAmount(), Double::sum);
if(personMoneyEvent.getTransactionPartner().equals(mode)) {
if (DrtFareHandler.PERSON_MONEY_EVENT_PURPOSE_DRT_FARE.equals(personMoneyEvent.getPurpose())) {
Id<DrtShift> key = shiftByRequest.get(Id.create(personMoneyEvent.getReference(), DrtRequest.class));
if (key != null) {
revenueByShift.merge(key, -personMoneyEvent.getAmount(), Double::sum);
}
}
}
}

@Override
public void handleEvent(PassengerDroppedOffEvent event) {
Id<DvrpVehicle> vehicleId = event.getVehicleId();
Gbl.assertIf(activeShifts.containsKey(vehicleId));
Id<DrtShift> drtShiftId = activeShifts.get(vehicleId);
shiftByRequest.put(event.getRequestId(), drtShiftId);
if(event.getMode().equals(mode)) {
Id<DvrpVehicle> vehicleId = event.getVehicleId();
Gbl.assertIf(activeShifts.containsKey(vehicleId));
Id<DrtShift> drtShiftId = activeShifts.get(vehicleId);
shiftByRequest.put(event.getRequestId(), drtShiftId);
}
}

@Override
public void handleEvent(DrtShiftStartedEvent event) {
revenueByShift.put(event.getShiftId(), 0.);
if(activeShifts.containsKey(event.getVehicleId())) {
throw new RuntimeException("Vehicle is already registered for another shift");
if(event.getMode().equals(mode)) {
revenueByShift.put(event.getShiftId(), 0.);
if (activeShifts.containsKey(event.getVehicleId())) {
throw new RuntimeException("Vehicle is already registered for another shift");
}
activeShifts.put(event.getVehicleId(), event.getShiftId());
}
activeShifts.put(event.getVehicleId(), event.getShiftId());
}

@Override
public void handleEvent(DrtShiftEndedEvent event) {
activeShifts.remove(event.getVehicleId());
finishedShifts.put(event.getShiftId(), event.getVehicleId());
if(event.getMode().equals(mode)) {
activeShifts.remove(event.getVehicleId());
finishedShifts.put(event.getShiftId(), event.getVehicleId());
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public class ShiftEfficiencyTest {
void testDrtShiftEfficiency() {

EventsManager events = new EventsManagerImpl();
ShiftEfficiencyTracker shiftEfficiencyTracker = new ShiftEfficiencyTracker();
ShiftEfficiencyTracker shiftEfficiencyTracker = new ShiftEfficiencyTracker("drt");
events.addHandler(shiftEfficiencyTracker);
events.initProcessing();

Expand Down

0 comments on commit b739633

Please sign in to comment.