DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/TelemetryTracker.cs
Фролов eab95cb7a1 Refactor GetLastTelemetryDate(..) in services (single resp).
Add State and LastTelemetryDate into StatWellDto.
2021-10-20 12:52:31 +05:00

35 lines
1.0 KiB
C#

using AsbCloudApp.Services;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AsbCloudInfrastructure.Services
{
public class TelemetryTracker : ITelemetryTracker
{
private Dictionary<string, DateTime> requests = new Dictionary<string, DateTime>();
private readonly TimeSpan timeout = TimeSpan.FromDays(7);
public void SaveRequestDate(string uid)
{
requests[uid] = DateTime.Now;
DeleteOldRequestDates();
}
public DateTime GetLastTelemetryDateByUid(string uid) =>
requests.GetValueOrDefault(uid, DateTime.MinValue);
public IEnumerable<string> GetTransmittingTelemetryUids()
{
DeleteOldRequestDates();
return requests.Keys;
}
private void DeleteOldRequestDates()
{
requests = requests.Where(dValue => (DateTime.Now - dValue.Value) < timeout)
.ToDictionary(dValue => dValue.Key, dValue => dValue.Value);
}
}
}