From 47bd9cb56b60c9d110181803aeca1a444599fc26 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=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Wed, 16 Aug 2023 17:30:03 +0500 Subject: [PATCH 1/6] =?UTF-8?q?=D0=9E=D1=82=D0=BE=D0=B1=D1=80=D0=B0=D0=B6?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= =?UTF-8?q?=20=D0=B2=20=D1=80=D0=B5=D0=B0=D0=BB=D1=8C=D0=BD=D0=BE=D0=BC=20?= =?UTF-8?q?=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Добавил инфраструктуру для доменных событий. 2. Сделал Hub отправки для информации о скважине. --- .../Events/IIntegrationEvent.cs | 6 +++ .../Events/WellInfoUpdaterEvent.cs | 7 +++ .../IIntegrationEventHandler.cs | 20 ++++++++ .../Services/WellInfoService.cs | 10 +++- AsbCloudWebApi/DependencyInjection.cs | 8 ++- AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs | 49 +++++++++++++++++++ AsbCloudWebApi/Startup.cs | 3 ++ SignalRTestClient/Program.cs | 6 +-- 8 files changed, 104 insertions(+), 5 deletions(-) create mode 100644 AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs create mode 100644 AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs create mode 100644 AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs create mode 100644 AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs diff --git a/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs b/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs new file mode 100644 index 00000000..c44d83a0 --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs @@ -0,0 +1,6 @@ +namespace AsbCloudApp.IntegrationEvents.Events; + +/// +/// Интерфейс маркер для доменных событий +/// +public interface IIntegrationEvent { } \ No newline at end of file diff --git a/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs b/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs new file mode 100644 index 00000000..6e95e72e --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs @@ -0,0 +1,7 @@ +namespace AsbCloudApp.IntegrationEvents.Events; + +/// +/// Обновление информации о скважине +/// +/// +public record WellInfoUpdaterEvent(int IdWell) : IIntegrationEvent; \ No newline at end of file diff --git a/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs b/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs new file mode 100644 index 00000000..1fec260a --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs @@ -0,0 +1,20 @@ +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents.Events; + +namespace AsbCloudApp.IntegrationEvents; + +/// +/// Обработчик событий +/// +/// +public interface IIntegrationEventHandler where T: IIntegrationEvent +{ + /// + /// Метод обработки события + /// + /// + /// + /// + Task HandleAsync(T integrationEvent, CancellationToken cancellationToken); +} \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/WellInfoService.cs b/AsbCloudInfrastructure/Services/WellInfoService.cs index 7066ef22..c05b51a2 100644 --- a/AsbCloudInfrastructure/Services/WellInfoService.cs +++ b/AsbCloudInfrastructure/Services/WellInfoService.cs @@ -16,6 +16,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Events; namespace AsbCloudInfrastructure.Services { @@ -36,7 +38,7 @@ namespace AsbCloudInfrastructure.Services private readonly IWitsRecordRepository witsRecord1Repository; private readonly IGtrRepository gtrRepository; private static IEnumerable WellMapInfo = Enumerable.Empty(); - + public WellInfoService( TelemetryDataCache telemetryDataSaubCache, TelemetryDataCache telemetryDataSpinCache, @@ -69,6 +71,7 @@ namespace AsbCloudInfrastructure.Services var processMapRepository = serviceProvider.GetRequiredService(); var subsystemOperationTimeService = serviceProvider.GetRequiredService(); var telemetryDataSaubCache = serviceProvider.GetRequiredService>(); + var messageHub = serviceProvider.GetRequiredService>(); var activeWells = await wellService.GetAsync(new() {IdState = 1}, token); @@ -167,6 +170,11 @@ namespace AsbCloudInfrastructure.Services return wellMapInfo; }).ToArray(); + + foreach (var idWell in activeWellsIds) + { + await messageHub.HandleAsync(new WellInfoUpdaterEvent(idWell), token); + } } private WellMapInfoWithTelemetryStat Convert(WellMapInfoWithComanies wellInfo) diff --git a/AsbCloudWebApi/DependencyInjection.cs b/AsbCloudWebApi/DependencyInjection.cs index 7477f745..a0a6ab24 100644 --- a/AsbCloudWebApi/DependencyInjection.cs +++ b/AsbCloudWebApi/DependencyInjection.cs @@ -3,7 +3,6 @@ using AsbCloudApp.Repositories; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services; using Microsoft.AspNetCore.Authentication.JwtBearer; -using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using Microsoft.OpenApi.Models; @@ -12,8 +11,12 @@ using System.Collections.Generic; using System.IO; using System.Reflection; using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Events; using AsbCloudApp.Services.Notifications; +using AsbCloudWebApi.SignalR; using AsbCloudWebApi.SignalR.Services; +using Microsoft.AspNetCore.Mvc; using Microsoft.OpenApi.Any; namespace AsbCloudWebApi @@ -140,5 +143,8 @@ namespace AsbCloudWebApi { services.AddTransient(); } + + public static void AddIntegrationEvents(this IServiceCollection services) => services + .AddTransient, WellInfoUpdaterHub>(); } } diff --git a/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs b/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs new file mode 100644 index 00000000..9490df4d --- /dev/null +++ b/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs @@ -0,0 +1,49 @@ +using System.Text.Json; +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Events; +using AsbCloudInfrastructure.Services; +using Microsoft.AspNetCore.SignalR; + +namespace AsbCloudWebApi.SignalR; + +public class WellInfoUpdaterHub : Hub, IIntegrationEventHandler +{ + private const string groupTemplate = "system_operation_updater_well_{0}"; + + private readonly IHubContext hubContext; + private readonly WellInfoService wellInfoService; + + public WellInfoUpdaterHub(IHubContext hubContext, + WellInfoService wellInfoService) + { + this.hubContext = hubContext; + this.wellInfoService = wellInfoService; + } + + public async Task OnConnectedAsync(int idWell) + { + await base.OnConnectedAsync(); + + await Groups.AddToGroupAsync(Context.ConnectionId, string.Format(groupTemplate, idWell)); + + await HandleAsync(new WellInfoUpdaterEvent(idWell), CancellationToken.None); + } + + public async Task HandleAsync(WellInfoUpdaterEvent integrationEvent, CancellationToken cancellationToken) + { + const string method = "well_info_update"; + + var wellInfo = wellInfoService.FirstOrDefault(w => w.Id == integrationEvent.IdWell); + + if (wellInfo != null) + { + var serializedObject = JsonSerializer.Serialize(wellInfo); + + await hubContext.Clients.Group(string.Format(groupTemplate, integrationEvent.IdWell)) + .SendCoreAsync(method, new object[] { serializedObject }, cancellationToken); + } + } + +} \ No newline at end of file diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index 0fb3ba87..aadaf829 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -45,6 +45,8 @@ namespace AsbCloudWebApi services.AddNotificationTransportServices(); + services.AddIntegrationEvents(); + services.AddJWTAuthentication(); services.AddSignalR() @@ -151,6 +153,7 @@ namespace AsbCloudWebApi app.UseEndpoints(endpoints => { endpoints.MapControllers(); + endpoints.MapHub("/hubs/limitingParameters"); endpoints.MapHub("/hubs/notifications"); endpoints.MapHub("/hubs/telemetry"); endpoints.MapHub("/hubs/reports"); diff --git a/SignalRTestClient/Program.cs b/SignalRTestClient/Program.cs index 487f1253..9ae72e04 100644 --- a/SignalRTestClient/Program.cs +++ b/SignalRTestClient/Program.cs @@ -10,7 +10,7 @@ internal class Program { var connectionBuilder = new HubConnectionBuilder(); var connection = connectionBuilder - .WithUrl("http://localhost:5000/hubs/notifications", connectionOptions => { + .WithUrl("http://localhost:5000/hubs/limitingParameters", connectionOptions => { connectionOptions.AccessTokenProvider = AccessTokenProvider; }) .WithAutomaticReconnect() @@ -26,9 +26,9 @@ internal class Program connection.StartAsync().Wait(); //Console.WriteLine("OnConnected"); - //connection.SendCoreAsync("OnConnected", new object[] { }, CancellationToken.None).Wait(); + connection.SendCoreAsync("OnConnectedAsync", new object[] { 1 }, CancellationToken.None).Wait(); - var subsction = connection.On("receiveNotifications", (str1) => { + var subsction = connection.On("well_info_update", (str1) => { Console.WriteLine(str1); } ); From 3f7f455281164e88c881a96705ff4bc7303ed941 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=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Fri, 18 Aug 2023 15:51:58 +0500 Subject: [PATCH 2/6] =?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=20+=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2?= =?UTF-8?q?=D0=B8=D0=BB=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D1=83?= =?UTF-8?q?=20=D0=B8=D0=BD=D1=84=D0=BE=D1=80=D0=BC=D0=B0=D1=86=D0=B8=D0=B8?= =?UTF-8?q?=20=D0=BE=20=D1=81=D0=BA=D0=B2=D0=B0=D0=B6=D0=B8=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=87=D0=B5=D1=80=D0=B5=D0=B7=20SignalR?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudApp/Data/WellInfoDto.cs | 5 ++ AsbCloudApp/Data/WellMapInfoDto.cs | 25 ++++++++ .../Events/WellInfoUpdaterEvent.cs | 7 --- .../IIntegrationEvent.cs | 2 +- .../IIntegrationEventHandler.cs | 3 +- .../IntegrationEvents/UpdateWellEvent.cs | 9 +++ .../IntegrationEvents/UpdateWellInfoEvent.cs | 9 +++ .../Services/WellInfoService.cs | 46 ++++++++++++--- .../Controllers/WellOperationController.cs | 15 ++++- AsbCloudWebApi/DependencyInjection.cs | 5 +- AsbCloudWebApi/SignalR/WellHub.cs | 58 +++++++++++++++++++ AsbCloudWebApi/SignalR/WellInfoHub.cs | 45 ++++++++++++++ AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs | 49 ---------------- AsbCloudWebApi/Startup.cs | 3 +- 14 files changed, 208 insertions(+), 73 deletions(-) delete mode 100644 AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs rename AsbCloudApp/IntegrationEvents/{Events => Interfaces}/IIntegrationEvent.cs (73%) rename AsbCloudApp/IntegrationEvents/{ => Interfaces}/IIntegrationEventHandler.cs (85%) create mode 100644 AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs create mode 100644 AsbCloudApp/IntegrationEvents/UpdateWellInfoEvent.cs create mode 100644 AsbCloudWebApi/SignalR/WellHub.cs create mode 100644 AsbCloudWebApi/SignalR/WellInfoHub.cs delete mode 100644 AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs diff --git a/AsbCloudApp/Data/WellInfoDto.cs b/AsbCloudApp/Data/WellInfoDto.cs index b42d90b4..65d26c50 100644 --- a/AsbCloudApp/Data/WellInfoDto.cs +++ b/AsbCloudApp/Data/WellInfoDto.cs @@ -20,6 +20,11 @@ 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 242ecb6c..65374f47 100644 --- a/AsbCloudApp/Data/WellMapInfoDto.cs +++ b/AsbCloudApp/Data/WellMapInfoDto.cs @@ -89,6 +89,31 @@ namespace AsbCloudApp.Data /// public PlanFactDto ROP { get; set; } = null!; + /// + /// Нагрузка на долота, Т + /// + public PlanFactDto AxialLoad { get; set; } = null!; + + /// + /// Обороты ротора + /// + public PlanFactDto TopDriveSpeed { get; set; } = null!; + + /// + /// Момент ротора кн/м + /// + public PlanFactDto TopDriveTorque { get; set; } = null!; + + /// + /// Перепад давления + /// + public PlanFactDto Pressure { get; set; } = null!; + + /// + /// Действующее задание давления, атм + /// + public double? PressureSp { get; set; } + /// /// Плановая и текущая глубина /// diff --git a/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs b/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs deleted file mode 100644 index 6e95e72e..00000000 --- a/AsbCloudApp/IntegrationEvents/Events/WellInfoUpdaterEvent.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace AsbCloudApp.IntegrationEvents.Events; - -/// -/// Обновление информации о скважине -/// -/// -public record WellInfoUpdaterEvent(int IdWell) : IIntegrationEvent; \ No newline at end of file diff --git a/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs b/AsbCloudApp/IntegrationEvents/Interfaces/IIntegrationEvent.cs similarity index 73% rename from AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs rename to AsbCloudApp/IntegrationEvents/Interfaces/IIntegrationEvent.cs index c44d83a0..a80dfdee 100644 --- a/AsbCloudApp/IntegrationEvents/Events/IIntegrationEvent.cs +++ b/AsbCloudApp/IntegrationEvents/Interfaces/IIntegrationEvent.cs @@ -1,4 +1,4 @@ -namespace AsbCloudApp.IntegrationEvents.Events; +namespace AsbCloudApp.IntegrationEvents.Interfaces; /// /// Интерфейс маркер для доменных событий diff --git a/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs b/AsbCloudApp/IntegrationEvents/Interfaces/IIntegrationEventHandler.cs similarity index 85% rename from AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs rename to AsbCloudApp/IntegrationEvents/Interfaces/IIntegrationEventHandler.cs index 1fec260a..18695f07 100644 --- a/AsbCloudApp/IntegrationEvents/IIntegrationEventHandler.cs +++ b/AsbCloudApp/IntegrationEvents/Interfaces/IIntegrationEventHandler.cs @@ -1,8 +1,7 @@ using System.Threading; using System.Threading.Tasks; -using AsbCloudApp.IntegrationEvents.Events; -namespace AsbCloudApp.IntegrationEvents; +namespace AsbCloudApp.IntegrationEvents.Interfaces; /// /// Обработчик событий diff --git a/AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs b/AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs new file mode 100644 index 00000000..3c259e10 --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/UpdateWellEvent.cs @@ -0,0 +1,9 @@ +using AsbCloudApp.IntegrationEvents.Interfaces; + +namespace AsbCloudApp.IntegrationEvents; + +/// +/// Обновление информации о скважине +/// +/// +public record UpdateWellEvent(int IdWell) : IIntegrationEvent; \ No newline at end of file diff --git a/AsbCloudApp/IntegrationEvents/UpdateWellInfoEvent.cs b/AsbCloudApp/IntegrationEvents/UpdateWellInfoEvent.cs new file mode 100644 index 00000000..4ae75a19 --- /dev/null +++ b/AsbCloudApp/IntegrationEvents/UpdateWellInfoEvent.cs @@ -0,0 +1,9 @@ +using AsbCloudApp.IntegrationEvents.Interfaces; + +namespace AsbCloudApp.IntegrationEvents; + +/// +/// Обновление показателей бурения +/// +/// +public record UpdateWellInfoEvent(int IdWell) : IIntegrationEvent; \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/WellInfoService.cs b/AsbCloudInfrastructure/Services/WellInfoService.cs index c05b51a2..ea934b14 100644 --- a/AsbCloudInfrastructure/Services/WellInfoService.cs +++ b/AsbCloudInfrastructure/Services/WellInfoService.cs @@ -17,7 +17,7 @@ using System.Linq; using System.Threading; using System.Threading.Tasks; using AsbCloudApp.IntegrationEvents; -using AsbCloudApp.IntegrationEvents.Events; +using AsbCloudApp.IntegrationEvents.Interfaces; namespace AsbCloudInfrastructure.Services { @@ -71,7 +71,7 @@ namespace AsbCloudInfrastructure.Services var processMapRepository = serviceProvider.GetRequiredService(); var subsystemOperationTimeService = serviceProvider.GetRequiredService(); var telemetryDataSaubCache = serviceProvider.GetRequiredService>(); - var messageHub = serviceProvider.GetRequiredService>(); + var messageHub = serviceProvider.GetRequiredService>(); var activeWells = await wellService.GetAsync(new() {IdState = 1}, token); @@ -103,10 +103,12 @@ namespace AsbCloudInfrastructure.Services double? currentDepth = null; + TelemetryDataSaubDto? lastSaubTelemetry = null; + if (well.IdTelemetry.HasValue) { wellMapInfo.IdTelemetry = well.IdTelemetry.Value; - var lastSaubTelemetry = telemetryDataSaubCache.GetLastOrDefault(well.IdTelemetry.Value); + lastSaubTelemetry = telemetryDataSaubCache.GetLastOrDefault(well.IdTelemetry.Value); if(lastSaubTelemetry is not null) { currentDepth = lastSaubTelemetry.WellDepth; @@ -122,16 +124,16 @@ namespace AsbCloudInfrastructure.Services .OrderBy(p => p.DepthEnd); int? idSection = wellLastFactSection?.Id; - ProcessMapPlanDto? welllProcessMap = null; + ProcessMapPlanDto? wellProcessMap = null; if (idSection.HasValue) { - welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.IdWellSectionType == idSection); + wellProcessMap = wellProcessMaps.FirstOrDefault(p => p.IdWellSectionType == idSection); } else if(currentDepth.HasValue) { - welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.DepthStart <= currentDepth.Value && p.DepthEnd >= currentDepth.Value); - idSection ??= welllProcessMap?.IdWellSectionType; + wellProcessMap = wellProcessMaps.FirstOrDefault(p => p.DepthStart <= currentDepth.Value && p.DepthEnd >= currentDepth.Value); + idSection ??= wellProcessMap?.IdWellSectionType; } double? planTotalDepth = null; @@ -143,6 +145,32 @@ namespace AsbCloudInfrastructure.Services wellMapInfo.LastPredictOperationDateEnd = wellOperationsStat?.Total.Plan?.End; + wellMapInfo.AxialLoad = new() + { + Plan = wellProcessMap?.AxialLoad.Plan, + Fact = lastSaubTelemetry?.AxialLoad + }; + + wellMapInfo.TopDriveSpeed = new() + { + Plan = wellProcessMap?.TopDriveSpeed.Plan, + Fact = lastSaubTelemetry?.RotorSpeed + }; + + wellMapInfo.TopDriveTorque = new() + { + Plan = wellProcessMap?.TopDriveTorque.Plan, + Fact = lastSaubTelemetry?.RotorTorque + }; + + wellMapInfo.Pressure = new() + { + Plan = wellProcessMap?.Pressure.Plan, + Fact = lastSaubTelemetry?.Pressure + }; + + wellMapInfo.PressureSp = lastSaubTelemetry?.PressureSp; + wellMapInfo.WellDepth = new() { Plan = planTotalDepth, @@ -151,7 +179,7 @@ namespace AsbCloudInfrastructure.Services wellMapInfo.ROP = new() { - Plan = welllProcessMap?.RopPlan, + Plan = wellProcessMap?.RopPlan, Fact = wellOperationsStat?.Total.Fact?.Rop, }; @@ -173,7 +201,7 @@ namespace AsbCloudInfrastructure.Services foreach (var idWell in activeWellsIds) { - await messageHub.HandleAsync(new WellInfoUpdaterEvent(idWell), token); + await messageHub.HandleAsync(new UpdateWellInfoEvent(idWell), token); } } diff --git a/AsbCloudWebApi/Controllers/WellOperationController.cs b/AsbCloudWebApi/Controllers/WellOperationController.cs index 0ab6f0c1..76d0777a 100644 --- a/AsbCloudWebApi/Controllers/WellOperationController.cs +++ b/AsbCloudWebApi/Controllers/WellOperationController.cs @@ -11,6 +11,8 @@ using System.ComponentModel.DataAnnotations; using System.IO; using System.Threading; using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Interfaces; namespace AsbCloudWebApi.Controllers { @@ -23,13 +25,18 @@ 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(IWellOperationRepository operationService, IWellService wellService, IWellOperationImportService wellOperationImportService) + public WellOperationController(IIntegrationEventHandler eventHandler, + IWellOperationRepository operationRepository, + IWellService wellService, + IWellOperationImportService wellOperationImportService) { - this.operationRepository = operationService; + this.eventHandler = eventHandler; + this.operationRepository = operationRepository; this.wellService = wellService; this.wellOperationImportService = wellOperationImportService; } @@ -212,6 +219,8 @@ namespace AsbCloudWebApi.Controllers var result = await operationRepository.InsertRangeAsync(values, token) .ConfigureAwait(false); + await eventHandler.HandleAsync(new UpdateWellEvent(idWell), token); + return Ok(result); } @@ -306,6 +315,8 @@ 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 a0a6ab24..00c95b80 100644 --- a/AsbCloudWebApi/DependencyInjection.cs +++ b/AsbCloudWebApi/DependencyInjection.cs @@ -12,7 +12,7 @@ using System.IO; using System.Reflection; using System.Threading.Tasks; using AsbCloudApp.IntegrationEvents; -using AsbCloudApp.IntegrationEvents.Events; +using AsbCloudApp.IntegrationEvents.Interfaces; using AsbCloudApp.Services.Notifications; using AsbCloudWebApi.SignalR; using AsbCloudWebApi.SignalR.Services; @@ -145,6 +145,7 @@ namespace AsbCloudWebApi } public static void AddIntegrationEvents(this IServiceCollection services) => services - .AddTransient, WellInfoUpdaterHub>(); + .AddTransient, WellInfoHub>() + .AddTransient, WellHub>(); } } diff --git a/AsbCloudWebApi/SignalR/WellHub.cs b/AsbCloudWebApi/SignalR/WellHub.cs new file mode 100644 index 00000000..125c4258 --- /dev/null +++ b/AsbCloudWebApi/SignalR/WellHub.cs @@ -0,0 +1,58 @@ +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 : Hub, IIntegrationEventHandler +{ + private const string groupTemplate = "update_id_well_{0}"; + + 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 async Task OnConnectedAsync(int idWell) + { + await base.OnConnectedAsync(); + + await Groups.AddToGroupAsync(Context.ConnectionId, string.Format(groupTemplate, idWell)); + + 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(string.Format(groupTemplate, 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 new file mode 100644 index 00000000..31838981 --- /dev/null +++ b/AsbCloudWebApi/SignalR/WellInfoHub.cs @@ -0,0 +1,45 @@ +using System.Threading; +using System.Threading.Tasks; +using AsbCloudApp.IntegrationEvents; +using AsbCloudApp.IntegrationEvents.Interfaces; +using AsbCloudInfrastructure.Services; +using Microsoft.AspNetCore.SignalR; + +namespace AsbCloudWebApi.SignalR; + +public class WellInfoHub : Hub, IIntegrationEventHandler +{ + private const string groupTemplate = "well_info_id_well_{0}"; + + private readonly IHubContext hubContext; + private readonly WellInfoService wellInfoService; + + public WellInfoHub(IHubContext hubContext, + WellInfoService wellInfoService) + { + this.hubContext = hubContext; + this.wellInfoService = wellInfoService; + } + + public async Task OnConnectedAsync(int idWell) + { + await base.OnConnectedAsync(); + + await Groups.AddToGroupAsync(Context.ConnectionId, string.Format(groupTemplate, idWell)); + + await HandleAsync(new UpdateWellInfoEvent(idWell), CancellationToken.None); + } + + public async Task HandleAsync(UpdateWellInfoEvent integrationEvent, CancellationToken cancellationToken) + { + const string method = "update_well_info"; + + var wellInfo = wellInfoService.FirstOrDefault(w => w.Id == integrationEvent.IdWell); + + if (wellInfo is null) + return; + + await hubContext.Clients.Group(string.Format(groupTemplate, integrationEvent.IdWell)) + .SendAsync(method, wellInfo, cancellationToken); + } +} \ No newline at end of file diff --git a/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs b/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs deleted file mode 100644 index 9490df4d..00000000 --- a/AsbCloudWebApi/SignalR/WellInfoUpdaterHub.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System.Text.Json; -using System.Threading; -using System.Threading.Tasks; -using AsbCloudApp.IntegrationEvents; -using AsbCloudApp.IntegrationEvents.Events; -using AsbCloudInfrastructure.Services; -using Microsoft.AspNetCore.SignalR; - -namespace AsbCloudWebApi.SignalR; - -public class WellInfoUpdaterHub : Hub, IIntegrationEventHandler -{ - private const string groupTemplate = "system_operation_updater_well_{0}"; - - private readonly IHubContext hubContext; - private readonly WellInfoService wellInfoService; - - public WellInfoUpdaterHub(IHubContext hubContext, - WellInfoService wellInfoService) - { - this.hubContext = hubContext; - this.wellInfoService = wellInfoService; - } - - public async Task OnConnectedAsync(int idWell) - { - await base.OnConnectedAsync(); - - await Groups.AddToGroupAsync(Context.ConnectionId, string.Format(groupTemplate, idWell)); - - await HandleAsync(new WellInfoUpdaterEvent(idWell), CancellationToken.None); - } - - public async Task HandleAsync(WellInfoUpdaterEvent integrationEvent, CancellationToken cancellationToken) - { - const string method = "well_info_update"; - - var wellInfo = wellInfoService.FirstOrDefault(w => w.Id == integrationEvent.IdWell); - - if (wellInfo != null) - { - var serializedObject = JsonSerializer.Serialize(wellInfo); - - await hubContext.Clients.Group(string.Format(groupTemplate, integrationEvent.IdWell)) - .SendCoreAsync(method, new object[] { serializedObject }, cancellationToken); - } - } - -} \ No newline at end of file diff --git a/AsbCloudWebApi/Startup.cs b/AsbCloudWebApi/Startup.cs index aadaf829..7ea1edf5 100644 --- a/AsbCloudWebApi/Startup.cs +++ b/AsbCloudWebApi/Startup.cs @@ -153,7 +153,8 @@ namespace AsbCloudWebApi app.UseEndpoints(endpoints => { endpoints.MapControllers(); - endpoints.MapHub("/hubs/limitingParameters"); + endpoints.MapHub("/hubs/well"); + endpoints.MapHub("/hubs/wellInfo"); endpoints.MapHub("/hubs/notifications"); endpoints.MapHub("/hubs/telemetry"); endpoints.MapHub("/hubs/reports"); From 4541fb42a9d21f451a6b5793a554fc31bf57bb33 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=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Mon, 21 Aug 2023 17:37:17 +0500 Subject: [PATCH 3/6] =?UTF-8?q?=D0=A0=D0=B0=D1=81=D0=BF=D0=B0=D1=80=D0=B0?= =?UTF-8?q?=D0=BB=D0=BB=D0=B5=D0=BB=D0=B8=D0=BB=20=D0=B2=D1=8B=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D0=BD=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B7=D0=B0=D0=B4=D0=B0?= =?UTF-8?q?=D1=87=20=D0=BE=D1=82=D0=BF=D1=80=D0=B0=D0=B2=D0=BA=D0=B8=20?= =?UTF-8?q?=D1=83=D0=B2=D0=B5=D0=B4=D0=BE=D0=BC=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- AsbCloudInfrastructure/Services/WellInfoService.cs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellInfoService.cs b/AsbCloudInfrastructure/Services/WellInfoService.cs index ea934b14..efc1cf28 100644 --- a/AsbCloudInfrastructure/Services/WellInfoService.cs +++ b/AsbCloudInfrastructure/Services/WellInfoService.cs @@ -199,10 +199,9 @@ namespace AsbCloudInfrastructure.Services return wellMapInfo; }).ToArray(); - foreach (var idWell in activeWellsIds) - { - await messageHub.HandleAsync(new UpdateWellInfoEvent(idWell), token); - } + var updateWellInfoEventTasks = activeWellsIds + .Select(idWell => messageHub.HandleAsync(new UpdateWellInfoEvent(idWell), token)); + await Task.WhenAll(updateWellInfoEventTasks); } private WellMapInfoWithTelemetryStat Convert(WellMapInfoWithComanies wellInfo) From 70ae387685b0874a193307053779528c51271a4c 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=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Wed, 23 Aug 2023 09:44:17 +0500 Subject: [PATCH 4/6] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=20=D0=90=D0=9F?= =?UTF-8?q?=D0=94?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Subsystems/SubsystemOperationTimeService.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs index 1faa769f..a971116e 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeService.cs @@ -230,11 +230,15 @@ namespace AsbCloudInfrastructure.Services.Subsystems var beginUTC = gtDate.HasValue ? gtDate.Value.ToUtcDateTimeOffset(hoursOffset) - : DateTime.Today.AddDays(-1).ToUtcDateTimeOffset(hoursOffset); + : db.SubsystemOperationTimes.Min(s => s.DateStart) + .DateTime + .ToUtcDateTimeOffset(hoursOffset); var endUTC = ltDate.HasValue ? ltDate.Value.ToUtcDateTimeOffset(hoursOffset) - : DateTime.Today.ToUtcDateTimeOffset(hoursOffset); + : db.SubsystemOperationTimes.Max(s => s.DateEnd) + .DateTime + .ToUtcDateTimeOffset(hoursOffset); var telemetryIds = wells .Where(w => w.IdTelemetry is not null) From c50878f5f16056ed3a1579dace9518c0f78389eb 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=20=D0=90=D0=BB=D0=B5=D0=BA?= =?UTF-8?q?=D1=81=D0=B0=D0=BD=D0=B4=D1=80=D0=BE=D0=B2=D0=B8=D1=87?= Date: Wed, 23 Aug 2023 14:51:04 +0500 Subject: [PATCH 5/6] =?UTF-8?q?=D0=A4=D0=B8=D0=BA=D1=81=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit На клиенте нет возможности передавать аргументы в функции. Сделал реализацию через группы --- AsbCloudWebApi/SignalR/WellHub.cs | 14 ++++++-------- AsbCloudWebApi/SignalR/WellInfoHub.cs | 14 ++++++-------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/AsbCloudWebApi/SignalR/WellHub.cs b/AsbCloudWebApi/SignalR/WellHub.cs index 125c4258..fcd62f56 100644 --- a/AsbCloudWebApi/SignalR/WellHub.cs +++ b/AsbCloudWebApi/SignalR/WellHub.cs @@ -11,10 +11,8 @@ using Microsoft.AspNetCore.SignalR; namespace AsbCloudWebApi.SignalR; -public class WellHub : Hub, IIntegrationEventHandler +public class WellHub : BaseHub, IIntegrationEventHandler { - private const string groupTemplate = "update_id_well_{0}"; - private readonly IHubContext hubContext; private readonly IWellService wellService; private readonly IWellOperationRepository wellOperationRepository; @@ -28,11 +26,11 @@ public class WellHub : Hub, IIntegrationEventHandler this.wellOperationRepository = wellOperationRepository; } - public async Task OnConnectedAsync(int idWell) + public override async Task AddToGroup(string groupName) { - await base.OnConnectedAsync(); - - await Groups.AddToGroupAsync(Context.ConnectionId, string.Format(groupTemplate, idWell)); + var idWell = int.Parse(groupName.Split('_')[1]); + + await Groups.AddToGroupAsync(Context.ConnectionId, groupName); await HandleAsync(new UpdateWellEvent(idWell), CancellationToken.None); } @@ -52,7 +50,7 @@ public class WellHub : Hub, IIntegrationEventHandler OperationType = WellOperation.IdOperationTypeFact }, cancellationToken)).MaxBy(o => o.DateStart)?.WellSectionTypeName; - await hubContext.Clients.Group(string.Format(groupTemplate, integrationEvent.IdWell)) + 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 31838981..86d5b4ef 100644 --- a/AsbCloudWebApi/SignalR/WellInfoHub.cs +++ b/AsbCloudWebApi/SignalR/WellInfoHub.cs @@ -7,10 +7,8 @@ using Microsoft.AspNetCore.SignalR; namespace AsbCloudWebApi.SignalR; -public class WellInfoHub : Hub, IIntegrationEventHandler +public class WellInfoHub : BaseHub, IIntegrationEventHandler { - private const string groupTemplate = "well_info_id_well_{0}"; - private readonly IHubContext hubContext; private readonly WellInfoService wellInfoService; @@ -21,11 +19,11 @@ public class WellInfoHub : Hub, IIntegrationEventHandler this.wellInfoService = wellInfoService; } - public async Task OnConnectedAsync(int idWell) + public override async Task AddToGroup(string groupName) { - await base.OnConnectedAsync(); - - await Groups.AddToGroupAsync(Context.ConnectionId, string.Format(groupTemplate, idWell)); + var idWell = int.Parse(groupName.Split('_')[2]); + + await Groups.AddToGroupAsync(Context.ConnectionId, groupName); await HandleAsync(new UpdateWellInfoEvent(idWell), CancellationToken.None); } @@ -39,7 +37,7 @@ public class WellInfoHub : Hub, IIntegrationEventHandler if (wellInfo is null) return; - await hubContext.Clients.Group(string.Format(groupTemplate, integrationEvent.IdWell)) + await hubContext.Clients.Group($"well_info_{integrationEvent.IdWell}") .SendAsync(method, wellInfo, cancellationToken); } } \ No newline at end of file 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 6/6] =?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");