2022-07-18 18:51:49 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2022-08-01 13:55:51 +05:00
|
|
|
|
using AsbCloudDb.Model.Subsystems;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
2022-07-14 03:47:11 +05:00
|
|
|
|
using Microsoft.Extensions.Hosting;
|
2022-08-19 00:00:30 +05:00
|
|
|
|
using Npgsql;
|
2022-07-14 03:47:11 +05:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2022-08-15 01:17:00 +05:00
|
|
|
|
using System.Data;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
using System.Diagnostics;
|
2022-07-14 03:47:11 +05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Services.Subsystems
|
|
|
|
|
{
|
2022-08-16 02:22:16 +05:00
|
|
|
|
#nullable enable
|
2022-07-14 03:47:11 +05:00
|
|
|
|
internal class SubsystemOperationTimeBackgroundService : BackgroundService
|
2022-09-18 23:25:34 +05:00
|
|
|
|
{
|
2022-07-14 03:47:11 +05:00
|
|
|
|
private readonly string connectionString;
|
2022-08-01 13:55:51 +05:00
|
|
|
|
private readonly TimeSpan period = TimeSpan.FromHours(1);
|
2022-08-15 01:17:00 +05:00
|
|
|
|
private const int idSubsytemTorqueMaster = 65537;
|
2022-08-17 17:23:24 +05:00
|
|
|
|
private const int idSubsytemSpinMaster = 65536;
|
2022-09-14 12:34:24 +05:00
|
|
|
|
private const int idSubsytemAkb = 1;
|
|
|
|
|
private const int idSubsytemMse = 2;
|
2022-07-14 03:47:11 +05:00
|
|
|
|
public SubsystemOperationTimeBackgroundService(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
connectionString = configuration.GetConnectionString("DefaultConnection");
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-07-18 18:51:49 +05:00
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken token)
|
2022-07-14 03:47:11 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var timeToStart = DateTime.Now;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
var options = new DbContextOptionsBuilder<AsbCloudDbContext>()
|
|
|
|
|
.UseNpgsql(connectionString)
|
|
|
|
|
.Options;
|
|
|
|
|
while (!token.IsCancellationRequested)
|
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
if (DateTime.Now > timeToStart)
|
2022-07-18 18:51:49 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
timeToStart = DateTime.Now + period;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
try
|
|
|
|
|
{
|
2022-08-01 13:55:51 +05:00
|
|
|
|
using var context = new AsbCloudDbContext(options);
|
|
|
|
|
var added = await OperationTimeAllTelemetriesAsync(context, token);
|
2022-08-15 01:17:00 +05:00
|
|
|
|
Trace.TraceInformation($"Total subsystem operation time complete. Added {added} operations time.");
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Trace.TraceError(ex.Message);
|
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
GC.Collect();
|
2022-07-18 18:51:49 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var ms = (int)(timeToStart - DateTime.Now).TotalMilliseconds;
|
2022-07-18 18:51:49 +05:00
|
|
|
|
ms = ms > 100 ? ms : 100;
|
|
|
|
|
await Task.Delay(ms, token).ConfigureAwait(false);
|
|
|
|
|
}
|
2022-08-17 17:23:24 +05:00
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
|
|
|
|
public override async Task StopAsync(CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
await base.StopAsync(token).ConfigureAwait(false);
|
2022-07-14 03:47:11 +05:00
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
|
|
|
|
|
private static async Task<int> OperationTimeAllTelemetriesAsync(IAsbCloudDbContext db, CancellationToken token)
|
|
|
|
|
{
|
2022-08-15 01:17:00 +05:00
|
|
|
|
var lastDetectedDates = await db.SubsystemOperationTimes
|
2022-08-01 13:55:51 +05:00
|
|
|
|
.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);
|
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var telemetryLastDetectedDates = telemetryIds
|
2022-08-01 13:55:51 +05:00
|
|
|
|
.GroupJoin(lastDetectedDates,
|
|
|
|
|
t => t,
|
|
|
|
|
o => o.IdTelemetry,
|
|
|
|
|
(outer, inner) => new
|
|
|
|
|
{
|
|
|
|
|
IdTelemetry = outer,
|
|
|
|
|
inner.SingleOrDefault()?.LastDate,
|
|
|
|
|
});
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-08-01 13:55:51 +05:00
|
|
|
|
var affected = 0;
|
2022-09-07 18:01:39 +05:00
|
|
|
|
foreach (var item in telemetryLastDetectedDates)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2022-08-16 02:22:16 +05:00
|
|
|
|
var newOperationsSaub = await OperationTimeSaubAsync(item.IdTelemetry, item.LastDate ?? DateTimeOffset.MinValue, db, token);
|
|
|
|
|
if (newOperationsSaub?.Any() == true)
|
2022-08-01 13:55:51 +05:00
|
|
|
|
{
|
2022-08-03 16:35:21 +05:00
|
|
|
|
db.SubsystemOperationTimes.AddRange(newOperationsSaub);
|
|
|
|
|
affected += await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
2022-08-15 01:17:00 +05:00
|
|
|
|
var newOperationsSpin = await OperationTimeSpinAsync(item.IdTelemetry, item.LastDate ?? DateTimeOffset.MinValue, db, token);
|
2022-08-16 02:22:16 +05:00
|
|
|
|
if (newOperationsSpin?.Any() == true)
|
2022-08-15 01:17:00 +05:00
|
|
|
|
{
|
|
|
|
|
db.SubsystemOperationTimes.AddRange(newOperationsSpin);
|
|
|
|
|
affected += await db.SaveChangesAsync(token);
|
|
|
|
|
}
|
2022-08-01 13:55:51 +05:00
|
|
|
|
}
|
|
|
|
|
return affected;
|
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
private static async Task<IEnumerable<SubsystemOperationTime>> OperationTimeSaubAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
|
|
|
|
{
|
2022-09-20 22:24:51 +05:00
|
|
|
|
static bool isSubsytemAkb(short? mode)
|
2022-09-07 18:01:39 +05:00
|
|
|
|
{
|
2022-09-20 22:24:51 +05:00
|
|
|
|
if (mode is null)
|
|
|
|
|
return false;
|
2022-09-14 12:34:24 +05:00
|
|
|
|
if (mode == 1 | mode == 3)
|
2022-09-20 22:24:51 +05:00
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2022-09-07 18:01:39 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-20 22:24:51 +05:00
|
|
|
|
static bool IsSubsystemMse(short? state)
|
2022-09-18 23:25:34 +05:00
|
|
|
|
{
|
2022-09-20 22:24:51 +05:00
|
|
|
|
if (state is null)
|
|
|
|
|
return false;
|
2022-09-16 13:47:19 +05:00
|
|
|
|
if ((state & 1) > 0)
|
|
|
|
|
return true;
|
|
|
|
|
return false;
|
2022-09-20 22:24:51 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-21 13:06:40 +05:00
|
|
|
|
static List<SubsystemOperationTime> GetSubsystemOperationTimes (List<DataRow> dataRows, Predicate<DataRow> satisfyCondition, int idSubsystem, int idTelemetry)
|
2022-09-20 22:24:51 +05:00
|
|
|
|
{
|
|
|
|
|
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;
|
2022-09-16 13:47:19 +05:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var query =
|
2022-09-14 12:34:24 +05:00
|
|
|
|
$"select tt.date, tt.mode, tt.well_depth, tt.mse_state " +
|
2022-09-07 18:01:39 +05:00
|
|
|
|
$"from ( " +
|
|
|
|
|
$" select " +
|
|
|
|
|
$" date, " +
|
|
|
|
|
$" mode, " +
|
2022-09-14 12:34:24 +05:00
|
|
|
|
$" mse_state, " +
|
2022-09-07 18:01:39 +05:00
|
|
|
|
$" well_depth, " +
|
|
|
|
|
$" lag(mode,1) over (order by date) as mode_prev " +
|
|
|
|
|
$" from t_telemetry_data_saub " +
|
|
|
|
|
$" where id_telemetry = @idTelemetry " +
|
|
|
|
|
$" order by date ) as tt " +
|
|
|
|
|
$"where (tt.mode_prev is null or tt.mode != tt.mode_prev) and tt.date >= @begin " +
|
|
|
|
|
$"order by tt.date;";
|
2022-09-18 23:25:34 +05:00
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var idTelemetryParam = new NpgsqlParameter("@idTelemetry", idTelemetry);
|
|
|
|
|
var beginParam = new NpgsqlParameter("@begin", begin);
|
2022-09-18 23:25:34 +05:00
|
|
|
|
|
2022-09-07 18:01:39 +05:00
|
|
|
|
await db.Database.OpenConnectionAsync(token);
|
2022-09-18 23:25:34 +05:00
|
|
|
|
using var command = db.Database.GetDbConnection().CreateCommand();
|
|
|
|
|
command.CommandText = query;
|
|
|
|
|
command.Parameters.Add(idTelemetryParam);
|
2022-09-07 18:01:39 +05:00
|
|
|
|
command.Parameters.Add(beginParam);
|
2022-09-18 23:25:34 +05:00
|
|
|
|
|
2022-08-17 17:23:24 +05:00
|
|
|
|
using var result = await command.ExecuteReaderAsync(token);
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-09-20 22:24:51 +05:00
|
|
|
|
var subsystemOperationTime = new List<SubsystemOperationTime>();
|
|
|
|
|
var dataRowList = new List<DataRow>();
|
|
|
|
|
while (result.Read())
|
2022-08-16 02:22:16 +05:00
|
|
|
|
{
|
2022-09-20 22:24:51 +05:00
|
|
|
|
var dateRowItem = new DataRow()
|
2022-08-16 02:22:16 +05:00
|
|
|
|
{
|
2022-09-20 22:24:51 +05:00
|
|
|
|
Date = result.GetFieldValue<DateTimeOffset>(0),
|
|
|
|
|
Mode = result.GetFieldValue<short?>(1),
|
|
|
|
|
Depth = result.GetFieldValue<float?>(2),
|
|
|
|
|
State = result.GetFieldValue<short?>(3)
|
|
|
|
|
};
|
|
|
|
|
dataRowList.Add(dateRowItem);
|
2022-08-16 02:22:16 +05:00
|
|
|
|
}
|
2022-09-21 13:06:40 +05:00
|
|
|
|
var akbOperationTimes = GetSubsystemOperationTimes(dataRowList, d => isSubsytemAkb(d.Mode), idSubsytemAkb, idTelemetry);
|
|
|
|
|
var mseOperationTimes = GetSubsystemOperationTimes(dataRowList, d => IsSubsystemMse(d.State), idSubsytemMse, idTelemetry);
|
2022-09-20 22:24:51 +05:00
|
|
|
|
subsystemOperationTime.AddRange(akbOperationTimes);
|
|
|
|
|
subsystemOperationTime.AddRange(mseOperationTimes);
|
2022-09-07 18:01:39 +05:00
|
|
|
|
return subsystemOperationTime;
|
2022-08-17 17:23:24 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
2022-08-16 02:22:16 +05:00
|
|
|
|
private static async Task<IEnumerable<SubsystemOperationTime>?> OperationTimeSpinAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
|
2022-08-03 16:35:21 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
static int? GetSubsytemId(short mode, int state)
|
|
|
|
|
{
|
|
|
|
|
if (state == 7 && (mode & 2) > 0)
|
|
|
|
|
return idSubsytemTorqueMaster;
|
|
|
|
|
|
|
|
|
|
if (state != 0 & state != 6 & state != 7)
|
|
|
|
|
return idSubsytemSpinMaster;
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var query =
|
|
|
|
|
$"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 and date >= @begin" +
|
|
|
|
|
$" order by date ) as tspin " +
|
|
|
|
|
$"left outer join ( " +
|
|
|
|
|
$" select " +
|
|
|
|
|
$" date, " +
|
|
|
|
|
$" well_depth " +
|
|
|
|
|
$" from t_telemetry_data_saub " +
|
|
|
|
|
$" where id_telemetry = @idTelemetry and date >= @begin) as tsaub " +
|
2022-08-15 01:17:00 +05:00
|
|
|
|
$"on EXTRACT(EPOCH from tspin.date) = EXTRACT(EPOCH from tsaub.date) " +
|
2022-09-07 18:01:39 +05:00
|
|
|
|
$"where mode_pre is null or state_pre is null or mode != mode_pre or state != state_pre " +
|
|
|
|
|
$"order by date;";
|
|
|
|
|
|
|
|
|
|
var idTelemetryParam = new NpgsqlParameter("@idTelemetry", idTelemetry);
|
|
|
|
|
var beginParam = new NpgsqlParameter("@begin", begin);
|
|
|
|
|
|
|
|
|
|
await db.Database.OpenConnectionAsync(token);
|
2022-08-17 17:23:24 +05:00
|
|
|
|
using var command = db.Database.GetDbConnection().CreateCommand();
|
2022-09-07 18:01:39 +05:00
|
|
|
|
command.CommandText = query;
|
|
|
|
|
command.Parameters.Add(idTelemetryParam);
|
|
|
|
|
command.Parameters.Add(beginParam);
|
|
|
|
|
|
2022-08-17 17:23:24 +05:00
|
|
|
|
using var result = await command.ExecuteReaderAsync(token);
|
2022-09-07 18:01:39 +05:00
|
|
|
|
|
|
|
|
|
var subsystemOperationTime = new List<SubsystemOperationTime>(32);
|
|
|
|
|
|
|
|
|
|
if (result.Read())
|
2022-08-17 17:23:24 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var mode = result.GetFieldValue<short>(1);
|
|
|
|
|
var state = result.GetFieldValue<int>(2);
|
|
|
|
|
var idSubsystem = GetSubsytemId(mode, state);
|
|
|
|
|
var dateStart = result.GetFieldValue<DateTimeOffset>(0);
|
|
|
|
|
var depthStart = result.GetFieldValue<float?>(3);
|
|
|
|
|
|
2022-08-15 01:17:00 +05:00
|
|
|
|
while (result.Read())
|
2022-08-03 16:35:21 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var dateEnd = result.GetFieldValue<DateTimeOffset>(0);
|
|
|
|
|
var depthEnd = result.GetFieldValue<float?>(3);
|
|
|
|
|
if (idSubsystem.HasValue)
|
2022-08-17 17:23:24 +05:00
|
|
|
|
{
|
2022-09-07 18:01:39 +05:00
|
|
|
|
var operationTimeItem = new SubsystemOperationTime()
|
|
|
|
|
{
|
|
|
|
|
IdTelemetry = idTelemetry,
|
|
|
|
|
IdSubsystem = idSubsystem.Value,
|
|
|
|
|
DateStart = dateStart,
|
|
|
|
|
DateEnd = dateEnd,
|
|
|
|
|
DepthStart = depthStart,
|
|
|
|
|
DepthEnd = depthEnd
|
|
|
|
|
};
|
|
|
|
|
subsystemOperationTime.Add(operationTimeItem);
|
2022-08-16 02:22:16 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
mode = result.GetFieldValue<short>(1);
|
|
|
|
|
state = result.GetFieldValue<int>(2);
|
|
|
|
|
idSubsystem = GetSubsytemId(mode, state);
|
|
|
|
|
dateStart = dateEnd;
|
|
|
|
|
depthStart = depthEnd;
|
2022-08-17 17:23:24 +05:00
|
|
|
|
}
|
2022-08-16 02:22:16 +05:00
|
|
|
|
}
|
2022-09-07 18:01:39 +05:00
|
|
|
|
return subsystemOperationTime;
|
2022-08-17 17:23:24 +05:00
|
|
|
|
}
|
2022-07-14 03:47:11 +05:00
|
|
|
|
}
|
2022-09-20 22:24:51 +05:00
|
|
|
|
|
|
|
|
|
internal class DataRow
|
|
|
|
|
{
|
|
|
|
|
public DateTimeOffset Date { get; set; }
|
|
|
|
|
public short? Mode { get; set; }
|
|
|
|
|
public float? Depth { get; set; }
|
|
|
|
|
public short? State { get; set; }
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-16 02:22:16 +05:00
|
|
|
|
#nullable disable
|
2022-07-14 03:47:11 +05:00
|
|
|
|
}
|