From 19c1bb796fc3dcc3d191d884e53107ee3598e972 Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Fri, 21 Nov 2025 15:24:11 +0100 Subject: Reject running services arriving >3min early in consolidation algorithm (#108) Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: arielcostas <94913521+arielcostas@users.noreply.github.com> --- src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs index f1f5f4a..12489ca 100644 --- a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs +++ b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs @@ -211,9 +211,11 @@ public class VigoController : ControllerBase // 1) Prefer a started trip whose scheduled calling time is close to the estimated arrival. // 2) If no good started match, pick the next not-started trip (soonest in the future). // 3) Reject matches where scheduled time is >10 minutes AFTER realtime (data inconsistency). - // 4) Fallbacks: if no future trips, use the best started one even if far. + // 4) Reject matches where the bus would arrive >3 minutes BEFORE schedule (too early). + // 5) Fallbacks: if no future trips, use the best started one even if far. const int startedMatchToleranceMinutes = 15; // how close a started trip must be to consider it a match const int maxScheduleDelayMinutes = 10; // reject if scheduled is this much later than realtime + const int maxEarlyArrivalMinutes = 3; // reject if bus arrives more than 3 minutes before schedule var startedCandidates = possibleCirculations .Where(c => c.StartingDateTime()!.Value <= now) @@ -240,7 +242,8 @@ public class VigoController : ControllerBase // Check best started candidate if (bestStarted != null && bestStarted.AbsDiff <= startedMatchToleranceMinutes && - bestStarted.TimeDiff <= maxScheduleDelayMinutes) // reject if scheduled too far after realtime + bestStarted.TimeDiff <= maxScheduleDelayMinutes && // reject if scheduled too far after realtime + bestStarted.TimeDiff >= -maxEarlyArrivalMinutes) // reject if bus arrives too early { closestCirculation = bestStarted.Circulation; } @@ -254,7 +257,9 @@ public class VigoController : ControllerBase } // Otherwise, leave it null (no valid match) } - else if (bestStarted != null && bestStarted.TimeDiff <= maxScheduleDelayMinutes) + else if (bestStarted != null && + bestStarted.TimeDiff <= maxScheduleDelayMinutes && + bestStarted.TimeDiff >= -maxEarlyArrivalMinutes) { // nothing upcoming today; fallback to the closest started one (if timing is reasonable) closestCirculation = bestStarted.Circulation; -- cgit v1.3