From c5da73d0ec68fd32425ed9cc31a07d4e0a230c8d Mon Sep 17 00:00:00 2001 From: Michael DiLeo Date: Thu, 6 Aug 2026 13:17:29 -0500 Subject: [PATCH] add upload times --- dotnet/SafelyYou/Program.cs | 40 +++++++++++++-------------------- dotnet/SafelyYou/UploadTimes.cs | 19 ++++++++++++++++ 2 files changed, 35 insertions(+), 24 deletions(-) create mode 100644 dotnet/SafelyYou/UploadTimes.cs diff --git a/dotnet/SafelyYou/Program.cs b/dotnet/SafelyYou/Program.cs index 7baefc0..831578c 100644 --- a/dotnet/SafelyYou/Program.cs +++ b/dotnet/SafelyYou/Program.cs @@ -1,4 +1,3 @@ -using Microsoft.AspNetCore.Http.HttpResults; using SafelyYou; using SafelyYou.Api; using SafelyYou.Config; @@ -22,6 +21,7 @@ builder.Services.AddSingleton(_ => }); builder.Services.AddSingleton(); +builder.Services.AddSingleton(); var app = builder.Build(); @@ -36,7 +36,6 @@ app.UseHttpsRedirection(); // heartbeat history // upload stats - app.MapPost("/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest request, KnownDevices knownDevices, HeartBeatHistories histories) => { @@ -50,36 +49,29 @@ app.MapPost("/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest return addResult switch { - DeviceHeartBeatHistory.AddResult.Added => Results.Ok(), + DeviceHeartBeatHistory.AddResult.Added => Results.NoContent(), DeviceHeartBeatHistory.AddResult.OutOfSequence => Results.InternalServerError( new ErrorResponse("Attempted to add a timestamp that was out of sequence or invalid.")), _ => throw new ArgumentOutOfRangeException() }; }); - -var summaries = new[] +app.MapPost("/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest request, KnownDevices knownDevices, UploadTimes uploadTimes) => { - "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching" -}; + var device = new DeviceId(deviceId); + if (!knownDevices.Contains(device)) + { + return Results.NotFound(new NotFoundResponse($"Device ID {deviceId} not found")); + } + + uploadTimes.Add(device, request.UploadTime); + + return Results.NoContent(); + + // not sure where the 500 would go here beyond doing a try/catch +}); + -app.MapGet("/weatherforecast", () => -{ - var forecast = Enumerable.Range(1, 5).Select(index => - new WeatherForecast - ( - DateOnly.FromDateTime(DateTime.Now.AddDays(index)), - Random.Shared.Next(-20, 55), - summaries[Random.Shared.Next(summaries.Length)] - )) - .ToArray(); - return forecast; -}) -.WithName("GetWeatherForecast"); app.Run(); -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} diff --git a/dotnet/SafelyYou/UploadTimes.cs b/dotnet/SafelyYou/UploadTimes.cs new file mode 100644 index 0000000..a89e500 --- /dev/null +++ b/dotnet/SafelyYou/UploadTimes.cs @@ -0,0 +1,19 @@ +using System.Collections.Concurrent; +using System.Numerics; + +namespace SafelyYou; + +public sealed class UploadTimes +{ + private readonly ConcurrentDictionary _uploadHistories = new(); + + public void Add(DeviceId deviceId, BigInteger uploadTimeNanos) + { + if (!_uploadHistories.TryGetValue(deviceId, out var history)) + { + history = (BigInteger.Zero, 0); + } + + _uploadHistories[deviceId] = (history.TotalUploadTime + uploadTimeNanos, history.UploadCounts + 1); + } +} \ No newline at end of file