forked from ddrilling/AsbCloudServer
Корректировка логики записи наработок подсистем Спин и Торк
This commit is contained in:
parent
7b900c1f9d
commit
684a56a100
@ -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<DataRow> rowsSaub, DateTimeOffset date)
|
||||
static async Task<DbDataReader> 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<SubsystemOperationTime>(32);
|
||||
var dataRowList = new List<DataRow>();
|
||||
while (result.Read())
|
||||
var dataSpinList = new List<DataRow>();
|
||||
while (resultSpin.Read())
|
||||
{
|
||||
var dateRowItem = new DataRow()
|
||||
{
|
||||
Date = result.GetFieldValue<DateTimeOffset>(0),
|
||||
Mode = result.GetFieldValue<short?>(1),
|
||||
State = result.GetFieldValue<short?>(2)
|
||||
};
|
||||
dateRowItem.Depth = GetDepthFromSaub(saubDepthAndDate, dateRowItem.Date);
|
||||
dataRowList.Add(dateRowItem);
|
||||
Date = resultSpin.GetFieldValue<DateTimeOffset>(0),
|
||||
Mode = resultSpin.GetFieldValue<short?>(1),
|
||||
State = resultSpin.GetFieldValue<short?>(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<DateTimeOffset>(0),
|
||||
// resultDepthFromSaub.GetFieldValue<float?>(1),
|
||||
// resultDepthFromSaub.GetFieldValue<DateTimeOffset>(2),
|
||||
// resultDepthFromSaub.GetFieldValue<float?>(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<dataRowList.Count;i++)
|
||||
var dateStart = dataSpinList[0].Date;
|
||||
var depthStart = interp.GetDepth(dateStart);
|
||||
for (int i = 1; i < dataSpinList.Count; i++)
|
||||
{
|
||||
var dateEnd = dataRowList[1].Date;
|
||||
var depthEnd = dataRowList[1].Depth;
|
||||
var dateEnd = dataSpinList[i].Date;
|
||||
var depthEnd = interp.GetDepth(dateEnd);
|
||||
if (idSubsystem.HasValue)
|
||||
{
|
||||
var operationTimeItem = new SubsystemOperationTime()
|
||||
@ -302,17 +337,17 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
||||
IdSubsystem = idSubsystem.Value,
|
||||
DateStart = dateStart,
|
||||
DateEnd = dateEnd,
|
||||
DepthEnd = depthEnd,
|
||||
DepthStart = depthStart,
|
||||
DepthEnd = depthEnd
|
||||
};
|
||||
subsystemOperationTime.Add(operationTimeItem);
|
||||
}
|
||||
mode = dataRowList[i].Mode;
|
||||
state = dataRowList[i].State;
|
||||
mode = dataSpinList[i].Mode;
|
||||
state = dataSpinList[i].State;
|
||||
idSubsystem = GetSubsytemId(mode, state);
|
||||
dateStart = dateEnd;
|
||||
depthStart = depthEnd;
|
||||
}
|
||||
}
|
||||
}
|
||||
return subsystemOperationTime;
|
||||
}
|
||||
@ -323,8 +358,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
||||
public DateTimeOffset Date { get; set; }
|
||||
public short? Mode { get; set; }
|
||||
public float? Depth { get; set; }
|
||||
public short? State { get; set; }
|
||||
}
|
||||
public short? State { get; set; }
|
||||
}
|
||||
|
||||
#nullable disable
|
||||
}
|
||||
|
49
AsbCloudInfrastructure/Services/Subsystems/Utils/Interp.cs
Normal file
49
AsbCloudInfrastructure/Services/Subsystems/Utils/Interp.cs
Normal file
@ -0,0 +1,49 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user