diff --git a/AsbCloudDb/Model/WellOperation.cs b/AsbCloudDb/Model/WellOperation.cs index 9926f0b1..06b330df 100644 --- a/AsbCloudDb/Model/WellOperation.cs +++ b/AsbCloudDb/Model/WellOperation.cs @@ -66,7 +66,7 @@ namespace AsbCloudDb.Model [JsonIgnore] [ForeignKey(nameof(IdPlan))] - public virtual WellOperation? OperationPlan { get; set; } = null!; + public virtual WellOperation? OperationPlan { get; set; } } } diff --git a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs index 638232f3..87fdc3a2 100644 --- a/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationService/OperationsStatService.cs @@ -420,46 +420,20 @@ public class OperationsStatService : IOperationsStatService public async Task>> GetTvdAsync(int idWell, CancellationToken token) { - var wellOperations = await db.WellOperations - .Include(o => o.OperationCategory) - .Include(o => o.WellSectionType) - .Include(o => o.OperationPlan) - .Where(o => o.IdWell == idWell) - .OrderBy(o => o.DateStart) - .ThenBy(o => o.DepthEnd) - .AsNoTracking() - .ToListAsync(token) - .ConfigureAwait(false); - - var wellOperationsPlan = wellOperations - .Where(o => o.IdType == WellOperation.IdOperationTypePlan) - .OrderBy(o => o.DateStart) - .ThenBy(o => o.DepthEnd); - - var wellOperationsFact = wellOperations - .Where(o => o.IdType == WellOperation.IdOperationTypeFact) - .OrderBy(o => o.DateStart) - .ThenBy(o => o.DepthEnd); - - var sectionsIds = wellOperations - .Select(o => o.IdWellSectionType) - .Distinct(); - - var tzOffsetHours = wellService.GetTimezone(idWell).Hours; - - var merged = MergeArraysBySections(sectionsIds, wellOperationsPlan, wellOperationsFact).ToList(); - if (merged.Count ==0) + var wellOperations = (await GetOperationsAsync(idWell, token)).ToArray(); + if (!wellOperations.Any()) return Enumerable.Empty>(); - - var tvd = new List>(merged.Count); - var (Plan, Fact) = merged.FirstOrDefault(); + + var tzOffsetHours = wellService.GetTimezone(idWell).Hours; + var tvd = new List>(wellOperations.Length); + var (Plan, Fact) = wellOperations.FirstOrDefault(); var dateStart = Plan?.DateStart ?? Fact!.DateStart; int? iLastMatch = null; int iLastFact = 0; var nptHours = 0d; - for (int i = 0; i < merged.Count; i++) + for (int i = 0; i < wellOperations.Length; i++) { - var item = merged[i]; + var item = wellOperations[i]; var plan = item.Plan; var fact = item.Fact; @@ -483,23 +457,23 @@ public class OperationsStatService : IOperationsStatService iLastFact = i; } - tvd.Add(planFactPredict); + tvd.Add(planFactPredict); } - if (iLastMatch is null || iLastMatch == merged.Count - 1) + if (iLastMatch is null || iLastMatch == wellOperations.Length - 1) return tvd; - var lastMatchPlan = merged[iLastMatch.Value].Plan!; + var lastMatchPlan = wellOperations[iLastMatch.Value].Plan!; var lastMatchPlanOperationEnd = lastMatchPlan.DateStart.AddHours(lastMatchPlan.DurationHours); - var lastFact = merged[iLastFact].Fact!; + var lastFact = wellOperations[iLastFact].Fact!; var lastFactDateEnd = lastFact.DateStart.AddHours(lastFact.DurationHours); var startOffset = lastFactDateEnd - lastMatchPlanOperationEnd; - for (int i = iLastMatch.Value + 1; i < merged.Count; i++) + for (int i = iLastMatch.Value + 1; i < wellOperations.Length; i++) { - if (merged[i].Plan is null) + if (wellOperations[i].Plan is null) continue; - var predict = Convert(merged[i].Plan!, tzOffsetHours); + var predict = Convert(wellOperations[i].Plan!, tzOffsetHours); predict.IdType = 2; predict.DateStart = predict.DateStart + startOffset; predict.Day = (predict.DateStart - dateStart).TotalDays; @@ -509,33 +483,33 @@ public class OperationsStatService : IOperationsStatService return tvd; } - private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArraysBySections( - IEnumerable sectionsIds, - IOrderedEnumerable wellOperationsPlan, - IOrderedEnumerable wellOperationsFact) + private async Task> GetOperationsAsync(int idWell, CancellationToken token) { - var merged = new List<(WellOperation? Plan, WellOperation? Fact)>(wellOperationsPlan.Count()); - 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; - } + var query = db.WellOperations + .Include(o => o.OperationCategory) + .Include(o => o.WellSectionType) + .Where(o => o.IdWell == idWell) + .OrderBy(o => o.DateStart) + .ThenBy(o => o.DepthEnd); - private static IEnumerable<(WellOperation? Plan, WellOperation? Fact)> MergeArrays(IEnumerable operationsPlan, IEnumerable operationsFact) - { - var operationsFactWithNoPlan = operationsFact.Where(x => x.IdPlan == null).ToArray(); - var operationsFactWithPlan = operationsFact.Where(x => x.IdPlan != null).ToArray(); + var operationsFactWithNoPlan = await query.Where(o => o.IdPlan == null && o.IdType == WellOperation.IdOperationTypeFact) + .AsNoTracking() + .ToArrayAsync(token); - 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(); + var operationsFactWithPlan = await query.Where(o => o.IdPlan != null && o.IdType == WellOperation.IdOperationTypeFact) + .Include(o => o.OperationPlan) + .ThenInclude(o => o!.WellSectionType) + .Include(o => o.OperationPlan) + .ThenInclude(o => o!.OperationCategory) + .AsNoTracking() + .ToArrayAsync(token); - var result = new List<(WellOperation? Plan, WellOperation? Fact)>(operationsFactWithNoPlan.Length + operationsFactWithPlan.Length + operationsPlanWithNoFact.Length); + var idsPlanWithFact = operationsFactWithPlan.Select(o => o.IdPlan).Distinct(); + var operationsPlanWithNoFact = await query + .Where(o => o.IdType == WellOperation.IdOperationTypePlan && !idsPlanWithFact.Contains(o.IdPlan)).ToArrayAsync(token); + + var capacity = operationsFactWithNoPlan.Length + operationsFactWithPlan.Length + operationsPlanWithNoFact.Length; + var result = new List<(WellOperation? Plan, WellOperation? Fact)>(capacity); foreach (var operation in operationsFactWithPlan) result.Add((operation.OperationPlan, operation)); diff --git a/AsbCloudWebApi/Controllers/OperationStatController.cs b/AsbCloudWebApi/Controllers/OperationStatController.cs index 7126c5f5..81a9431f 100644 --- a/AsbCloudWebApi/Controllers/OperationStatController.cs +++ b/AsbCloudWebApi/Controllers/OperationStatController.cs @@ -1,4 +1,5 @@ -using AsbCloudApp.Data; +using System; +using AsbCloudApp.Data; using AsbCloudApp.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; @@ -150,9 +151,8 @@ namespace AsbCloudWebApi.Controllers { if (!await CanUserAccessToWellAsync(idWell, token).ConfigureAwait(false)) return Forbid(); - - var result = await operationsStatService.GetTvdAsync(idWell, token) - .ConfigureAwait(false); + + var result = await operationsStatService.GetTvdAsync(idWell, token); return Ok(result); }