add the stats endpoint
This commit is contained in:
@@ -69,8 +69,24 @@ app.MapPost("/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest re
|
|||||||
return Results.NoContent();
|
return Results.NoContent();
|
||||||
|
|
||||||
// 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();
|
||||||
|
|||||||
@@ -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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user