From 5e933fb3ba77409301ff53806f738a1bf46bc921 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Karla=C5=A1?= Date: Tue, 23 Apr 2024 15:39:47 +0200 Subject: [PATCH] Switch to using github releases ExpectedStateOfAdmins.json and implement caching via ETag --- .../AdminCountPerCountryAnalyzer.cs | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/OsmNightWatch/Analyzers/AdminCountPerCountry/AdminCountPerCountryAnalyzer.cs b/OsmNightWatch/Analyzers/AdminCountPerCountry/AdminCountPerCountryAnalyzer.cs index f52531c..b6b6dd4 100644 --- a/OsmNightWatch/Analyzers/AdminCountPerCountry/AdminCountPerCountryAnalyzer.cs +++ b/OsmNightWatch/Analyzers/AdminCountPerCountry/AdminCountPerCountryAnalyzer.cs @@ -7,6 +7,7 @@ using System.Data.SQLite; using System.Text.Json; using NetTopologySuite.Index.Strtree; +using System.Net.Http.Headers; namespace OsmNightWatch.Analyzers.AdminCountPerCountry; @@ -14,7 +15,7 @@ public partial class AdminCountPerCountryAnalyzer : IOsmAnalyzer, IDisposable { SQLiteConnection sqlConnection; private KeyValueDatabase database; - + private HttpClient httpClient = new HttpClient(); public AdminCountPerCountryAnalyzer(KeyValueDatabase database, string storePath) { this.database = database; @@ -153,9 +154,7 @@ private IEnumerable UpdateRelations((uint Id, Relation Relation)[] re } EndAdminsUpsertTransaction(true); - var expectedState = JsonSerializer.Deserialize(new HttpClient().GetStringAsync("https://davidupload.blob.core.windows.net/data/current.json").Result, new JsonSerializerOptions() { - ReadCommentHandling = JsonCommentHandling.Skip - }); + var expectedState = GetExpectedState(); if (currentState == null) { @@ -239,6 +238,28 @@ private IEnumerable UpdateRelations((uint Id, Relation Relation)[] re } } + private StateOfTheAdmins? cachedExpectedState = null; + private EntityTagHeaderValue? latestEtag; + + private StateOfTheAdmins GetExpectedState() + { + var httpRequest = new HttpRequestMessage(HttpMethod.Get, "https://github.com/DavidKarlas/OsmNightWatch/releases/latest/download/ExpectedStateOfAdmins.json"); + if (latestEtag != null) + httpRequest.Headers.IfNoneMatch.Add(latestEtag); + var responseMessage = httpClient.Send(httpRequest); + if (cachedExpectedState != null && responseMessage.StatusCode == System.Net.HttpStatusCode.NotModified) + return cachedExpectedState; + var expectedStateJson = responseMessage.Content.ReadAsStringAsync().Result; + var expectedState = new StateOfTheAdmins() { + Countries = JsonSerializer.Deserialize>(expectedStateJson, new JsonSerializerOptions() { + ReadCommentHandling = JsonCommentHandling.Skip + })! + }; + latestEtag = responseMessage.Headers.ETag; + cachedExpectedState = expectedState; + return expectedState; + } + public IEnumerable ProcessPbf(IEnumerable relevantThings, IOsmGeoBatchSource newOsmSource) { Utils.BatchLoad(relevantThings, newOsmSource, true, true);