Fix finding best connection (#49)

* Log and fix connection

* Update Services/TransportConnectionCalculator.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Services/TransportConnectionCalculator.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* Update Services/TransportConnectionCalculator.cs

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* better logging

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Claudio Schaad 2025-09-06 21:14:44 +02:00 committed by GitHub
parent 5103aecacf
commit 28429d78d0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 14 additions and 18 deletions

View file

@ -25,12 +25,8 @@ namespace ShiftScheduler.Services
if (apiResponse?.Connections.Count > 0) if (apiResponse?.Connections.Count > 0)
{ {
var allConnections = apiResponse.Connections.Select(MapToTransportConnection).ToList(); var allConnections = apiResponse.Connections.Select(MapToTransportConnection).ToList();
allConnections.ForEach(tc => logger.LogDebug($"Found connection {tc}"));
foreach (var connection in allConnections)
{
logger.LogDebug($"Found connection {connection}");
}
var bestConnection = TransportConnectionCalculator.FindBestConnection( var bestConnection = TransportConnectionCalculator.FindBestConnection(
allConnections, new ConnectionPickArgument(shiftStartTime, config.SafetyBufferMinutes, config.MaxEarlyArrivalMinutes, config.MaxLateArrivalMinutes), logger); allConnections, new ConnectionPickArgument(shiftStartTime, config.SafetyBufferMinutes, config.MaxEarlyArrivalMinutes, config.MaxLateArrivalMinutes), logger);

View file

@ -7,13 +7,7 @@ public record ConnectionPickArgument(
DateTimeOffset ShiftStartTime, DateTimeOffset ShiftStartTime,
int SafetyBufferMinutes, int SafetyBufferMinutes,
int MaxEarlyArrivalMinutes, int MaxEarlyArrivalMinutes,
int MaxLateArrivalMinutes) int MaxLateArrivalMinutes);
{
public override string ToString()
{
return $"ShiftStartTime: {ShiftStartTime}, SafetyBufferMinutes: {SafetyBufferMinutes}, MaxEarlyArrivalMinutes{MaxEarlyArrivalMinutes}, MaxLateArrivalMinutes:{MaxLateArrivalMinutes}";
}
}
public static class TransportConnectionCalculator public static class TransportConnectionCalculator
{ {
@ -24,10 +18,13 @@ public static class TransportConnectionCalculator
var earliestAcceptableTime = args.ShiftStartTime.AddMinutes(-args.MaxEarlyArrivalMinutes); var earliestAcceptableTime = args.ShiftStartTime.AddMinutes(-args.MaxEarlyArrivalMinutes);
var latestAcceptableTime = args.ShiftStartTime.AddMinutes(args.MaxLateArrivalMinutes); var latestAcceptableTime = args.ShiftStartTime.AddMinutes(args.MaxLateArrivalMinutes);
logger.LogDebug(args.ToString()); logger.LogDebug("ShiftStartTime: {ShiftStartTime}", args.ShiftStartTime);
logger.LogDebug($"latestArrivalTime: {latestArrivalTime}"); logger.LogDebug("SafetyBufferMinutes: {SafetyBufferMinutes}", args.SafetyBufferMinutes);
logger.LogDebug($"earliestAcceptableTime: {earliestAcceptableTime}"); logger.LogDebug("MaxEarlyArrivalMinutes: {MaxEarlyArrivalMinutes}", args.MaxEarlyArrivalMinutes);
logger.LogDebug($"latestAcceptableTime: {latestAcceptableTime}"); logger.LogDebug("MaxLateArrivalMinutes: {MaxLateArrivalMinutes}", args.MaxLateArrivalMinutes);
logger.LogDebug("latestArrivalTime: {LatestArrivalTime}", latestArrivalTime);
logger.LogDebug("earliestAcceptableTime: {EarliestAcceptableTime}", earliestAcceptableTime);
logger.LogDebug("latestAcceptableTime: {LatestAcceptableTime}", latestAcceptableTime);
var validConnections = new List<TransportConnection>(); var validConnections = new List<TransportConnection>();
var lateValidConnections = new List<TransportConnection>(); var lateValidConnections = new List<TransportConnection>();
@ -50,6 +47,7 @@ public static class TransportConnectionCalculator
var bestValidConnection = validConnections.Last(); var bestValidConnection = validConnections.Last();
if (bestValidConnection.ArrivalTime > earliestAcceptableTime) if (bestValidConnection.ArrivalTime > earliestAcceptableTime)
{ {
logger.LogDebug("Found bestValidConnection {BestValidConnection}", bestValidConnection);
return bestValidConnection; return bestValidConnection;
} }
} }
@ -57,9 +55,11 @@ public static class TransportConnectionCalculator
// If no connections arrive before latest arrival time, check if any arrive within acceptable late range // If no connections arrive before latest arrival time, check if any arrive within acceptable late range
if (lateValidConnections.Count > 0) if (lateValidConnections.Count > 0)
{ {
logger.LogDebug("Found lateValidConnection {LateValidConnection}", lateValidConnections.First());
return lateValidConnections.First(); return lateValidConnections.First();
} }
return validConnections.FirstOrDefault(); logger.LogDebug("Use last valid connection {LastValidConnection}", validConnections.LastOrDefault());
return validConnections.LastOrDefault();
} }
} }