forked from ddrilling/AsbCloudServer
Корректировка логики записи наработок подсистем Спин и Торк
This commit is contained in:
parent
7b900c1f9d
commit
684a56a100
@ -1,5 +1,6 @@
|
|||||||
using AsbCloudDb.Model;
|
using AsbCloudDb.Model;
|
||||||
using AsbCloudDb.Model.Subsystems;
|
using AsbCloudDb.Model.Subsystems;
|
||||||
|
using AsbCloudInfrastructure.Services.Subsystems.Utils;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.Extensions.Configuration;
|
using Microsoft.Extensions.Configuration;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
@ -7,6 +8,7 @@ using Npgsql;
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Data;
|
using System.Data;
|
||||||
|
using System.Data.Common;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
@ -225,17 +227,18 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
|
|
||||||
return null;
|
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;
|
var idTelemetryParam = new NpgsqlParameter("@idTelemetry", paramIdTelemetry);
|
||||||
for (int i = 0; i < rowsSaub.Count; i++)
|
var beginParam = new NpgsqlParameter("@begin", paramDate);
|
||||||
{
|
await db.Database.OpenConnectionAsync(token);
|
||||||
if (date <= rowsSaub[i].Date)
|
var command = db.Database.GetDbConnection().CreateCommand();
|
||||||
depth = (float)rowsSaub[i].Depth;
|
command.CommandText = query;
|
||||||
}
|
command.Parameters.Add(idTelemetryParam);
|
||||||
return depth;
|
command.Parameters.Add(beginParam);
|
||||||
|
var result = await command.ExecuteReaderAsync(token);
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
var querySpin =
|
var querySpin =
|
||||||
$"select " +
|
$"select " +
|
||||||
$" tspin.date, " +
|
$" tspin.date, " +
|
||||||
@ -253,47 +256,79 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
$" 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_pre is null or state_pre is null or mode != mode_pre or state != state_pre " +
|
||||||
$"order by date;";
|
$"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)
|
var saubDepthAndDate = await db.TelemetryDataSaub.Where(s => s.IdTelemetry == idTelemetry & s.DateTime >= begin)
|
||||||
.Select(s => new DataRow()
|
.Select(s => new DataRow()
|
||||||
{
|
{
|
||||||
Date = s.DateTime,
|
Date = s.DateTime,
|
||||||
Depth = s.WellDepth
|
Depth = s.WellDepth
|
||||||
})
|
})
|
||||||
.OrderBy(s => s.Date)
|
|
||||||
.ToListAsync(token);
|
.ToListAsync(token);
|
||||||
var idTelemetryParam = new NpgsqlParameter("@idTelemetry", idTelemetry);
|
|
||||||
var beginParam = new NpgsqlParameter("@begin", begin);
|
|
||||||
|
|
||||||
await db.Database.OpenConnectionAsync(token);
|
var resultSpin = await GetResultFromQueryAsync(querySpin,begin,idTelemetry,db,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 subsystemOperationTime = new List<SubsystemOperationTime>(32);
|
var subsystemOperationTime = new List<SubsystemOperationTime>(32);
|
||||||
var dataRowList = new List<DataRow>();
|
var dataSpinList = new List<DataRow>();
|
||||||
while (result.Read())
|
while (resultSpin.Read())
|
||||||
{
|
{
|
||||||
var dateRowItem = new DataRow()
|
var dateRowItem = new DataRow()
|
||||||
{
|
{
|
||||||
Date = result.GetFieldValue<DateTimeOffset>(0),
|
Date = resultSpin.GetFieldValue<DateTimeOffset>(0),
|
||||||
Mode = result.GetFieldValue<short?>(1),
|
Mode = resultSpin.GetFieldValue<short?>(1),
|
||||||
State = result.GetFieldValue<short?>(2)
|
State = resultSpin.GetFieldValue<short?>(2)
|
||||||
};
|
};
|
||||||
dateRowItem.Depth = GetDepthFromSaub(saubDepthAndDate, dateRowItem.Date);
|
dataSpinList.Add(dateRowItem);
|
||||||
dataRowList.Add(dateRowItem);
|
|
||||||
}
|
}
|
||||||
if (dataRowList.Any()==true)
|
await resultSpin.DisposeAsync();
|
||||||
{
|
|
||||||
var mode = dataRowList[0].Mode;
|
|
||||||
var state = dataRowList[0].State;
|
//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 idSubsystem = GetSubsytemId(mode, state);
|
||||||
var dateStart = dataRowList[0].Date;
|
var dateStart = dataSpinList[0].Date;
|
||||||
var depthStart = dataRowList[0].Depth;
|
var depthStart = interp.GetDepth(dateStart);
|
||||||
for (int i = 1; i<dataRowList.Count;i++)
|
for (int i = 1; i < dataSpinList.Count; i++)
|
||||||
{
|
{
|
||||||
var dateEnd = dataRowList[1].Date;
|
var dateEnd = dataSpinList[i].Date;
|
||||||
var depthEnd = dataRowList[1].Depth;
|
var depthEnd = interp.GetDepth(dateEnd);
|
||||||
if (idSubsystem.HasValue)
|
if (idSubsystem.HasValue)
|
||||||
{
|
{
|
||||||
var operationTimeItem = new SubsystemOperationTime()
|
var operationTimeItem = new SubsystemOperationTime()
|
||||||
@ -302,17 +337,17 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
IdSubsystem = idSubsystem.Value,
|
IdSubsystem = idSubsystem.Value,
|
||||||
DateStart = dateStart,
|
DateStart = dateStart,
|
||||||
DateEnd = dateEnd,
|
DateEnd = dateEnd,
|
||||||
|
DepthEnd = depthEnd,
|
||||||
DepthStart = depthStart,
|
DepthStart = depthStart,
|
||||||
DepthEnd = depthEnd
|
|
||||||
};
|
};
|
||||||
subsystemOperationTime.Add(operationTimeItem);
|
subsystemOperationTime.Add(operationTimeItem);
|
||||||
}
|
}
|
||||||
mode = dataRowList[i].Mode;
|
mode = dataSpinList[i].Mode;
|
||||||
state = dataRowList[i].State;
|
state = dataSpinList[i].State;
|
||||||
idSubsystem = GetSubsytemId(mode, state);
|
idSubsystem = GetSubsytemId(mode, state);
|
||||||
dateStart = dateEnd;
|
dateStart = dateEnd;
|
||||||
depthStart = depthEnd;
|
depthStart = depthEnd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return subsystemOperationTime;
|
return subsystemOperationTime;
|
||||||
}
|
}
|
||||||
@ -323,8 +358,8 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
public DateTimeOffset Date { get; set; }
|
public DateTimeOffset Date { get; set; }
|
||||||
public short? Mode { get; set; }
|
public short? Mode { get; set; }
|
||||||
public float? Depth { get; set; }
|
public float? Depth { get; set; }
|
||||||
public short? State { get; set; }
|
public short? State { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
#nullable disable
|
#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