dotnet - add locking since the simulator seems to run a lot in parallel
change the port to match what the simulator uses by default add serilog as part of debugging
This commit is contained in:
@@ -1,15 +1,23 @@
|
||||
using SafelyYou;
|
||||
using SafelyYou.Api;
|
||||
using Serilog;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
|
||||
Log.Logger = new LoggerConfiguration()
|
||||
.WriteTo.Console()
|
||||
.CreateLogger();
|
||||
|
||||
builder.Services.AddSerilog();
|
||||
builder.Services.AddOpenApi();
|
||||
|
||||
builder.Services.AddSingleton<KnownDevices>(_ =>
|
||||
{
|
||||
var deviceIds = File.ReadAllLines("/devices.csv").Skip(1).Select(id => new DeviceId(id)).ToHashSet();
|
||||
var deviceIds = File.ReadAllLines("./devices.csv").Skip(1).Select(id => new DeviceId(id)).ToHashSet();
|
||||
Log.Logger.Information("@deviceIds", deviceIds);
|
||||
return new KnownDevices(deviceIds);
|
||||
});
|
||||
|
||||
@@ -17,6 +25,7 @@ builder.Services.AddSingleton<HeartBeatHistories>();
|
||||
builder.Services.AddSingleton<UploadTimes>();
|
||||
|
||||
var app = builder.Build();
|
||||
app.UseSerilogRequestLogging();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
@@ -24,12 +33,12 @@ if (app.Environment.IsDevelopment())
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
//app.UseHttpsRedirection();
|
||||
|
||||
// heartbeat history
|
||||
// upload stats
|
||||
|
||||
app.MapPost("/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest request,
|
||||
app.MapPost("api/v1/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest request,
|
||||
KnownDevices knownDevices, HeartBeatHistories histories) =>
|
||||
{
|
||||
var device = new DeviceId(deviceId);
|
||||
@@ -49,7 +58,7 @@ app.MapPost("/devices/{deviceId}/heartbeat", (string deviceId, HeartbeatRequest
|
||||
};
|
||||
});
|
||||
|
||||
app.MapPost("/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest request, KnownDevices knownDevices, UploadTimes uploadTimes) =>
|
||||
app.MapPost("api/v1/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest request, KnownDevices knownDevices, UploadTimes uploadTimes) =>
|
||||
{
|
||||
var device = new DeviceId(deviceId);
|
||||
if (!knownDevices.Contains(device))
|
||||
@@ -62,9 +71,10 @@ app.MapPost("/devices/{deviceId}/stats", (string deviceId, UploadStatsRequest re
|
||||
return Results.NoContent();
|
||||
|
||||
// not sure where the 500 would go here beyond doing a try/catch
|
||||
// note: sent_at isn't used here since the data is a short-cut of aggregation, so it's dropped.
|
||||
});
|
||||
|
||||
app.MapGet("/devices/{deviceId}/stats", (string deviceId, KnownDevices knownDevices, HeartBeatHistories histories, UploadTimes uploadTimes) =>
|
||||
app.MapGet("api/v1/devices/{deviceId}/stats", (string deviceId, KnownDevices knownDevices, HeartBeatHistories histories, UploadTimes uploadTimes) =>
|
||||
{
|
||||
var device = new DeviceId(deviceId);
|
||||
if (!knownDevices.Contains(device))
|
||||
@@ -73,7 +83,7 @@ app.MapGet("/devices/{deviceId}/stats", (string deviceId, KnownDevices knownDevi
|
||||
}
|
||||
|
||||
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 timeSpanText = FormatAsGoDuration(averageUploadTimeNanos);
|
||||
var uptime = histories.Uptime(device);
|
||||
|
||||
return Results.Ok(new GetDeviceStatsResponse(timeSpanText, uptime));
|
||||
@@ -84,3 +94,63 @@ app.MapGet("/devices/{deviceId}/stats", (string deviceId, KnownDevices knownDevi
|
||||
|
||||
app.Run();
|
||||
|
||||
// Matches Go's time.Duration.String() (e.g. "3m17.331667813s").
|
||||
static string FormatAsGoDuration(System.Numerics.BigInteger nanos)
|
||||
{
|
||||
if (nanos <= 0) return "0s";
|
||||
|
||||
var u = (ulong)nanos;
|
||||
Span<char> buf = stackalloc char[32];
|
||||
var w = buf.Length;
|
||||
|
||||
// Fractional seconds from nanoseconds, omitting trailing zeros — same as Go fmtFrac(..., 9).
|
||||
var frac = u % 1_000_000_000UL;
|
||||
u /= 1_000_000_000UL;
|
||||
|
||||
buf[--w] = 's';
|
||||
if (frac != 0)
|
||||
{
|
||||
var print = false;
|
||||
for (var i = 0; i < 9; i++)
|
||||
{
|
||||
var digit = frac % 10;
|
||||
frac /= 10;
|
||||
if (digit != 0) print = true;
|
||||
if (print) buf[--w] = (char)('0' + digit);
|
||||
}
|
||||
buf[--w] = '.';
|
||||
}
|
||||
|
||||
var secs = u % 60;
|
||||
u /= 60;
|
||||
do
|
||||
{
|
||||
buf[--w] = (char)('0' + secs % 10);
|
||||
secs /= 10;
|
||||
} while (secs > 0);
|
||||
|
||||
if (u > 0)
|
||||
{
|
||||
buf[--w] = 'm';
|
||||
var mins = u % 60;
|
||||
u /= 60;
|
||||
do
|
||||
{
|
||||
buf[--w] = (char)('0' + mins % 10);
|
||||
mins /= 10;
|
||||
} while (mins > 0);
|
||||
|
||||
if (u > 0)
|
||||
{
|
||||
buf[--w] = 'h';
|
||||
do
|
||||
{
|
||||
buf[--w] = (char)('0' + u % 10);
|
||||
u /= 10;
|
||||
} while (u > 0);
|
||||
}
|
||||
}
|
||||
|
||||
return new string(buf[w..]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user