add the stats endpoint

This commit is contained in:
2026-08-06 13:40:07 -05:00
parent 9f94290439
commit 6e88380fea
2 changed files with 23 additions and 1 deletions
+16
View File
@@ -71,6 +71,22 @@ app.MapPost("/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest re
// not sure where the 500 would go here beyond doing a try/catch // not sure where the 500 would go here beyond doing a try/catch
}); });
app.MapGet("/devices/{deviceId}/stats", (string deviceId, KnownDevices knownDevices, HeartBeatHistories histories, UploadTimes uploadTimes) =>
{
var device = new DeviceId(deviceId);
if (!knownDevices.Contains(device))
{
return Results.NotFound(new NotFoundResponse($"Device ID {deviceId} not found"));
}
var averageUploadTimeNanos = uploadTimes.AverageUploadTimeNanoseconds(device);
var timeSpanText = TimeSpan.FromTicks((long)(averageUploadTimeNanos / 100)).ToString(@"m\ms\s"); // loses accuracy on the Go nanos for this text conversion
var uptime = histories.Uptime(device);
return Results.Ok(new GetDeviceStatsResponse(timeSpanText, uptime));
// no 500 here
});
app.Run(); app.Run();
+6
View File
@@ -16,4 +16,10 @@ public sealed class UploadTimes
_uploadHistories[deviceId] = (history.TotalUploadTime + uploadTimeNanos, history.UploadCounts + 1); _uploadHistories[deviceId] = (history.TotalUploadTime + uploadTimeNanos, history.UploadCounts + 1);
} }
public BigInteger AverageUploadTimeNanoseconds(DeviceId deviceId)
{
if (!_uploadHistories.TryGetValue(deviceId, out var history)) return BigInteger.Zero;
return history.TotalUploadTime / history.UploadCounts;
}
} }