aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Controllers/ArrivalsController.cs
blob: 50d4012e0accc48a9bc77438551aa2951e852a38 (plain)
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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 FuzzySharp;
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;
    private readonly OtpService _otpService;

    public ArrivalsController(
        ILogger<ArrivalsController> logger,
        IMemoryCache cache,
        HttpClient httpClient,
        ArrivalsPipeline pipeline,
        FeedService feedService,
        IOptions<AppConfiguration> configOptions,
        OtpService otpService
    )
    {
        _logger = logger;
        _cache = cache;
        _httpClient = httpClient;
        _pipeline = pipeline;
        _feedService = feedService;
        _config = configOptions.Value;
        _otpService = otpService;
    }

    [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.Trip.TripHeadsign ?? item.Headsign,
                },
                Estimate = new ArrivalDetails
                {
                    Minutes = minutesToArrive,
                    Precision = departureTime < nowLocal.AddMinutes(-1) ? ArrivalPrecision.Past : ArrivalPrecision.Scheduled
                },
                RawOtpTrip = item
            };

            arrivals.Add(arrival);
        }

        var context = new ArrivalsContext
        {
            StopId = id,
            StopCode = stop.Code,
            IsReduced = reduced,
            Arrivals = arrivals,
            NowLocal = nowLocal,
            StopLocation = new Position { Latitude = stop.Lat, Longitude = stop.Lon }
        };

        await _pipeline.ExecuteAsync(context);

        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 = [.. _feedService.ConsolidateRoutes(feedId,
                stop.Routes
                    .OrderBy(r => SortingHelper.GetRouteSortKey(r.ShortName, r.GtfsId))
                    .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)],
            Usage = context.Usage
        });

    }

    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 = _feedService.ConsolidateRoutes(feedId,
                        s.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
                            }))
                        .Select(r => new
                        {
                            shortName = r.ShortName,
                            colour = r.Colour,
                            textColour = r.TextColour
                        })
                        .ToList()
                };
            }
        );

        return Ok(result);
    }

    [HttpGet("search")]
    public async Task<IActionResult> SearchStops([FromQuery] string q)
    {
        if (string.IsNullOrWhiteSpace(q))
        {
            return Ok(new List<object>());
        }

        const string cacheKey = "arrivals_search_mapped_stops";
        if (!_cache.TryGetValue(cacheKey, out List<dynamic>? allStops) || allStops == null)
        {
            var allStopsRaw = await _otpService.GetStopsByBboxAsync(-9.3, 41.7, -6.7, 43.8);

            allStops = allStopsRaw.Select(s =>
            {
                var feedId = s.GtfsId.Split(':', 2)[0];
                var (fallbackColor, _) = _feedService.GetFallbackColourForFeed(feedId);
                var code = _feedService.NormalizeStopCode(feedId, s.Code ?? "");
                var name = _feedService.NormalizeStopName(feedId, s.Name);

                return (dynamic)new
                {
                    stopId = s.GtfsId,
                    stopCode = code,
                    name = name,
                    latitude = s.Lat,
                    longitude = s.Lon,
                    lines = _feedService.ConsolidateRoutes(feedId,
                        (s.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
                            }))
                        .Select(r => new
                        {
                            line = r.ShortName,
                            colour = r.Colour,
                            textColour = r.TextColour
                        })
                        .ToList(),
                    label = string.IsNullOrWhiteSpace(code) ? name : $"{name} ({code})"
                };
            }).ToList();

            _cache.Set(cacheKey, allStops, TimeSpan.FromHours(1));
        }

        // 1. Exact or prefix matches by stop code
        var codeMatches = allStops
            .Where(s => s.stopCode != null && ((string)s.stopCode).StartsWith(q, StringComparison.OrdinalIgnoreCase))
            .OrderBy(s => ((string)s.stopCode).Length)
            .Take(10)
            .ToList();

        // 2. Fuzzy search stops by label
        var fuzzyResults = Process.ExtractSorted(
            q,
            allStops.Select(s => (string)s.label),
            cutoff: 60
        ).Take(15).Select(r => allStops[r.Index]).ToList();

        // Combine and deduplicate
        var results = codeMatches.Concat(fuzzyResults)
            .GroupBy(s => s.stopId)
            .Select(g => g.First())
            .Take(20)
            .ToList();

        return Ok(results);
    }

    [LoggerMessage(LogLevel.Error, "Error fetching stop data, received {statusCode} {responseBody}")]
    partial void LogErrorFetchingStopData(HttpStatusCode statusCode, string responseBody);
}