forked from ddrilling/AsbCloudServer
test new rep
This commit is contained in:
parent
dcda244e3c
commit
babd1076a4
@ -19,7 +19,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
private readonly string connectionString;
|
private readonly string connectionString;
|
||||||
private readonly TimeSpan period = TimeSpan.FromHours(1);
|
private readonly TimeSpan period = TimeSpan.FromHours(1);
|
||||||
private const int idSubsytemTorqueMaster = 65537;
|
private const int idSubsytemTorqueMaster = 65537;
|
||||||
private const int idSubsytemSpinMaster = 65536;
|
private const int idSubsytemSpinMaster = 65536;
|
||||||
public SubsystemOperationTimeBackgroundService(IConfiguration configuration)
|
public SubsystemOperationTimeBackgroundService(IConfiguration configuration)
|
||||||
{
|
{
|
||||||
connectionString = configuration.GetConnectionString("DefaultConnection");
|
connectionString = configuration.GetConnectionString("DefaultConnection");
|
||||||
@ -51,7 +51,7 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
ms = ms > 100 ? ms : 100;
|
ms = ms > 100 ? ms : 100;
|
||||||
await Task.Delay(ms, token).ConfigureAwait(false);
|
await Task.Delay(ms, token).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public override async Task StopAsync(CancellationToken token)
|
public override async Task StopAsync(CancellationToken token)
|
||||||
{
|
{
|
||||||
@ -101,62 +101,48 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
}
|
}
|
||||||
return affected;
|
return affected;
|
||||||
}
|
}
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
var query = db.TelemetryDataSaub
|
var dateStart = db.TelemetryDataSaub
|
||||||
.AsNoTracking()
|
.AsNoTracking()
|
||||||
.Where(d => d.IdTelemetry == idTelemetry)
|
.Where(d => (d.IdTelemetry == idTelemetry) & (d.DateTime >= begin))
|
||||||
.Select(d => new
|
.Min(d => d.DateTime);
|
||||||
{
|
var depthStart = db.TelemetryDataSaub
|
||||||
DateTime = d.DateTime,
|
.AsNoTracking()
|
||||||
Mode = d.Mode,
|
.Where(d => (d.IdTelemetry == idTelemetry) & (d.DateTime >= begin))
|
||||||
Depth = d.WellDepth
|
.Min(d => d.WellDepth);
|
||||||
})
|
|
||||||
.OrderBy(d => d.DateTime);
|
var operationTimeSpinWithDepth =
|
||||||
var take = 4 * 86_400; // 4 дня
|
$"select tt.date, tt.mode, tt.mode_prev, tt.well_depth" +
|
||||||
var startDate = begin;
|
$" from (select date, mode, well_depth, lag(mode,1) over (order by date) as mode_prev" +
|
||||||
|
$" from t_telemetry_data_saub where id_telemetry = {idTelemetry}) as tt" +
|
||||||
|
$" where tt.mode != tt.mode_prev and tt.date >='{begin}' order by tt.date;";
|
||||||
|
using var command = db.Database.GetDbConnection().CreateCommand();
|
||||||
|
command.CommandText = operationTimeSpinWithDepth;
|
||||||
|
command.
|
||||||
|
db.Database.OpenConnection();
|
||||||
|
using var result = await command.ExecuteReaderAsync(token);
|
||||||
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
|
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
|
||||||
while (true)
|
if (result.HasRows)
|
||||||
{
|
{
|
||||||
var data = await query
|
while (result.Read())
|
||||||
.Where(d => d.DateTime > startDate)
|
|
||||||
.Take(take)
|
|
||||||
.ToArrayAsync(token);
|
|
||||||
var firstItem = data.FirstOrDefault();
|
|
||||||
if (firstItem is null)
|
|
||||||
break;
|
|
||||||
short? mode = firstItem.Mode;
|
|
||||||
DateTimeOffset dateBegin = firstItem.DateTime;
|
|
||||||
float? depthStart = firstItem.Depth;
|
|
||||||
for (int i = 1; i < data.Length; i++)
|
|
||||||
{
|
{
|
||||||
if (data[i].Mode != mode)
|
var itemEntity = new SubsystemOperationTime()
|
||||||
{
|
{
|
||||||
|
IdTelemetry = idTelemetry,
|
||||||
var operationTimeItem = new SubsystemOperationTime()
|
DateStart = dateStart,
|
||||||
{
|
DateEnd = result.GetFieldValue<DateTimeOffset>(0),
|
||||||
IdTelemetry = idTelemetry,
|
DepthStart = depthStart,
|
||||||
DateStart = dateBegin,
|
DepthEnd = result.GetFieldValue<float>(3),
|
||||||
DateEnd = data[i - 1].DateTime,
|
IdSubsystem = result.GetFieldValue<short>(2) + 1
|
||||||
DepthStart = depthStart,
|
};
|
||||||
DepthEnd = data[i - 1].Depth
|
dateStart = itemEntity.DateEnd;
|
||||||
};
|
depthStart = itemEntity.DepthStart;
|
||||||
if (mode.HasValue)
|
resultSubsystemOperationTime.Add(itemEntity);
|
||||||
{
|
|
||||||
operationTimeItem.IdSubsystem = (int)mode + 1;
|
|
||||||
resultSubsystemOperationTime.Add(operationTimeItem);
|
|
||||||
}
|
|
||||||
mode = data[i].Mode;
|
|
||||||
dateBegin = data[i].DateTime;
|
|
||||||
depthStart = data[i].Depth;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
startDate = data.Last().DateTime;
|
|
||||||
}
|
}
|
||||||
return resultSubsystemOperationTime;
|
return resultSubsystemOperationTime;
|
||||||
}
|
}
|
||||||
private static async Task<IEnumerable<SubsystemOperationTime>?> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
private static async Task<IEnumerable<SubsystemOperationTime>?> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
||||||
{
|
{
|
||||||
Predicate<int> isSubsystemTorqueState = (int state) => state == 7;
|
Predicate<int> isSubsystemTorqueState = (int state) => state == 7;
|
||||||
@ -168,14 +154,14 @@ 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}') as tspin " +
|
$"lag(state, 1) over (order by \"date\") as state_pre from t_telemetry_data_spin where id_telemetry = {idTelemetry} and date >= '{begin}') as tspin " +
|
||||||
$"join (select \"date\", well_depth from t_telemetry_data_saub where id_telemetry = {idTelemetry} and date >= '{begin}') as tsaub " +
|
$"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) " +
|
$"on EXTRACT(EPOCH from tspin.date) = EXTRACT(EPOCH from tsaub.date) " +
|
||||||
$"where mode!=mode_pre or state != state_pre order by \"date\";";
|
$"where mode!=mode_pre or state != state_pre order by \"date\";";
|
||||||
using var command = db.Database.GetDbConnection().CreateCommand();
|
using var command = db.Database.GetDbConnection().CreateCommand();
|
||||||
command.CommandText = operationTimeSpinWithDepth;
|
command.CommandText = operationTimeSpinWithDepth;
|
||||||
db.Database.OpenConnection();
|
db.Database.OpenConnection();
|
||||||
using var result = await command.ExecuteReaderAsync(token);
|
using var result = await command.ExecuteReaderAsync(token);
|
||||||
var telemetryOpearationSpinSubsystems = new List<SubsystemsSpinWithDepth>();
|
var telemetryOpearationSpinSubsystems = new List<SubsystemsSpinWithDepth>();
|
||||||
if (result.HasRows)
|
if (result.HasRows)
|
||||||
{
|
{
|
||||||
while (result.Read())
|
while (result.Read())
|
||||||
{
|
{
|
||||||
var itemEntity = new SubsystemsSpinWithDepth()
|
var itemEntity = new SubsystemsSpinWithDepth()
|
||||||
@ -194,51 +180,41 @@ namespace AsbCloudInfrastructure.Services.Subsystems
|
|||||||
{
|
{
|
||||||
itemEntity.IdSubsystem = subsystemId.Value;
|
itemEntity.IdSubsystem = subsystemId.Value;
|
||||||
telemetryOpearationSpinSubsystems.Add(itemEntity);
|
telemetryOpearationSpinSubsystems.Add(itemEntity);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
var take = 4 * 86_400; // 4 дня
|
|
||||||
var startDate = begin;
|
|
||||||
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
|
|
||||||
while (true)
|
|
||||||
{
|
|
||||||
var data = telemetryOpearationSpinSubsystems
|
|
||||||
.Where(d => d.Date > startDate)
|
|
||||||
.Take(take)
|
|
||||||
.ToArray();
|
|
||||||
var firstItem = data.FirstOrDefault();
|
|
||||||
if (firstItem is null)
|
|
||||||
break;
|
|
||||||
int idSubsystem = firstItem.IdSubsystem;
|
|
||||||
DateTimeOffset dateBegin = firstItem.Date;
|
|
||||||
float? depthStart = firstItem.Depth;
|
|
||||||
for (int i = 1; i < data.Length; i++)
|
|
||||||
{
|
|
||||||
if (data[i].IdSubsystem != idSubsystem)
|
|
||||||
{
|
|
||||||
var operationTimeItem = new SubsystemOperationTime()
|
|
||||||
{
|
|
||||||
IdTelemetry = idTelemetry,
|
|
||||||
DateStart = dateBegin,
|
|
||||||
IdSubsystem = data[i - 1].IdSubsystem,
|
|
||||||
DateEnd = data[i].Date,
|
|
||||||
DepthStart = depthStart,
|
|
||||||
DepthEnd = data[i].Depth
|
|
||||||
|
|
||||||
};
|
|
||||||
dateBegin = data[i].Date;
|
|
||||||
depthStart = data[i].Depth;
|
|
||||||
idSubsystem = data[i].IdSubsystem;
|
|
||||||
if (data[i - 1].IdSubsystem != 0)
|
|
||||||
{
|
|
||||||
resultSubsystemOperationTime.Add(operationTimeItem);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
startDate = data.LastOrDefault().Date;
|
}
|
||||||
|
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
|
||||||
|
var firstItem = telemetryOpearationSpinSubsystems.FirstOrDefault();
|
||||||
|
if (firstItem is null)
|
||||||
|
return null;
|
||||||
|
int idSubsystem = firstItem.IdSubsystem;
|
||||||
|
DateTimeOffset dateBegin = firstItem.Date;
|
||||||
|
float? depthStart = firstItem.Depth;
|
||||||
|
for (int i = 1; i < telemetryOpearationSpinSubsystems.Count; i++)
|
||||||
|
{
|
||||||
|
if (telemetryOpearationSpinSubsystems[i].IdSubsystem != idSubsystem)
|
||||||
|
{
|
||||||
|
var operationTimeItem = new SubsystemOperationTime()
|
||||||
|
{
|
||||||
|
IdTelemetry = idTelemetry,
|
||||||
|
DateStart = dateBegin,
|
||||||
|
IdSubsystem = telemetryOpearationSpinSubsystems[i - 1].IdSubsystem,
|
||||||
|
DateEnd = telemetryOpearationSpinSubsystems[i].Date,
|
||||||
|
DepthStart = depthStart,
|
||||||
|
DepthEnd = telemetryOpearationSpinSubsystems[i].Depth
|
||||||
|
|
||||||
|
};
|
||||||
|
dateBegin = telemetryOpearationSpinSubsystems[i].Date;
|
||||||
|
depthStart = telemetryOpearationSpinSubsystems[i].Depth;
|
||||||
|
idSubsystem = telemetryOpearationSpinSubsystems[i].IdSubsystem;
|
||||||
|
if (telemetryOpearationSpinSubsystems[i - 1].IdSubsystem != 0)
|
||||||
|
{
|
||||||
|
resultSubsystemOperationTime.Add(operationTimeItem);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return resultSubsystemOperationTime;
|
return resultSubsystemOperationTime;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#nullable disable
|
#nullable disable
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user