From b487f01fae5f8641528f2e8bfc50ee8844626fb8 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Tue, 11 Oct 2022 12:12:32 +0500 Subject: [PATCH] =?UTF-8?q?=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5=D0=BD?= =?UTF-8?q?=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0=20=D0=B8?= =?UTF-8?q?=D0=BD=D1=82=D0=B5=D1=80=D0=BF=D0=BE=D0=BB=D1=8F=D1=86=D0=B8?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Subsystems/Interpolation.cs | 51 ++++++++++++++ ...SubsystemOperationTimeBackgroundService.cs | 66 +++++++------------ .../Services/Subsystems/Utils/Interp.cs | 49 -------------- 3 files changed, 76 insertions(+), 90 deletions(-) create mode 100644 AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs delete mode 100644 AsbCloudInfrastructure/Services/Subsystems/Utils/Interp.cs diff --git a/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs new file mode 100644 index 00000000..56601561 --- /dev/null +++ b/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AsbCloudInfrastructure.Services.Subsystems.Utils +{ +#nullable enable + internal class Interpolation + { + private IEnumerator<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> enumerator; + (DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax) lastValue = default; + + public Interpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> collection) + { + enumerator = collection.GetEnumerator(); + enumerator.MoveNext(); + } + + public float GetDepth(DateTimeOffset date) + { + if (lastValue.depthMin is not null && + lastValue.depthMax is not null) + { + while (date > enumerator.Current.dateMax) + { + if (isLess(date)) + CalcValue(enumerator.Current, date.Second); + enumerator.MoveNext(); + } + } + return (float)lastValue.depthMin; + } + + private bool isLess(DateTimeOffset date) + { + if (date <= enumerator.Current.dateMax && date >= enumerator.Current.dateMin) + return true; + return false; + } + private float CalcValue((DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax) item, int x) + { + + var a = (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second); + var b = item.depthMin * (item.dateMax.Second - (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second)); + var resultDepth = a * x + b; + return (float)resultDepth; + } + + } +#nullable disable +} diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index 9b83c9a2..fc63e795 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -257,27 +257,17 @@ namespace AsbCloudInfrastructure.Services.Subsystems $"where mode_pre is null or state_pre is null or mode != mode_pre or state != state_pre " + $"order by date;"; - //var queryDepthFromSaub = - // "select " + - // " min(\"date\") as min_date, " + - // " min(well_depth) as min_well_depth, " + - // " max(\"date\") as max_date, " + - // " max(well_depth) as max_well_depth " + - // "from " + - // " t_telemetry_data_saub " + - // "where id_telemetry = @idTelemetry and \"date\" > @begin " + - // "group by ceil(well_depth * 10) " + - // "order by min_date; "; - - var saubDepthAndDate = await db.TelemetryDataSaub.Where(s => s.IdTelemetry == idTelemetry & s.DateTime >= begin) - .Select(s => new DataRow() - { - Date = s.DateTime, - Depth = s.WellDepth - }) - .ToListAsync(token); - - + var queryDepthFromSaub = + "select " + + " min(\"date\") as min_date, " + + " min(well_depth) as min_well_depth, " + + " max(\"date\") as max_date, " + + " max(well_depth) as max_well_depth " + + "from " + + " t_telemetry_data_saub " + + "where id_telemetry = @idTelemetry and \"date\" > @begin " + + "group by ceil(well_depth * 10) " + + "order by min_date; "; var resultSpin = await GetResultFromQueryAsync(querySpin,begin,idTelemetry,db,token); @@ -296,28 +286,22 @@ namespace AsbCloudInfrastructure.Services.Subsystems await resultSpin.DisposeAsync(); - //using var resultDepthFromSaub = await GetResultFromQueryAsync(queryDepthFromSaub, begin, idTelemetry, db, token); + using var resultDepthFromSaub = await GetResultFromQueryAsync(queryDepthFromSaub, begin, idTelemetry, db, token); - //var dataDepthFromSaub = new List<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)>(); - //while (resultDepthFromSaub.Read()) - //{ - // var dateRowItem = - // ( - // resultDepthFromSaub.GetFieldValue(0), - // resultDepthFromSaub.GetFieldValue(1), - // resultDepthFromSaub.GetFieldValue(2), - // resultDepthFromSaub.GetFieldValue(3) + var dataDepthFromSaub = new List<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)>(); + while (resultDepthFromSaub.Read()) + { + var dateRowItem = + ( + resultDepthFromSaub.GetFieldValue(0), + resultDepthFromSaub.GetFieldValue(1), + resultDepthFromSaub.GetFieldValue(2), + resultDepthFromSaub.GetFieldValue(3) - // ); - // dataDepthFromSaub.Add(dateRowItem); - //} - - - var interp = new Interp(saubDepthAndDate.Select(t => ( - t.Date, - t.Depth - )) - .OrderBy(t => t.Date)); + ); + dataDepthFromSaub.Add(dateRowItem); + } + var interp = new Interpolation(dataDepthFromSaub.OrderBy(t => t.depthMin)); if (dataSpinList.Any()) { var mode = dataSpinList[0].Mode; diff --git a/AsbCloudInfrastructure/Services/Subsystems/Utils/Interp.cs b/AsbCloudInfrastructure/Services/Subsystems/Utils/Interp.cs deleted file mode 100644 index 2bc6f7f1..00000000 --- a/AsbCloudInfrastructure/Services/Subsystems/Utils/Interp.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace AsbCloudInfrastructure.Services.Subsystems.Utils -{ - internal class Interp - { - //private IEnumerator<(DateTimeOffset dateMin, float? depthMin,DateTimeOffset dateMax, float? depthMax)> enumerator; - //(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax) lastValue = default; - - //public Interp (IOrderedEnumerable<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> collection) - //{ - // enumerator = collection.GetEnumerator(); - // enumerator.MoveNext(); - //} - - //public float? GetDepth (DateTimeOffset date) - //{ - // while(date < enumerator.Current.dateMax) - // { - // lastValue = enumerator.Current; - // enumerator.MoveNext(); - // } - // return lastValue.depthMin; - //} - - - - private IEnumerator<(DateTimeOffset date, float? y)> enumerator; - (DateTimeOffset date, float? y) lastValue = default; - - public Interp(IOrderedEnumerable<(DateTimeOffset date, float? y)> collection) - { - enumerator = collection.GetEnumerator(); - enumerator.MoveNext(); - } - - public float? GetDepth(DateTimeOffset date) - { - while (date > enumerator.Current.date) - { - lastValue = enumerator.Current; - enumerator.MoveNext(); - } - return lastValue.y; - } - } -}