Skip to content

Commit

Permalink
Switch to using github releases ExpectedStateOfAdmins.json and implem…
Browse files Browse the repository at this point in the history
…ent caching via ETag
  • Loading branch information
DavidKarlas committed Apr 23, 2024
1 parent 15b0aa9 commit 5e933fb
Showing 1 changed file with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
using System.Data.SQLite;
using System.Text.Json;
using NetTopologySuite.Index.Strtree;
using System.Net.Http.Headers;

namespace OsmNightWatch.Analyzers.AdminCountPerCountry;

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;
Expand Down Expand Up @@ -153,9 +154,7 @@ private IEnumerable<IssueData> UpdateRelations((uint Id, Relation Relation)[] re
}
EndAdminsUpsertTransaction(true);

var expectedState = JsonSerializer.Deserialize<StateOfTheAdmins>(new HttpClient().GetStringAsync("https://davidupload.blob.core.windows.net/data/current.json").Result, new JsonSerializerOptions() {
ReadCommentHandling = JsonCommentHandling.Skip
});
var expectedState = GetExpectedState();

if (currentState == null)
{
Expand Down Expand Up @@ -239,6 +238,28 @@ private IEnumerable<IssueData> 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<List<Country>>(expectedStateJson, new JsonSerializerOptions() {
ReadCommentHandling = JsonCommentHandling.Skip
})!
};
latestEtag = responseMessage.Headers.ETag;
cachedExpectedState = expectedState;
return expectedState;
}

public IEnumerable<IssueData> ProcessPbf(IEnumerable<OsmGeo> relevantThings, IOsmGeoBatchSource newOsmSource)
{
Utils.BatchLoad(relevantThings, newOsmSource, true, true);
Expand Down

0 comments on commit 5e933fb

Please sign in to comment.