forked from ddrilling/AsbCloudServer
#6370638 refactored
This commit is contained in:
parent
abd0008615
commit
5a2435795a
@ -7,35 +7,56 @@ namespace AsbCloudInfrastructure.Services.Subsystems.Utils
|
|||||||
#nullable enable
|
#nullable enable
|
||||||
internal class DepthInterpolation
|
internal class DepthInterpolation
|
||||||
{
|
{
|
||||||
private List<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection;
|
private readonly TimeSpan maxDeltaDate = TimeSpan.FromHours(12);
|
||||||
(DateTimeOffset x, float y) lastValue = default;
|
private readonly IEnumerator<(DateTimeOffset x, float y)> enumerator;
|
||||||
public DepthInterpolation(List<(DateTimeOffset dateMin, float depthMin, DateTimeOffset dateMax, float depthMax)> collection)
|
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)
|
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)
|
p0 = enumerator.Current;
|
||||||
{
|
canMoveNext = enumerator.MoveNext();
|
||||||
lastValue.x = date;
|
|
||||||
lastValue.y = CalcValue(collection[i], (date - collection[i].dateMin).TotalSeconds);
|
|
||||||
}
|
}
|
||||||
}
|
return CalcValue(date);
|
||||||
return lastValue.y;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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;
|
var a = (p1.y - p0.y) / (p1.x - p0.x).TotalSeconds;
|
||||||
return (float)d;
|
var b = p0.y;
|
||||||
|
var x = (date - p0.x).TotalSeconds;
|
||||||
|
var y = a * x + b;
|
||||||
|
return (float)y;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#nullable disable
|
#nullable disable
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using AsbCloudDb.Model.Subsystems;
|
using AsbCloudDb.Model.Subsystems;
|
||||||
using AsbCloudInfrastructure.Services.Subsystems.Utils;
|
using AsbCloudInfrastructure.Services.Subsystems.Utils;
|
||||||
|
using iText.Forms.Xfdf;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
@ -25,6 +26,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
private const int idSubsytemSpinMaster = 65536;
|
private const int idSubsytemSpinMaster = 65536;
|
||||||
private const int idSubsytemAkb = 1;
|
private const int idSubsytemAkb = 1;
|
||||||
private const int idSubsytemMse = 2;
|
private const int idSubsytemMse = 2;
|
||||||
|
|
||||||
public SubsystemOperationTimeBackgroundService(IConfiguration configuration)
|
public SubsystemOperationTimeBackgroundService(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
connectionString = configuration.GetConnectionString("DefaultConnection");
|
connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||||
@ -108,6 +110,25 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
}
|
}
|
||||||
return affected;
|
return affected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static async Task<DbDataReader> 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<IEnumerable<SubsystemOperationTime>> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
||||||
{
|
{
|
||||||
static bool isSubsytemAkb(short? mode)
|
static bool isSubsytemAkb(short? mode)
|
||||||
@ -128,47 +149,6 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static List<SubsystemOperationTime> GetSubsystemOperationTimes (List<DataRow> dataRows, Predicate<DataRow> satisfyCondition, int idSubsystem, int idTelemetry)
|
|
||||||
{
|
|
||||||
if (dataRows is null)
|
|
||||||
return new List<SubsystemOperationTime>();
|
|
||||||
if (dataRows.Count < 2)
|
|
||||||
return new List<SubsystemOperationTime>();
|
|
||||||
var listSubsystemOperationTime = new List<SubsystemOperationTime>();
|
|
||||||
var foundSubsystem = satisfyCondition(dataRows[0]);
|
|
||||||
var dateStart = dataRows[0].Date;
|
|
||||||
var depthStart = dataRows[0].Depth;
|
|
||||||
|
|
||||||
for (int i = 1; i<dataRows.Count; i++)
|
|
||||||
{
|
|
||||||
var dateEnd = dataRows[i].Date;
|
|
||||||
var depthEnd = dataRows[i].Depth;
|
|
||||||
var currentSatisfy = satisfyCondition(dataRows[i]);
|
|
||||||
var endSubsystem = !currentSatisfy;
|
|
||||||
if (foundSubsystem && endSubsystem)
|
|
||||||
{
|
|
||||||
var operationTimeItem = new SubsystemOperationTime()
|
|
||||||
{
|
|
||||||
IdTelemetry = idTelemetry,
|
|
||||||
IdSubsystem = idSubsystem,
|
|
||||||
DateStart = dateStart,
|
|
||||||
DateEnd = dateEnd,
|
|
||||||
DepthStart = depthStart,
|
|
||||||
DepthEnd = depthEnd
|
|
||||||
};
|
|
||||||
listSubsystemOperationTime.Add(operationTimeItem);
|
|
||||||
foundSubsystem = false;
|
|
||||||
}
|
|
||||||
if (currentSatisfy && !foundSubsystem)
|
|
||||||
{
|
|
||||||
dateStart = dateEnd;
|
|
||||||
foundSubsystem = true;
|
|
||||||
depthStart = depthEnd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return listSubsystemOperationTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
var query =
|
var query =
|
||||||
$"select tt.date, tt.mode, tt.well_depth, tt.mse_state " +
|
$"select tt.date, tt.mode, tt.well_depth, tt.mse_state " +
|
||||||
$"from ( " +
|
$"from ( " +
|
||||||
@ -177,45 +157,77 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
$" mode, " +
|
$" mode, " +
|
||||||
$" mse_state, " +
|
$" mse_state, " +
|
||||||
$" well_depth, " +
|
$" well_depth, " +
|
||||||
$" lag(mode,1) over (order by date) as mode_prev " +
|
$" lag(mode,1) over (order by date) as mode_lag, " +
|
||||||
|
$" lead(mode,1) over (order by date) as mode_lead " +
|
||||||
$" from t_telemetry_data_saub " +
|
$" from t_telemetry_data_saub " +
|
||||||
$" where id_telemetry = @idTelemetry " +
|
$" where id_telemetry = {idTelemetry} and well_depth is not null and well_depth > 0" +
|
||||||
$" order by date ) as tt " +
|
$" 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;";
|
$"order by tt.date;";
|
||||||
|
|
||||||
var idTelemetryParam = new NpgsqlParameter("@idTelemetry", idTelemetry);
|
using var result = await ExecuteReaderAsync(db, query, token);
|
||||||
var beginParam = new NpgsqlParameter("@begin", begin);
|
|
||||||
|
|
||||||
await db.Database.OpenConnectionAsync(token);
|
var subsystemsOperationTimes = new List<SubsystemOperationTime>();
|
||||||
using var command = db.Database.GetDbConnection().CreateCommand();
|
|
||||||
command.CommandText = query;
|
|
||||||
command.Parameters.Add(idTelemetryParam);
|
|
||||||
command.Parameters.Add(beginParam);
|
|
||||||
|
|
||||||
using var result = await command.ExecuteReaderAsync(token);
|
(bool isEnable, DateTimeOffset date, float depth) akbPre = default;
|
||||||
|
(bool isEnable, DateTimeOffset date, float depth) msePre = default;
|
||||||
|
|
||||||
var subsystemOperationTime = new List<SubsystemOperationTime>();
|
|
||||||
var dataRowList = new List<DataRow>();
|
|
||||||
while (result.Read())
|
while (result.Read())
|
||||||
{
|
{
|
||||||
var dateRowItem = new DataRow()
|
var mode = result.GetFieldValue<short?>(1);
|
||||||
|
var state = result.GetFieldValue<short?>(3);
|
||||||
|
|
||||||
|
var isAkbEnable = isSubsytemAkb(mode);
|
||||||
|
var isMseEnable = IsSubsystemMse(state);
|
||||||
|
var date = result.GetFieldValue<DateTimeOffset>(0);
|
||||||
|
var depth = result.GetFieldValue<float>(2);
|
||||||
|
|
||||||
|
if (!akbPre.isEnable && isAkbEnable)
|
||||||
{
|
{
|
||||||
Date = result.GetFieldValue<DateTimeOffset>(0),
|
akbPre = (true, date, depth);
|
||||||
Mode = result.GetFieldValue<short?>(1),
|
|
||||||
Depth = result.GetFieldValue<float?>(2),
|
|
||||||
State = result.GetFieldValue<short?>(3)
|
|
||||||
};
|
|
||||||
dataRowList.Add(dateRowItem);
|
|
||||||
}
|
}
|
||||||
var akbOperationTimes = GetSubsystemOperationTimes(dataRowList, d => isSubsytemAkb(d.Mode), idSubsytemAkb, idTelemetry);
|
else if (akbPre.isEnable && !isAkbEnable)
|
||||||
var mseOperationTimes = GetSubsystemOperationTimes(dataRowList, d => IsSubsystemMse(d.State), idSubsytemMse, idTelemetry);
|
{
|
||||||
subsystemOperationTime.AddRange(akbOperationTimes);
|
var subsystemOperationTime = new SubsystemOperationTime
|
||||||
subsystemOperationTime.AddRange(mseOperationTimes);
|
{
|
||||||
return subsystemOperationTime;
|
IdTelemetry = idTelemetry,
|
||||||
|
IdSubsystem = idSubsytemAkb,
|
||||||
|
DateStart = akbPre.date,
|
||||||
|
DateEnd = date,
|
||||||
|
DepthStart = akbPre.depth,
|
||||||
|
DepthEnd = depth,
|
||||||
|
};
|
||||||
|
if (IsValid(subsystemOperationTime))
|
||||||
|
subsystemsOperationTimes.Add(subsystemOperationTime);
|
||||||
|
akbPre.isEnable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async Task<IEnumerable<SubsystemOperationTime>?> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return subsystemsOperationTimes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
||||||
{
|
{
|
||||||
static int? GetSubsytemId(short? mode, int? state)
|
static int? GetSubsytemId(short? mode, int? state)
|
||||||
{
|
{
|
||||||
@ -227,18 +239,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
static async Task<DbDataReader> 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 =
|
var querySpin =
|
||||||
$"select " +
|
$"select " +
|
||||||
$" tspin.date, " +
|
$" tspin.date, " +
|
||||||
@ -248,104 +249,125 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
$" select " +
|
$" select " +
|
||||||
$" date, " +
|
$" date, " +
|
||||||
$" mode, " +
|
$" 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, " +
|
$" 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 " +
|
$" 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 " +
|
$" 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;";
|
$"order by date;";
|
||||||
|
|
||||||
var queryDepthFromSaub =
|
var rows = new List<(int? IdSubsystem, DateTimeOffset Date)>(32);
|
||||||
"select " +
|
{
|
||||||
" min(\"date\") as min_date, " +
|
using var resultSpin = await ExecuteReaderAsync(db, querySpin, token);
|
||||||
" min(well_depth) as min_well_depth, " +
|
int? idSubsystemLast = null;
|
||||||
" 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<SubsystemOperationTime>(32);
|
|
||||||
var dataSpinList = new List<DataRow>();
|
|
||||||
while (resultSpin.Read())
|
while (resultSpin.Read())
|
||||||
{
|
{
|
||||||
var dateRowItem = new DataRow()
|
var mode = resultSpin.GetFieldValue<short?>(1);
|
||||||
|
var state = resultSpin.GetFieldValue<short?>(2);
|
||||||
|
var idSubsystem = GetSubsytemId(mode, state);
|
||||||
|
if(idSubsystemLast != idSubsystem)
|
||||||
{
|
{
|
||||||
Date = resultSpin.GetFieldValue<DateTimeOffset>(0),
|
idSubsystemLast = idSubsystem;
|
||||||
Mode = resultSpin.GetFieldValue<short?>(1),
|
var date = resultSpin.GetFieldValue<DateTimeOffset>(0);
|
||||||
State = resultSpin.GetFieldValue<short?>(2)
|
rows.Add((idSubsystem, date));
|
||||||
};
|
}
|
||||||
dataSpinList.Add(dateRowItem);
|
|
||||||
}
|
}
|
||||||
await resultSpin.DisposeAsync();
|
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<DateTimeOffset>(0),
|
|
||||||
resultDepthFromSaub.GetFieldValue<float>(1),
|
|
||||||
resultDepthFromSaub.GetFieldValue<DateTimeOffset>(2),
|
|
||||||
resultDepthFromSaub.GetFieldValue<float>(3)
|
|
||||||
);
|
|
||||||
dataDepthFromSaub.Add(dateRowItem);
|
|
||||||
}
|
}
|
||||||
dataDepthFromSaub.OrderBy(o => o.dateMin);
|
|
||||||
var depthInterpolation = new DepthInterpolation(dataDepthFromSaub);
|
if (rows.Count < 2)
|
||||||
foreach (var item in dataSpinList)
|
return Enumerable.Empty<SubsystemOperationTime>();
|
||||||
|
|
||||||
|
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<SubsystemOperationTime>();
|
||||||
|
|
||||||
|
var subsystemsOperationTimes = new List<SubsystemOperationTime>(32);
|
||||||
|
|
||||||
|
for (int i = 1; i < rows.Count; i++)
|
||||||
{
|
{
|
||||||
item.Depth = depthInterpolation.GetDepth(item.Date);
|
var r0 = rows[i - 1];
|
||||||
}
|
var r1 = rows[i];
|
||||||
if (dataSpinList.Any())
|
if (r0.IdSubsystem is not null && r0.IdSubsystem != r1.IdSubsystem)
|
||||||
{
|
{
|
||||||
var mode = dataSpinList[0].Mode;
|
var subsystemOperationTime = new SubsystemOperationTime()
|
||||||
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 operationTimeItem = new SubsystemOperationTime()
|
|
||||||
{
|
{
|
||||||
IdTelemetry = idTelemetry,
|
IdTelemetry = idTelemetry,
|
||||||
IdSubsystem = idSubsystem.Value,
|
IdSubsystem = r0.IdSubsystem.Value,
|
||||||
DateStart = dateStart,
|
DateStart = r0.Date,
|
||||||
DateEnd = dateEnd,
|
DateEnd = r1.Date,
|
||||||
DepthEnd = depthEnd,
|
DepthStart = depthInterpolation.GetDepth(r0.Date),
|
||||||
DepthStart = depthStart
|
DepthEnd = depthInterpolation.GetDepth(r1.Date),
|
||||||
};
|
};
|
||||||
subsystemOperationTime.Add(operationTimeItem);
|
|
||||||
}
|
if (IsValid(subsystemOperationTime))
|
||||||
mode = dataSpinList[i].Mode;
|
subsystemsOperationTimes.Add(subsystemOperationTime);
|
||||||
state = dataSpinList[i].State;
|
|
||||||
idSubsystem = GetSubsytemId(mode, state);
|
|
||||||
dateStart = dateEnd;
|
|
||||||
depthStart = depthEnd;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return subsystemOperationTime;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal class DataRow
|
return subsystemsOperationTimes;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsValid(SubsystemOperationTime item)
|
||||||
{
|
{
|
||||||
public DateTimeOffset Date { get; set; }
|
var validateCode = GetValidateErrorCode(item);
|
||||||
public short? Mode { get; set; }
|
if (validateCode != 0)
|
||||||
public float? Depth { get; set; }
|
{
|
||||||
public short? State { get; set; }
|
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<DepthInterpolation?> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
#nullable disable
|
#nullable disable
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user