From e0d3187ef22c2dee6ff3373f73786cbd3ce0755a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A1=D1=82=D0=B5=D0=BF=D0=B0=D0=BD=D0=BE=D0=B2=20=D0=94?= =?UTF-8?q?=D0=BC=D0=B8=D1=82=D1=80=D0=B8=D0=B9?= Date: Thu, 24 Aug 2023 10:50:34 +0500 Subject: [PATCH] =?UTF-8?q?=D0=A0=D0=B5=D1=84=D0=B0=D0=BA=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D0=B8=D0=BD=D0=B3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Два хаба избыточно, объеденил всё в один хаб 2. Уведомление клиенту будет отправляться только при обновлении кэша в сервисе WellInfoService 3. В WellInfoService теперь формируется статистика по всем скважинам, а не только по активным 4. Небольшой рефакторинг --- AsbCloudApp/Data/WellInfoDto.cs | 5 -- AsbCloudApp/Data/WellMapInfoDto.cs | 5 ++ .../IntegrationEvents/UpdateWellEvent.cs | 9 --- .../Services/WellInfoService.cs | 28 ++++------ .../Services/WellService.cs | 2 +- .../Controllers/WellOperationController.cs | 11 +--- AsbCloudWebApi/DependencyInjection.cs | 5 +- AsbCloudWebApi/SignalR/WellHub.cs | 56 ------------------- AsbCloudWebApi/SignalR/WellInfoHub.cs | 16 +++++- AsbCloudWebApi/Startup.cs | 1 - 10 files changed, 34 insertions(+), 104 deletions(-) delete mode 100644 AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs delete mode 100644 AsbCloudWebApi/SignalR/WellHub.cs diff --git a/AsbCloudApp/Data/WellInfoDto.cs b/AsbCloudApp/Data/WellInfoDto.cs index 65d26c50..b42d90b4 100644 --- a/AsbCloudApp/Data/WellInfoDto.cs +++ b/AsbCloudApp/Data/WellInfoDto.cs @@ -20,11 +20,6 @@ namespace AsbCloudApp.Data /// Название куста /// public string? Cluster { get; set; } - - /// - /// Название секции - /// - public string? Section { get; set; } /// /// Название месторождения diff --git a/AsbCloudApp/Data/WellMapInfoDto.cs b/AsbCloudApp/Data/WellMapInfoDto.cs index 65374f47..8b0bdb58 100644 --- a/AsbCloudApp/Data/WellMapInfoDto.cs +++ b/AsbCloudApp/Data/WellMapInfoDto.cs @@ -50,6 +50,11 @@ namespace AsbCloudApp.Data /// 2 - завершена /// public int IdState { get; set; } + + /// + /// Название текущей секции + /// + public string? Section { get; set; } /// /// Коэф-т использования автоподачи долота (суммарный ротор + слайд) diff --git a/AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs b/AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs deleted file mode 100644 index 3c259e10..00000000 --- a/AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs +++ /dev/null @@ -1,9 +0,0 @@ -using AsbCloudApp.IntegrationEvents.Interfaces; - -namespace AsbCloudApp.IntegrationEvents; - -/// -/// Обновление информации о скважине -/// -/// -public record UpdateWellEvent(int IdWell) : IIntegrationEvent; \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/WellInfoService.cs b/AsbCloudInfrastructure/Services/WellInfoService.cs index efc1cf28..efcfd54d 100644 --- a/AsbCloudInfrastructure/Services/WellInfoService.cs +++ b/AsbCloudInfrastructure/Services/WellInfoService.cs @@ -6,7 +6,6 @@ using AsbCloudApp.Repositories; using AsbCloudApp.Requests; using AsbCloudApp.Services; using AsbCloudApp.Services.Subsystems; -using AsbCloudDb.Model; using AsbCloudInfrastructure.Background; using AsbCloudInfrastructure.Services.SAUB; using Mapster; @@ -65,7 +64,6 @@ namespace AsbCloudInfrastructure.Services private static async Task WorkAction(string workName, IServiceProvider serviceProvider, CancellationToken token) { - var db = serviceProvider.GetRequiredService(); var wellService = serviceProvider.GetRequiredService(); var operationsStatService = serviceProvider.GetRequiredService(); var processMapRepository = serviceProvider.GetRequiredService(); @@ -73,16 +71,11 @@ namespace AsbCloudInfrastructure.Services var telemetryDataSaubCache = serviceProvider.GetRequiredService>(); var messageHub = serviceProvider.GetRequiredService>(); - var activeWells = await wellService.GetAsync(new() {IdState = 1}, token); + var wells = await wellService.GetAllAsync(token); - IEnumerable activeWellsIds = activeWells - .Select(w => w.Id); + var wellsIds = wells.Select(w => w.Id); - var idTelemetries = activeWells - .Where(w => w.IdTelemetry != null) - .Select(t => t.IdTelemetry); - - var processMapRequests = activeWellsIds.Select(id => new ProcessMapRequest { IdWell = id }); + var processMapRequests = wellsIds.Select(id => new ProcessMapRequest { IdWell = id }); var processMaps = await processMapRepository.GetProcessMapAsync(processMapRequests, token); var wellDepthByProcessMap = processMaps @@ -93,11 +86,12 @@ namespace AsbCloudInfrastructure.Services DepthEnd = g.Max(p => p.DepthEnd) }); - var operationsStat = await operationsStatService.GetWellsStatAsync(activeWellsIds, token); + var operationsStat = await operationsStatService.GetWellsStatAsync(wellsIds, token); - var subsystemStat = await subsystemOperationTimeService.GetStatByActiveWells(activeWellsIds, token); + var subsystemStat = await subsystemOperationTimeService + .GetStatByActiveWells(wellsIds, token); - WellMapInfo = activeWells.Select(well => { + WellMapInfo = wells.Select(well => { var wellMapInfo = well.Adapt(); wellMapInfo.IdState = well.IdState; @@ -133,13 +127,14 @@ namespace AsbCloudInfrastructure.Services else if(currentDepth.HasValue) { wellProcessMap = wellProcessMaps.FirstOrDefault(p => p.DepthStart <= currentDepth.Value && p.DepthEnd >= currentDepth.Value); - idSection ??= wellProcessMap?.IdWellSectionType; } double? planTotalDepth = null; planTotalDepth = wellDepthByProcessMap.FirstOrDefault(p => p.Id == well.Id)?.DepthEnd; planTotalDepth ??= wellOperationsStat?.Total.Plan?.WellDepthEnd; + wellMapInfo.Section = wellLastFactSection?.Caption; + wellMapInfo.FirstFactOperationDateStart = wellOperationsStat?.Total.Fact?.Start ?? wellOperationsStat?.Total.Plan?.Start; @@ -199,8 +194,9 @@ namespace AsbCloudInfrastructure.Services return wellMapInfo; }).ToArray(); - var updateWellInfoEventTasks = activeWellsIds - .Select(idWell => messageHub.HandleAsync(new UpdateWellInfoEvent(idWell), token)); + var updateWellInfoEventTasks = wellsIds.Select(idWell => + messageHub.HandleAsync(new UpdateWellInfoEvent(idWell), token)); + await Task.WhenAll(updateWellInfoEventTasks); } diff --git a/AsbCloudInfrastructure/Services/WellService.cs b/AsbCloudInfrastructure/Services/WellService.cs index c47dd204..4973baf0 100644 --- a/AsbCloudInfrastructure/Services/WellService.cs +++ b/AsbCloudInfrastructure/Services/WellService.cs @@ -92,7 +92,7 @@ namespace AsbCloudInfrastructure.Services Longitude = gCluster.Key.Longitude ?? gDeposit.Key.Longitude, Wells = gCluster.Select(well => { - var dto = wellInfoService.FirstOrDefault(w => w.Id == well.Id); + var dto = wellInfoService.FirstOrDefault(w => w.Id == well.Id && well.IdState == 1); dto ??= well.Adapt(); dto.Latitude ??= gCluster.Key.Latitude ?? gDeposit.Key.Latitude; dto.Longitude ??= gCluster.Key.Longitude ?? gDeposit.Key.Longitude; diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index 76d0777a..388eb81b 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -11,8 +11,6 @@ using System.ComponentModel.DataAnnotations; using System.IO; using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.IntegrationEvents; -using AsbCloudApp.IntegrationEvents.Interfaces; namespace AsbCloudWebApi.Controllers { @@ -25,17 +23,14 @@ namespace AsbCloudWebApi.Controllers [Authorize] public class WellOperationController : ControllerBase { - private readonly IIntegrationEventHandler eventHandler; private readonly IWellOperationRepository operationRepository; private readonly IWellService wellService; private readonly IWellOperationImportService wellOperationImportService; - public WellOperationController(IIntegrationEventHandler eventHandler, - IWellOperationRepository operationRepository, + public WellOperationController(IWellOperationRepository operationRepository, IWellService wellService, IWellOperationImportService wellOperationImportService) { - this.eventHandler = eventHandler; this.operationRepository = operationRepository; this.wellService = wellService; this.wellOperationImportService = wellOperationImportService; @@ -218,8 +213,6 @@ namespace AsbCloudWebApi.Controllers var result = await operationRepository.InsertRangeAsync(values, token) .ConfigureAwait(false); - - await eventHandler.HandleAsync(new UpdateWellEvent(idWell), token); return Ok(result); } @@ -315,8 +308,6 @@ namespace AsbCloudWebApi.Controllers { return BadRequest(ex.Message); } - - await eventHandler.HandleAsync(new UpdateWellEvent(idWell), token); return Ok(); } diff --git a/AsbCloudWebApi/DependencyInjection.cs b/AsbCloudWebApi/DependencyInjection.cs index 00c95b80..09922854 100644 --- a/AsbCloudWebApi/DependencyInjection.cs +++ b/AsbCloudWebApi/DependencyInjection.cs @@ -143,9 +143,8 @@ namespace AsbCloudWebApi { services.AddTransient(); } - + public static void AddIntegrationEvents(this IServiceCollection services) => services - .AddTransient, WellInfoHub>() - .AddTransient, WellHub>(); + .AddTransient, WellInfoHub>(); } } diff --git a/AsbCloudWebApi/SignalR/WellHub.cs b/AsbCloudWebApi/SignalR/WellHub.cs deleted file mode 100644 index fcd62f56..00000000 --- a/AsbCloudWebApi/SignalR/WellHub.cs +++ /dev/null @@ -1,56 +0,0 @@ -using System.Linq; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.IntegrationEvents; -using AsbCloudApp.IntegrationEvents.Interfaces; -using AsbCloudApp.Repositories; -using AsbCloudApp.Requests; -using AsbCloudApp.Services; -using AsbCloudDb.Model; -using Microsoft.AspNetCore.SignalR; - -namespace AsbCloudWebApi.SignalR; - -public class WellHub : BaseHub, IIntegrationEventHandler -{ - private readonly IHubContext hubContext; - private readonly IWellService wellService; - private readonly IWellOperationRepository wellOperationRepository; - - public WellHub(IHubContext hubContext, - IWellService wellService, - IWellOperationRepository wellOperationRepository) - { - this.hubContext = hubContext; - this.wellService = wellService; - this.wellOperationRepository = wellOperationRepository; - } - - public override async Task AddToGroup(string groupName) - { - var idWell = int.Parse(groupName.Split('_')[1]); - - await Groups.AddToGroupAsync(Context.ConnectionId, groupName); - - await HandleAsync(new UpdateWellEvent(idWell), CancellationToken.None); - } - - public async Task HandleAsync(UpdateWellEvent integrationEvent, CancellationToken cancellationToken) - { - const string method = "update_well"; - - var well = await wellService.GetOrDefaultAsync(integrationEvent.IdWell, cancellationToken); - - if(well is null) - return; - - well.Section = (await wellOperationRepository.GetAsync(new WellOperationRequest - { - IdWell = integrationEvent.IdWell, - OperationType = WellOperation.IdOperationTypeFact - }, cancellationToken)).MaxBy(o => o.DateStart)?.WellSectionTypeName; - - await hubContext.Clients.Group($"well_{integrationEvent.IdWell}") - .SendAsync(method, new object[] { well }, cancellationToken); - } -} \ No newline at end of file diff --git a/AsbCloudWebApi/SignalR/WellInfoHub.cs b/AsbCloudWebApi/SignalR/WellInfoHub.cs index 86d5b4ef..677b4f64 100644 --- a/AsbCloudWebApi/SignalR/WellInfoHub.cs +++ b/AsbCloudWebApi/SignalR/WellInfoHub.cs @@ -2,6 +2,7 @@ using System.Threading; using System.Threading.Tasks; using AsbCloudApp.IntegrationEvents; using AsbCloudApp.IntegrationEvents.Interfaces; +using AsbCloudApp.Services; using AsbCloudInfrastructure.Services; using Microsoft.AspNetCore.SignalR; @@ -10,12 +11,15 @@ namespace AsbCloudWebApi.SignalR; public class WellInfoHub : BaseHub, IIntegrationEventHandler { private readonly IHubContext hubContext; + private readonly IWellService wellService; private readonly WellInfoService wellInfoService; public WellInfoHub(IHubContext hubContext, + IWellService wellService, WellInfoService wellInfoService) { this.hubContext = hubContext; + this.wellService = wellService; this.wellInfoService = wellInfoService; } @@ -32,12 +36,18 @@ public class WellInfoHub : BaseHub, IIntegrationEventHandler w.Id == integrationEvent.IdWell); + var well = await wellService.GetOrDefaultAsync(integrationEvent.IdWell, cancellationToken); - if (wellInfo is null) + if(well is null) return; + + var wellInfo = wellInfoService.FirstOrDefault(w => w.Id == well.Id); await hubContext.Clients.Group($"well_info_{integrationEvent.IdWell}") - .SendAsync(method, wellInfo, cancellationToken); + .SendAsync(method, new + { + Well = well, + WellInfo = wellInfo + }, cancellationToken); } } \ No newline at end of file diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index 7ea1edf5..f04a53d1 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -153,7 +153,6 @@ namespace AsbCloudWebApi app.UseEndpoints(endpoints => { endpoints.MapControllers(); - endpoints.MapHub("/hubs/well"); endpoints.MapHub("/hubs/wellInfo"); endpoints.MapHub("/hubs/notifications"); endpoints.MapHub("/hubs/telemetry");