- Thurgau hourly traffic counts (data.tg.ch) refreshed every 6 h into a per-day JSON cache - Road geometry per station direction from swisstopo swissTLM3D; roundabout arms run outward from the roundabout centre - Leaflet map with animated vehicles by class, time controls and base map switcher (grey, colour, aerial) - Station panel with per-class counts and Chart.js charts - Start page with map tiles; traffic map at /verkehr/thurgau - Dockerfile and docker-compose, xUnit tests Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: Claudio Schaad <c.schaad@pog.ch> Co-committed-by: Claudio Schaad <c.schaad@pog.ch>
58 lines
2.1 KiB
C#
58 lines
2.1 KiB
C#
using System.Collections.Frozen;
|
|
using System.Collections.Immutable;
|
|
using Schaad.OpenData.Traffic.Models;
|
|
|
|
namespace Schaad.OpenData.Traffic.Storage;
|
|
|
|
public sealed class TrafficSnapshot
|
|
{
|
|
public TrafficSnapshot(
|
|
IEnumerable<Station> stations,
|
|
IEnumerable<HourlyCount> counts,
|
|
IEnumerable<StationGeometry> geometry,
|
|
DateTimeOffset? lastRefresh)
|
|
{
|
|
Stations = stations.ToFrozenDictionary(s => s.Code);
|
|
CountsByDate = counts
|
|
.GroupBy(c => c.Date)
|
|
.ToFrozenDictionary(g => g.Key, g => g.ToImmutableArray());
|
|
Geometry = geometry.ToFrozenDictionary(g => g.StationCode);
|
|
Dates = [.. CountsByDate.Keys.Order()];
|
|
LastRefresh = lastRefresh;
|
|
}
|
|
|
|
public static TrafficSnapshot Empty { get; } = new([], [], [], null);
|
|
|
|
public FrozenDictionary<int, Station> Stations { get; }
|
|
|
|
public FrozenDictionary<DateOnly, ImmutableArray<HourlyCount>> CountsByDate { get; }
|
|
|
|
public FrozenDictionary<int, StationGeometry> Geometry { get; }
|
|
|
|
public ImmutableArray<DateOnly> Dates { get; }
|
|
|
|
public DateTimeOffset? LastRefresh { get; }
|
|
|
|
public TrafficSnapshot Merge(TrafficImport import, DateTimeOffset refreshedAt, out IReadOnlyList<DateOnly> changedDays)
|
|
{
|
|
var importedDays = import.Counts.Select(c => c.Date).ToHashSet();
|
|
changedDays = [.. importedDays.Order()];
|
|
|
|
var stations = Stations.Values
|
|
.Where(s => import.Stations.All(i => i.Code != s.Code))
|
|
.Concat(import.Stations);
|
|
var counts = CountsByDate
|
|
.Where(kv => !importedDays.Contains(kv.Key))
|
|
.SelectMany(kv => kv.Value)
|
|
.Concat(import.Counts);
|
|
|
|
return new TrafficSnapshot(stations, counts, Geometry.Values, refreshedAt);
|
|
}
|
|
|
|
public TrafficSnapshot WithGeometry(IEnumerable<StationGeometry> geometry)
|
|
{
|
|
var added = geometry.ToList();
|
|
var merged = Geometry.Values.Where(g => added.All(a => a.StationCode != g.StationCode)).Concat(added);
|
|
return new TrafficSnapshot(Stations.Values, CountsByDate.Values.SelectMany(c => c), merged, LastRefresh);
|
|
}
|
|
}
|