Правки к предыдущему коммиту по задаче CS2-6

This commit is contained in:
KharchenkoVV 2021-05-12 17:25:52 +05:00
parent a1eb94901e
commit 7bbcdf8483
5 changed files with 27 additions and 14 deletions

View File

@ -5,7 +5,6 @@ namespace AsbCloudApp.Services
public interface ITelemetryTracker
{
void SaveRequest(string id);
void DeleteOldRequests(int minutes);
IEnumerable<string> GetRequests();
}
}

View File

@ -8,19 +8,22 @@ namespace AsbCloudInfrastructure.Services
public class TelemetryTracker : ITelemetryTracker
{
private IDictionary<string, DateTime> _requests = new Dictionary<string, DateTime>();
private readonly TimeSpan _timeout = TimeSpan.FromMinutes(6);
private void _deleteOldRequests()
{
_requests = _requests.Where(dValue => (DateTime.Now - dValue.Value) < _timeout).ToDictionary(dValue => dValue.Key, dValue => dValue.Value);
}
public void SaveRequest(string id)
{
_requests[id] = DateTime.Now;
DeleteOldRequests();
}
public void DeleteOldRequests(int minutes = 6)
{
_requests = _requests.Where(dValue => (DateTime.Now - dValue.Value).Minutes < minutes).ToDictionary(dValue => dValue.Key, dValue => dValue.Value);
_deleteOldRequests();
}
public IEnumerable<string> GetRequests()
{
DeleteOldRequests();
_deleteOldRequests();
return _requests.Keys;
}
}

View File

@ -20,10 +20,14 @@ namespace AsbCloudInfrastructure.Services
public IEnumerable<WellDto> GetTransmittingWells(int idCustomer)
{
var wells = new List<Well>();
IEnumerable<string> activeTelemetriesUids = telemetryTracker.GetRequests();
var wells = db.GetWellsByCustomer(idCustomer)
if (activeTelemetriesUids.Count() > 0)
{
wells = db.GetWellsByCustomer(idCustomer)
.Where(w => activeTelemetriesUids.Contains(w.Telemetry.RemoteUid))
.ToList();
}
return wells.Select(w => From(w));
}

View File

@ -42,10 +42,17 @@ namespace AsbCloudWebApi.Controllers
this.telemetryHubContext = telemetryHubContext;
this.telemetryTracker = telemetryTracker;
Request.Query.TryGetValue("uid", out Microsoft.Extensions.Primitives.StringValues pId);
string panelId = pId;
_registerRequest();
}
private void _registerRequest()
{
if (Request.Query.TryGetValue("uid", out Microsoft.Extensions.Primitives.StringValues pId) && pId.Count > 0)
{
string panelId = pId.First();
telemetryTracker.SaveRequest(panelId);
}
}
/// <summary>
/// Принимает общую информацию по скважине

View File

@ -45,7 +45,7 @@ namespace AsbCloudWebApi.Controllers
return NoContent();
}
IEnumerable<WellDto> transmittingWells = wellService.GetTransmittingWells((int)idCustomer);
var transmittingWells = wellService.GetTransmittingWells((int)idCustomer);
return Ok(transmittingWells);