From 9347e9610b62cf8a9f22186be791f9bfa2ea800f Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Mon, 22 May 2023 10:20:45 +0500 Subject: [PATCH] =?UTF-8?q?WellInfoService=20=D0=B8=D1=81=D0=BF=D0=BE?= =?UTF-8?q?=D0=BB=D1=8C=D0=B7=D1=83=D0=B5=D1=82=20=D0=BA=D0=B5=D1=88=20?= =?UTF-8?q?=D1=82=D0=B5=D0=BB=D0=B5=D0=BC=D0=B5=D1=82=D1=80=D0=B8=D0=B8=20?= =?UTF-8?q?=D0=B2=D0=BC=D0=B5=D1=81=D1=82=D0=BE=20=D0=B4=D0=BE=D0=BB=D0=B3?= =?UTF-8?q?=D0=BE=D0=B3=D0=BE=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=D0=B0?= =?UTF-8?q?.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/SAUB/TelemetryDataCache.cs | 8 +++ .../Services/WellInfoService.cs | 54 +++++++++---------- 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs index bb3bb20c..53089f17 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs @@ -125,6 +125,14 @@ namespace AsbCloudInfrastructure.Services.SAUB return items; } + public TDto? GetLastOrDefault(int idTelemetry) + { + if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem)) + return default; + + return cacheItem.LastData.LastOrDefault(); + } + public DatesRangeDto? GetOrDefaultDataDateRange(int idTelemetry) { if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem)) diff --git a/AsbCloudInfrastructure/Services/WellInfoService.cs b/AsbCloudInfrastructure/Services/WellInfoService.cs index f37cb2f6..e298fe7c 100644 --- a/AsbCloudInfrastructure/Services/WellInfoService.cs +++ b/AsbCloudInfrastructure/Services/WellInfoService.cs @@ -1,13 +1,14 @@ using AsbCloudApp.Data; using AsbCloudApp.Data.ProcessMap; +using AsbCloudApp.Data.SAUB; 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; -using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; using System; using System.Collections.Generic; @@ -46,7 +47,8 @@ namespace AsbCloudInfrastructure.Services var operationsStatService = serviceProvider.GetRequiredService(); var processMapRepository = serviceProvider.GetRequiredService(); var subsystemOperationTimeService = serviceProvider.GetRequiredService(); - + var telemetryDataSaubCache = serviceProvider.GetRequiredService>(); + var activeWells = await wellService.GetAsync(new() {IdState = 1}, token); IEnumerable activeWellsIds = activeWells @@ -56,21 +58,6 @@ namespace AsbCloudInfrastructure.Services .Where(w => w.IdTelemetry != null) .Select(t => t.IdTelemetry); - var lastTelemetryInfo = await db.TelemetryDataSaub - .Where(t => idTelemetries.Contains(t.IdTelemetry)) - .Select(t => new - { - t.IdTelemetry, - t.WellDepth, - t.DateTime, - }) - .GroupBy(t => t.IdTelemetry) - .Select(g => g.OrderByDescending(t => t.DateTime) - .First() - ) - .AsNoTracking() - .ToArrayAsync(token); - var processMapRequests = activeWellsIds.Select(id => new ProcessMapRequest { IdWell = id }); var processMaps = await processMapRepository.GetProcessMapAsync(processMapRequests, token); @@ -83,40 +70,46 @@ namespace AsbCloudInfrastructure.Services }); var operationsStat = await operationsStatService.GetWellsStatAsync(activeWellsIds, token); + var subsystemStat = await subsystemOperationTimeService.GetStatByActiveWells(activeWellsIds, token); WellMapInfo = activeWells.Select(well => { var wellMapInfo = well.Adapt(); - - var wellLastTelemetryInfo = lastTelemetryInfo.FirstOrDefault(t => t.IdTelemetry == well.IdTelemetry); - - var wellOperationsStat = operationsStat.FirstOrDefault(s => s.Id == well.Id); + var wellOperationsStat = operationsStat.FirstOrDefault(s => s.Id == well.Id); var wellLastFactSection = wellOperationsStat?.Sections.LastOrDefault(s => s.Fact is not null); - var wellSubsystemStat = subsystemStat.FirstOrDefault(s => s.Well.Id == well.Id); + double? currentDepth = null; + DateTime lastTelemetryDate = default; - double currentDepth = wellLastTelemetryInfo?.WellDepth - ?? wellLastFactSection?.Fact?.WellDepthEnd - ?? 0d; + if (well.IdTelemetry.HasValue) + { + var lastSaubTelemetry = telemetryDataSaubCache.GetLastOrDefault(well.IdTelemetry.Value); + if(lastSaubTelemetry is not null) + { + currentDepth = lastSaubTelemetry.WellDepth; + lastTelemetryDate = lastSaubTelemetry.DateTime; + } + } + + currentDepth ??= wellLastFactSection?.Fact?.WellDepthEnd; var wellProcessMaps = processMaps .Where(p => p.IdWell == well.Id) .OrderBy(p => p.DepthEnd); int? idSection = wellLastFactSection?.Id; + ProcessMapPlanDto? welllProcessMap = null; - ProcessMapPlanDto? welllProcessMap; - if (idSection is not null) + if (idSection.HasValue) { welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.IdWellSectionType == idSection); } - else + else if(currentDepth.HasValue) { - welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.DepthStart <= currentDepth && p.DepthEnd >= currentDepth); + welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.DepthStart <= currentDepth.Value && p.DepthEnd >= currentDepth.Value); idSection ??= welllProcessMap?.IdWellSectionType; } - wellMapInfo.LastTelemetryDate = wellLastTelemetryInfo?.DateTime.ToRemoteDateTime(5) ?? new DateTime(); wellMapInfo.WellDepth = new() { Plan = wellDepthByProcessMap.FirstOrDefault(p => p.Id == well.Id)?.DepthEnd, @@ -135,6 +128,7 @@ namespace AsbCloudInfrastructure.Services Fact = wellOperationsStat?.Total.Fact?.RouteSpeed, }; + var wellSubsystemStat = subsystemStat.FirstOrDefault(s => s.Well.Id == well.Id); wellMapInfo.SaubUsage = wellSubsystemStat?.SubsystemAKB?.KUsage ?? 0d; wellMapInfo.SpinUsage = wellSubsystemStat?.SubsystemSpinMaster?.KUsage ?? 0d; wellMapInfo.TvdLagPercent = wellOperationsStat?.TvdLagDays ?? 0d;