From 7b900c1f9d79aeffed79fd2ca6c0d39d0727c81b Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Thu, 6 Oct 2022 14:43:43 +0500 Subject: [PATCH 1/9] =?UTF-8?q?=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=BE=D0=BF=D1=80=D0=B5=D0=B4=D0=B5=D0=BB=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D1=8F=20=D0=BD=D0=B0=D1=80=D0=B0=D0=B1=D0=BE=D1=82?= =?UTF-8?q?=D0=BE=D0=BA=20=D0=A1=D0=BF=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...SubsystemOperationTimeBackgroundService.cs | 77 +++++++++++-------- 1 file changed, 47 insertions(+), 30 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index da9e63a3..b1f92eae 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -215,7 +215,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems private static async Task?> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) { - static int? GetSubsytemId(short mode, int state) + static int? GetSubsytemId(short? mode, int? state) { if (state == 7 && (mode & 2) > 0) return idSubsytemTorqueMaster; @@ -225,13 +225,22 @@ namespace AsbCloudInfrastructure.Services.Subsystems return null; } + static float GetDepthFromSaub(List rowsSaub, DateTimeOffset date) + { + float depth = 0; + for (int i = 0; i < rowsSaub.Count; i++) + { + if (date <= rowsSaub[i].Date) + depth = (float)rowsSaub[i].Depth; + } + return depth; + } - var query = + var querySpin = $"select " + $" tspin.date, " + $" tspin.mode, " + - $" tspin.state, " + - $" tsaub.well_depth " + + $" tspin.state " + $"from ( " + $" select " + $" date, " + @@ -241,42 +250,50 @@ namespace AsbCloudInfrastructure.Services.Subsystems $" lag(state, 1) over (order by date) as state_pre " + $" from t_telemetry_data_spin " + $" where id_telemetry = @idTelemetry and date >= @begin" + - $" order by date ) as tspin " + - $"left outer join ( " + - $" select " + - $" date, " + - $" well_depth " + - $" from t_telemetry_data_saub " + - $" where id_telemetry = @idTelemetry and date >= @begin) as tsaub " + - $"on EXTRACT(EPOCH from tspin.date) = EXTRACT(EPOCH from tsaub.date) " + + $" order by date ) as tspin " + $"where mode_pre is null or state_pre is null or mode != mode_pre or state != state_pre " + $"order by date;"; - + var saubDepthAndDate = await db.TelemetryDataSaub.Where(s => s.IdTelemetry == idTelemetry & s.DateTime >= begin) + .Select(s => new DataRow() + { + Date = s.DateTime, + Depth = s.WellDepth + }) + .OrderBy(s => s.Date) + .ToListAsync(token); var idTelemetryParam = new NpgsqlParameter("@idTelemetry", idTelemetry); var beginParam = new NpgsqlParameter("@begin", begin); await db.Database.OpenConnectionAsync(token); using var command = db.Database.GetDbConnection().CreateCommand(); - command.CommandText = query; + command.CommandText = querySpin; command.Parameters.Add(idTelemetryParam); command.Parameters.Add(beginParam); - using var result = await command.ExecuteReaderAsync(token); - var subsystemOperationTime = new List(32); - - if (result.Read()) + var dataRowList = new List(); + while (result.Read()) { - var mode = result.GetFieldValue(1); - var state = result.GetFieldValue(2); - var idSubsystem = GetSubsytemId(mode, state); - var dateStart = result.GetFieldValue(0); - var depthStart = result.GetFieldValue(3); - - while (result.Read()) + var dateRowItem = new DataRow() { - var dateEnd = result.GetFieldValue(0); - var depthEnd = result.GetFieldValue(3); + Date = result.GetFieldValue(0), + Mode = result.GetFieldValue(1), + State = result.GetFieldValue(2) + }; + dateRowItem.Depth = GetDepthFromSaub(saubDepthAndDate, dateRowItem.Date); + dataRowList.Add(dateRowItem); + } + if (dataRowList.Any()==true) + { + var mode = dataRowList[0].Mode; + var state = dataRowList[0].State; + var idSubsystem = GetSubsytemId(mode, state); + var dateStart = dataRowList[0].Date; + var depthStart = dataRowList[0].Depth; + for (int i = 1; i(1); - state = result.GetFieldValue(2); + mode = dataRowList[i].Mode; + state = dataRowList[i].State; idSubsystem = GetSubsytemId(mode, state); dateStart = dateEnd; depthStart = depthEnd; - } + } } return subsystemOperationTime; } From 684a56a1005de5cf473385dcc45e2e8440b64f06 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Mon, 10 Oct 2022 15:05:32 +0500 Subject: [PATCH 2/9] =?UTF-8?q?=D0=9A=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BB=D0=BE=D0=B3=D0=B8?= =?UTF-8?q?=D0=BA=D0=B8=20=D0=B7=D0=B0=D0=BF=D0=B8=D1=81=D0=B8=20=D0=BD?= =?UTF-8?q?=D0=B0=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=BE=D0=BA=20=D0=BF=D0=BE?= =?UTF-8?q?=D0=B4=D1=81=D0=B8=D1=81=D1=82=D0=B5=D0=BC=20=D0=A1=D0=BF=D0=B8?= =?UTF-8?q?=D0=BD=20=D0=B8=20=D0=A2=D0=BE=D1=80=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...SubsystemOperationTimeBackgroundService.cs | 117 ++++++++++++------ .../Services/Subsystems/Utils/Interp.cs | 49 ++++++++ 2 files changed, 125 insertions(+), 41 deletions(-) create mode 100644 AsbCloudInfrastructure/Services/Subsystems/Utils/Interp.cs diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index b1f92eae..9b83c9a2 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -1,5 +1,6 @@ using AsbCloudDb.Model; using AsbCloudDb.Model.Subsystems; +using AsbCloudInfrastructure.Services.Subsystems.Utils; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; @@ -7,6 +8,7 @@ using Npgsql; using System; using System.Collections.Generic; using System.Data; +using System.Data.Common; using System.Diagnostics; using System.Linq; using System.Threading; @@ -225,17 +227,18 @@ namespace AsbCloudInfrastructure.Services.Subsystems return null; } - static float GetDepthFromSaub(List rowsSaub, DateTimeOffset date) + static async Task GetResultFromQueryAsync(string query, DateTimeOffset paramDate, int paramIdTelemetry, IAsbCloudDbContext db, CancellationToken token) { - float depth = 0; - for (int i = 0; i < rowsSaub.Count; i++) - { - if (date <= rowsSaub[i].Date) - depth = (float)rowsSaub[i].Depth; - } - return depth; + var idTelemetryParam = new NpgsqlParameter("@idTelemetry", paramIdTelemetry); + var beginParam = new NpgsqlParameter("@begin", paramDate); + await db.Database.OpenConnectionAsync(token); + var command = db.Database.GetDbConnection().CreateCommand(); + command.CommandText = query; + command.Parameters.Add(idTelemetryParam); + command.Parameters.Add(beginParam); + var result = await command.ExecuteReaderAsync(token); + return result; } - var querySpin = $"select " + $" tspin.date, " + @@ -253,47 +256,79 @@ namespace AsbCloudInfrastructure.Services.Subsystems $" order by date ) as tspin " + $"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 }) - .OrderBy(s => s.Date) .ToListAsync(token); - var idTelemetryParam = new NpgsqlParameter("@idTelemetry", idTelemetry); - var beginParam = new NpgsqlParameter("@begin", begin); + + - await db.Database.OpenConnectionAsync(token); - using var command = db.Database.GetDbConnection().CreateCommand(); - command.CommandText = querySpin; - command.Parameters.Add(idTelemetryParam); - command.Parameters.Add(beginParam); - using var result = await command.ExecuteReaderAsync(token); + var resultSpin = await GetResultFromQueryAsync(querySpin,begin,idTelemetry,db,token); + var subsystemOperationTime = new List(32); - var dataRowList = new List(); - while (result.Read()) + var dataSpinList = new List(); + while (resultSpin.Read()) { var dateRowItem = new DataRow() { - Date = result.GetFieldValue(0), - Mode = result.GetFieldValue(1), - State = result.GetFieldValue(2) - }; - dateRowItem.Depth = GetDepthFromSaub(saubDepthAndDate, dateRowItem.Date); - dataRowList.Add(dateRowItem); + Date = resultSpin.GetFieldValue(0), + Mode = resultSpin.GetFieldValue(1), + State = resultSpin.GetFieldValue(2) + }; + dataSpinList.Add(dateRowItem); } - if (dataRowList.Any()==true) - { - var mode = dataRowList[0].Mode; - var state = dataRowList[0].State; + await resultSpin.DisposeAsync(); + + + //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) + + // ); + // dataDepthFromSaub.Add(dateRowItem); + //} + + + var interp = new Interp(saubDepthAndDate.Select(t => ( + t.Date, + t.Depth + )) + .OrderBy(t => t.Date)); + if (dataSpinList.Any()) + { + var mode = dataSpinList[0].Mode; + var state = dataSpinList[0].State; var idSubsystem = GetSubsytemId(mode, state); - var dateStart = dataRowList[0].Date; - var depthStart = dataRowList[0].Depth; - for (int i = 1; i 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; + } + } +} From b487f01fae5f8641528f2e8bfc50ee8844626fb8 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Tue, 11 Oct 2022 12:12:32 +0500 Subject: [PATCH 3/9] =?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; - } - } -} From 37e29e003c7bd942c4269e7ce991bb300b388245 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Tue, 11 Oct 2022 14:28:34 +0500 Subject: [PATCH 4/9] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BA=D0=BB=D0=B0=D1=81=D1=81=D0=B0?= =?UTF-8?q?=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=BF=D0=BE=D0=BB=D1=8F=D1=86?= =?UTF-8?q?=D0=B8=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Subsystems/Interpolation.cs | 15 ++++++++------- .../SubsystemOperationTimeBackgroundService.cs | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs index 56601561..cf87da98 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs @@ -8,7 +8,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems.Utils internal class Interpolation { private IEnumerator<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> enumerator; - (DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax) lastValue = default; + (DateTimeOffset x, float? y) lastValue = default; public Interpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)> collection) { @@ -17,18 +17,19 @@ namespace AsbCloudInfrastructure.Services.Subsystems.Utils } public float GetDepth(DateTimeOffset date) - { - if (lastValue.depthMin is not null && - lastValue.depthMax is not null) + { { - while (date > enumerator.Current.dateMax) + while (date >= enumerator.Current.dateMax) { if (isLess(date)) - CalcValue(enumerator.Current, date.Second); + { + lastValue.x = date; + lastValue.y = CalcValue(enumerator.Current, date.Second); + } enumerator.MoveNext(); } } - return (float)lastValue.depthMin; + return (float)lastValue.y; } private bool isLess(DateTimeOffset date) diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index fc63e795..b2cf39fc 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -265,7 +265,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems " max(well_depth) as max_well_depth " + "from " + " t_telemetry_data_saub " + - "where id_telemetry = @idTelemetry and \"date\" > @begin " + + "where id_telemetry = @idTelemetry and \"date\" >= @begin " + "group by ceil(well_depth * 10) " + "order by min_date; "; From cb2a8dfdeb6ef5a9ecbd6907025f2e27b5b8e9ed Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Tue, 11 Oct 2022 17:25:59 +0500 Subject: [PATCH 5/9] =?UTF-8?q?=D0=BA=D0=BE=D1=80=D1=80=D0=B5=D0=BA=D1=82?= =?UTF-8?q?=D0=B8=D1=80=D0=BE=D0=B2=D0=BA=D0=B0=20=D0=BA=D0=BB=D0=B0=D1=81?= =?UTF-8?q?=D1=81=D0=B0=20=D0=B8=D0=BD=D1=82=D0=B5=D1=80=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=8F=D1=86=D0=B8=D0=B8=20=D0=B3=D0=BB=D1=83=D0=B1=D0=B8=D0=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Subsystems/DepthInterpolation.cs | 50 ++++++++++++++++++ .../Services/Subsystems/Interpolation.cs | 52 ------------------- ...SubsystemOperationTimeBackgroundService.cs | 11 ++-- 3 files changed, 54 insertions(+), 59 deletions(-) create mode 100644 AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs delete mode 100644 AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs diff --git a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs new file mode 100644 index 00000000..def5886a --- /dev/null +++ b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs @@ -0,0 +1,50 @@ +using System; +using System.Collections.Generic; +using System.Linq; + +namespace AsbCloudInfrastructure.Services.Subsystems.Utils +{ +#nullable enable + internal class DepthInterpolation + { + private IEnumerator<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> enumerator; + (DateTimeOffset x, float? y) lastValue = default; + + private DateTimeOffset dateMin; + public DepthInterpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection) + { + enumerator = collection.GetEnumerator(); + enumerator.MoveNext(); + dateMin = enumerator.Current.dateMin; + } + + public float GetDepth(DateTimeOffset date) + { + { + while (isLess(date)) + { + lastValue.x = date; + lastValue.y = CalcValue(enumerator.Current, (date - dateMin).TotalSeconds); + enumerator.MoveNext(); + } + } + return (float)lastValue.y; + } + + private bool isLess(DateTimeOffset date) + { + if (date >= enumerator.Current.dateMin) + return true; + return false; + } + private float CalcValue((DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax) item, double timestamp) + { + var a = (item.depthMax - item.depthMin) / (item.dateMax - item.dateMin).TotalSeconds; + var b = item.depthMin * (item.dateMax.Second - (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second)); + var resultDepth = a * timestamp + b; + return (float)resultDepth; + } + + } +#nullable disable +} diff --git a/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs deleted file mode 100644 index cf87da98..00000000 --- a/AsbCloudInfrastructure/Services/Subsystems/Interpolation.cs +++ /dev/null @@ -1,52 +0,0 @@ -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 x, float? y) lastValue = default; - - public Interpolation(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) - { - if (isLess(date)) - { - lastValue.x = date; - lastValue.y = CalcValue(enumerator.Current, date.Second); - } - enumerator.MoveNext(); - } - } - return (float)lastValue.y; - } - - 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 b2cf39fc..def85fa4 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -284,24 +284,21 @@ namespace AsbCloudInfrastructure.Services.Subsystems dataSpinList.Add(dateRowItem); } await resultSpin.DisposeAsync(); - - using var resultDepthFromSaub = await GetResultFromQueryAsync(queryDepthFromSaub, begin, idTelemetry, db, token); - - var dataDepthFromSaub = new List<(DateTimeOffset dateMin, float? depthMin, DateTimeOffset dateMax, float? depthMax)>(); + 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(1), resultDepthFromSaub.GetFieldValue(2), - resultDepthFromSaub.GetFieldValue(3) + resultDepthFromSaub.GetFieldValue(3) ); dataDepthFromSaub.Add(dateRowItem); } - var interp = new Interpolation(dataDepthFromSaub.OrderBy(t => t.depthMin)); + var interp = new DepthInterpolation(dataDepthFromSaub.OrderBy(t => t.depthMin)); if (dataSpinList.Any()) { var mode = dataSpinList[0].Mode; From af6eea370c3dc90425702c0433d49b07cf9b5efa Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Thu, 13 Oct 2022 23:54:14 +0500 Subject: [PATCH 6/9] # correction DepthInterval --- .../Services/Subsystems/DepthInterpolation.cs | 40 +++++++++---------- ...SubsystemOperationTimeBackgroundService.cs | 2 +- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs index def5886a..edb41678 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs @@ -8,43 +8,41 @@ namespace AsbCloudInfrastructure.Services.Subsystems.Utils internal class DepthInterpolation { private IEnumerator<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> enumerator; - (DateTimeOffset x, float? y) lastValue = default; + (DateTimeOffset x, float y) lastValue = default; private DateTimeOffset dateMin; public DepthInterpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection) { enumerator = collection.GetEnumerator(); enumerator.MoveNext(); - dateMin = enumerator.Current.dateMin; + dateMin = enumerator.Current.dateMin; } public float GetDepth(DateTimeOffset date) - { + { + var isNotEnd = true; + while (isOnInterval(date) && isNotEnd) { - while (isLess(date)) - { - lastValue.x = date; - lastValue.y = CalcValue(enumerator.Current, (date - dateMin).TotalSeconds); - enumerator.MoveNext(); - } + lastValue.x = date; + lastValue.y = CalcValue(enumerator.Current, (date- enumerator.Current.dateMin).TotalSeconds); + isNotEnd = enumerator.MoveNext(); } - return (float)lastValue.y; + return lastValue.y; + } - private bool isLess(DateTimeOffset date) - { - if (date >= enumerator.Current.dateMin) - return true; - return false; - } + private bool isOnInterval(DateTimeOffset date) + => date >= enumerator.Current.dateMin && date <= enumerator.Current.dateMax; private float CalcValue((DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax) item, double timestamp) { - var a = (item.depthMax - item.depthMin) / (item.dateMax - item.dateMin).TotalSeconds; - var b = item.depthMin * (item.dateMax.Second - (item.depthMax - item.depthMin) / (item.dateMax.Second - item.dateMin.Second)); - var resultDepth = a * timestamp + b; - return (float)resultDepth; + if (item.depthMin == item.depthMax) + { + return item.depthMin; + } + var w = timestamp / (item.dateMax - item.dateMin).TotalSeconds; + var d = ((item.depthMax - item.depthMin) * w)+ item.depthMin; + return (float)d; } - } #nullable disable } diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index def85fa4..62e9e927 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -298,7 +298,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems ); dataDepthFromSaub.Add(dateRowItem); } - var interp = new DepthInterpolation(dataDepthFromSaub.OrderBy(t => t.depthMin)); + var interp = new DepthInterpolation(dataDepthFromSaub.OrderBy(t => t.dateMin)); if (dataSpinList.Any()) { var mode = dataSpinList[0].Mode; From abd00086153d573e9abf59a9df569be217a169f2 Mon Sep 17 00:00:00 2001 From: eugeniy_ivanov Date: Fri, 14 Oct 2022 15:12:30 +0500 Subject: [PATCH 7/9] =?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=D1=84=D0=BE=D1=80=D0=BC=D1=83=D0=BB=D1=8B=20?= =?UTF-8?q?=D1=80=D0=B0=D1=81=D1=87=D0=B5=D1=82=D0=B0=20=D0=B3=D0=BB=D1=83?= =?UTF-8?q?=D0=B1=D0=B8=D0=BD=D1=8B=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD=D0=B5?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8=D1=82?= =?UTF-8?q?=D0=BC=D0=B0=20=D1=84=D0=BE=D1=80=D0=BC=D0=B8=D1=80=D0=BE=D0=B2?= =?UTF-8?q?=D0=B0=D0=BD=D0=B8=D1=8F=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA=D0=B0?= =?UTF-8?q?=20=D0=A1=D0=9F=D0=98=D0=9D=20=D0=B8=D0=B7=D0=BC=D0=B5=D0=BD?= =?UTF-8?q?=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=B0=D0=BB=D0=B3=D0=BE=D1=80=D0=B8?= =?UTF-8?q?=D1=82=D0=BC=D0=B0=20=D0=BF=D0=BE=D0=B8=D1=81=D0=BA=D0=B0=20?= =?UTF-8?q?=D0=B3=D0=BB=D1=83=D0=B1=D0=B8=D0=BD=D1=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/Subsystems/DepthInterpolation.cs | 32 ++++++++----------- ...SubsystemOperationTimeBackgroundService.cs | 15 ++++++--- 2 files changed, 23 insertions(+), 24 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs index edb41678..06a0fd24 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs @@ -7,40 +7,34 @@ namespace AsbCloudInfrastructure.Services.Subsystems.Utils #nullable enable internal class DepthInterpolation { - private IEnumerator<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> enumerator; - (DateTimeOffset x, float y) lastValue = default; - - private DateTimeOffset dateMin; - public DepthInterpolation(IOrderedEnumerable<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection) + private List<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection; + (DateTimeOffset x, float y) lastValue = default; + public DepthInterpolation(List<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection) { - enumerator = collection.GetEnumerator(); - enumerator.MoveNext(); - dateMin = enumerator.Current.dateMin; + this.collection = collection; } public float GetDepth(DateTimeOffset date) - { - var isNotEnd = true; - while (isOnInterval(date) && isNotEnd) + { + for (int i = 0; i < collection.Count; i++) { - lastValue.x = date; - lastValue.y = CalcValue(enumerator.Current, (date- enumerator.Current.dateMin).TotalSeconds); - isNotEnd = enumerator.MoveNext(); + if (date <= collection[i].dateMax && date >= collection[i].dateMin) + { + lastValue.x = date; + lastValue.y = CalcValue(collection[i], (date - collection[i].dateMin).TotalSeconds); + } } return lastValue.y; - } - private bool isOnInterval(DateTimeOffset date) - => date >= enumerator.Current.dateMin && date <= enumerator.Current.dateMax; private float CalcValue((DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax) item, double timestamp) { if (item.depthMin == item.depthMax) { return item.depthMin; - } + } var w = timestamp / (item.dateMax - item.dateMin).TotalSeconds; - var d = ((item.depthMax - item.depthMin) * w)+ item.depthMin; + var d = ((item.depthMax - item.depthMin) * w) + item.depthMin; return (float)d; } } diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index 62e9e927..70e87d76 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -284,6 +284,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems dataSpinList.Add(dateRowItem); } await resultSpin.DisposeAsync(); + dataSpinList.OrderBy(s => s.Date); 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()) @@ -294,22 +295,26 @@ namespace AsbCloudInfrastructure.Services.Subsystems resultDepthFromSaub.GetFieldValue(1), resultDepthFromSaub.GetFieldValue(2), resultDepthFromSaub.GetFieldValue(3) - ); dataDepthFromSaub.Add(dateRowItem); } - var interp = new DepthInterpolation(dataDepthFromSaub.OrderBy(t => t.dateMin)); + dataDepthFromSaub.OrderBy(o => o.dateMin); + var depthInterpolation = new DepthInterpolation(dataDepthFromSaub); + foreach (var item in dataSpinList) + { + item.Depth = depthInterpolation.GetDepth(item.Date); + } if (dataSpinList.Any()) { var mode = dataSpinList[0].Mode; var state = dataSpinList[0].State; var idSubsystem = GetSubsytemId(mode, state); var dateStart = dataSpinList[0].Date; - var depthStart = interp.GetDepth(dateStart); + var depthStart = dataSpinList[0].Depth; for (int i = 1; i < dataSpinList.Count; i++) { var dateEnd = dataSpinList[i].Date; - var depthEnd = interp.GetDepth(dateEnd); + var depthEnd = dataSpinList[i].Depth; if (idSubsystem.HasValue) { var operationTimeItem = new SubsystemOperationTime() @@ -319,7 +324,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems DateStart = dateStart, DateEnd = dateEnd, DepthEnd = depthEnd, - DepthStart = depthStart, + DepthStart = depthStart }; subsystemOperationTime.Add(operationTimeItem); } From 5a2435795ac4f0fcaeb5fec294d8ed4146857f51 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Wed, 19 Oct 2022 13:55:10 +0500 Subject: [PATCH 8/9] #6370638 refactored --- .../Services/Subsystems/DepthInterpolation.cs | 57 ++- ...SubsystemOperationTimeBackgroundService.cs | 356 ++++++++++-------- 2 files changed, 228 insertions(+), 185 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs index 06a0fd24..750786c4 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs @@ -7,35 +7,56 @@ namespace AsbCloudInfrastructure.Services.Subsystems.Utils #nullable enable internal class DepthInterpolation { - private List<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection; - (DateTimeOffset x, float y) lastValue = default; - public DepthInterpolation(List<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection) + private readonly TimeSpan maxDeltaDate = TimeSpan.FromHours(12); + private readonly IEnumerator<(DateTimeOffset x, float y)> enumerator; + private (DateTimeOffset x, float y) p0; + private bool canMoveNext; + + public DepthInterpolation(IEnumerable<(DateTimeOffset x, float y)> collection) { - this.collection = collection; + enumerator = collection.GetEnumerator(); + canMoveNext = enumerator.MoveNext(); + p0 = enumerator.Current; + } + + ~DepthInterpolation() + { + enumerator.Dispose(); } public float GetDepth(DateTimeOffset date) - { - for (int i = 0; i < collection.Count; i++) + { + // ошибка в телеметрии см. прим.: idTelemetry = 93 && date between '2021-11-16 17:18:40.000 +0500' and '2021-11-16 17:19:37.000 +0500' + while (canMoveNext && enumerator.Current.x < date) { - if (date <= collection[i].dateMax && date >= collection[i].dateMin) - { - lastValue.x = date; - lastValue.y = CalcValue(collection[i], (date - collection[i].dateMin).TotalSeconds); - } + p0 = enumerator.Current; + canMoveNext = enumerator.MoveNext(); } - return lastValue.y; + return CalcValue(date); } - private float CalcValue((DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax) item, double timestamp) + private float CalcValue(DateTimeOffset date) { - if (item.depthMin == item.depthMax) + var p1 = enumerator.Current; + if (canMoveNext || p1 == default || p0.y == p1.y || p0.x > date) + return p0.y; + + if (p1.x < date) + return p1.y; + + if (p1.x - p0.x > maxDeltaDate && Math.Abs(p1.y - p0.y) > 0.2d) { - return item.depthMin; + if (date - p0.x < p1.x - date) + return p0.y; + else + return p1.y; } - var w = timestamp / (item.dateMax - item.dateMin).TotalSeconds; - var d = ((item.depthMax - item.depthMin) * w) + item.depthMin; - return (float)d; + + var a = (p1.y - p0.y) / (p1.x - p0.x).TotalSeconds; + var b = p0.y; + var x = (date - p0.x).TotalSeconds; + var y = a * x + b; + return (float)y; } } #nullable disable diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index 70e87d76..9f77a77e 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -1,6 +1,7 @@ using AsbCloudDb.Model; using AsbCloudDb.Model.Subsystems; using AsbCloudInfrastructure.Services.Subsystems.Utils; +using iText.Forms.Xfdf; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; @@ -25,6 +26,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems private const int idSubsytemSpinMaster = 65536; private const int idSubsytemAkb = 1; private const int idSubsytemMse = 2; + public SubsystemOperationTimeBackgroundService(IConfiguration configuration) { connectionString = configuration.GetConnectionString("DefaultConnection"); @@ -108,6 +110,25 @@ namespace AsbCloudInfrastructure.Services.Subsystems } return affected; } + + private static async Task ExecuteReaderAsync(IAsbCloudDbContext db, string query, CancellationToken token) + { + var connection = db.Database.GetDbConnection(); + if ( + connection?.State is null || + connection.State == ConnectionState.Broken || + connection.State == ConnectionState.Closed) + { + await db.Database.OpenConnectionAsync(token); + connection = db.Database.GetDbConnection(); + } + using var command = connection.CreateCommand(); + command.CommandText = query; + + var result = await command.ExecuteReaderAsync(token); + return result; + } + private static async Task> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) { static bool isSubsytemAkb(short? mode) @@ -126,47 +147,6 @@ namespace AsbCloudInfrastructure.Services.Subsystems if ((state & 1) > 0) return true; return false; - } - - static List GetSubsystemOperationTimes (List dataRows, Predicate satisfyCondition, int idSubsystem, int idTelemetry) - { - if (dataRows is null) - return new List(); - if (dataRows.Count < 2) - return new List(); - var listSubsystemOperationTime = new List(); - var foundSubsystem = satisfyCondition(dataRows[0]); - var dateStart = dataRows[0].Date; - var depthStart = dataRows[0].Depth; - - for (int i = 1; i 0" + $" order by date ) as tt " + - $"where (tt.mode_prev is null or tt.mode != tt.mode_prev) and tt.date >= @begin " + + $"where (tt.mode_lag is null or (tt.mode != tt.mode_lag and tt.mode_lead != tt.mode_lag)) and tt.date >= '{begin:u}' " + $"order by tt.date;"; - var idTelemetryParam = new NpgsqlParameter("@idTelemetry", idTelemetry); - var beginParam = new NpgsqlParameter("@begin", begin); + using var result = await ExecuteReaderAsync(db, query, token); - await db.Database.OpenConnectionAsync(token); - using var command = db.Database.GetDbConnection().CreateCommand(); - command.CommandText = query; - command.Parameters.Add(idTelemetryParam); - command.Parameters.Add(beginParam); + var subsystemsOperationTimes = new List(); + + (bool isEnable, DateTimeOffset date, float depth) akbPre = default; + (bool isEnable, DateTimeOffset date, float depth) msePre = default; - using var result = await command.ExecuteReaderAsync(token); - - var subsystemOperationTime = new List(); - var dataRowList = new List(); while (result.Read()) { - var dateRowItem = new DataRow() + var mode = result.GetFieldValue(1); + var state = result.GetFieldValue(3); + + var isAkbEnable = isSubsytemAkb(mode); + var isMseEnable = IsSubsystemMse(state); + var date = result.GetFieldValue(0); + var depth = result.GetFieldValue(2); + + if (!akbPre.isEnable && isAkbEnable) { - Date = result.GetFieldValue(0), - Mode = result.GetFieldValue(1), - Depth = result.GetFieldValue(2), - State = result.GetFieldValue(3) - }; - dataRowList.Add(dateRowItem); + akbPre = (true, date, depth); + } + else if (akbPre.isEnable && !isAkbEnable) + { + var subsystemOperationTime = new SubsystemOperationTime + { + IdTelemetry = idTelemetry, + IdSubsystem = idSubsytemAkb, + DateStart = akbPre.date, + DateEnd = date, + DepthStart = akbPre.depth, + DepthEnd = depth, + }; + if (IsValid(subsystemOperationTime)) + subsystemsOperationTimes.Add(subsystemOperationTime); + akbPre.isEnable = false; + } + + if (!msePre.isEnable && isMseEnable) + { + msePre = (true, date, depth); + } + else if (msePre.isEnable && !isMseEnable) + { + var subsystemOperationTime = new SubsystemOperationTime + { + IdTelemetry = idTelemetry, + IdSubsystem = idSubsytemMse, + DateStart = akbPre.date, + DateEnd = date, + DepthStart = akbPre.depth, + DepthEnd = depth, + }; + if (IsValid(subsystemOperationTime)) + subsystemsOperationTimes.Add(subsystemOperationTime); + + msePre.isEnable = false; + } } - var akbOperationTimes = GetSubsystemOperationTimes(dataRowList, d => isSubsytemAkb(d.Mode), idSubsytemAkb, idTelemetry); - var mseOperationTimes = GetSubsystemOperationTimes(dataRowList, d => IsSubsystemMse(d.State), idSubsytemMse, idTelemetry); - subsystemOperationTime.AddRange(akbOperationTimes); - subsystemOperationTime.AddRange(mseOperationTimes); - return subsystemOperationTime; + + return subsystemsOperationTimes; } - private static async Task?> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) + private static async Task> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token) { static int? GetSubsytemId(short? mode, int? state) { @@ -227,18 +239,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems return null; } - static async Task GetResultFromQueryAsync(string query, DateTimeOffset paramDate, int paramIdTelemetry, IAsbCloudDbContext db, CancellationToken token) - { - var idTelemetryParam = new NpgsqlParameter("@idTelemetry", paramIdTelemetry); - var beginParam = new NpgsqlParameter("@begin", paramDate); - await db.Database.OpenConnectionAsync(token); - var command = db.Database.GetDbConnection().CreateCommand(); - command.CommandText = query; - command.Parameters.Add(idTelemetryParam); - command.Parameters.Add(beginParam); - var result = await command.ExecuteReaderAsync(token); - return result; - } + var querySpin = $"select " + $" tspin.date, " + @@ -248,104 +249,125 @@ namespace AsbCloudInfrastructure.Services.Subsystems $" select " + $" date, " + $" mode, " + - $" lag(mode, 1) over (order by date) as mode_pre, " + + $" lag(mode, 1) over (order by date) as mode_lag, " + + $" lead(mode, 1) over (order by date) as mode_lead, " + $" state, " + - $" lag(state, 1) over (order by date) as state_pre " + + $" lag(state, 1) over (order by date) as state_lag " + $" from t_telemetry_data_spin " + - $" where id_telemetry = @idTelemetry and date >= @begin" + + $" where id_telemetry = {idTelemetry} and date >= '{begin:u}'" + $" order by date ) as tspin " + - $"where mode_pre is null or state_pre is null or mode != mode_pre or state != state_pre " + + $"where mode_lag is null or state_lag is null or (mode != mode_lag and mode_lead != mode_lag) or state != state_lag " + $"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 resultSpin = await GetResultFromQueryAsync(querySpin,begin,idTelemetry,db,token); - - var subsystemOperationTime = new List(32); - var dataSpinList = new List(); - while (resultSpin.Read()) + var rows = new List<(int? IdSubsystem, DateTimeOffset Date)>(32); { - var dateRowItem = new DataRow() + using var resultSpin = await ExecuteReaderAsync(db, querySpin, token); + int? idSubsystemLast = null; + while (resultSpin.Read()) { - Date = resultSpin.GetFieldValue(0), - Mode = resultSpin.GetFieldValue(1), - State = resultSpin.GetFieldValue(2) - }; - dataSpinList.Add(dateRowItem); - } - await resultSpin.DisposeAsync(); - dataSpinList.OrderBy(s => s.Date); - 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) - ); - dataDepthFromSaub.Add(dateRowItem); - } - dataDepthFromSaub.OrderBy(o => o.dateMin); - var depthInterpolation = new DepthInterpolation(dataDepthFromSaub); - foreach (var item in dataSpinList) - { - item.Depth = depthInterpolation.GetDepth(item.Date); - } - if (dataSpinList.Any()) - { - var mode = dataSpinList[0].Mode; - var state = dataSpinList[0].State; - var idSubsystem = GetSubsytemId(mode, state); - var dateStart = dataSpinList[0].Date; - var depthStart = dataSpinList[0].Depth; - for (int i = 1; i < dataSpinList.Count; i++) - { - var dateEnd = dataSpinList[i].Date; - var depthEnd = dataSpinList[i].Depth; - if (idSubsystem.HasValue) + var mode = resultSpin.GetFieldValue(1); + var state = resultSpin.GetFieldValue(2); + var idSubsystem = GetSubsytemId(mode, state); + if(idSubsystemLast != idSubsystem) { - var operationTimeItem = new SubsystemOperationTime() - { - IdTelemetry = idTelemetry, - IdSubsystem = idSubsystem.Value, - DateStart = dateStart, - DateEnd = dateEnd, - DepthEnd = depthEnd, - DepthStart = depthStart - }; - subsystemOperationTime.Add(operationTimeItem); - } - mode = dataSpinList[i].Mode; - state = dataSpinList[i].State; - idSubsystem = GetSubsytemId(mode, state); - dateStart = dateEnd; - depthStart = depthEnd; + idSubsystemLast = idSubsystem; + var date = resultSpin.GetFieldValue(0); + rows.Add((idSubsystem, date)); + } + } + await resultSpin.DisposeAsync(); + } + + if (rows.Count < 2) + return Enumerable.Empty(); + + var minSpinDate = rows.Min(i => i.Date); + var maxSpinDate = rows.Max(i => i.Date); + var depthInterpolation = await GetInterpolation(db, idTelemetry, minSpinDate, maxSpinDate, token); + + if (depthInterpolation is null) + return Enumerable.Empty(); + + var subsystemsOperationTimes = new List(32); + + for (int i = 1; i < rows.Count; i++) + { + var r0 = rows[i - 1]; + var r1 = rows[i]; + if (r0.IdSubsystem is not null && r0.IdSubsystem != r1.IdSubsystem) + { + var subsystemOperationTime = new SubsystemOperationTime() + { + IdTelemetry = idTelemetry, + IdSubsystem = r0.IdSubsystem.Value, + DateStart = r0.Date, + DateEnd = r1.Date, + DepthStart = depthInterpolation.GetDepth(r0.Date), + DepthEnd = depthInterpolation.GetDepth(r1.Date), + }; + + if (IsValid(subsystemOperationTime)) + subsystemsOperationTimes.Add(subsystemOperationTime); } } - return subsystemOperationTime; + + return subsystemsOperationTimes; + } + + private static bool IsValid(SubsystemOperationTime item) + { + var validateCode = GetValidateErrorCode(item); + if (validateCode != 0) + { + var str = System.Text.Json.JsonSerializer.Serialize(item); + Trace.TraceWarning($"Wrong({validateCode}) SubsystemOperationTime: {str}"); + } + return validateCode == 0; + } + + private static int GetValidateErrorCode(SubsystemOperationTime item) + { + if (item.DateStart > item.DateEnd) + return -1; + if ((item.DateEnd - item.DateStart).TotalHours > 48) + return -2; + if (item.DepthEnd < item.DepthStart) + return -3; + if (item.DepthEnd - item.DepthStart > 2000d) + return -4; + if (item.DepthEnd < 0d) + return -5; + if (item.DepthStart < 0d) + return -6; + if (item.DepthEnd > 24_0000d) + return -5; + if (item.DepthStart > 24_0000d) + return -6; + return 0; + } + + private static async Task GetInterpolation(IAsbCloudDbContext db, int idTelemetry, DateTimeOffset dateBegin, DateTimeOffset dateEnd, CancellationToken token) + { + var dataDepthFromSaub = await db.TelemetryDataSaub + .Where(d => d.IdTelemetry == idTelemetry) + .Where(d => d.DateTime >= dateBegin) + .Where(d => d.DateTime <= dateEnd) + .Where(d => d.WellDepth != null) + .Where(d => d.WellDepth > 0) + .GroupBy(d => Math.Ceiling(d.WellDepth ?? 0 * 10)) + .Select(g => new { + DateMin = g.Min(d => d.DateTime), + DepthMin = g.Min(d => d.WellDepth) ?? 0, + }) + .OrderBy(i => i.DateMin) + .ToArrayAsync(token); + + if (!dataDepthFromSaub.Any()) + return null; + + var depthInterpolation = new DepthInterpolation(dataDepthFromSaub.Select(i=>(i.DateMin, i.DepthMin))); + return depthInterpolation; } } - - internal class DataRow - { - public DateTimeOffset Date { get; set; } - public short? Mode { get; set; } - public float? Depth { get; set; } - public short? State { get; set; } - } - #nullable disable } From f49245f9cf349b3ec3249e50a9ead5afa93dd106 Mon Sep 17 00:00:00 2001 From: "ai.astrakhantsev" Date: Wed, 19 Oct 2022 14:36:01 +0500 Subject: [PATCH 9/9] fix --- .../Services/Subsystems/DepthInterpolation.cs | 1 - .../Subsystems/SubsystemOperationTimeBackgroundService.cs | 6 ++---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs index 750786c4..2a2fa8eb 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/DepthInterpolation.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Linq; namespace AsbCloudInfrastructure.Services.Subsystems.Utils { diff --git a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs index 9f77a77e..ddcdbca1 100644 --- a/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/Subsystems/SubsystemOperationTimeBackgroundService.cs @@ -1,11 +1,9 @@ using AsbCloudDb.Model; using AsbCloudDb.Model.Subsystems; using AsbCloudInfrastructure.Services.Subsystems.Utils; -using iText.Forms.Xfdf; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Hosting; -using Npgsql; using System; using System.Collections.Generic; using System.Data; @@ -340,9 +338,9 @@ namespace AsbCloudInfrastructure.Services.Subsystems if (item.DepthStart < 0d) return -6; if (item.DepthEnd > 24_0000d) - return -5; + return -7; if (item.DepthStart > 24_0000d) - return -6; + return -8; return 0; }