From 197142eca2026a2eb4eaa624b8973417be45c5f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A4=D1=80=D0=BE=D0=BB=D0=BE=D0=B2?= Date: Wed, 6 Oct 2021 16:30:46 +0500 Subject: [PATCH] Fix tvd predict --- .../Services/WellOperationsStatService.cs | 65 ++++++++++++++----- ConsoleApp1/Program.cs | 21 +++++- 2 files changed, 68 insertions(+), 18 deletions(-) diff --git a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs index e45294d9..328bd5a1 100644 --- a/AsbCloudInfrastructure/Services/WellOperationsStatService.cs +++ b/AsbCloudInfrastructure/Services/WellOperationsStatService.cs @@ -367,10 +367,14 @@ namespace AsbCloudInfrastructure.Services .OrderBy(o => o.StartDate) .ThenBy(o => o.WellDepth); + var sectionsIds = wellOperations + .Select(o => o.IdWellSectionType) + .Distinct(); + if (!wellOperationsPlan.Any()) return null; - var merged = MergeArrays(wellOperationsPlan, wellOperationsFact); + var merged = MergeArraysBySections(sectionsIds, wellOperationsPlan, wellOperationsFact); var tvd = new List>(merged.Count); int iLastMatch = 0; int iLastFact = 0; @@ -411,6 +415,24 @@ namespace AsbCloudInfrastructure.Services return tvd; } + private List> MergeArraysBySections( + IEnumerable sectionsIds, + IOrderedEnumerable wellOperationsPlan, + IOrderedEnumerable wellOperationsFact) + { + var merged = new List>(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; + } + public static List> MergeArrays(IEnumerable array1, IEnumerable array2) { var a1 = array1.ToArray(); @@ -420,8 +442,8 @@ namespace AsbCloudInfrastructure.Services void Add(WellOperation item1, WellOperation item2) => m.Add(new Tuple(item1, item2)); - bool Compare(WellOperation item1, WellOperation item2) => - item1.IdCategory == item2.IdCategory && Math.Abs(item1.WellDepth - item2.WellDepth) < 250; + static bool Compare(WellOperation item1, WellOperation item2) => + item1.IdCategory == item2.IdCategory && Math.Abs(item1.WellDepth - item2.WellDepth) < (30d + 0.005d*(item1.WellDepth + item2.WellDepth)); int i1 = 0; int i2 = 0; @@ -441,25 +463,34 @@ namespace AsbCloudInfrastructure.Services int nextI1 = Array.FindIndex(a1, i1, (item) => Compare(item, a2[i2])); int nextI2 = Array.FindIndex(a2, i2, (item) => Compare(item, a1[i1])); - if ((nextI1 > -1) && ((nextI2 == -1) || ((nextI1 - i1) < (nextI2 - i2)))) + bool deltaI1_Lt_deltaI2 = (nextI1 - i1) < (nextI2 - i2); + + if (nextI1 == -1 && nextI2 == -1) { - for (; i1 < nextI1; i1++) - Add(a1[i1], null); + if (a1[i1].WellDepth < a2[i2].WellDepth) + { + Add(a1[i1++], null); + } + else + { + Add(null, a2[i2++]); + } } - - if ((nextI2 > -1) && ((nextI1 == -1) || ((nextI1 - i1) > (nextI2 - i2)))) + else if (nextI1 > -1 && nextI2 == -1) { - for (; i2 < nextI2; i2++) - Add(null, a2[i2]); + Add(a1[i1++], null); } - - if ((nextI1 == -1) && (nextI2 == -1)) + else if (nextI1 == -1 && nextI2 > -1) { - for (; i2 < a2.Length; i2++) - Add(null, a2[i2]); - - for (; i1 < a1.Length; i1++) - Add(a1[i1], null); + Add(null, a2[i2++]); + } + else if (deltaI1_Lt_deltaI2) + { + Add(a1[i1++], null); + } + else if (!deltaI1_Lt_deltaI2) + { + Add(null, a2[i2++]); } } } diff --git a/ConsoleApp1/Program.cs b/ConsoleApp1/Program.cs index b37c283b..2c6f70a5 100644 --- a/ConsoleApp1/Program.cs +++ b/ConsoleApp1/Program.cs @@ -34,8 +34,27 @@ namespace ConsoleApp1 var wellOptsStat = new WellOperationsStatService(db, cacheDb, wellService); var tvd = wellOptsStat.GetTvdAsync(1, default).Result; - + Print(tvd); Console.WriteLine("_"); } + + private static void Print(IEnumerable> tvd) + { + Console.WriteLine("|\tplan\t|\tfact\t|\tprog\t|"); + Console.WriteLine("|:-------------:|:-------------:|:-------------:|"); + foreach (var item in tvd) + Print(item); + } + + private static void Print(PlanFactPredictBase item) + { + static string GetText(WellOperationDto item) + => (item is null) + ? " --------- " + : $"{item.IdCategory} d:{item.WellDepth} "; + + Console.WriteLine($"|\t{GetText(item.Plan)}\t|\t{GetText(item.Fact)}\t|\t{GetText(item.Predict)}\t|"); + + } } }