ASP.NET Core 10 API and React client that visualize the canton of Thurgau's hourly traffic counts by vehicle class (data.tg.ch, CC0). - Background refresh from data.tg.ch every 6 h into a per-day JSON cache, so history grows beyond the source's two-week window - Road geometry per station direction from swisstopo swissTLM3D, oriented by geocoding the direction's target town; roundabout arms follow the road outward only - Leaflet map with canvas animation: dots drive along the road in both directions, density by hourly count, colour by vehicle class, right-hand traffic; play/pause, time slider and day selection - Station panel with per-class counts and Chart.js charts (hourly, weekday/weekend profile, daily totals, class mix) - swisstopo base map switcher (grey, colour, aerial) - Dockerfile and docker-compose with a data volume - xUnit tests for mapping, cache, geometry, geocoding and analysis Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
73 lines
2.7 KiB
C#
73 lines
2.7 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Schaad.OpenData.Traffic.Geometry;
|
|
using Schaad.OpenData.Traffic.Models;
|
|
using Schaad.OpenData.Traffic.Source;
|
|
using Schaad.OpenData.Traffic.Storage;
|
|
|
|
namespace Schaad.OpenData.Traffic;
|
|
|
|
public class TrafficRefresher(
|
|
ThurgauTrafficClient client,
|
|
TrafficCacheStorage storage,
|
|
TrafficDataStore store,
|
|
StationGeometryService geometryService,
|
|
TimeProvider timeProvider,
|
|
ILogger<TrafficRefresher> logger)
|
|
{
|
|
public async Task LoadCacheAsync(CancellationToken cancellationToken)
|
|
{
|
|
var snapshot = await storage.LoadAsync(cancellationToken);
|
|
store.Replace(snapshot);
|
|
logger.LogInformation("Loaded traffic cache with {Stations} stations and {Days} days", snapshot.Stations.Count, snapshot.Dates.Length);
|
|
}
|
|
|
|
public bool IsRefreshDue(TimeSpan interval) =>
|
|
store.Current.LastRefresh is not { } lastRefresh || timeProvider.GetUtcNow() - lastRefresh >= interval;
|
|
|
|
public async Task RefreshAsync(CancellationToken cancellationToken)
|
|
{
|
|
var current = store.Current;
|
|
DateOnly? since = current.Dates.IsEmpty ? null : current.Dates[^1].AddDays(-1);
|
|
var import = await client.FetchAsync(since, cancellationToken);
|
|
var updated = current.Merge(import, timeProvider.GetUtcNow(), out var changedDays);
|
|
await storage.SaveAsync(updated, changedDays, cancellationToken);
|
|
store.Replace(updated);
|
|
logger.LogInformation("Imported {Rows} traffic rows for {Days} days since {Since}", import.Counts.Count, changedDays.Count, since);
|
|
}
|
|
|
|
public async Task BuildMissingGeometryAsync(CancellationToken cancellationToken)
|
|
{
|
|
var snapshot = store.Current;
|
|
var missing = snapshot.Stations.Values.Where(s => !snapshot.Geometry.ContainsKey(s.Code)).ToList();
|
|
if (missing.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var built = new List<StationGeometry>();
|
|
foreach (var station in missing)
|
|
{
|
|
try
|
|
{
|
|
built.Add(await geometryService.BuildAsync(station, cancellationToken));
|
|
}
|
|
catch (HttpRequestException exception)
|
|
{
|
|
logger.LogWarning(exception, "Could not build road geometry for station {Code}", station.Code);
|
|
}
|
|
}
|
|
|
|
if (built.Count == 0)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var updated = store.Current.WithGeometry(built);
|
|
await storage.SaveGeometryAsync(updated, cancellationToken);
|
|
store.Replace(updated);
|
|
logger.LogInformation(
|
|
"Built road geometry for {Count} stations, {Straight} without matching road",
|
|
built.Count,
|
|
built.Count(g => !g.FollowsRoad));
|
|
}
|
|
}
|