Merge branch 'dev' into feature/refactoring_api_route

This commit is contained in:
commit b3ed280d8f
4 changed files with 571 additions and 536 deletions

View File

@ -53,8 +53,8 @@ namespace AsbCloudApp.Data
public IEnumerable<CompanyDto> Companies { get; set; } = Enumerable.Empty<CompanyDto>();
/// <summary>
/// Отставание от ГГД, дней
/// Отставание от ГГД, проценты
/// </summary>
public double TvdLagDays { get; set; } = 0;
public double? TvdLagPercent { get; set; }
}
}

View File

@ -95,9 +95,8 @@ namespace AsbCloudApp.Data
public PlanFactDto<double?> WellDepth { get; set; } = null!;
/// <summary>
/// Отставание от ГГД, дни
/// Отставание от ГГД, проценты
/// </summary>
public double TvdLagDays { get; set; }
public double TvdLagPercent { get; set; }
}
}

View File

@ -138,8 +138,7 @@ namespace AsbCloudInfrastructure.Services
wellMapInfo.FirstFactOperationDateStart = wellOperationsStat?.Total.Fact?.Start
?? wellOperationsStat?.Total.Plan?.Start;
wellMapInfo.LastPredictOperationDateEnd = wellOperationsStat?.Total.Plan?.End?
.AddDays(wellOperationsStat?.TvdLagDays ?? 0d);
wellMapInfo.LastPredictOperationDateEnd = wellOperationsStat?.Total.Plan?.End;
wellMapInfo.WellDepth = new()
{
@ -163,7 +162,7 @@ namespace AsbCloudInfrastructure.Services
wellMapInfo.SaubUsage = wellSubsystemStat?.SubsystemAKB?.KUsage ?? 0d;
wellMapInfo.SpinUsage = wellSubsystemStat?.SubsystemSpinMaster?.KUsage ?? 0d;
wellMapInfo.TorqueKUsage = wellSubsystemStat?.SubsystemTorqueMaster?.KUsage ?? 0d;
wellMapInfo.TvdLagDays = wellOperationsStat?.TvdLagDays ?? 0d;
wellMapInfo.TvdLagPercent = wellOperationsStat?.TvdLagPercent ?? 0d;
wellMapInfo.IdsCompanies = well.Companies.Select(c => c.Id);
return wellMapInfo;

View File

@ -9,21 +9,25 @@ using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudApp.Data.SAUB;
using AsbCloudInfrastructure.Services.SAUB;
namespace AsbCloudInfrastructure.Services.WellOperationService
namespace AsbCloudInfrastructure.Services.WellOperationService;
public class OperationsStatService : IOperationsStatService
{
public class OperationsStatService : IOperationsStatService
{
private readonly IAsbCloudDbContext db;
private readonly IMemoryCache memoryCache;
private readonly IWellService wellService;
private readonly TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache;
public OperationsStatService(IAsbCloudDbContext db, IMemoryCache memoryCache, IWellService wellService)
public OperationsStatService(IAsbCloudDbContext db, IMemoryCache memoryCache, IWellService wellService,
TelemetryDataCache<TelemetryDataSaubDto> telemetryDataCache)
{
this.db = db;
this.memoryCache = memoryCache;
this.wellService = wellService;
this.telemetryDataCache = telemetryDataCache;
}
public async Task<StatClusterDto?> GetOrDefaultStatClusterAsync(int idCluster, int idCompany, CancellationToken token)
@ -168,46 +172,81 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
var timezoneOffsetH = wellService.GetTimezone(well.Id).Hours;
statWellDto.Sections = CalcSectionsStats(wellOperations, timezoneOffsetH);
statWellDto.Total = GetStatTotal(wellOperations, well.IdState, timezoneOffsetH);
statWellDto.TvdLagDays = CalcTvdLagDays(wellOperations);
statWellDto.TvdLagPercent = CalcTvdLagPercent(well.IdTelemetry, wellOperations);
return statWellDto;
}
private static double CalcTvdLagDays(IOrderedEnumerable<WellOperation> wellOperations)
private double? CalcTvdLagPercent(int? idTelemetry, IOrderedEnumerable<WellOperation> wellOperations)
{
var operationsOrdered = wellOperations
.OrderBy(o => o.DateStart);
var currentDate = DateTimeOffset.UtcNow;
var factOperations = operationsOrdered
.Where(o => o.IdType == WellOperation.IdOperationTypeFact);
var wellDepth = wellOperations
.LastOrDefault(o => o.IdType == WellOperation.IdOperationTypeFact)?.DepthEnd;
var lastCorrespondingFactOperation = factOperations
.LastOrDefault(o => o.IdPlan is not null);
if (idTelemetry.HasValue)
wellDepth = telemetryDataCache.GetLastOrDefault(idTelemetry.Value)?.WellDepth;
if (lastCorrespondingFactOperation is null)
return 0d;
if (wellDepth is null)
return null;
var lastCorrespondingPlanOperation = wellOperations
.FirstOrDefault(o => o.Id == lastCorrespondingFactOperation.IdPlan);
if (lastCorrespondingPlanOperation is null)
return 0d;
var lastFactOperation = factOperations.Last();
var remainingPlanOperations = operationsOrdered
var planOperations = wellOperations
.Where(o => o.IdType == WellOperation.IdOperationTypePlan)
.Where(o => o.DateStart > lastCorrespondingPlanOperation.DateStart);
.OrderBy(o => o.DateStart.AddHours(o.DurationHours));
var durationRemain = remainingPlanOperations.Sum(o => o.DurationHours);
if (!planOperations.Any())
return null;
var factEnd = lastFactOperation.DateStart.AddHours(durationRemain + lastFactOperation.DurationHours);
var planEnd = lastCorrespondingFactOperation.DateStart.AddHours(durationRemain + lastCorrespondingFactOperation.DurationHours);
var lagDays = (planEnd - factEnd).TotalDays;
var planDepth = CalcPlanDepth(planOperations, currentDate);
return lagDays;
if (planDepth is null)
return null;
if (planDepth == 0d)
return 0d;
return (1 - wellDepth / planDepth) * 100;
}
private static double? CalcPlanDepth(IOrderedEnumerable<WellOperation> planOperations, DateTimeOffset currentDate)
{
var operationIn = planOperations
.FirstOrDefault(o => o.DateStart <= currentDate && o.DateStart.AddHours(o.DurationHours) >= currentDate);
if (operationIn is not null)
return Interpolate(
operationIn.DepthStart,
operationIn.DepthEnd,
operationIn.DateStart,
operationIn.DateStart.AddHours(operationIn.DurationHours),
currentDate);
var operationFrom = planOperations
.LastOrDefault(o => o.DateStart.AddHours(o.DurationHours) <= currentDate);
var operationTo = planOperations
.FirstOrDefault(o => o.DateStart >= currentDate);
if (operationFrom is null && operationTo is not null)
return 0d;
else if (operationFrom is not null && operationTo is not null)
{
return Interpolate(
operationFrom.DepthEnd,
operationTo.DepthStart,
operationFrom.DateStart.AddHours(operationTo.DurationHours),
operationTo.DateStart,
currentDate);
}
else if (operationFrom is not null && operationTo is null)
return operationFrom.DepthEnd;
return null;
}
private static double Interpolate(double y0, double y1, DateTimeOffset x0, DateTimeOffset x1, DateTimeOffset x)
=> y0 + (y1 - y0) * (x - x0).TotalMinutes / (x1 - x0).TotalMinutes;
private IEnumerable<StatSectionDto> CalcSectionsStats(IEnumerable<WellOperation> operations, double timezoneOffsetH)
{
var sectionTypeIds = operations
@ -542,6 +581,4 @@ namespace AsbCloudInfrastructure.Services.WellOperationService
destination.DateStart = source.DateStart.ToRemoteDateTime(tzOffsetHours);
return destination;
}
}
}