From da9e94b4b9cf4001735cf3e9d4a08f81e555e27a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Thu, 28 Oct 2021 10:56:18 +0500 Subject: [PATCH] Modify telemetry tracker to keep remote and server dates. Update related services. Small refactor TelemetryController: exclude dictionaries updates from tracker update. All tracker updates moved to services. --- AsbCloudApp/Services/ITelemetryService.cs | 2 +- AsbCloudApp/Services/ITelemetryTracker.cs | 4 +- .../Services/MessageService.cs | 3 ++ .../Services/TelemetryDataBaseService.cs | 4 +- .../Services/TelemetryService.cs | 17 ++----- .../Services/TelemetryTracker.cs | 49 ++++++++++++++++--- .../Controllers/TelemetryController.cs | 4 -- .../TelemetryDataBaseController.cs | 3 +- 8 files changed, 54 insertions(+), 32 deletions(-) diff --git a/AsbCloudApp/Services/ITelemetryService.cs b/AsbCloudApp/Services/ITelemetryService.cs index 64d8acec..f94627f9 100644 --- a/AsbCloudApp/Services/ITelemetryService.cs +++ b/AsbCloudApp/Services/ITelemetryService.cs @@ -8,7 +8,6 @@ namespace AsbCloudApp.Services { public interface ITelemetryService { - void SaveRequestDate(string uid); int? GetidWellByTelemetryUid(string uid); int GetOrCreateTemetryIdByUid(string uid); double GetTimezoneOffsetByTelemetryId(int idTelemetry); @@ -19,5 +18,6 @@ namespace AsbCloudApp.Services IEnumerable GetTransmittingTelemetriesAsync(int idCompany); DateTime GetLastTelemetryDate(string telemetryUid); DateTime GetLastTelemetryDate(int telemetryId); + void SaveRequestDate(string uid, DateTime remoteDate); } } diff --git a/AsbCloudApp/Services/ITelemetryTracker.cs b/AsbCloudApp/Services/ITelemetryTracker.cs index 6a3aa555..3f8da9f8 100644 --- a/AsbCloudApp/Services/ITelemetryTracker.cs +++ b/AsbCloudApp/Services/ITelemetryTracker.cs @@ -5,8 +5,8 @@ namespace AsbCloudApp.Services { public interface ITelemetryTracker { - void SaveRequestDate(string uid); DateTime GetLastTelemetryDateByUid(string uid); - IEnumerable GetTransmittingTelemetryUids(); + IEnumerable GetTransmittingTelemetriesUids(); + void SaveRequestDate(string uid, DateTime remoteDate); } } diff --git a/AsbCloudInfrastructure/Services/MessageService.cs b/AsbCloudInfrastructure/Services/MessageService.cs index 7fb9fe3d..07f08283 100644 --- a/AsbCloudInfrastructure/Services/MessageService.cs +++ b/AsbCloudInfrastructure/Services/MessageService.cs @@ -144,6 +144,9 @@ namespace AsbCloudInfrastructure.Services return null; var telemetryId = telemetryService.GetOrCreateTemetryIdByUid(uid); + + var maxDateDto = dtos.Max(m => m.Date); + telemetryService.SaveRequestDate(uid, maxDateDto); foreach (var dto in dtos) { diff --git a/AsbCloudInfrastructure/Services/TelemetryDataBaseService.cs b/AsbCloudInfrastructure/Services/TelemetryDataBaseService.cs index 4c1e2c79..ee015db9 100644 --- a/AsbCloudInfrastructure/Services/TelemetryDataBaseService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryDataBaseService.cs @@ -6,7 +6,6 @@ using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Threading; using System.Threading.Tasks; @@ -45,6 +44,9 @@ namespace AsbCloudInfrastructure.Services var idTelemetry = telemetryService.GetOrCreateTemetryIdByUid(uid); var dtoMinDate = dtos.Min(d => d.Date); var dtoMaxDate = dtos.Max(d => d.Date); + + telemetryService.SaveRequestDate(uid, dtoMaxDate); + var dataSet = db.Set(); var oldData = await (from d in dataSet diff --git a/AsbCloudInfrastructure/Services/TelemetryService.cs b/AsbCloudInfrastructure/Services/TelemetryService.cs index 7b8dcf9c..3502a766 100644 --- a/AsbCloudInfrastructure/Services/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryService.cs @@ -29,7 +29,7 @@ namespace AsbCloudInfrastructure.Services public IEnumerable GetTransmittingTelemetriesAsync(int idCompany) { var telemetryDtos = new List(); - IEnumerable activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetryUids(); + IEnumerable activeTelemetriesUids = telemetryTracker.GetTransmittingTelemetriesUids(); if (activeTelemetriesUids.Any()) { var telemetries = cacheTelemetry @@ -40,8 +40,8 @@ namespace AsbCloudInfrastructure.Services return telemetryDtos; } - public void SaveRequestDate(string uid) => - telemetryTracker.SaveRequestDate(uid); + public void SaveRequestDate(string uid, DateTime remoteDate) => + telemetryTracker.SaveRequestDate(uid, remoteDate); public DateTime GetLastTelemetryDate(string telemetryUid) => telemetryTracker.GetLastTelemetryDateByUid(telemetryUid); @@ -113,17 +113,6 @@ namespace AsbCloudInfrastructure.Services // найти телеметрию с наиболее полными справочниками и принять её за основную // отделить основную от остальных - // связонные сущности: - // Telemetry - // TelemetryInfo - // TelemetryEvent - // TelemetryUser - - // TelemetryMessage - // TelemetryDataSpin - // TelemetryDataSaub - // TelemetryAnalysis - // Well // Оценка трудоебкости var telemetriesGrade = db.Telemetries diff --git a/AsbCloudInfrastructure/Services/TelemetryTracker.cs b/AsbCloudInfrastructure/Services/TelemetryTracker.cs index ed38e6af..0484cdf9 100644 --- a/AsbCloudInfrastructure/Services/TelemetryTracker.cs +++ b/AsbCloudInfrastructure/Services/TelemetryTracker.cs @@ -10,7 +10,24 @@ namespace AsbCloudInfrastructure.Services { public class TelemetryTracker : ITelemetryTracker { - private readonly Dictionary requests; + class TrackerStat + { + //public int Id { get; set; } + + public string RemoteUid { get; set; } + + /// + /// Время последнего запроса (по времени сервера) + /// + public DateTime LastTimeServer { get; set; } + + /// + /// Время указанное в данных последнего запроса (из удаленного источника) + /// + public DateTime LastTimeRemote { get; set; } + } + + private readonly Dictionary requests; public TelemetryTracker(CacheDb cacheDb) { @@ -33,18 +50,34 @@ namespace AsbCloudInfrastructure.Services requests = lastTelemetriesDates.Select(t => new { - Uid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry).RemoteUid, - t.Date - }).ToDictionary(t => t.Uid, u => u.Date); + //Id = t.IdTelemetry, + RemoteUid = cacheTelemetry.FirstOrDefault(c => c.Id == t.IdTelemetry).RemoteUid, + LastTimeRemote = t.Date, + + }).ToDictionary(t => t.RemoteUid, t => new TrackerStat { + //Id = t.Id, + RemoteUid = t.RemoteUid, + LastTimeRemote = t.LastTimeRemote, + LastTimeServer = t.LastTimeRemote, + }); } - public void SaveRequestDate(string uid) => - requests[uid] = DateTime.Now; + public void SaveRequestDate(string uid, DateTime remoteDate) + { + var stat = requests.GetValueOrDefault(uid); + if(stat is null) + { + stat = new TrackerStat{ RemoteUid = uid }; + requests[uid] = stat; + } + stat.LastTimeServer = DateTime.Now; + stat.LastTimeRemote = remoteDate; + } public DateTime GetLastTelemetryDateByUid(string uid) => - requests.GetValueOrDefault(uid, DateTime.MinValue); + requests.GetValueOrDefault(uid)?.LastTimeRemote ?? default; - public IEnumerable GetTransmittingTelemetryUids() => + public IEnumerable GetTransmittingTelemetriesUids() => requests.Keys; } } diff --git a/AsbCloudWebApi/Controllers/TelemetryController.cs b/AsbCloudWebApi/Controllers/TelemetryController.cs index 5a5455ae..77736f10 100644 --- a/AsbCloudWebApi/Controllers/TelemetryController.cs +++ b/AsbCloudWebApi/Controllers/TelemetryController.cs @@ -49,7 +49,6 @@ namespace AsbCloudWebApi.Controllers public IActionResult PostInfo(string uid, [FromBody] TelemetryInfoDto info) { telemetryService.UpdateInfo(uid, info); - telemetryService.SaveRequestDate(uid); return Ok(); } @@ -72,7 +71,6 @@ namespace AsbCloudWebApi.Controllers await Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}") .SendAsync("ReceiveMessages", dtos), token).ConfigureAwait(false); - telemetryService.SaveRequestDate(uid); return Ok(); } @@ -90,7 +88,6 @@ namespace AsbCloudWebApi.Controllers { await eventService.UpsertAsync(uid, events, token) .ConfigureAwait(false); - telemetryService.SaveRequestDate(uid); return Ok(); } @@ -105,7 +102,6 @@ namespace AsbCloudWebApi.Controllers public IActionResult PostUsers(string uid, [FromBody] List users) { telemetryUserService.Upsert(uid, users); - telemetryService.SaveRequestDate(uid); return Ok(); } diff --git a/AsbCloudWebApi/Controllers/TelemetryDataBaseController.cs b/AsbCloudWebApi/Controllers/TelemetryDataBaseController.cs index 07e326ff..04fdc659 100644 --- a/AsbCloudWebApi/Controllers/TelemetryDataBaseController.cs +++ b/AsbCloudWebApi/Controllers/TelemetryDataBaseController.cs @@ -56,8 +56,7 @@ namespace AsbCloudWebApi.Controllers if (idWell != null && dtos.Any()) await Task.Run(() => telemetryHubContext.Clients.Group($"well_{idWell}") .SendAsync(SirnalRMethodGetDataName, dtos), token).ConfigureAwait(false); - - telemetryService.SaveRequestDate(uid); + return Ok(); }