forked from ddrilling/AsbCloudServer
eugeniy_ivanov
62384b5673
-добавлена модель для запроса в Data Spin фонового сервиса -корректировка моделей (удаление пробелов и лишних библиотек) -добавление данных по умолчанию о подсистемах (через Entity Filler) - в методе Convert в SubsystemOperationTimeService сделана корректировка дат
250 lines
11 KiB
C#
250 lines
11 KiB
C#
using AsbCloudDb.Model;
|
|
using AsbCloudDb.Model.Subsystems;
|
|
using AsbCloudInfrastructure.Services.DetectOperations;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Data;
|
|
using System.Data.Common;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace AsbCloudInfrastructure.Services.Subsystems
|
|
{
|
|
internal class SubsystemOperationTimeBackgroundService : BackgroundService
|
|
{
|
|
private readonly string connectionString;
|
|
private readonly TimeSpan period = TimeSpan.FromHours(1);
|
|
private const int idSubsytemTorqueMaster = 65537;
|
|
private const int idSubsytemSpinMaster = 65536;
|
|
|
|
public SubsystemOperationTimeBackgroundService(IConfiguration configuration)
|
|
{
|
|
connectionString = configuration.GetConnectionString("DefaultConnection");
|
|
}
|
|
protected override async Task ExecuteAsync(CancellationToken token)
|
|
{
|
|
var timeToStartAnalysis = DateTime.Now;
|
|
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
|
.UseNpgsql(connectionString)
|
|
.Options;
|
|
|
|
while (!token.IsCancellationRequested)
|
|
{
|
|
if (DateTime.Now > timeToStartAnalysis)
|
|
{
|
|
timeToStartAnalysis = DateTime.Now + period;
|
|
try
|
|
{
|
|
using var context = new AsbCloudDbContext(options);
|
|
var added = await OperationTimeAllTelemetriesAsync(context, token);
|
|
Trace.TraceInformation($"Total subsystem operation time complete. Added {added} operations time.");
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Trace.TraceError(ex.Message);
|
|
}
|
|
GC.Collect();
|
|
}
|
|
var ms = (int)(timeToStartAnalysis - DateTime.Now).TotalMilliseconds;
|
|
ms = ms > 100 ? ms : 100;
|
|
await Task.Delay(ms, token).ConfigureAwait(false);
|
|
}
|
|
}
|
|
|
|
public override async Task StopAsync(CancellationToken token)
|
|
{
|
|
await base.StopAsync(token).ConfigureAwait(false);
|
|
}
|
|
|
|
private static async Task<int> OperationTimeAllTelemetriesAsync(IAsbCloudDbContext db, CancellationToken token)
|
|
{
|
|
var lastDetectedDates = await db.SubsystemOperationTimes
|
|
.GroupBy(o => o.IdTelemetry)
|
|
.Select(g => new
|
|
{
|
|
IdTelemetry = g.Key,
|
|
LastDate = g.Max(o => o.DateEnd)
|
|
})
|
|
.ToListAsync(token);
|
|
|
|
var telemetryIds = await db.Telemetries
|
|
.Where(t => t.Info != null && t.TimeZone != null)
|
|
.Select(t => t.Id)
|
|
.ToListAsync(token);
|
|
|
|
var JounedlastDetectedDates = telemetryIds
|
|
.GroupJoin(lastDetectedDates,
|
|
t => t,
|
|
o => o.IdTelemetry,
|
|
(outer, inner) => new
|
|
{
|
|
IdTelemetry = outer,
|
|
inner.SingleOrDefault()?.LastDate,
|
|
});
|
|
var affected = 0;
|
|
foreach (var item in JounedlastDetectedDates)
|
|
{
|
|
var stopwatch = Stopwatch.StartNew();
|
|
var newOperationsSaub = await OperationTimeSaubAsync(item.IdTelemetry, item.LastDate ?? DateTimeOffset.MinValue, db, token);
|
|
stopwatch.Stop();
|
|
if (newOperationsSaub is not null && newOperationsSaub.Any())
|
|
{
|
|
db.SubsystemOperationTimes.AddRange(newOperationsSaub);
|
|
affected += await db.SaveChangesAsync(token);
|
|
}
|
|
var newOperationsSpin = await OperationTimeSpinAsync(item.IdTelemetry, item.LastDate ?? DateTimeOffset.MinValue, db, token);
|
|
if (newOperationsSpin is not null && newOperationsSpin.Any())
|
|
{
|
|
db.SubsystemOperationTimes.AddRange(newOperationsSpin);
|
|
affected += await db.SaveChangesAsync(token);
|
|
}
|
|
}
|
|
return affected;
|
|
}
|
|
|
|
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
|
{
|
|
var query = db.TelemetryDataSaub
|
|
.AsNoTracking()
|
|
.Where(d => d.IdTelemetry == idTelemetry)
|
|
.Select(d => new
|
|
{
|
|
DateTime = d.DateTime,
|
|
Mode = d.Mode,
|
|
Depth = d.WellDepth
|
|
})
|
|
.OrderBy(d => d.DateTime);
|
|
var take = 4 * 86_400; // 4 дня
|
|
var startDate = begin;
|
|
var firstItem = query.FirstOrDefault();
|
|
if (firstItem is null)
|
|
return null;
|
|
short? mode = firstItem.Mode;
|
|
DateTimeOffset dateBegin = firstItem.DateTime;
|
|
float? depthStart = firstItem.Depth;
|
|
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
|
|
var data = await query
|
|
.Where(d => d.DateTime > startDate)
|
|
.Take(take)
|
|
.ToArrayAsync(token);
|
|
for (int i = 1; i < data.Length; i++)
|
|
{
|
|
if (data[i].Mode != mode)
|
|
{
|
|
var operationTimeItem = new SubsystemOperationTime()
|
|
{
|
|
IdTelemetry = idTelemetry,
|
|
DateStart = dateBegin,
|
|
IdSubsystem = (int)data[i - 1].Mode + 1,
|
|
DateEnd = data[i - 1].DateTime,
|
|
DepthStart = depthStart,
|
|
DepthEnd = data[i - 1].Depth
|
|
};
|
|
resultSubsystemOperationTime.Add(operationTimeItem);
|
|
mode = data[i].Mode;
|
|
dateBegin = data[i].DateTime;
|
|
depthStart = data[i].Depth;
|
|
}
|
|
}
|
|
startDate = data.Last().DateTime;
|
|
return resultSubsystemOperationTime;
|
|
}
|
|
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
|
{
|
|
Predicate<int> isSubsystemTorqueState = (int state) => state == 7;
|
|
Predicate<short> isSubsystemTorqueMode = (short mode) => (mode & 2) > 0;
|
|
Predicate<int> isSubsystemSpin = (int state) => state != 0 & state != 6 & state != 7;
|
|
var operationTimeSpinWithDepth =
|
|
$"select tspin.\"date\", tspin.\"mode\", tspin.\"state\", tsaub.\"well_depth\"" +
|
|
$" from (select \"date\" ,\"mode\" ,lag(mode, 1) over (order by \"date\") as mode_pre, state , " +
|
|
$"lag(state, 1) over (order by \"date\") as state_pre from t_telemetry_data_spin where id_telemetry = {idTelemetry}) as tspin " +
|
|
$"join (select \"date\", well_depth from t_telemetry_data_saub where id_telemetry = {idTelemetry}) as tsaub " +
|
|
$"on EXTRACT(EPOCH from tspin.date) = EXTRACT(EPOCH from tsaub.date) " +
|
|
$"where mode!=mode_pre or state != state_pre order by \"date\";";
|
|
using var command = db.Database.GetDbConnection().CreateCommand();
|
|
command.CommandText = operationTimeSpinWithDepth;
|
|
db.Database.OpenConnection();
|
|
using var result = command.ExecuteReader();
|
|
var query = new List<SubsystemsSpinWithDepthDto>();
|
|
//DataTable dt = new DataTable();
|
|
//dt.Load(result);
|
|
//var query = from c in dt.AsEnumerable()
|
|
// select new
|
|
// {
|
|
// Date = (DateTimeOffset)c["date"],
|
|
// Mode = (short)c["mode"],
|
|
// State = (int)c["state"],
|
|
// Depth = (float)c["float"]
|
|
// };
|
|
if (result.HasRows)
|
|
while (result.Read())
|
|
{
|
|
var itemEntity = new SubsystemsSpinWithDepthDto()
|
|
{
|
|
Date = result.GetFieldValue<DateTimeOffset>(0),
|
|
Mode = result.GetFieldValue<short>(1),
|
|
State = result.GetFieldValue<int>(2),
|
|
Depth = result.GetFieldValue<float>(3)
|
|
};
|
|
int? subsystemId = isSubsystemTorqueState(itemEntity.State) && isSubsystemTorqueMode(itemEntity.Mode)
|
|
? idSubsytemTorqueMaster
|
|
: isSubsystemSpin(itemEntity.State)
|
|
? idSubsytemSpinMaster
|
|
: 0;
|
|
if (subsystemId.HasValue)
|
|
{
|
|
itemEntity.IdSubsystem = subsystemId.Value;
|
|
query.Add(itemEntity);
|
|
}
|
|
}
|
|
var take = 4 * 86_400; // 4 дня
|
|
var startDate = begin;
|
|
var firstItem = query.FirstOrDefault();
|
|
if (firstItem is null)
|
|
return null;
|
|
int idSubsystem = firstItem.IdSubsystem;
|
|
DateTimeOffset dateBegin = firstItem.Date;
|
|
float? depthStart = firstItem.Depth;
|
|
var resultSubsystemOperationTime = new List<SubsystemOperationTime>();
|
|
var data = query
|
|
.Where(d => d.Date > startDate)
|
|
.Take(take)
|
|
.ToArray();
|
|
if (data.Length==0)
|
|
return null;
|
|
|
|
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 - 1].Date,
|
|
DepthStart = depthStart,
|
|
DepthEnd = data[i - 1].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;
|
|
return resultSubsystemOperationTime;
|
|
}
|
|
}
|
|
}
|