Files
SafelyYouCodingChallenge/dotnet/SafelyYou/UploadTimes.cs
T
michael_dileo 459b90dcd7 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
2026-08-06 15:54:07 -05:00

29 lines
909 B
C#

using System.Collections.Concurrent;
using System.Numerics;
namespace SafelyYou;
public sealed class UploadTimes
{
private readonly Lock _lock = new();
private readonly ConcurrentDictionary<DeviceId, (BigInteger TotalUploadTime, int UploadCounts)> _uploadHistories = new();
public void Add(DeviceId deviceId, BigInteger uploadTimeNanos)
{
lock (_lock)
{
if (!_uploadHistories.TryGetValue(deviceId, out var history))
{
history = (BigInteger.Zero, 0);
}
_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;
}
}