WellInfoService использует кеш телеметрии вместо долгого запроса.

This commit is contained in:
ngfrolov 2023-05-22 10:20:45 +05:00
parent ed156b1ce8
commit 9347e9610b
Signed by: ng.frolov
GPG Key ID: E99907A0357B29A7
2 changed files with 32 additions and 30 deletions

View File

@ -125,6 +125,14 @@ namespace AsbCloudInfrastructure.Services.SAUB
return items; 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) public DatesRangeDto? GetOrDefaultDataDateRange(int idTelemetry)
{ {
if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem)) if (!caches.TryGetValue(idTelemetry, out TelemetryDataCacheItem? cacheItem))

View File

@ -1,13 +1,14 @@
using AsbCloudApp.Data; using AsbCloudApp.Data;
using AsbCloudApp.Data.ProcessMap; using AsbCloudApp.Data.ProcessMap;
using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories; using AsbCloudApp.Repositories;
using AsbCloudApp.Requests; using AsbCloudApp.Requests;
using AsbCloudApp.Services; using AsbCloudApp.Services;
using AsbCloudApp.Services.Subsystems; using AsbCloudApp.Services.Subsystems;
using AsbCloudDb.Model; using AsbCloudDb.Model;
using AsbCloudInfrastructure.Background; using AsbCloudInfrastructure.Background;
using AsbCloudInfrastructure.Services.SAUB;
using Mapster; using Mapster;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -46,7 +47,8 @@ namespace AsbCloudInfrastructure.Services
var operationsStatService = serviceProvider.GetRequiredService<IOperationsStatService>(); var operationsStatService = serviceProvider.GetRequiredService<IOperationsStatService>();
var processMapRepository = serviceProvider.GetRequiredService<IProcessMapPlanRepository>(); var processMapRepository = serviceProvider.GetRequiredService<IProcessMapPlanRepository>();
var subsystemOperationTimeService = serviceProvider.GetRequiredService<ISubsystemOperationTimeService>(); var subsystemOperationTimeService = serviceProvider.GetRequiredService<ISubsystemOperationTimeService>();
var telemetryDataSaubCache = serviceProvider.GetRequiredService<TelemetryDataCache<TelemetryDataSaubDto>>();
var activeWells = await wellService.GetAsync(new() {IdState = 1}, token); var activeWells = await wellService.GetAsync(new() {IdState = 1}, token);
IEnumerable<int> activeWellsIds = activeWells IEnumerable<int> activeWellsIds = activeWells
@ -56,21 +58,6 @@ namespace AsbCloudInfrastructure.Services
.Where(w => w.IdTelemetry != null) .Where(w => w.IdTelemetry != null)
.Select(t => t.IdTelemetry); .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 processMapRequests = activeWellsIds.Select(id => new ProcessMapRequest { IdWell = id });
var processMaps = await processMapRepository.GetProcessMapAsync(processMapRequests, token); var processMaps = await processMapRepository.GetProcessMapAsync(processMapRequests, token);
@ -83,40 +70,46 @@ namespace AsbCloudInfrastructure.Services
}); });
var operationsStat = await operationsStatService.GetWellsStatAsync(activeWellsIds, token); var operationsStat = await operationsStatService.GetWellsStatAsync(activeWellsIds, token);
var subsystemStat = await subsystemOperationTimeService.GetStatByActiveWells(activeWellsIds, token); var subsystemStat = await subsystemOperationTimeService.GetStatByActiveWells(activeWellsIds, token);
WellMapInfo = activeWells.Select(well => { WellMapInfo = activeWells.Select(well => {
var wellMapInfo = well.Adapt<WellMapInfoWithComanies>(); var wellMapInfo = well.Adapt<WellMapInfoWithComanies>();
var wellOperationsStat = operationsStat.FirstOrDefault(s => s.Id == well.Id);
var wellLastTelemetryInfo = lastTelemetryInfo.FirstOrDefault(t => t.IdTelemetry == well.IdTelemetry);
var wellOperationsStat = operationsStat.FirstOrDefault(s => s.Id == well.Id);
var wellLastFactSection = wellOperationsStat?.Sections.LastOrDefault(s => s.Fact is not null); 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 if (well.IdTelemetry.HasValue)
?? wellLastFactSection?.Fact?.WellDepthEnd {
?? 0d; 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 var wellProcessMaps = processMaps
.Where(p => p.IdWell == well.Id) .Where(p => p.IdWell == well.Id)
.OrderBy(p => p.DepthEnd); .OrderBy(p => p.DepthEnd);
int? idSection = wellLastFactSection?.Id; int? idSection = wellLastFactSection?.Id;
ProcessMapPlanDto? welllProcessMap = null;
ProcessMapPlanDto? welllProcessMap; if (idSection.HasValue)
if (idSection is not null)
{ {
welllProcessMap = wellProcessMaps.FirstOrDefault(p => p.IdWellSectionType == idSection); 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; idSection ??= welllProcessMap?.IdWellSectionType;
} }
wellMapInfo.LastTelemetryDate = wellLastTelemetryInfo?.DateTime.ToRemoteDateTime(5) ?? new DateTime();
wellMapInfo.WellDepth = new() wellMapInfo.WellDepth = new()
{ {
Plan = wellDepthByProcessMap.FirstOrDefault(p => p.Id == well.Id)?.DepthEnd, Plan = wellDepthByProcessMap.FirstOrDefault(p => p.Id == well.Id)?.DepthEnd,
@ -135,6 +128,7 @@ namespace AsbCloudInfrastructure.Services
Fact = wellOperationsStat?.Total.Fact?.RouteSpeed, Fact = wellOperationsStat?.Total.Fact?.RouteSpeed,
}; };
var wellSubsystemStat = subsystemStat.FirstOrDefault(s => s.Well.Id == well.Id);
wellMapInfo.SaubUsage = wellSubsystemStat?.SubsystemAKB?.KUsage ?? 0d; wellMapInfo.SaubUsage = wellSubsystemStat?.SubsystemAKB?.KUsage ?? 0d;
wellMapInfo.SpinUsage = wellSubsystemStat?.SubsystemSpinMaster?.KUsage ?? 0d; wellMapInfo.SpinUsage = wellSubsystemStat?.SubsystemSpinMaster?.KUsage ?? 0d;
wellMapInfo.TvdLagPercent = wellOperationsStat?.TvdLagDays ?? 0d; wellMapInfo.TvdLagPercent = wellOperationsStat?.TvdLagDays ?? 0d;