Add logger
This commit is contained in:
parent
3c01b33070
commit
f1d7d75ab6
4 changed files with 76 additions and 43 deletions
|
|
@ -28,17 +28,11 @@ namespace ShiftScheduler.Services
|
||||||
|
|
||||||
foreach (var connection in allConnections)
|
foreach (var connection in allConnections)
|
||||||
{
|
{
|
||||||
logger.LogInformation($"Found connection {connection}");
|
logger.LogDebug($"Found connection {connection}");
|
||||||
}
|
}
|
||||||
|
|
||||||
var bestConnection = TransportConnectionCalculator.FindBestConnection(
|
var bestConnection = TransportConnectionCalculator.FindBestConnection(
|
||||||
allConnections,
|
allConnections, new ConnectionPickArgument(shiftStartTime, config.SafetyBufferMinutes, config.MaxEarlyArrivalMinutes, config.MaxLateArrivalMinutes), logger);
|
||||||
shiftStartTime,
|
|
||||||
config.SafetyBufferMinutes,
|
|
||||||
config.MaxEarlyArrivalMinutes,
|
|
||||||
config.MaxLateArrivalMinutes);
|
|
||||||
|
|
||||||
logger.LogInformation($"Found best {bestConnection}");
|
|
||||||
|
|
||||||
return bestConnection;
|
return bestConnection;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,19 +1,34 @@
|
||||||
|
using System.Globalization;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
using ShiftScheduler.Shared;
|
using ShiftScheduler.Shared;
|
||||||
|
|
||||||
namespace ShiftScheduler.Services;
|
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 class TransportConnectionCalculator
|
||||||
{
|
{
|
||||||
public static TransportConnection? FindBestConnection(
|
public static TransportConnection? FindBestConnection(
|
||||||
IReadOnlyList<TransportConnection> connections,
|
IReadOnlyList<TransportConnection> connections, ConnectionPickArgument args, ILogger logger)
|
||||||
DateTime shiftStartTime,
|
|
||||||
int safetyBufferMinutes,
|
|
||||||
int maxEarlyArrivalMinutes,
|
|
||||||
int maxLateArrivalMinutes)
|
|
||||||
{
|
{
|
||||||
var latestArrivalTime = shiftStartTime.AddMinutes(-safetyBufferMinutes);
|
var latestArrivalTime = args.ShiftStartTime.AddMinutes(-args.SafetyBufferMinutes);
|
||||||
var earliestAcceptableTime = shiftStartTime.AddMinutes(-maxEarlyArrivalMinutes);
|
var earliestAcceptableTime = args.ShiftStartTime.AddMinutes(-args.MaxEarlyArrivalMinutes);
|
||||||
var latestAcceptableTime = shiftStartTime.AddMinutes(maxLateArrivalMinutes);
|
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<TransportConnection>();
|
var validConnections = new List<TransportConnection>();
|
||||||
var lateValidConnections = new List<TransportConnection>();
|
var lateValidConnections = new List<TransportConnection>();
|
||||||
|
|
@ -67,7 +82,6 @@ public static class TransportConnectionCalculator
|
||||||
return sortedLateValid.First();
|
return sortedLateValid.First();
|
||||||
}
|
}
|
||||||
|
|
||||||
// No suitable connections found
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -14,6 +14,15 @@ namespace ShiftScheduler.Shared
|
||||||
|
|
||||||
public class TransportConnection
|
public class TransportConnection
|
||||||
{
|
{
|
||||||
|
public TransportConnection()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public TransportConnection(string arrivalTime)
|
||||||
|
{
|
||||||
|
ArrivalTime = arrivalTime;
|
||||||
|
}
|
||||||
|
|
||||||
public string DepartureTime { get; set; } = string.Empty;
|
public string DepartureTime { get; set; } = string.Empty;
|
||||||
public string ArrivalTime { get; set; } = string.Empty;
|
public string ArrivalTime { get; set; } = string.Empty;
|
||||||
public string Duration { get; set; } = string.Empty;
|
public string Duration { get; set; } = string.Empty;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
using ShiftScheduler.Services;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Moq;
|
||||||
using ShiftScheduler.Shared;
|
using ShiftScheduler.Shared;
|
||||||
using Shouldly;
|
using Shouldly;
|
||||||
|
|
||||||
|
|
@ -6,6 +7,8 @@ namespace ShiftScheduler.Services.Tests;
|
||||||
|
|
||||||
public class TransportConnectionCalculatorTests
|
public class TransportConnectionCalculatorTests
|
||||||
{
|
{
|
||||||
|
private readonly Mock<ILogger> loggerMock = new();
|
||||||
|
|
||||||
// Tests for new enhanced logic
|
// Tests for new enhanced logic
|
||||||
[Fact]
|
[Fact]
|
||||||
public void FindBestConnectionEnhanced_WithGoodTimingConnection_ShouldReturnLatestValidConnection()
|
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
|
// Arrange - Shift starts at 8:00, safety buffer 30 min, so latest arrival is 7:30
|
||||||
var connections = new List<TransportConnection>
|
var connections = new List<TransportConnection>
|
||||||
{
|
{
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T07:15:00" },
|
new(arrivalTime: "2023-12-15T07:15:00"),
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T07:25:00" }, // Should be selected (latest valid)
|
new(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-15T08:05:00") // Too late
|
||||||
};
|
};
|
||||||
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = TransportConnectionCalculator.FindBestConnection(
|
var result = TransportConnectionCalculator.FindBestConnection(
|
||||||
connections, shiftStartTime, 30, 60, 15);
|
connections,
|
||||||
|
new ConnectionPickArgument(shiftStartTime, 30, 60, 15),
|
||||||
|
loggerMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
result.ShouldNotBeNull();
|
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
|
// Arrange - Shift starts at 8:00, max early arrival 60 min, so earliest acceptable is 7:00
|
||||||
var connections = new List<TransportConnection>
|
var connections = new List<TransportConnection>
|
||||||
{
|
{
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T06:45:00" }, // Too early (more than 60 min before 8:00)
|
new(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-15T08:05:00") // Within acceptable late range (15 min after 8:00)
|
||||||
};
|
};
|
||||||
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = TransportConnectionCalculator.FindBestConnection(
|
var result = TransportConnectionCalculator.FindBestConnection(
|
||||||
connections, shiftStartTime, 30, 60, 15);
|
connections,
|
||||||
|
new ConnectionPickArgument(shiftStartTime, 30, 60, 15),
|
||||||
|
loggerMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
result.ShouldNotBeNull();
|
result.ShouldNotBeNull();
|
||||||
|
|
@ -54,15 +61,17 @@ public class TransportConnectionCalculatorTests
|
||||||
// Arrange - Shift starts at 8:00, no valid early connections
|
// Arrange - Shift starts at 8:00, no valid early connections
|
||||||
var connections = new List<TransportConnection>
|
var connections = new List<TransportConnection>
|
||||||
{
|
{
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T08:10:00" },
|
new(arrivalTime: "2023-12-15T08:10:00"),
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T08:05:00" }, // Should be selected (earliest late)
|
new(arrivalTime: "2023-12-15T08:05:00"), // Should be selected (earliest late)
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T08:12:00" }
|
new(arrivalTime: "2023-12-15T08:12:00")
|
||||||
};
|
};
|
||||||
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = TransportConnectionCalculator.FindBestConnection(
|
var result = TransportConnectionCalculator.FindBestConnection(
|
||||||
connections, shiftStartTime, 30, 60, 15);
|
connections,
|
||||||
|
new ConnectionPickArgument(shiftStartTime, 30, 60, 15),
|
||||||
|
loggerMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
result.ShouldNotBeNull();
|
result.ShouldNotBeNull();
|
||||||
|
|
@ -75,15 +84,17 @@ public class TransportConnectionCalculatorTests
|
||||||
// Arrange - All connections are too late (beyond max late arrival)
|
// Arrange - All connections are too late (beyond max late arrival)
|
||||||
var connections = new List<TransportConnection>
|
var connections = new List<TransportConnection>
|
||||||
{
|
{
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T08:20:00" },
|
new(arrivalTime: "2023-12-15T08:20:00"),
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T08:25:00" },
|
new(arrivalTime: "2023-12-15T08:25:00"),
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T08:30:00" }
|
new(arrivalTime: "2023-12-15T08:30:00")
|
||||||
};
|
};
|
||||||
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = TransportConnectionCalculator.FindBestConnection(
|
var result = TransportConnectionCalculator.FindBestConnection(
|
||||||
connections, shiftStartTime, 30, 60, 15);
|
connections,
|
||||||
|
new ConnectionPickArgument(shiftStartTime, 30, 60, 15),
|
||||||
|
loggerMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
result.ShouldBeNull();
|
result.ShouldBeNull();
|
||||||
|
|
@ -95,14 +106,16 @@ public class TransportConnectionCalculatorTests
|
||||||
// Arrange - Early connection is acceptable (not too early), should prefer it over late
|
// Arrange - Early connection is acceptable (not too early), should prefer it over late
|
||||||
var connections = new List<TransportConnection>
|
var connections = new List<TransportConnection>
|
||||||
{
|
{
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T07:20:00" }, // 40 min before shift - acceptable
|
new(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-15T08:05:00") // 5 min after shift
|
||||||
};
|
};
|
||||||
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = TransportConnectionCalculator.FindBestConnection(
|
var result = TransportConnectionCalculator.FindBestConnection(
|
||||||
connections, shiftStartTime, 30, 60, 15);
|
connections,
|
||||||
|
new ConnectionPickArgument(shiftStartTime, 30, 60, 15),
|
||||||
|
loggerMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
result.ShouldNotBeNull();
|
result.ShouldNotBeNull();
|
||||||
|
|
@ -113,12 +126,13 @@ public class TransportConnectionCalculatorTests
|
||||||
public void FindBestConnectionEnhanced_WithEmptyConnections_ShouldReturnNull()
|
public void FindBestConnectionEnhanced_WithEmptyConnections_ShouldReturnNull()
|
||||||
{
|
{
|
||||||
// Arrange
|
// Arrange
|
||||||
var connections = new List<TransportConnection>();
|
|
||||||
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
var result = TransportConnectionCalculator.FindBestConnection(
|
var result = TransportConnectionCalculator.FindBestConnection(
|
||||||
connections, shiftStartTime, 30, 60, 15);
|
new List<TransportConnection>(),
|
||||||
|
new ConnectionPickArgument(shiftStartTime, 30, 60, 15),
|
||||||
|
loggerMock.Object);
|
||||||
|
|
||||||
// Assert
|
// Assert
|
||||||
result.ShouldBeNull();
|
result.ShouldBeNull();
|
||||||
|
|
@ -130,16 +144,18 @@ public class TransportConnectionCalculatorTests
|
||||||
// Arrange - Test scenario where both valid and late connections exist
|
// Arrange - Test scenario where both valid and late connections exist
|
||||||
var connections = new List<TransportConnection>
|
var connections = new List<TransportConnection>
|
||||||
{
|
{
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T06:30:00" }, // Too early (90 min before shift)
|
new(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(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(arrivalTime: "2023-12-15T08:05:00"), // Late but acceptable
|
||||||
new TransportConnection { ArrivalTime = "2023-12-15T08:20:00" } // Too late
|
new(arrivalTime: "2023-12-15T08:20:00") // Too late
|
||||||
};
|
};
|
||||||
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
var shiftStartTime = new DateTime(2023, 12, 15, 8, 0, 0);
|
||||||
|
|
||||||
// Act - MaxEarlyArrivalMinutes = 30, so earliest acceptable is 7:30
|
// Act - MaxEarlyArrivalMinutes = 30, so earliest acceptable is 7:30
|
||||||
var result = TransportConnectionCalculator.FindBestConnection(
|
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
|
// Assert - 7:25 arrives at 7:25, which is 35 min before shift (more than 30 min), so should prefer late connection
|
||||||
result.ShouldNotBeNull();
|
result.ShouldNotBeNull();
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue