add upload times
This commit is contained in:
+16
-24
@@ -1,4 +1,3 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using SafelyYou;
|
||||
using SafelyYou.Api;
|
||||
using SafelyYou.Config;
|
||||
@@ -22,6 +21,7 @@ builder.Services.AddSingleton<HeartBeatHistoryConfig>(_ =>
|
||||
});
|
||||
|
||||
builder.Services.AddSingleton<HeartBeatHistories>();
|
||||
builder.Services.AddSingleton<UploadTimes>();
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Numerics;
|
||||
|
||||
namespace SafelyYou;
|
||||
|
||||
public sealed class UploadTimes
|
||||
{
|
||||
private readonly ConcurrentDictionary<DeviceId, (BigInteger TotalUploadTime, int UploadCounts)> _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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user