From eab95cb7a1bafe2f25f242b3f0dd888b88433423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Wed, 20 Oct 2021 12:52:31 +0500 Subject: [PATCH] Refactor GetLastTelemetryDate(..) in services (single resp). Add State and LastTelemetryDate into StatWellDto. --- AsbCloudApp/Data/StatWellDto.cs | 5 ++- AsbCloudApp/Data/WellDto.cs | 11 +++-- .../{WellUpdateDto.cs => WellParamsDto.cs} | 4 +- AsbCloudApp/Services/ITelemetryService.cs | 3 +- AsbCloudApp/Services/IWellService.cs | 5 ++- .../Services/ClusterService.cs | 8 ++-- .../Services/TelemetryService.cs | 16 +++---- .../Services/TelemetryTracker.cs | 6 +-- .../WellOperationsStatService.cs | 4 +- .../Services/WellService.cs | 45 ++++++++++++++----- AsbCloudWebApi/Controllers/WellController.cs | 2 +- 11 files changed, 69 insertions(+), 40 deletions(-) rename AsbCloudApp/Data/{WellUpdateDto.cs => WellParamsDto.cs} (74%) diff --git a/AsbCloudApp/Data/StatWellDto.cs b/AsbCloudApp/Data/StatWellDto.cs index 6fba2c4a..f72a29a6 100644 --- a/AsbCloudApp/Data/StatWellDto.cs +++ b/AsbCloudApp/Data/StatWellDto.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; namespace AsbCloudApp.Data { @@ -7,6 +8,8 @@ namespace AsbCloudApp.Data public int Id { get; set; } public string Caption { get; set; } public string WellType { get; set; } + public string State { get; set; } + public DateTime LastTelemetryDate { get; set; } public IEnumerable Sections { get; set; } public PlanFactBase Total { get; set; } public IEnumerable Companies { get; set; } diff --git a/AsbCloudApp/Data/WellDto.cs b/AsbCloudApp/Data/WellDto.cs index 7f3859ba..7fe20899 100644 --- a/AsbCloudApp/Data/WellDto.cs +++ b/AsbCloudApp/Data/WellDto.cs @@ -9,10 +9,13 @@ namespace AsbCloudApp.Data public double? Latitude { get; set; } public double? Longitude { get; set; } public string WellType { get; set; } - // 0 - незвестно, - // 1 - в работе, - // 2 - завершена - public int State { get; set; } + + /// + /// 0 - незвестно, + /// 1 - в работе, + /// 2 - завершена + /// + public int IdState { get; set; } public DateTime LastTelemetryDate { get; set; } public TelemetryDto Telemetry { get; set; } } diff --git a/AsbCloudApp/Data/WellUpdateDto.cs b/AsbCloudApp/Data/WellParamsDto.cs similarity index 74% rename from AsbCloudApp/Data/WellUpdateDto.cs rename to AsbCloudApp/Data/WellParamsDto.cs index 998dd40c..36fe5418 100644 --- a/AsbCloudApp/Data/WellUpdateDto.cs +++ b/AsbCloudApp/Data/WellParamsDto.cs @@ -1,11 +1,11 @@ namespace AsbCloudApp.Data { - public class WellUpdateParamsDto + public class WellParamsDto { public string Caption { get; set; } public double? Latitude { get; set; } public double? Longitude { get; set; } public int IdWellType { get; set; } - public int State { get; set; } + public int IdState { get; set; } } } \ No newline at end of file diff --git a/AsbCloudApp/Services/ITelemetryService.cs b/AsbCloudApp/Services/ITelemetryService.cs index 8cd7bf4c..73c5bae6 100644 --- a/AsbCloudApp/Services/ITelemetryService.cs +++ b/AsbCloudApp/Services/ITelemetryService.cs @@ -9,7 +9,6 @@ namespace AsbCloudApp.Services public interface ITelemetryService { void SaveRequestDate(string uid); - DateTime GetLastTelemetryDateByWellId(int idWell); int? GetidWellByTelemetryUid(string uid); int GetOrCreateTemetryIdByUid(string uid); double GetTimezoneOffsetByTelemetryId(int idTelemetry); @@ -19,5 +18,7 @@ namespace AsbCloudApp.Services IEnumerable<(string Key, int[] Ids)> GetRedundentRemoteUids(); Task> GetTransmittingWellsAsync(int idCompany, CancellationToken token); + DateTime GetLastTelemetryDate(string telemetryUid); + DateTime GetLastTelemetryDate(int telemetryId); } } diff --git a/AsbCloudApp/Services/IWellService.cs b/AsbCloudApp/Services/IWellService.cs index 627bb572..d6f08988 100644 --- a/AsbCloudApp/Services/IWellService.cs +++ b/AsbCloudApp/Services/IWellService.cs @@ -1,4 +1,5 @@ using AsbCloudApp.Data; +using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; @@ -8,12 +9,14 @@ namespace AsbCloudApp.Services public interface IWellService { Task> GetWellsByCompanyAsync(int idCompany, CancellationToken token); - Task UpdateWellAsync(int idWell, WellUpdateParamsDto dto, CancellationToken token = default); + Task UpdateWellAsync(int idWell, WellParamsDto dto, CancellationToken token = default); Task> GetTransmittingWellsAsync(int idCompany, CancellationToken token); Task IsCompanyInvolvedInWellAsync(int idCompany, int idWell, CancellationToken token); Task GetWellCaptionByIdAsync(int idWell, CancellationToken token); Task> GetCompaniesAsync(int idWell, CancellationToken token); Task GetAsync(int idWell, CancellationToken token); bool IsCompanyInvolvedInWell(int idCompany, int idWell); + string GetStateText(int state); + DateTime GetLastTelemetryDate(int idWell); } } diff --git a/AsbCloudInfrastructure/Services/ClusterService.cs b/AsbCloudInfrastructure/Services/ClusterService.cs index 10901010..141ac3b4 100644 --- a/AsbCloudInfrastructure/Services/ClusterService.cs +++ b/AsbCloudInfrastructure/Services/ClusterService.cs @@ -13,12 +13,12 @@ namespace AsbCloudInfrastructure.Services public class ClusterService : IClusterService { private readonly IAsbCloudDbContext db; - private readonly ITelemetryService telemetryService; + private readonly IWellService wellService; - public ClusterService(IAsbCloudDbContext db, ITelemetryService telemetryService) + public ClusterService(IAsbCloudDbContext db, IWellService wellService) { this.db = db; - this.telemetryService = telemetryService; + this.wellService = wellService; } public async Task> GetDepositsAsync(int idCompany, @@ -144,7 +144,7 @@ namespace AsbCloudInfrastructure.Services Latitude = well.Latitude, Longitude = well.Longitude, WellType = well.WellType?.Caption, - LastTelemetryDate = telemetryService.GetLastTelemetryDateByWellId(well.Id), + LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id), Cluster = gCluster.Key.Caption, Deposit = gDeposit.Key.Caption, }), diff --git a/AsbCloudInfrastructure/Services/TelemetryService.cs b/AsbCloudInfrastructure/Services/TelemetryService.cs index 4cc652df..6d23f904 100644 --- a/AsbCloudInfrastructure/Services/TelemetryService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryService.cs @@ -53,15 +53,12 @@ namespace AsbCloudInfrastructure.Services public void SaveRequestDate(string uid) => telemetryTracker.SaveRequestDate(uid); - public DateTime GetLastTelemetryDateByWellId(int idWell) + public DateTime GetLastTelemetryDate(string telemetryUid) => + telemetryTracker.GetLastTelemetryDateByUid(telemetryUid); + + public DateTime GetLastTelemetryDate(int telemetryId) { var lastTelemetryDate = DateTime.MinValue; - - var telemetryId = GetIdTelemetryByIdWell(idWell); - - if (telemetryId is null) - return lastTelemetryDate; - var telemetry = cacheTelemetry.FirstOrDefault(t => t.Id == telemetryId); if (telemetry is null) @@ -69,10 +66,7 @@ namespace AsbCloudInfrastructure.Services var uid = telemetry.RemoteUid; - if(!telemetryTracker.GetTransmittingTelemetryUids().Contains(uid)) - return lastTelemetryDate; - - lastTelemetryDate = telemetryTracker.GetLastTelemetryDateByUid(uid); + lastTelemetryDate = GetLastTelemetryDate(uid); return lastTelemetryDate; } diff --git a/AsbCloudInfrastructure/Services/TelemetryTracker.cs b/AsbCloudInfrastructure/Services/TelemetryTracker.cs index fb34f968..7f7fa4ba 100644 --- a/AsbCloudInfrastructure/Services/TelemetryTracker.cs +++ b/AsbCloudInfrastructure/Services/TelemetryTracker.cs @@ -7,8 +7,8 @@ namespace AsbCloudInfrastructure.Services { public class TelemetryTracker : ITelemetryTracker { - private IDictionary requests = new Dictionary(); - private readonly TimeSpan timeout = TimeSpan.FromMinutes(6); + private Dictionary requests = new Dictionary(); + private readonly TimeSpan timeout = TimeSpan.FromDays(7); public void SaveRequestDate(string uid) { @@ -17,7 +17,7 @@ namespace AsbCloudInfrastructure.Services } public DateTime GetLastTelemetryDateByUid(string uid) => - requests[uid]; + requests.GetValueOrDefault(uid, DateTime.MinValue); public IEnumerable GetTransmittingTelemetryUids() { diff --git a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs index db6e3b5b..7f765575 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/WellOperationsStatService.cs @@ -101,7 +101,9 @@ namespace AsbCloudInfrastructure.Services.WellOperationService { Id = well.Id, Caption = well.Caption, - WellType = wellType.Caption + WellType = wellType.Caption, + State = wellService.GetStateText(well.IdState), + LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id), }; statWellDto.Companies = await wellService.GetCompaniesAsync(well.Id, token); diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index 5da5027a..54a95cbe 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -26,7 +26,19 @@ namespace AsbCloudInfrastructure.Services cacheRelationCompaniesWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); cacheWells = cacheDb.GetCachedTable((AsbCloudDbContext)db); } - + + public DateTime GetLastTelemetryDate(int idWell) + { + var lastTelemetryDate = DateTime.MinValue; + var well = cacheWells.FirstOrDefault(w => w.Id == idWell); + + if (well is null || well.IdTelemetry is null) + return lastTelemetryDate; + + lastTelemetryDate = telemetryService.GetLastTelemetryDate(well.IdTelemetry??0); + return lastTelemetryDate; + } + public async Task> GetTransmittingWellsAsync(int idCompany, CancellationToken token) => await telemetryService.GetTransmittingWellsAsync(idCompany, token); @@ -43,29 +55,29 @@ namespace AsbCloudInfrastructure.Services }); } - public async Task UpdateWellAsync(int idWell, WellUpdateParamsDto dto, + public async Task UpdateWellAsync(int idWell, WellParamsDto dto, CancellationToken token = default) { if (dto.IdWellType is < 1 or > 2) throw new ArgumentException("Тип секции указан неправильно.", nameof(dto)); - if (dto.State is < 0 or > 2) + if (dto.IdState is < 0 or > 2) throw new ArgumentException("Текущее состояние работы скважины" + "указано неправильно.", nameof(dto)); - var well = await db.Wells + var entity = await db.Wells .FirstOrDefaultAsync(w => w.Id == idWell, token) .ConfigureAwait(false); - if (well is null) + if (entity is null) throw new ArgumentException("Тип секции указан неправильно.", nameof(idWell)); - well.Caption = dto.Caption; - well.Latitude = dto.Latitude; - well.Longitude = dto.Longitude; - well.IdWellType = dto.IdWellType; - well.State = dto.State; + entity.Caption = dto.Caption; + entity.Latitude = dto.Latitude; + entity.Longitude = dto.Longitude; + entity.IdWellType = dto.IdWellType; + entity.IdState = dto.IdState; - db.Wells.Update(well); + db.Wells.Update(entity); return await db.SaveChangesAsync(token); } @@ -88,6 +100,7 @@ namespace AsbCloudInfrastructure.Services var dto = entity.Adapt(); dto.Cluster = entity.Cluster?.Caption; dto.Deposit = entity.Cluster?.Deposit?.Caption; + return dto; } public async Task GetWellCaptionByIdAsync(int idWell, CancellationToken token) @@ -107,5 +120,15 @@ namespace AsbCloudInfrastructure.Services var companies = well.RelationCompaniesWells.Select(r => r.Company); return companies.Adapt(); } + + public string GetStateText(int state) + { + return state switch + { + 1 => "В работе", + 2 => "Завершена", + _ => "Незвестно", + }; + } } } diff --git a/AsbCloudWebApi/Controllers/WellController.cs b/AsbCloudWebApi/Controllers/WellController.cs index b01ca18e..2590671f 100644 --- a/AsbCloudWebApi/Controllers/WellController.cs +++ b/AsbCloudWebApi/Controllers/WellController.cs @@ -81,7 +81,7 @@ namespace AsbCloudWebApi.Controllers /// [HttpPut] [ProducesResponseType(typeof(int), (int)System.Net.HttpStatusCode.OK)] - public async Task UpdateWellAsync(int idWell, WellUpdateParamsDto dto, + public async Task UpdateWellAsync(int idWell, WellParamsDto dto, CancellationToken token = default) { var idCompany = User.GetCompanyId();