aboutsummaryrefslogtreecommitdiff
path: root/src/Enmarcha.Backend/Services/Processors/VigoUsageProcessor.cs
blob: 7f98fff8402c1f0ad0f3814df341c58d47b617a9 (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
using System.Text.Json;
using CsvHelper;
using Enmarcha.Backend.Types.Arrivals;
using Microsoft.Extensions.Caching.Memory;
using System.Globalization;

namespace Enmarcha.Backend.Services.Processors;

public class VigoUsageProcessor : IArrivalsProcessor
{
    private readonly HttpClient _httpClient;
    private readonly IMemoryCache _cache;
    private readonly ILogger<VigoUsageProcessor> _logger;
    private readonly IHostEnvironment _environment;
    private readonly FeedService _feedService;
    private static readonly HashSet<string> _vigoStopsWhitelist = [];
    private static bool _whitelistLoaded = false;
    private static readonly object _lock = new();

    public VigoUsageProcessor(
        HttpClient httpClient,
        IMemoryCache cache,
        ILogger<VigoUsageProcessor> logger,
        IHostEnvironment environment,
        FeedService feedService)
    {
        _httpClient = httpClient;
        _cache = cache;
        _logger = logger;
        _environment = environment;
        _feedService = feedService;

        LoadWhitelist();
    }

    private void LoadWhitelist()
    {
        if (_whitelistLoaded) return;

        lock (_lock)
        {
            if (_whitelistLoaded) return;

            try
            {
                var path = Path.Combine(_environment.ContentRootPath, "Data", "vitrasa_stops_p95.csv");
                if (File.Exists(path))
                {
                    using var reader = new StreamReader(path);
                    using var csv = new CsvReader(reader, CultureInfo.InvariantCulture);
                    csv.Read();
                    csv.ReadHeader();
                    while (csv.Read())
                    {
                        var code = csv.GetField("codigo");
                        if (!string.IsNullOrWhiteSpace(code))
                        {
                            _vigoStopsWhitelist.Add(code.Trim());
                        }
                    }
                    _logger.LogInformation("Loaded {Count} Vigo stops for usage data whitelist", _vigoStopsWhitelist.Count);
                }
                else
                {
                    _logger.LogWarning("Vigo stops whitelist CSV not found at {Path}", path);
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "Error loading Vigo stops whitelist");
            }
            finally
            {
                _whitelistLoaded = true;
            }
        }
    }

    public async Task ProcessAsync(ArrivalsContext context)
    {
        if (!context.StopId.StartsWith("vitrasa:")) return;

        var normalizedCode = _feedService.NormalizeStopCode("vitrasa", context.StopCode);
        if (!_vigoStopsWhitelist.Contains(normalizedCode)) return;

        var cacheKey = $"vigo_usage_{normalizedCode}";
        if (_cache.TryGetValue(cacheKey, out List<BusStopUsagePoint>? cachedUsage))
        {
            context.Usage = cachedUsage;
            return;
        }

        try
        {
            using var activity = Telemetry.Source.StartActivity("FetchVigoUsage");
            var url = $"https://datos.vigo.org/vci_api_app/api2.jsp?tipo=TRANSPORTE_PARADA_HORAS_USO&parada={normalizedCode}";
            var response = await _httpClient.GetAsync(url);

            if (response.IsSuccessStatusCode)
            {
                var json = await response.Content.ReadAsStringAsync();
                var usage = JsonSerializer.Deserialize<List<BusStopUsagePoint>>(json);

                if (usage != null)
                {
                    _cache.Set(cacheKey, usage, TimeSpan.FromDays(7));
                    context.Usage = usage;
                }
            }
            else
            {
                _logger.LogWarning("Failed to fetch usage data for stop {StopCode}, status: {Status}", normalizedCode, response.StatusCode);
            }
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "Error fetching usage data for Vigo stop {StopCode}", normalizedCode);
        }
    }
}