From f1d7d75ab6e155eeb92a499ae3d8723d833312ed Mon Sep 17 00:00:00 2001 From: Claudio Schaad Date: Wed, 3 Sep 2025 19:50:13 +0200 Subject: [PATCH] Add logger --- Services/TransportApiService.cs | 10 +-- Services/TransportConnectionCalculator.cs | 32 ++++++--- Shared/TransportModels.cs | 9 +++ .../TransportConnectionCalculatorTests.cs | 68 ++++++++++++------- 4 files changed, 76 insertions(+), 43 deletions(-) diff --git a/Services/TransportApiService.cs b/Services/TransportApiService.cs index c6e579e..fd37755 100644 --- a/Services/TransportApiService.cs +++ b/Services/TransportApiService.cs @@ -28,17 +28,11 @@ namespace ShiftScheduler.Services foreach (var connection in allConnections) { - logger.LogInformation($"Found connection {connection}"); + logger.LogDebug($"Found connection {connection}"); } var bestConnection = TransportConnectionCalculator.FindBestConnection( - allConnections, - shiftStartTime, - config.SafetyBufferMinutes, - config.MaxEarlyArrivalMinutes, - config.MaxLateArrivalMinutes); - - logger.LogInformation($"Found best {bestConnection}"); + allConnections, new ConnectionPickArgument(shiftStartTime, config.SafetyBufferMinutes, config.MaxEarlyArrivalMinutes, config.MaxLateArrivalMinutes), logger); return bestConnection; } diff --git a/Services/TransportConnectionCalculator.cs b/Services/TransportConnectionCalculator.cs index ee0364a..aeadd86 100644 --- a/Services/TransportConnectionCalculator.cs +++ b/Services/TransportConnectionCalculator.cs @@ -1,19 +1,34 @@ +using System.Globalization; +using Microsoft.Extensions.Logging; using ShiftScheduler.Shared; namespace ShiftScheduler.Services; +public record ConnectionPickArgument( + DateTime ShiftStartTime, + int SafetyBufferMinutes, + int MaxEarlyArrivalMinutes, + int MaxLateArrivalMinutes) +{ + public override string ToString() + { + return $"ShiftStartTime: {ShiftStartTime}, SafetyBufferMinutes: {SafetyBufferMinutes}, MaxEarlyArrivalMinutes{MaxEarlyArrivalMinutes}, MaxLateArrivalMinutes:{MaxLateArrivalMinutes}"; + } +} + public static class TransportConnectionCalculator { public static TransportConnection? FindBestConnection( - IReadOnlyList connections, - DateTime shiftStartTime, - int safetyBufferMinutes, - int maxEarlyArrivalMinutes, - int maxLateArrivalMinutes) + IReadOnlyList connections, ConnectionPickArgument args, ILogger logger) { - var latestArrivalTime = shiftStartTime.AddMinutes(-safetyBufferMinutes); - var earliestAcceptableTime = shiftStartTime.AddMinutes(-maxEarlyArrivalMinutes); - var latestAcceptableTime = shiftStartTime.AddMinutes(maxLateArrivalMinutes); + var latestArrivalTime = args.ShiftStartTime.AddMinutes(-args.SafetyBufferMinutes); + var earliestAcceptableTime = args.ShiftStartTime.AddMinutes(-args.MaxEarlyArrivalMinutes); + var latestAcceptableTime = args.ShiftStartTime.AddMinutes(args.MaxLateArrivalMinutes); + + logger.LogDebug(args.ToString()); + logger.LogDebug(latestArrivalTime.ToString(CultureInfo.CurrentCulture)); + logger.LogDebug(earliestAcceptableTime.ToString(CultureInfo.CurrentCulture)); + logger.LogDebug(latestAcceptableTime.ToString(CultureInfo.CurrentCulture)); var validConnections = new List(); var lateValidConnections = new List(); @@ -67,7 +82,6 @@ public static class TransportConnectionCalculator return sortedLateValid.First(); } - // No suitable connections found return null; } } \ No newline at end of file diff --git a/Shared/TransportModels.cs b/Shared/TransportModels.cs index 66fa378..9b14d26 100644 --- a/Shared/TransportModels.cs +++ b/Shared/TransportModels.cs @@ -14,6 +14,15 @@ namespace ShiftScheduler.Shared public class TransportConnection { + public TransportConnection() + { + } + + public TransportConnection(string arrivalTime) + { + ArrivalTime = arrivalTime; + } + public string DepartureTime { get; set; } = string.Empty; public string ArrivalTime { get; set; } = string.Empty; public string Duration { get; set; } = string.Empty; diff --git a/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs b/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs index d5c9a42..2f1e2c7 100644 --- a/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs +++ b/ShiftScheduler.Services.Tests/TransportConnectionCalculatorTests.cs @@ -1,4 +1,5 @@ -using ShiftScheduler.Services; +using Microsoft.Extensions.Logging; +using Moq; using ShiftScheduler.Shared; using Shouldly; @@ -6,6 +7,8 @@ namespace ShiftScheduler.Services.Tests; public class TransportConnectionCalculatorTests { + private readonly Mock loggerMock = new(); + // Tests for new enhanced logic [Fact] public void FindBestConnectionEnhanced_WithGoodTimingConnection_ShouldReturnLatestValidConnection() @@ -13,15 +16,17 @@ public class TransportConnectionCalculatorTests // Arrange - Shift starts at 8:00, safety buffer 30 min, so latest arrival is 7:30 var connections = new List { - new TransportConnection { ArrivalTime = "2023-12-15T07:15:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T07:25:00" }, // Should be selected (latest valid) - new TransportConnection { ArrivalTime = "2023-12-15T08:05:00" } // Too late + new(arrivalTime: "2023-12-15T07:15:00"), + new(arrivalTime: "2023-12-15T07:25:00"), // Should be selected (latest valid) + new(arrivalTime: "2023-12-15T08:05:00") // Too late }; var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); // Act var result = TransportConnectionCalculator.FindBestConnection( - connections, shiftStartTime, 30, 60, 15); + connections, + new ConnectionPickArgument(shiftStartTime, 30, 60, 15), + loggerMock.Object); // Assert result.ShouldNotBeNull(); @@ -34,14 +39,16 @@ public class TransportConnectionCalculatorTests // Arrange - Shift starts at 8:00, max early arrival 60 min, so earliest acceptable is 7:00 var connections = new List { - new TransportConnection { ArrivalTime = "2023-12-15T06:45:00" }, // Too early (more than 60 min before 8:00) - new TransportConnection { ArrivalTime = "2023-12-15T08:05:00" } // Within acceptable late range (15 min after 8:00) + new(arrivalTime: "2023-12-15T06:45:00"), // Too early (more than 60 min before 8:00) + new(arrivalTime: "2023-12-15T08:05:00") // Within acceptable late range (15 min after 8:00) }; var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); // Act var result = TransportConnectionCalculator.FindBestConnection( - connections, shiftStartTime, 30, 60, 15); + connections, + new ConnectionPickArgument(shiftStartTime, 30, 60, 15), + loggerMock.Object); // Assert result.ShouldNotBeNull(); @@ -54,15 +61,17 @@ public class TransportConnectionCalculatorTests // Arrange - Shift starts at 8:00, no valid early connections var connections = new List { - new TransportConnection { ArrivalTime = "2023-12-15T08:10:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T08:05:00" }, // Should be selected (earliest late) - new TransportConnection { ArrivalTime = "2023-12-15T08:12:00" } + new(arrivalTime: "2023-12-15T08:10:00"), + new(arrivalTime: "2023-12-15T08:05:00"), // Should be selected (earliest late) + new(arrivalTime: "2023-12-15T08:12:00") }; var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); // Act var result = TransportConnectionCalculator.FindBestConnection( - connections, shiftStartTime, 30, 60, 15); + connections, + new ConnectionPickArgument(shiftStartTime, 30, 60, 15), + loggerMock.Object); // Assert result.ShouldNotBeNull(); @@ -75,15 +84,17 @@ public class TransportConnectionCalculatorTests // Arrange - All connections are too late (beyond max late arrival) var connections = new List { - new TransportConnection { ArrivalTime = "2023-12-15T08:20:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T08:25:00" }, - new TransportConnection { ArrivalTime = "2023-12-15T08:30:00" } + new(arrivalTime: "2023-12-15T08:20:00"), + new(arrivalTime: "2023-12-15T08:25:00"), + new(arrivalTime: "2023-12-15T08:30:00") }; var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); // Act var result = TransportConnectionCalculator.FindBestConnection( - connections, shiftStartTime, 30, 60, 15); + connections, + new ConnectionPickArgument(shiftStartTime, 30, 60, 15), + loggerMock.Object); // Assert result.ShouldBeNull(); @@ -95,14 +106,16 @@ public class TransportConnectionCalculatorTests // Arrange - Early connection is acceptable (not too early), should prefer it over late var connections = new List { - new TransportConnection { ArrivalTime = "2023-12-15T07:20:00" }, // 40 min before shift - acceptable - new TransportConnection { ArrivalTime = "2023-12-15T08:05:00" } // 5 min after shift + new(arrivalTime: "2023-12-15T07:20:00"), // 40 min before shift - acceptable + new(arrivalTime: "2023-12-15T08:05:00") // 5 min after shift }; var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); // Act var result = TransportConnectionCalculator.FindBestConnection( - connections, shiftStartTime, 30, 60, 15); + connections, + new ConnectionPickArgument(shiftStartTime, 30, 60, 15), + loggerMock.Object); // Assert result.ShouldNotBeNull(); @@ -113,12 +126,13 @@ public class TransportConnectionCalculatorTests public void FindBestConnectionEnhanced_WithEmptyConnections_ShouldReturnNull() { // Arrange - var connections = new List(); var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); // Act var result = TransportConnectionCalculator.FindBestConnection( - connections, shiftStartTime, 30, 60, 15); + new List(), + new ConnectionPickArgument(shiftStartTime, 30, 60, 15), + loggerMock.Object); // Assert result.ShouldBeNull(); @@ -130,16 +144,18 @@ public class TransportConnectionCalculatorTests // Arrange - Test scenario where both valid and late connections exist var connections = new List { - new TransportConnection { ArrivalTime = "2023-12-15T06:30:00" }, // Too early (90 min before shift) - new TransportConnection { ArrivalTime = "2023-12-15T07:25:00" }, // Valid but would be considered too early - new TransportConnection { ArrivalTime = "2023-12-15T08:05:00" }, // Late but acceptable - new TransportConnection { ArrivalTime = "2023-12-15T08:20:00" } // Too late + new(arrivalTime: "2023-12-15T06:30:00"), // Too early (90 min before shift) + new(arrivalTime: "2023-12-15T07:25:00"), // Valid but would be considered too early + new(arrivalTime: "2023-12-15T08:05:00"), // Late but acceptable + new(arrivalTime: "2023-12-15T08:20:00") // Too late }; var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0); // Act - MaxEarlyArrivalMinutes = 30, so earliest acceptable is 7:30 var result = TransportConnectionCalculator.FindBestConnection( - connections, shiftStartTime, 30, 30, 15); + connections, + new ConnectionPickArgument(shiftStartTime, 30, 30, 15), + loggerMock.Object); // Assert - 7:25 arrives at 7:25, which is 35 min before shift (more than 30 min), so should prefer late connection result.ShouldNotBeNull();