2021-08-24 10:59:10 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
2021-08-24 16:47:10 +05:00
|
|
|
|
using AsbCloudApp.Services;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2021-09-10 11:28:57 +05:00
|
|
|
|
using Mapster;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-11-18 15:25:38 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
2021-10-09 20:16:22 +05:00
|
|
|
|
namespace AsbCloudInfrastructure.Services.WellOperationService
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2021-11-22 17:29:19 +05:00
|
|
|
|
public class OperationsStatService : IOperationsStatService
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
|
|
|
|
private readonly IAsbCloudDbContext db;
|
2022-11-18 15:25:38 +05:00
|
|
|
|
private readonly IMemoryCache memoryCache;
|
2021-08-25 11:13:56 +05:00
|
|
|
|
private readonly IWellService wellService;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
|
2022-11-18 15:25:38 +05:00
|
|
|
|
public OperationsStatService(IAsbCloudDbContext db, IMemoryCache memoryCache, IWellService wellService)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
|
|
|
|
this.db = db;
|
2022-11-18 15:25:38 +05:00
|
|
|
|
this.memoryCache = memoryCache;
|
2021-08-25 11:13:56 +05:00
|
|
|
|
this.wellService = wellService;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
public async Task<StatClusterDto?> GetOrDefaultStatClusterAsync(int idCluster, int idCompany, CancellationToken token)
|
2021-08-24 16:47:10 +05:00
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var cluster = (await memoryCache
|
|
|
|
|
.GetOrCreateBasicAsync(db.Set<Cluster>(), token))
|
|
|
|
|
.FirstOrDefault(c => c.Id == idCluster);
|
|
|
|
|
|
|
|
|
|
if (cluster is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-02-15 17:57:32 +05:00
|
|
|
|
var allWellsByCompany = await wellService.GetAsync(new() { IdCompany = idCompany }, token).ConfigureAwait(false);
|
2021-10-12 18:06:47 +05:00
|
|
|
|
|
2022-04-11 18:00:34 +05:00
|
|
|
|
var idWellsByCompany = allWellsByCompany.Select(w => w.Id).Distinct();
|
2021-10-12 18:06:47 +05:00
|
|
|
|
|
2021-09-02 11:42:05 +05:00
|
|
|
|
var wells = await db.Wells
|
|
|
|
|
.Include(w => w.WellOperations)
|
|
|
|
|
.Where(o => o.IdCluster == idCluster)
|
2021-10-12 18:06:47 +05:00
|
|
|
|
.Where(w => idWellsByCompany.Contains(w.Id))
|
|
|
|
|
.Select(w => w.Id)
|
2021-08-24 16:47:10 +05:00
|
|
|
|
.ToListAsync(token);
|
|
|
|
|
|
2021-10-12 18:06:47 +05:00
|
|
|
|
var statsWells = await GetWellsStatAsync(wells, token).ConfigureAwait(false);
|
2021-08-24 16:47:10 +05:00
|
|
|
|
|
|
|
|
|
var statClusterDto = new StatClusterDto
|
|
|
|
|
{
|
2021-08-25 11:13:56 +05:00
|
|
|
|
Id = idCluster,
|
|
|
|
|
Caption = cluster.Caption,
|
|
|
|
|
StatsWells = statsWells,
|
2021-08-24 16:47:10 +05:00
|
|
|
|
};
|
|
|
|
|
return statClusterDto;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-12 16:07:08 +05:00
|
|
|
|
public async Task<IEnumerable<StatWellDto>> GetWellsStatAsync(IEnumerable<int> idWells, CancellationToken token)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2021-10-12 16:07:08 +05:00
|
|
|
|
var wells = await db.Wells
|
|
|
|
|
.Include(w => w.WellOperations)
|
|
|
|
|
.Where(w => idWells.Contains(w.Id))
|
2021-08-24 10:59:10 +05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token);
|
|
|
|
|
|
2021-10-12 16:07:08 +05:00
|
|
|
|
var statsWells = new List<StatWellDto>(wells.Count);
|
2021-08-29 11:59:20 +05:00
|
|
|
|
|
2021-10-12 16:07:08 +05:00
|
|
|
|
foreach (var well in wells)
|
|
|
|
|
{
|
2022-03-02 16:21:07 +05:00
|
|
|
|
var statWellDto = await CalcWellStatAsync(well, token);
|
2021-10-12 16:07:08 +05:00
|
|
|
|
statsWells.Add(statWellDto);
|
|
|
|
|
}
|
|
|
|
|
return statsWells;
|
2021-08-25 11:13:56 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
public async Task<StatWellDto?> GetOrDefaultWellStatAsync(int idWell,
|
2021-08-25 11:13:56 +05:00
|
|
|
|
CancellationToken token = default)
|
|
|
|
|
{
|
2021-10-12 16:07:08 +05:00
|
|
|
|
var well = await db.Wells
|
|
|
|
|
.Include(w => w.WellOperations)
|
|
|
|
|
.FirstOrDefaultAsync(w => w.Id == idWell, token)
|
|
|
|
|
.ConfigureAwait(false);
|
2021-08-25 11:13:56 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
if(well is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
2022-03-02 16:21:07 +05:00
|
|
|
|
var statWellDto = await CalcWellStatAsync(well, token);
|
2021-10-12 16:07:08 +05:00
|
|
|
|
return statWellDto;
|
|
|
|
|
}
|
2021-08-25 11:13:56 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
public async Task<ClusterRopStatDto?> GetOrDefaultRopStatAsync(int idWell, CancellationToken token)
|
2021-11-22 17:29:19 +05:00
|
|
|
|
{
|
|
|
|
|
var clusterWellsIds = await wellService.GetClusterWellsIdsAsync(idWell, token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
if (clusterWellsIds is null)
|
|
|
|
|
return null;
|
|
|
|
|
|
|
|
|
|
var idLastSectionType = await (from o in db.WellOperations
|
2022-04-11 18:00:34 +05:00
|
|
|
|
where o.IdWell == idWell &&
|
|
|
|
|
o.IdType == 1
|
|
|
|
|
orderby o.DepthStart
|
|
|
|
|
select o.IdWellSectionType)
|
2021-11-22 17:29:19 +05:00
|
|
|
|
.LastOrDefaultAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2021-11-23 11:31:47 +05:00
|
|
|
|
if (idLastSectionType == default)
|
|
|
|
|
return null;
|
|
|
|
|
|
2021-11-22 17:29:19 +05:00
|
|
|
|
var operations = await (from o in db.WellOperations
|
2022-04-11 18:00:34 +05:00
|
|
|
|
where clusterWellsIds.Contains(o.IdWell) &&
|
|
|
|
|
o.IdType == 1 &&
|
|
|
|
|
o.IdWellSectionType == idLastSectionType
|
|
|
|
|
select o)
|
2021-11-22 17:29:19 +05:00
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var statsList = new List<StatOperationsDto>(clusterWellsIds.Count());
|
|
|
|
|
foreach (var clusterWellId in clusterWellsIds)
|
2021-11-22 17:29:19 +05:00
|
|
|
|
{
|
|
|
|
|
var currentWellOps = operations.Where(o => o.IdWell == clusterWellId);
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var timezoneOffsetHours = wellService.GetTimezone(clusterWellId).Hours;
|
|
|
|
|
var stat = CalcStat(currentWellOps, timezoneOffsetHours);
|
|
|
|
|
if(stat is not null)
|
|
|
|
|
statsList.Add(stat);
|
|
|
|
|
};
|
2021-11-22 17:29:19 +05:00
|
|
|
|
|
|
|
|
|
if (!statsList.Any())
|
|
|
|
|
return null;
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2021-11-22 17:29:19 +05:00
|
|
|
|
var clusterRops = new ClusterRopStatDto()
|
|
|
|
|
{
|
|
|
|
|
RopMax = statsList.Max(s => s.Rop),
|
|
|
|
|
RopAverage = statsList.Average(s => s.Rop)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return clusterRops;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
private async Task<StatWellDto> CalcWellStatAsync(Well well, CancellationToken token)
|
2021-10-12 16:07:08 +05:00
|
|
|
|
{
|
2022-11-18 15:25:38 +05:00
|
|
|
|
var wellType = (await memoryCache
|
2023-02-22 09:40:02 +05:00
|
|
|
|
.GetOrCreateBasicAsync(db.Set<WellType>(), token))
|
2022-11-18 15:25:38 +05:00
|
|
|
|
.FirstOrDefault(t => t.Id == well.IdWellType);
|
2022-12-28 17:38:53 +05:00
|
|
|
|
var statWellDto = new StatWellDto
|
2021-08-24 16:47:10 +05:00
|
|
|
|
{
|
2021-10-12 16:07:08 +05:00
|
|
|
|
Id = well.Id,
|
2021-08-25 11:13:56 +05:00
|
|
|
|
Caption = well.Caption,
|
2021-11-11 10:57:08 +05:00
|
|
|
|
WellType = wellType?.Caption ?? "",
|
2021-10-20 14:15:53 +05:00
|
|
|
|
IdState = well.IdState,
|
2021-10-20 12:52:31 +05:00
|
|
|
|
State = wellService.GetStateText(well.IdState),
|
2023-05-19 17:57:07 +05:00
|
|
|
|
LastTelemetryDate = wellService.GetLastTelemetryDate(well.Id),
|
2022-12-28 17:38:53 +05:00
|
|
|
|
Companies = await wellService.GetCompaniesAsync(well.Id, token)
|
2021-08-24 16:47:10 +05:00
|
|
|
|
};
|
2021-10-12 16:07:08 +05:00
|
|
|
|
|
2021-10-13 15:45:24 +05:00
|
|
|
|
if (well.WellOperations is null)
|
|
|
|
|
return statWellDto;
|
|
|
|
|
|
|
|
|
|
var wellOperations = well.WellOperations
|
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthEnd);
|
|
|
|
|
|
2021-10-12 16:07:08 +05:00
|
|
|
|
if (!wellOperations.Any())
|
|
|
|
|
return statWellDto;
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var timezoneOffsetH = wellService.GetTimezone(well.Id).Hours;
|
|
|
|
|
statWellDto.Sections = CalcSectionsStats(wellOperations, timezoneOffsetH);
|
|
|
|
|
statWellDto.Total = GetStatTotal(wellOperations, well.IdState, timezoneOffsetH);
|
2023-03-06 16:12:26 +05:00
|
|
|
|
statWellDto.TvdLagDays = CalcTvdLagDays(wellOperations);
|
2021-10-12 16:07:08 +05:00
|
|
|
|
|
2021-08-24 16:47:10 +05:00
|
|
|
|
return statWellDto;
|
|
|
|
|
}
|
2021-08-24 10:59:10 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
private static double CalcTvdLagDays(IOrderedEnumerable<WellOperation> wellOperations)
|
2023-03-06 16:12:26 +05:00
|
|
|
|
{
|
|
|
|
|
var operationsOrdered = wellOperations
|
|
|
|
|
.OrderBy(o => o.DateStart);
|
|
|
|
|
|
|
|
|
|
var factOperations = operationsOrdered
|
|
|
|
|
.Where(o => o.IdType == WellOperation.IdOperationTypeFact);
|
|
|
|
|
|
|
|
|
|
var lastCorrespondingFactOperation = factOperations
|
|
|
|
|
.LastOrDefault(o => o.IdPlan is not null);
|
|
|
|
|
|
|
|
|
|
if (lastCorrespondingFactOperation is null)
|
|
|
|
|
return 0d;
|
2023-03-10 15:37:49 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var lastCorrespondingPlanOperation = wellOperations
|
|
|
|
|
.FirstOrDefault(o => o.Id == lastCorrespondingFactOperation.IdPlan);
|
2023-03-06 16:12:26 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
if (lastCorrespondingPlanOperation is null)
|
|
|
|
|
return 0d;
|
|
|
|
|
|
|
|
|
|
var lastFactOperation = factOperations.Last();
|
2023-03-06 16:12:26 +05:00
|
|
|
|
|
|
|
|
|
var remainingPlanOperations = operationsOrdered
|
|
|
|
|
.Where(o => o.IdType == WellOperation.IdOperationTypePlan)
|
2023-03-10 15:37:49 +05:00
|
|
|
|
.Where(o => o.DateStart > lastCorrespondingPlanOperation.DateStart);
|
2023-03-06 16:12:26 +05:00
|
|
|
|
|
|
|
|
|
var durationRemain = remainingPlanOperations.Sum(o => o.DurationHours);
|
2023-03-10 15:37:49 +05:00
|
|
|
|
|
2023-03-06 16:12:26 +05:00
|
|
|
|
var factEnd = lastFactOperation.DateStart.AddHours(durationRemain + lastFactOperation.DurationHours);
|
|
|
|
|
var planEnd = lastCorrespondingFactOperation.DateStart.AddHours(durationRemain + lastCorrespondingFactOperation.DurationHours);
|
|
|
|
|
var lagDays = (planEnd - factEnd).TotalDays;
|
|
|
|
|
|
|
|
|
|
return lagDays;
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
private IEnumerable<StatSectionDto> CalcSectionsStats(IEnumerable<WellOperation> operations, double timezoneOffsetH)
|
2021-08-24 16:47:10 +05:00
|
|
|
|
{
|
2021-08-25 11:13:56 +05:00
|
|
|
|
var sectionTypeIds = operations
|
|
|
|
|
.Select(o => o.IdWellSectionType)
|
|
|
|
|
.Distinct();
|
|
|
|
|
|
2022-11-18 15:25:38 +05:00
|
|
|
|
var sectionTypes = memoryCache
|
2023-02-22 09:40:02 +05:00
|
|
|
|
.GetOrCreateBasic(db.Set<WellSectionType>())
|
2021-08-25 11:13:56 +05:00
|
|
|
|
.Where(s => sectionTypeIds.Contains(s.Id))
|
|
|
|
|
.ToDictionary(s => s.Id);
|
|
|
|
|
|
|
|
|
|
var sections = new List<StatSectionDto>(sectionTypes.Count);
|
2022-12-28 17:38:53 +05:00
|
|
|
|
var operationsPlan = operations.Where(o => o.IdType == WellOperation.IdOperationTypePlan);
|
|
|
|
|
var operationsFact = operations.Where(o => o.IdType == WellOperation.IdOperationTypeFact);
|
2021-08-24 10:59:10 +05:00
|
|
|
|
|
2021-08-25 11:13:56 +05:00
|
|
|
|
foreach ((var id, var sectionType) in sectionTypes)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2021-10-08 11:30:06 +05:00
|
|
|
|
var section = new StatSectionDto
|
2021-08-25 11:13:56 +05:00
|
|
|
|
{
|
|
|
|
|
Id = id,
|
2021-09-10 11:28:57 +05:00
|
|
|
|
Caption = sectionType.Caption,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
Plan = CalcSectionStat(operationsPlan, id, timezoneOffsetH),
|
|
|
|
|
Fact = CalcSectionStat(operationsFact, id, timezoneOffsetH),
|
2021-08-25 11:13:56 +05:00
|
|
|
|
};
|
2021-08-24 10:59:10 +05:00
|
|
|
|
sections.Add(section);
|
|
|
|
|
}
|
|
|
|
|
return sections;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 09:53:04 +05:00
|
|
|
|
private static PlanFactDto<StatOperationsDto> GetStatTotal(IEnumerable<WellOperation> operations,
|
2022-01-05 17:50:45 +05:00
|
|
|
|
int idWellState, double timezoneOffsetH)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2022-12-28 17:38:53 +05:00
|
|
|
|
var operationsPlan = operations.Where(o => o.IdType == WellOperation.IdOperationTypePlan);
|
|
|
|
|
var operationsFact = operations.Where(o => o.IdType == WellOperation.IdOperationTypeFact);
|
2022-01-05 17:50:45 +05:00
|
|
|
|
var factEnd = CalcStat(operationsFact, timezoneOffsetH);
|
2022-01-25 15:00:17 +05:00
|
|
|
|
if (factEnd is not null && idWellState != 2)
|
2021-12-21 16:36:28 +05:00
|
|
|
|
factEnd.End = null;
|
2023-05-30 09:53:04 +05:00
|
|
|
|
var section = new PlanFactDto<StatOperationsDto>
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
Plan = CalcStat(operationsPlan, timezoneOffsetH),
|
2021-12-21 16:36:28 +05:00
|
|
|
|
Fact = factEnd,
|
2021-08-24 10:59:10 +05:00
|
|
|
|
};
|
2021-08-24 16:47:10 +05:00
|
|
|
|
return section;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
private static StatOperationsDto? CalcSectionStat(IEnumerable<WellOperation> operations, int idSectionType, double timezoneOffsetHours)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2021-08-24 16:47:10 +05:00
|
|
|
|
var sectionOperations = operations
|
2021-08-24 10:59:10 +05:00
|
|
|
|
.Where(o => o.IdWellSectionType == idSectionType)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthStart);
|
2021-09-10 11:28:57 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
return CalcStat(sectionOperations, timezoneOffsetHours);
|
2021-08-24 16:47:10 +05:00
|
|
|
|
}
|
2022-04-11 18:00:34 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
private static StatOperationsDto? CalcStat(IEnumerable<WellOperation> operations, double timezoneOffsetHours)
|
2021-08-24 16:47:10 +05:00
|
|
|
|
{
|
2021-08-29 12:05:43 +05:00
|
|
|
|
if (!operations.Any())
|
|
|
|
|
return null;
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var races = GetCompleteRaces(operations, timezoneOffsetHours);
|
2021-08-26 10:18:59 +05:00
|
|
|
|
|
2021-08-25 11:13:56 +05:00
|
|
|
|
var section = new StatOperationsDto
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
Start = operations.FirstOrDefault()?.DateStart.ToRemoteDateTime(timezoneOffsetHours),
|
|
|
|
|
End = operations.Max(o => o.DateStart.ToRemoteDateTime(timezoneOffsetHours).AddHours(o.DurationHours)),
|
2021-10-08 11:30:06 +05:00
|
|
|
|
WellDepthStart = operations.Min(o => o.DepthStart),
|
|
|
|
|
WellDepthEnd = operations.Max(o => o.DepthStart),
|
2021-08-24 16:47:10 +05:00
|
|
|
|
Rop = CalcROP(operations),
|
2021-08-26 10:18:59 +05:00
|
|
|
|
RouteSpeed = CalcAvgRaceSpeed(races),
|
|
|
|
|
BhaDownSpeed = CalcBhaDownSpeed(races),
|
|
|
|
|
BhaUpSpeed = CalcBhaUpSpeed(races),
|
|
|
|
|
CasingDownSpeed = CalcCasingDownSpeed(operations),
|
|
|
|
|
NonProductiveHours = operations
|
2023-02-16 09:51:55 +05:00
|
|
|
|
.Where(o => WellOperationCategory.NonProductiveTimeSubIds.Contains(o.IdCategory))
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.Sum(o => o.DurationHours),
|
2021-08-24 10:59:10 +05:00
|
|
|
|
};
|
|
|
|
|
return section;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 11:30:06 +05:00
|
|
|
|
private static double CalcROP(IEnumerable<WellOperation> operationsProps)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2022-12-09 18:32:18 +05:00
|
|
|
|
var drillingOperations = operationsProps.Where(o => WellOperationCategory.MechanicalDrillingSubIds.Contains(o.IdCategory));
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var dDepth = 0d;
|
|
|
|
|
var dHours = 0d;
|
|
|
|
|
foreach (var operation in drillingOperations)
|
|
|
|
|
{
|
2021-10-08 11:30:06 +05:00
|
|
|
|
var deltaDepth = operation.DepthEnd - operation.DepthStart;
|
|
|
|
|
dDepth += deltaDepth;
|
|
|
|
|
dHours += operation.DurationHours;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
|
|
|
|
return dDepth / (dHours + double.Epsilon);
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-08 11:30:06 +05:00
|
|
|
|
private static double CalcCasingDownSpeed(IEnumerable<WellOperation> operationsProps)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2022-12-09 18:32:18 +05:00
|
|
|
|
var ops = operationsProps.Where(o => o.IdCategory == WellOperationCategory.IdCasingDown);
|
2021-08-26 10:18:59 +05:00
|
|
|
|
var depth = 0d;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
var dHours = 0d;
|
2021-08-26 10:18:59 +05:00
|
|
|
|
foreach (var operation in ops)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2021-10-08 11:30:06 +05:00
|
|
|
|
depth += operation.DepthStart;
|
|
|
|
|
dHours += operation.DurationHours;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
2021-08-26 10:18:59 +05:00
|
|
|
|
return depth / (dHours + double.Epsilon);
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-01-05 17:50:45 +05:00
|
|
|
|
private static IEnumerable<Race> GetCompleteRaces(IEnumerable<WellOperation> operations, double timezoneOffsetH)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2021-08-24 16:47:10 +05:00
|
|
|
|
var races = new List<Race>();
|
2021-08-25 17:58:35 +05:00
|
|
|
|
var iterator = operations
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.OrderBy(o => o.DateStart)
|
2021-08-25 17:58:35 +05:00
|
|
|
|
.GetEnumerator();
|
2021-08-24 10:59:10 +05:00
|
|
|
|
while (iterator.MoveNext())
|
|
|
|
|
{
|
2022-12-09 18:32:18 +05:00
|
|
|
|
if (iterator.Current.IdCategory == WellOperationCategory.IdBhaAssembly)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
|
|
|
|
var race = new Race
|
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
StartDate = iterator.Current.DateStart.ToRemoteDateTime(timezoneOffsetH).AddHours(iterator.Current.DurationHours),
|
2021-10-08 11:30:06 +05:00
|
|
|
|
StartWellDepth = iterator.Current.DepthStart,
|
|
|
|
|
Operations = new List<WellOperation>(10),
|
2021-08-24 10:59:10 +05:00
|
|
|
|
};
|
|
|
|
|
while (iterator.MoveNext())
|
|
|
|
|
{
|
2022-12-09 18:32:18 +05:00
|
|
|
|
if (iterator.Current.IdCategory == WellOperationCategory.IdEquipmentRepair)
|
2022-10-19 14:54:53 +05:00
|
|
|
|
race.RepairHours += iterator.Current.DurationHours;
|
|
|
|
|
|
2022-12-09 18:32:18 +05:00
|
|
|
|
if (WellOperationCategory.NonProductiveTimeSubIds.Contains(iterator.Current.IdCategory))
|
2021-10-08 11:30:06 +05:00
|
|
|
|
race.NonProductiveHours += iterator.Current.DurationHours;
|
2022-10-18 11:00:34 +05:00
|
|
|
|
|
2022-12-09 18:32:18 +05:00
|
|
|
|
if (iterator.Current.IdCategory == WellOperationCategory.IdBhaDisassembly)
|
2021-08-24 10:59:10 +05:00
|
|
|
|
{
|
2022-01-05 17:50:45 +05:00
|
|
|
|
race.EndDate = iterator.Current.DateStart.ToRemoteDateTime(timezoneOffsetH);
|
2021-10-08 11:30:06 +05:00
|
|
|
|
race.EndWellDepth = iterator.Current.DepthStart;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
races.Add(race);
|
2021-08-25 17:58:35 +05:00
|
|
|
|
break;
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
2021-08-27 15:53:38 +05:00
|
|
|
|
race.Operations.Add(iterator.Current);
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return races;
|
|
|
|
|
}
|
2021-08-26 10:18:59 +05:00
|
|
|
|
|
|
|
|
|
private static double CalcAvgRaceSpeed(IEnumerable<Race> races)
|
|
|
|
|
{
|
|
|
|
|
var dDepth = 0d;
|
|
|
|
|
var dHours = 0d;
|
|
|
|
|
foreach (var race in races)
|
|
|
|
|
{
|
2022-10-19 14:54:53 +05:00
|
|
|
|
dHours += race.DeltaHours - race.NonProductiveHours - race.RepairHours;
|
2021-08-26 10:18:59 +05:00
|
|
|
|
dDepth += race.DeltaDepth;
|
|
|
|
|
}
|
|
|
|
|
return dDepth / (dHours + double.Epsilon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static double CalcBhaDownSpeed(IEnumerable<Race> races)
|
|
|
|
|
{
|
|
|
|
|
var dDepth = 0d;
|
|
|
|
|
var dHours = 0d;
|
2023-04-07 16:49:28 +05:00
|
|
|
|
foreach (Race race in races)
|
2021-08-26 10:18:59 +05:00
|
|
|
|
{
|
|
|
|
|
dDepth += race.StartWellDepth;
|
2021-08-27 15:53:38 +05:00
|
|
|
|
for (var i = 0; i < race.Operations.Count; i++)
|
2021-08-26 10:18:59 +05:00
|
|
|
|
{
|
2022-12-09 18:32:18 +05:00
|
|
|
|
if (race.Operations[i].IdCategory == WellOperationCategory.IdBhaDown)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
dHours += race.Operations[i].DurationHours;
|
2023-02-16 09:51:55 +05:00
|
|
|
|
if (WellOperationCategory.MechanicalDrillingSubIds.Contains(race.Operations[i].IdCategory))
|
2021-08-26 10:18:59 +05:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dDepth / (dHours + double.Epsilon);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static double CalcBhaUpSpeed(IEnumerable<Race> races)
|
|
|
|
|
{
|
|
|
|
|
var dDepth = 0d;
|
|
|
|
|
var dHours = 0d;
|
|
|
|
|
foreach (var race in races)
|
|
|
|
|
{
|
|
|
|
|
dDepth += race.EndWellDepth;
|
2021-09-10 11:28:57 +05:00
|
|
|
|
for (var i = race.Operations.Count - 1; i > 0; i--)
|
2021-08-26 10:18:59 +05:00
|
|
|
|
{
|
2022-12-09 18:32:18 +05:00
|
|
|
|
if (race.Operations[i].IdCategory == WellOperationCategory.IdBhaUp)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
dHours += race.Operations[i].DurationHours;
|
2022-12-09 18:32:18 +05:00
|
|
|
|
if (WellOperationCategory.MechanicalDrillingSubIds.Contains(race.Operations[i].IdCategory))
|
2021-08-26 10:18:59 +05:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return dDepth / (dHours + double.Epsilon);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-27 12:15:04 +05:00
|
|
|
|
public async Task<IEnumerable<PlanFactPredictBase<WellOperationDto>>> GetTvdAsync(int idWell, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
var wellOperations = await db.WellOperations
|
|
|
|
|
.Include(o => o.OperationCategory)
|
|
|
|
|
.Include(o => o.WellSectionType)
|
2023-02-15 17:02:43 +05:00
|
|
|
|
.Include(o => o.OperationPlan)
|
2021-08-27 12:15:04 +05:00
|
|
|
|
.Where(o => o.IdWell == idWell)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthEnd)
|
2021-08-27 12:15:04 +05:00
|
|
|
|
.AsNoTracking()
|
|
|
|
|
.ToListAsync(token)
|
|
|
|
|
.ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
var wellOperationsPlan = wellOperations
|
2022-12-28 17:38:53 +05:00
|
|
|
|
.Where(o => o.IdType == WellOperation.IdOperationTypePlan)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthEnd);
|
2021-08-27 12:15:04 +05:00
|
|
|
|
|
|
|
|
|
var wellOperationsFact = wellOperations
|
2022-12-28 17:38:53 +05:00
|
|
|
|
.Where(o => o.IdType == WellOperation.IdOperationTypeFact)
|
2021-10-08 11:30:06 +05:00
|
|
|
|
.OrderBy(o => o.DateStart)
|
|
|
|
|
.ThenBy(o => o.DepthEnd);
|
2021-08-27 12:15:04 +05:00
|
|
|
|
|
2021-10-06 16:30:46 +05:00
|
|
|
|
var sectionsIds = wellOperations
|
|
|
|
|
.Select(o => o.IdWellSectionType)
|
|
|
|
|
.Distinct();
|
|
|
|
|
|
2022-03-29 10:36:18 +05:00
|
|
|
|
var tzOffsetHours = wellService.GetTimezone(idWell).Hours;
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var merged = MergeArraysBySections(sectionsIds, wellOperationsPlan, wellOperationsFact).ToList();
|
2023-07-28 15:28:30 +05:00
|
|
|
|
if (merged.Count ==0)
|
|
|
|
|
return Enumerable.Empty<PlanFactPredictBase<WellOperationDto>>();
|
|
|
|
|
|
2021-08-27 12:15:04 +05:00
|
|
|
|
var tvd = new List<PlanFactPredictBase<WellOperationDto>>(merged.Count);
|
2023-07-03 10:13:21 +05:00
|
|
|
|
var (Plan, Fact) = merged.FirstOrDefault();
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var dateStart = Plan?.DateStart ?? Fact!.DateStart;
|
|
|
|
|
int? iLastMatch = null;
|
2021-08-27 12:15:04 +05:00
|
|
|
|
int iLastFact = 0;
|
2022-03-11 18:07:25 +05:00
|
|
|
|
var nptHours = 0d;
|
2021-08-27 12:15:04 +05:00
|
|
|
|
for (int i = 0; i < merged.Count; i++)
|
|
|
|
|
{
|
2021-08-29 11:59:20 +05:00
|
|
|
|
var item = merged[i];
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var plan = item.Plan;
|
|
|
|
|
var fact = item.Fact;
|
2022-03-04 10:15:52 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var planFactPredict = new PlanFactPredictBase<WellOperationDto>();
|
|
|
|
|
if (plan is not null)
|
2022-03-04 10:15:52 +05:00
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
planFactPredict.Plan = Convert(plan, tzOffsetHours);
|
|
|
|
|
planFactPredict.Plan.Day = (planFactPredict.Plan.DateStart - dateStart).TotalDays;
|
|
|
|
|
if (fact is not null)
|
|
|
|
|
iLastMatch = i;
|
2022-03-04 10:15:52 +05:00
|
|
|
|
}
|
2021-08-29 11:59:20 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
if (fact is not null)
|
2021-08-27 12:15:04 +05:00
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
if(WellOperationCategory.NonProductiveTimeSubIds.Contains(fact.IdCategory))
|
|
|
|
|
nptHours += fact.DurationHours;
|
2021-08-27 12:15:04 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
planFactPredict.Fact = Convert(fact, tzOffsetHours);
|
2022-01-12 17:46:33 +05:00
|
|
|
|
planFactPredict.Fact.Day = (planFactPredict.Fact.DateStart - dateStart).TotalDays;
|
2022-03-11 18:07:25 +05:00
|
|
|
|
planFactPredict.Fact.NptHours = nptHours;
|
2023-04-07 16:49:28 +05:00
|
|
|
|
iLastFact = i;
|
2022-03-04 10:15:52 +05:00
|
|
|
|
}
|
2022-01-12 17:46:33 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
tvd.Add(planFactPredict);
|
2021-08-27 12:15:04 +05:00
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
if (iLastMatch is null || iLastMatch == merged.Count - 1)
|
2021-08-27 12:15:04 +05:00
|
|
|
|
return tvd;
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var lastMatchPlan = merged[iLastMatch.Value].Plan!;
|
2021-10-08 11:30:06 +05:00
|
|
|
|
var lastMatchPlanOperationEnd = lastMatchPlan.DateStart.AddHours(lastMatchPlan.DurationHours);
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var lastFact = merged[iLastFact].Fact!;
|
2021-10-08 11:30:06 +05:00
|
|
|
|
var lastFactDateEnd = lastFact.DateStart.AddHours(lastFact.DurationHours);
|
|
|
|
|
var startOffset = lastFactDateEnd - lastMatchPlanOperationEnd;
|
2021-08-27 12:15:04 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
for (int i = iLastMatch.Value + 1; i < merged.Count; i++)
|
2021-08-27 12:15:04 +05:00
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
if (merged[i].Plan is null)
|
2021-08-27 12:15:04 +05:00
|
|
|
|
continue;
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var predict = Convert(merged[i].Plan!, tzOffsetHours);
|
|
|
|
|
predict.IdType = 2;
|
|
|
|
|
predict.DateStart = predict.DateStart + startOffset;
|
|
|
|
|
predict.Day = (predict.DateStart - dateStart).TotalDays;
|
|
|
|
|
tvd[i].Predict = predict;
|
2021-08-27 12:15:04 +05:00
|
|
|
|
}
|
2022-01-12 17:46:33 +05:00
|
|
|
|
|
2021-08-27 12:15:04 +05:00
|
|
|
|
return tvd;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArraysBySections(
|
2021-10-08 11:30:06 +05:00
|
|
|
|
IEnumerable<int> sectionsIds,
|
2021-10-06 16:30:46 +05:00
|
|
|
|
IOrderedEnumerable<WellOperation> wellOperationsPlan,
|
|
|
|
|
IOrderedEnumerable<WellOperation> wellOperationsFact)
|
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var merged = new List<(WellOperation? Plan, WellOperation? Fact)>(wellOperationsPlan.Count());
|
2021-10-06 16:30:46 +05:00
|
|
|
|
foreach (var sectionId in sectionsIds)
|
|
|
|
|
{
|
|
|
|
|
var sectionOperationsPlan = wellOperationsPlan
|
|
|
|
|
.Where(o => o.IdWellSectionType == sectionId);
|
|
|
|
|
var sectionOperationsFact = wellOperationsFact
|
|
|
|
|
.Where(o => o.IdWellSectionType == sectionId);
|
|
|
|
|
var sectionMerged = MergeArrays(sectionOperationsPlan, sectionOperationsFact);
|
|
|
|
|
merged.AddRange(sectionMerged);
|
|
|
|
|
}
|
|
|
|
|
return merged;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArrays(IEnumerable<WellOperation> operationsPlan, IEnumerable<WellOperation> operationsFact)
|
2021-08-29 11:59:20 +05:00
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var operationsFactWithNoPlan = operationsFact.Where(x => x.IdPlan == null).ToArray();
|
|
|
|
|
var operationsFactWithPlan = operationsFact.Where(x => x.IdPlan != null).ToArray();
|
2021-08-29 11:59:20 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var idsPlanWithFact = operationsFact.Where(x => x.IdPlan is not null).Select(x => x.IdPlan).Distinct();
|
|
|
|
|
var operationsPlanWithNoFact = operationsPlan.Where(x => !idsPlanWithFact.Contains(x.IdPlan)).ToArray();
|
2021-08-29 11:59:20 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var result = new List<(WellOperation? Plan, WellOperation? Fact)>(operationsFactWithNoPlan.Length + operationsFactWithPlan.Length + operationsPlanWithNoFact.Length);
|
2021-08-29 11:59:20 +05:00
|
|
|
|
|
2023-04-07 16:49:28 +05:00
|
|
|
|
foreach (var operation in operationsFactWithPlan)
|
|
|
|
|
result.Add((operation.OperationPlan, operation));
|
|
|
|
|
|
|
|
|
|
foreach (var operation in operationsFactWithNoPlan)
|
|
|
|
|
result.Add((null, operation));
|
|
|
|
|
|
|
|
|
|
foreach (var operation in operationsPlanWithNoFact)
|
|
|
|
|
result.Add((operation, null));
|
|
|
|
|
|
|
|
|
|
return result
|
|
|
|
|
.OrderBy(x => x.Plan?.DateStart)
|
|
|
|
|
.ThenBy(x => x.Fact?.DateStart);
|
2021-08-29 11:59:20 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-03-29 10:36:18 +05:00
|
|
|
|
private static WellOperationDto Convert(WellOperation source, double tzOffsetHours)
|
2021-09-10 11:28:57 +05:00
|
|
|
|
{
|
2023-04-07 16:49:28 +05:00
|
|
|
|
var destination = source.Adapt<WellOperationDto>();
|
|
|
|
|
destination.CategoryName = source.OperationCategory?.Name;
|
|
|
|
|
destination.WellSectionTypeName = source.WellSectionType?.Caption;
|
|
|
|
|
destination.DateStart = source.DateStart.ToRemoteDateTime(tzOffsetHours);
|
|
|
|
|
return destination;
|
2022-03-29 10:36:18 +05:00
|
|
|
|
}
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|
2023-04-18 16:22:53 +05:00
|
|
|
|
|
2021-08-24 10:59:10 +05:00
|
|
|
|
}
|