Choropleth of the 80 Thurgau municipalities from the road vehicle stock dataset (djs-stv-17): vehicles per 1000 inhabitants by vehicle group or share of a car brand among passenger cars, with year controls and a municipality panel. Population comes from sk-stat-56, outlines from swisstopo swissBOUNDARIES3D. Also fixes the broken ENV line in the Dockerfile. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
75 lines
3.2 KiB
C#
75 lines
3.2 KiB
C#
using Microsoft.Extensions.Logging;
|
|
using Microsoft.Extensions.Options;
|
|
using Schaad.OpenData.Vehicles.Models;
|
|
using Schaad.OpenData.Vehicles.Source;
|
|
using Schaad.OpenData.Vehicles.Storage;
|
|
|
|
namespace Schaad.OpenData.Vehicles;
|
|
|
|
public class VehicleRefresher(
|
|
ThurgauVehicleClient client,
|
|
MunicipalityBoundaryClient boundaryClient,
|
|
VehicleCacheStorage storage,
|
|
VehicleDataStore store,
|
|
IOptions<VehicleOptions> options,
|
|
TimeProvider timeProvider,
|
|
ILogger<VehicleRefresher> logger)
|
|
{
|
|
public async Task LoadCacheAsync(CancellationToken cancellationToken)
|
|
{
|
|
var snapshot = await storage.LoadAsync(cancellationToken);
|
|
store.Replace(snapshot);
|
|
logger.LogInformation(
|
|
"Loaded vehicle cache with {Entries} stock entries for {Years} years",
|
|
snapshot.Stock.Count,
|
|
snapshot.Years.Count);
|
|
}
|
|
|
|
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;
|
|
var stockVersion = await client.GetVersionAsync(options.Value.StockDatasetId, cancellationToken);
|
|
var populationVersion = await client.GetVersionAsync(options.Value.PopulationDatasetId, cancellationToken);
|
|
|
|
var stockChanged = current.Stock.Count == 0 || current.StockVersion != stockVersion;
|
|
var populationChanged = current.Population.Count == 0 || current.PopulationVersion != populationVersion;
|
|
var stock = stockChanged ? await client.FetchStockAsync(cancellationToken) : current.Stock;
|
|
var population = populationChanged ? await client.FetchPopulationAsync(cancellationToken) : current.Population;
|
|
var outlines = await CompleteOutlinesAsync(current.Outlines, stock, cancellationToken);
|
|
|
|
var updated = new VehicleSnapshot(stock, population, outlines, stockVersion, populationVersion, timeProvider.GetUtcNow());
|
|
await storage.SaveAsync(updated, cancellationToken);
|
|
store.Replace(updated);
|
|
logger.LogInformation(
|
|
"Vehicle refresh: stock {StockState} ({Entries} entries), population {PopulationState}, {Outlines} municipality outlines",
|
|
stockChanged ? "imported" : "unchanged",
|
|
stock.Count,
|
|
populationChanged ? "imported" : "unchanged",
|
|
outlines.Count);
|
|
}
|
|
|
|
private async Task<IReadOnlyList<MunicipalityOutline>> CompleteOutlinesAsync(
|
|
IReadOnlyList<MunicipalityOutline> cached,
|
|
IReadOnlyList<StockEntry> stock,
|
|
CancellationToken cancellationToken)
|
|
{
|
|
var outlines = cached.ToList();
|
|
var known = outlines.Select(o => o.Id).ToHashSet(StringComparer.Ordinal);
|
|
foreach (var id in stock.Select(e => e.MunicipalityId).Distinct(StringComparer.Ordinal).Where(id => !known.Contains(id)))
|
|
{
|
|
if (await boundaryClient.FetchAsync(id, cancellationToken) is { } outline)
|
|
{
|
|
outlines.Add(outline);
|
|
}
|
|
else
|
|
{
|
|
logger.LogWarning("No boundary found for municipality {MunicipalityId}", id);
|
|
}
|
|
}
|
|
|
|
return outlines;
|
|
}
|
|
}
|