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>
65 lines
2.7 KiB
C#
65 lines
2.7 KiB
C#
using System.Text.Json;
|
|
using Microsoft.Extensions.Options;
|
|
using Schaad.OpenData.Vehicles.Models;
|
|
|
|
namespace Schaad.OpenData.Vehicles.Storage;
|
|
|
|
public class VehicleCacheStorage(IOptions<VehicleOptions> options)
|
|
{
|
|
private const string MetaFileName = "meta.json";
|
|
private const string StockFileName = "stock.json";
|
|
private const string PopulationFileName = "population.json";
|
|
private const string OutlinesFileName = "outlines-v1.json";
|
|
|
|
private static readonly JsonSerializerOptions JsonOptions = new(JsonSerializerDefaults.Web);
|
|
|
|
private string RootPath => Path.Combine(Path.GetFullPath(options.Value.DataPath), "vehicles", "thurgau");
|
|
|
|
public virtual async Task<VehicleSnapshot> LoadAsync(CancellationToken cancellationToken)
|
|
{
|
|
var meta = await ReadAsync<VehicleCacheMeta>(MetaFileName, cancellationToken);
|
|
var stock = await ReadAsync<List<StockEntry>>(StockFileName, cancellationToken) ?? [];
|
|
var population = await ReadAsync<List<PopulationEntry>>(PopulationFileName, cancellationToken) ?? [];
|
|
var outlines = await ReadAsync<List<MunicipalityOutline>>(OutlinesFileName, cancellationToken) ?? [];
|
|
|
|
return stock.Count == 0
|
|
? new VehicleSnapshot([], [], outlines, null, null, null)
|
|
: new VehicleSnapshot(stock, population, outlines, meta?.StockVersion, meta?.PopulationVersion, meta?.LastRefresh);
|
|
}
|
|
|
|
public virtual async Task SaveAsync(VehicleSnapshot snapshot, CancellationToken cancellationToken)
|
|
{
|
|
await WriteAsync(StockFileName, snapshot.Stock, cancellationToken);
|
|
await WriteAsync(PopulationFileName, snapshot.Population, cancellationToken);
|
|
await WriteAsync(OutlinesFileName, snapshot.Outlines, cancellationToken);
|
|
await WriteAsync(
|
|
MetaFileName,
|
|
new VehicleCacheMeta(snapshot.StockVersion, snapshot.PopulationVersion, snapshot.LastRefresh),
|
|
cancellationToken);
|
|
}
|
|
|
|
private async Task<T?> ReadAsync<T>(string fileName, CancellationToken cancellationToken)
|
|
{
|
|
var path = Path.Combine(RootPath, fileName);
|
|
if (!File.Exists(path))
|
|
{
|
|
return default;
|
|
}
|
|
|
|
await using var stream = File.OpenRead(path);
|
|
return await JsonSerializer.DeserializeAsync<T>(stream, JsonOptions, cancellationToken);
|
|
}
|
|
|
|
private async Task WriteAsync<T>(string fileName, T value, CancellationToken cancellationToken)
|
|
{
|
|
var path = Path.Combine(RootPath, fileName);
|
|
Directory.CreateDirectory(RootPath);
|
|
var tempPath = path + ".tmp";
|
|
await using (var stream = File.Create(tempPath))
|
|
{
|
|
await JsonSerializer.SerializeAsync(stream, value, JsonOptions, cancellationToken);
|
|
}
|
|
|
|
File.Move(tempPath, path, true);
|
|
}
|
|
}
|