diff options
| author | Copilot <198982749+Copilot@users.noreply.github.com> | 2025-11-06 19:42:29 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-11-06 19:42:29 +0100 |
| commit | 006201d8b9e5c4f8fd4547d5b0d67091d8df97f9 (patch) | |
| tree | 53e7057993418701363baac44a40aa218d31a6c0 /src | |
| parent | aae873518de96f9fd0acfd8c03d921dabf02a84a (diff) | |
Skip scheduled trips with unparseable GTFS times (#71)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: arielcostas <94913521+arielcostas@users.noreply.github.com>
Diffstat (limited to 'src')
4 files changed, 30 insertions, 19 deletions
diff --git a/src/Costasdev.Busurbano.Backend/Configuration/AppConfiguration.cs b/src/Costasdev.Busurbano.Backend/Configuration/AppConfiguration.cs index 97296e5..9fa5e75 100644 --- a/src/Costasdev.Busurbano.Backend/Configuration/AppConfiguration.cs +++ b/src/Costasdev.Busurbano.Backend/Configuration/AppConfiguration.cs @@ -1,4 +1,4 @@ -namespace Costasdev.Busurbano.Backend.Configuration; +namespace Costasdev.Busurbano.Backend.Configuration; public class AppConfiguration { diff --git a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs index 3bb1ba0..4d57cc1 100644 --- a/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs +++ b/src/Costasdev.Busurbano.Backend/Controllers/VigoController.cs @@ -99,7 +99,10 @@ public class VigoController : ControllerBase await Task.WhenAll(realtimeTask, timetableTask); var realTimeEstimates = realtimeTask.Result.Estimates; - var timetable = timetableTask.Result; + // Filter out records with unparseable times (e.g., hours >= 24) + var timetable = timetableTask.Result + .Where(c => c.StartingDateTime() != null && c.CallingDateTime() != null) + .ToList(); var now = DateTime.Now.AddSeconds(60 - DateTime.Now.Second); // Define the scope end as the time of the last realtime arrival (no extra buffer) @@ -118,7 +121,7 @@ public class VigoController : ControllerBase c.Line.Trim() == estimate.Line.Trim() && c.Route.Trim() == estimate.Route.Trim() ) - .OrderBy(c => c.CallingDateTime()) + .OrderBy(c => c.CallingDateTime()!.Value) .ToArray(); ScheduledStop? closestCirculation = null; @@ -130,11 +133,11 @@ public class VigoController : ControllerBase const int startedMatchToleranceMinutes = 15; // how close a started trip must be to consider it a match var startedCandidates = possibleCirculations - .Where(c => c.StartingDateTime() <= now) + .Where(c => c.StartingDateTime()!.Value <= now) .Select(c => new { Circulation = c, - AbsDiff = Math.Abs((estimatedArrivalTime - c.CallingDateTime()).TotalMinutes) + AbsDiff = Math.Abs((estimatedArrivalTime - c.CallingDateTime()!.Value).TotalMinutes) }) .OrderBy(x => x.AbsDiff) .ToList(); @@ -142,7 +145,7 @@ public class VigoController : ControllerBase var bestStarted = startedCandidates.FirstOrDefault(); var futureCandidates = possibleCirculations - .Where(c => c.StartingDateTime() > now) + .Where(c => c.StartingDateTime()!.Value > now) .ToList(); if (bestStarted != null && bestStarted.AbsDiff <= startedMatchToleranceMinutes) @@ -181,7 +184,7 @@ public class VigoController : ControllerBase foreach (var circulation in possibleCirculations) { outputBuffer.AppendLine( - $"Circulation {circulation.TripId} stopping at {circulation.CallingDateTime()} (diff: {estimatedArrivalTime - circulation.CallingDateTime():HH:mm})"); + $"Circulation {circulation.TripId} stopping at {circulation.CallingDateTime()!.Value} (diff: {estimatedArrivalTime - circulation.CallingDateTime()!.Value:HH:mm})"); } outputBuffer.AppendLine(); @@ -195,8 +198,8 @@ public class VigoController : ControllerBase Route = estimate.Route, Schedule = new ScheduleData { - Running = closestCirculation.StartingDateTime() <= now, - Minutes = (int)(closestCirculation.CallingDateTime() - now).TotalMinutes, + Running = closestCirculation.StartingDateTime()!.Value <= now, + Minutes = (int)(closestCirculation.CallingDateTime()!.Value - now).TotalMinutes, TripId = closestCirculation.TripId, ServiceId = closestCirculation.ServiceId, }, @@ -218,8 +221,8 @@ public class VigoController : ControllerBase ); var scheduledWindow = timetable - .Where(c => c.CallingDateTime() >= now && c.CallingDateTime() <= scopeEnd) - .OrderBy(c => c.CallingDateTime()); + .Where(c => c.CallingDateTime()!.Value >= now && c.CallingDateTime()!.Value <= scopeEnd) + .OrderBy(c => c.CallingDateTime()!.Value); foreach (var sched in scheduledWindow) { @@ -234,8 +237,8 @@ public class VigoController : ControllerBase Route = sched.Route, Schedule = new ScheduleData { - Running = sched.StartingDateTime() <= now, - Minutes = (int)(sched.CallingDateTime() - now).TotalMinutes, + Running = sched.StartingDateTime()!.Value <= now, + Minutes = (int)(sched.CallingDateTime()!.Value - now).TotalMinutes, TripId = sched.TripId, ServiceId = sched.ServiceId, }, diff --git a/src/Costasdev.Busurbano.Backend/Types/ConsolidatedCirculation.cs b/src/Costasdev.Busurbano.Backend/Types/ConsolidatedCirculation.cs index ed2374f..7cc79c0 100644 --- a/src/Costasdev.Busurbano.Backend/Types/ConsolidatedCirculation.cs +++ b/src/Costasdev.Busurbano.Backend/Types/ConsolidatedCirculation.cs @@ -1,4 +1,4 @@ -namespace Costasdev.Busurbano.Backend.Types; +namespace Costasdev.Busurbano.Backend.Types; public class ConsolidatedCirculation { diff --git a/src/Costasdev.Busurbano.Backend/Types/VigoSchedules.cs b/src/Costasdev.Busurbano.Backend/Types/VigoSchedules.cs index 25fc34f..76c8fa1 100644 --- a/src/Costasdev.Busurbano.Backend/Types/VigoSchedules.cs +++ b/src/Costasdev.Busurbano.Backend/Types/VigoSchedules.cs @@ -1,4 +1,4 @@ -using System.Text.Json.Serialization; +using System.Text.Json.Serialization; namespace Costasdev.Busurbano.Backend.Types; @@ -18,17 +18,25 @@ public class ScheduledStop [JsonPropertyName("starting_code")] public required string StartingCode { get; set; } [JsonPropertyName("starting_name")] public required string StartingName { get; set; } [JsonPropertyName("starting_time")] public required string StartingTime { get; set; } - public DateTime StartingDateTime() + public DateTime? StartingDateTime() { - var dt = DateTime.Today + TimeOnly.Parse(StartingTime).ToTimeSpan(); + if (!TimeOnly.TryParse(StartingTime, out var time)) + { + return null; + } + var dt = DateTime.Today + time.ToTimeSpan(); return dt.AddSeconds(60 - dt.Second); } [JsonPropertyName("calling_ssm")] public required int CallingSsm { get; set; } [JsonPropertyName("calling_time")] public required string CallingTime { get; set; } - public DateTime CallingDateTime() + public DateTime? CallingDateTime() { - var dt = DateTime.Today + TimeOnly.Parse(CallingTime).ToTimeSpan(); + if (!TimeOnly.TryParse(CallingTime, out var time)) + { + return null; + } + var dt = DateTime.Today + time.ToTimeSpan(); return dt.AddSeconds(60 - dt.Second); } |
