1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
|
using System.Net;
using Enmarcha.Sources.OpenTripPlannerGql;
using Enmarcha.Sources.OpenTripPlannerGql.Queries;
using Enmarcha.Backend.Configuration;
using Enmarcha.Backend.Helpers;
using Enmarcha.Backend.Services;
using Enmarcha.Backend.Types;
using Enmarcha.Backend.Types.Arrivals;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
namespace Enmarcha.Backend.Controllers;
[ApiController]
[Route("api/stops")]
public partial class ArrivalsController : ControllerBase
{
private readonly ILogger<ArrivalsController> _logger;
private readonly IMemoryCache _cache;
private readonly HttpClient _httpClient;
private readonly ArrivalsPipeline _pipeline;
private readonly FeedService _feedService;
private readonly AppConfiguration _config;
public ArrivalsController(
ILogger<ArrivalsController> logger,
IMemoryCache cache,
HttpClient httpClient,
ArrivalsPipeline pipeline,
FeedService feedService,
IOptions<AppConfiguration> configOptions
)
{
_logger = logger;
_cache = cache;
_httpClient = httpClient;
_pipeline = pipeline;
_feedService = feedService;
_config = configOptions.Value;
}
[HttpGet("arrivals")]
public async Task<IActionResult> GetArrivals(
[FromQuery] string id,
[FromQuery] bool reduced
)
{
using var activity = Telemetry.Source.StartActivity("GetArrivals");
activity?.SetTag("stop.id", id);
activity?.SetTag("reduced", reduced);
var tz = TimeZoneInfo.FindSystemTimeZoneById("Europe/Madrid");
var nowLocal = TimeZoneInfo.ConvertTime(DateTime.UtcNow, tz);
var todayLocal = nowLocal.Date;
var requestContent = ArrivalsAtStopContent.Query(
new ArrivalsAtStopContent.Args(id, reduced)
);
var request = new HttpRequestMessage(HttpMethod.Post, $"{_config.OpenTripPlannerBaseUrl}/gtfs/v1");
request.Content = JsonContent.Create(new GraphClientRequest
{
Query = requestContent
});
var response = await _httpClient.SendAsync(request);
var responseBody = await response.Content.ReadFromJsonAsync<GraphClientResponse<ArrivalsAtStopResponse>>();
if (responseBody is not { IsSuccess: true } || responseBody.Data?.Stop == null)
{
activity?.SetStatus(System.Diagnostics.ActivityStatusCode.Error, "Error fetching stop data from OTP");
LogErrorFetchingStopData(response.StatusCode, await response.Content.ReadAsStringAsync());
return StatusCode(500, "Error fetching stop data");
}
var stop = responseBody.Data.Stop;
_logger.LogInformation("Fetched {Count} arrivals for stop {StopName} ({StopId})", stop.Arrivals.Count, stop.Name, id);
activity?.SetTag("arrivals.count", stop.Arrivals.Count);
List<Arrival> arrivals = [];
foreach (var item in stop.Arrivals)
{
// Discard trip without pickup at stop
if (item.PickupTypeParsed.Equals(ArrivalsAtStopResponse.PickupType.None))
{
continue;
}
// Discard on last stop
if (item.Trip.ArrivalStoptime.Stop.GtfsId == id)
{
continue;
}
// Calculate departure time using the service day in the feed's timezone (Europe/Madrid)
// This ensures we treat ScheduledDepartureSeconds as relative to the local midnight of the service day
var serviceDayLocal = TimeZoneInfo.ConvertTime(DateTimeOffset.FromUnixTimeSeconds(item.ServiceDay), tz);
var departureTime = serviceDayLocal.Date.AddSeconds(item.ScheduledDepartureSeconds);
var minutesToArrive = (int)(departureTime - nowLocal).TotalMinutes;
//var isRunning = departureTime < nowLocal;
Arrival arrival = new()
{
TripId = item.Trip.GtfsId,
Route = new RouteInfo
{
GtfsId = item.Trip.Route.GtfsId,
ShortName = item.Trip.RouteShortName,
Colour = item.Trip.Route.Color ?? "FFFFFF",
TextColour = item.Trip.Route.TextColor ?? "000000"
},
Headsign = new HeadsignInfo
{
Destination = item.Headsign
},
Estimate = new ArrivalDetails
{
Minutes = minutesToArrive,
Precision = departureTime < nowLocal.AddMinutes(-1) ? ArrivalPrecision.Past : ArrivalPrecision.Scheduled
},
RawOtpTrip = item
};
arrivals.Add(arrival);
}
await _pipeline.ExecuteAsync(new ArrivalsContext
{
StopId = id,
StopCode = stop.Code,
IsReduced = reduced,
Arrivals = arrivals,
NowLocal = nowLocal,
StopLocation = new Position { Latitude = stop.Lat, Longitude = stop.Lon }
});
var feedId = id.Split(':')[0];
// Time after an arrival's time to still include it in the response. This is useful without real-time data, for delayed buses.
var timeThreshold = GetThresholdForFeed(id);
var (fallbackColor, fallbackTextColor) = _feedService.GetFallbackColourForFeed(feedId);
return Ok(new StopArrivalsResponse
{
StopCode = _feedService.NormalizeStopCode(feedId, stop.Code),
StopName = _feedService.NormalizeStopName(feedId, stop.Name),
StopLocation = new Position
{
Latitude = stop.Lat,
Longitude = stop.Lon
},
Routes = [.. stop.Routes
.OrderBy(
r => r.ShortName,
Comparer<string?>.Create(SortingHelper.SortRouteShortNames)
)
.Select(r => new RouteInfo
{
GtfsId = r.GtfsId,
ShortName = _feedService.NormalizeRouteShortName(feedId, r.ShortName ?? ""),
Colour = r.Color ?? fallbackColor,
TextColour = r.TextColor is null or "000000" ?
ContrastHelper.GetBestTextColour(r.Color ?? fallbackColor) :
r.TextColor
})],
Arrivals = [.. arrivals.Where(a => a.Estimate.Minutes >= timeThreshold)]
});
}
private static int GetThresholdForFeed(string id)
{
string feedId = id.Split(':', 2)[0];
if (feedId is "vitrasa" or "tranvias" or "tussa")
{
return 0;
}
return -30;
}
[HttpGet]
public async Task<IActionResult> GetStops([FromQuery] string ids)
{
if (string.IsNullOrWhiteSpace(ids))
{
return BadRequest("Ids parameter is required");
}
var stopIds = ids.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var requestContent = StopsInfoContent.Query(new StopsInfoContent.Args(stopIds));
var request = new HttpRequestMessage(HttpMethod.Post, $"{_config.OpenTripPlannerBaseUrl}/gtfs/v1");
request.Content = JsonContent.Create(new GraphClientRequest
{
Query = requestContent
});
var response = await _httpClient.SendAsync(request);
var responseBody = await response.Content.ReadFromJsonAsync<GraphClientResponse<StopsInfoResponse>>();
if (responseBody is not { IsSuccess: true } || responseBody.Data?.Stops == null)
{
return StatusCode(500, "Error fetching stops data");
}
var result = responseBody.Data.Stops.ToDictionary(
s => s.GtfsId,
s =>
{
var feedId = s.GtfsId.Split(':', 2)[0];
var (fallbackColor, _) = _feedService.GetFallbackColourForFeed(feedId);
return new
{
id = s.GtfsId,
code = _feedService.NormalizeStopCode(feedId, s.Code ?? ""),
name = s.Name,
routes = s.Routes
.OrderBy(r => r.ShortName, Comparer<string?>.Create(SortingHelper.SortRouteShortNames))
.Select(r => new
{
shortName = _feedService.NormalizeRouteShortName(feedId, r.ShortName ?? ""),
colour = r.Color ?? fallbackColor,
textColour = r.TextColor is null or "000000" ?
ContrastHelper.GetBestTextColour(r.Color ?? fallbackColor) :
r.TextColor
})
.ToList()
};
}
);
return Ok(result);
}
[HttpGet("search")]
public IActionResult SearchStops([FromQuery] string q)
{
// Placeholder for future implementation with Postgres and fuzzy searching
return Ok(new List<object>());
}
[LoggerMessage(LogLevel.Error, "Error fetching stop data, received {statusCode} {responseBody}")]
partial void LogErrorFetchingStopData(HttpStatusCode statusCode, string responseBody);
}
|