2024-02-05 10:47:00 +05:00
|
|
|
|
using AsbCloudApp.Data;
|
|
|
|
|
using AsbCloudApp.Data.SAUB;
|
2024-02-01 13:44:27 +05:00
|
|
|
|
using AsbCloudApp.Repositories;
|
|
|
|
|
using AsbCloudApp.Requests;
|
|
|
|
|
using AsbCloudDb.Model;
|
2024-01-30 16:27:24 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services.DetectOperations.Detectors;
|
2024-02-01 13:44:27 +05:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System;
|
2024-02-01 13:44:27 +05:00
|
|
|
|
using System.Collections.Generic;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Background.PeriodicWorks
|
|
|
|
|
{
|
2024-01-31 15:20:14 +05:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// задача по добавлению данных в таблицу DataSaubStat, которая используется дл построения РТК-отчета
|
|
|
|
|
/// </summary>
|
|
|
|
|
internal class WorkDataSaubStat : Work
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
|
|
|
|
private int MechanicalDrillingCategoryId = 4001;
|
2024-02-01 13:44:27 +05:00
|
|
|
|
private int Gap = 60;
|
2024-01-31 15:20:14 +05:00
|
|
|
|
|
2024-01-31 15:25:16 +05:00
|
|
|
|
public WorkDataSaubStat() : base("Generate DataSaubStat entries and save them into Db")
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
2024-01-29 12:50:31 +05:00
|
|
|
|
Timeout = TimeSpan.FromMinutes(10);
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 13:44:27 +05:00
|
|
|
|
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
|
|
|
|
using var db = services.GetRequiredService<IAsbCloudDbContext>();
|
2024-02-05 10:47:00 +05:00
|
|
|
|
|
2024-02-01 13:44:27 +05:00
|
|
|
|
var telemetryDataCache = services.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>();
|
|
|
|
|
|
2024-02-01 17:17:26 +05:00
|
|
|
|
var cacheRequest = new TelemetryDataRequest()
|
2024-02-01 13:44:27 +05:00
|
|
|
|
{
|
2024-02-05 13:26:07 +05:00
|
|
|
|
GeDate = DateTime.UtcNow.AddDays(-Gap)
|
2024-02-01 17:17:26 +05:00
|
|
|
|
};
|
2024-02-02 15:54:56 +05:00
|
|
|
|
var idTelemetries = telemetryDataCache.GetIds(cacheRequest).ToArray();
|
2024-02-01 13:44:27 +05:00
|
|
|
|
|
2024-02-01 17:17:26 +05:00
|
|
|
|
if (!idTelemetries.Any())
|
2024-02-01 13:44:27 +05:00
|
|
|
|
return;
|
|
|
|
|
|
2024-02-05 10:47:00 +05:00
|
|
|
|
var dataSaubStatRepo = services.GetRequiredService<IDataSaubStatRepository>();
|
2024-02-06 11:44:29 +05:00
|
|
|
|
var stats = await dataSaubStatRepo.GetLastsAsync(idTelemetries, token);
|
2024-02-01 17:17:26 +05:00
|
|
|
|
|
2024-02-02 15:54:56 +05:00
|
|
|
|
for( var i =0; i < idTelemetries.Length; i++)
|
2024-02-01 17:17:26 +05:00
|
|
|
|
{
|
2024-02-02 15:54:56 +05:00
|
|
|
|
var idTelemetry = idTelemetries[i];
|
2024-02-02 13:51:58 +05:00
|
|
|
|
var lastDate = stats.FirstOrDefault(s => s.IdTelemetry == idTelemetry)?.DateEnd ?? DateTimeOffset.UnixEpoch;
|
2024-02-05 10:47:00 +05:00
|
|
|
|
var statsCount = await CreateStatForTelemetryFromDate(db, idTelemetry, lastDate, dataSaubStatRepo, token);
|
2024-02-02 15:54:56 +05:00
|
|
|
|
onProgressCallback($"Calculate stat for telemetry: {idTelemetry}; from {lastDate}; results count: {statsCount};", 100*i / idTelemetries.Length);
|
2024-02-01 17:17:26 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-29 11:51:09 +05:00
|
|
|
|
|
2024-02-05 10:47:00 +05:00
|
|
|
|
private async Task<int> CreateStatForTelemetryFromDate(IAsbCloudDbContext db, int idTelemetry, DateTimeOffset begin, IDataSaubStatRepository dataSaubStatRepo, CancellationToken token)
|
2024-02-01 17:17:26 +05:00
|
|
|
|
{
|
2024-02-01 13:44:27 +05:00
|
|
|
|
var detectedOperations = await db.Set<DetectedOperation>()
|
2024-02-01 17:17:26 +05:00
|
|
|
|
.Where(o => o.IdTelemetry == idTelemetry)
|
|
|
|
|
.Where(o => o.DateStart > begin)
|
2024-01-29 11:51:09 +05:00
|
|
|
|
.Where(o => o.OperationCategory.IdParent == MechanicalDrillingCategoryId)
|
2024-01-30 16:27:24 +05:00
|
|
|
|
.OrderBy(o => o.DateStart)
|
2024-02-02 15:54:56 +05:00
|
|
|
|
.Take(250)
|
2024-02-01 13:44:27 +05:00
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
if (!detectedOperations.Any())
|
2024-02-02 15:54:56 +05:00
|
|
|
|
return 0;
|
2024-01-31 15:20:14 +05:00
|
|
|
|
|
2024-02-01 17:17:26 +05:00
|
|
|
|
var minDate = detectedOperations.First().DateStart;
|
|
|
|
|
var maxDate = detectedOperations.OrderByDescending(d => d.DateEnd).First().DateEnd;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
|
2024-02-01 17:17:26 +05:00
|
|
|
|
var telemetryDataSaub = await db.Set<TelemetryDataSaub>()
|
|
|
|
|
.Where(t => t.IdTelemetry == idTelemetry)
|
2024-01-29 11:51:09 +05:00
|
|
|
|
.Where(t => t.DateTime >= minDate)
|
|
|
|
|
.Where(t => t.DateTime <= maxDate)
|
2024-01-30 16:27:24 +05:00
|
|
|
|
.Where(t => Math.Abs(t.BitDepth - t.WellDepth) < 0.0001)
|
2024-02-02 13:51:58 +05:00
|
|
|
|
.OrderBy(t => t.DateTime)
|
2024-02-02 15:54:56 +05:00
|
|
|
|
.Take(100_000)
|
2024-02-01 17:17:26 +05:00
|
|
|
|
.ToArrayAsync(token);
|
|
|
|
|
|
|
|
|
|
if (!telemetryDataSaub.Any())
|
2024-02-02 15:54:56 +05:00
|
|
|
|
return 0;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
|
2024-02-01 17:17:26 +05:00
|
|
|
|
var dataSaubStats = CreateDataSaubStat(detectedOperations, telemetryDataSaub);
|
|
|
|
|
|
2024-02-05 10:47:00 +05:00
|
|
|
|
return await dataSaubStatRepo.InsertRangeAsync(dataSaubStats, token);
|
2024-02-01 13:44:27 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 10:47:00 +05:00
|
|
|
|
private static IEnumerable<DataSaubStatDto> CreateDataSaubStat(IEnumerable<DetectedOperation> detectedOperations, TelemetryDataSaub[] telemetryDataSaub)
|
2024-02-01 13:44:27 +05:00
|
|
|
|
{
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var indexStart = 0;
|
|
|
|
|
var indexEnd = 0;
|
2024-02-05 10:47:00 +05:00
|
|
|
|
var result = new List<DataSaubStatDto>();
|
2024-02-02 13:51:58 +05:00
|
|
|
|
|
|
|
|
|
if (!telemetryDataSaub.Any())
|
|
|
|
|
return result;
|
|
|
|
|
|
2024-01-29 11:51:09 +05:00
|
|
|
|
foreach (var operation in detectedOperations)
|
|
|
|
|
{
|
2024-02-01 17:17:26 +05:00
|
|
|
|
indexStart = Array.FindIndex(telemetryDataSaub, indexEnd, t => t.DateTime >= operation.DateStart);
|
2024-02-02 13:51:58 +05:00
|
|
|
|
if (indexStart < 0)
|
|
|
|
|
break;
|
2024-01-30 16:27:24 +05:00
|
|
|
|
|
2024-02-02 13:51:58 +05:00
|
|
|
|
indexEnd = Array.FindIndex(telemetryDataSaub, indexStart, t => t.DateTime > operation.DateEnd);
|
2024-01-30 16:27:24 +05:00
|
|
|
|
|
2024-02-02 13:51:58 +05:00
|
|
|
|
if (indexEnd < 0)
|
|
|
|
|
indexEnd = telemetryDataSaub.Length - 1;
|
|
|
|
|
|
|
|
|
|
if (indexEnd == indexStart)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
var length = indexEnd - indexStart;
|
|
|
|
|
|
|
|
|
|
var subset = telemetryDataSaub.AsSpan(indexStart, length);
|
|
|
|
|
var stats = CalcStats(operation, subset);
|
|
|
|
|
result.AddRange(stats);
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
2024-02-02 11:15:03 +05:00
|
|
|
|
return result;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 10:47:00 +05:00
|
|
|
|
private static IEnumerable<DataSaubStatDto> CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub)
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
2024-02-05 10:47:00 +05:00
|
|
|
|
var result = new List<DataSaubStatDto>();
|
2024-02-02 11:15:03 +05:00
|
|
|
|
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var indexStart = 0;
|
|
|
|
|
for (var i = 1; i < telemetryDataSaub.Length; i++)
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var previous = telemetryDataSaub[i - 1];
|
|
|
|
|
var current = telemetryDataSaub[i];
|
|
|
|
|
|
2024-01-31 15:20:14 +05:00
|
|
|
|
if (IsNewCacheItem(previous, current) || i == telemetryDataSaub.Length - 1)
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
2024-01-30 17:28:41 +05:00
|
|
|
|
var length = i - indexStart;
|
|
|
|
|
var span = telemetryDataSaub.Slice(indexStart, length);
|
2024-01-30 16:27:24 +05:00
|
|
|
|
indexStart = i;
|
2024-02-02 15:54:56 +05:00
|
|
|
|
if (length <= 2 || (span[^1].WellDepth - span[0].WellDepth) < 0.001)
|
|
|
|
|
continue; // мелкие выборки не учитываем.
|
2024-02-01 17:17:26 +05:00
|
|
|
|
var stat = CalcStat(operation, span);
|
2024-02-02 11:15:03 +05:00
|
|
|
|
result.Add(stat);
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-02 11:15:03 +05:00
|
|
|
|
|
|
|
|
|
return result;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-02-05 10:47:00 +05:00
|
|
|
|
private static DataSaubStatDto CalcStat(DetectedOperation operation, Span<TelemetryDataSaub> span)
|
2024-01-30 16:27:24 +05:00
|
|
|
|
{
|
2024-01-31 15:20:14 +05:00
|
|
|
|
var hasOscillation = operation.ExtraData.TryGetValue(DetectorDrilling.ExtraDataKeyHasOscillation, out object? hasOscillationObject)
|
|
|
|
|
&& hasOscillationObject is true;
|
2024-01-30 16:27:24 +05:00
|
|
|
|
|
2024-02-01 13:44:27 +05:00
|
|
|
|
var aggregatedValues = CalcAggregate(span);
|
2024-02-05 10:47:00 +05:00
|
|
|
|
var processMapDrillingCacheItem = new DataSaubStatDto
|
2024-01-30 16:27:24 +05:00
|
|
|
|
{
|
2024-01-31 10:47:00 +05:00
|
|
|
|
DateStart = operation.DateStart,
|
|
|
|
|
DateEnd = operation.DateEnd,
|
2024-01-31 15:20:14 +05:00
|
|
|
|
DepthStart = operation.DepthStart,
|
|
|
|
|
DepthEnd = operation.DepthEnd,
|
|
|
|
|
Speed = (operation.DepthEnd - operation.DepthStart) / ((operation.DateEnd - operation.DateStart).TotalHours),
|
2024-01-30 16:27:24 +05:00
|
|
|
|
BlockSpeedSp = span[0].BlockSpeedSp,
|
2024-02-01 13:44:27 +05:00
|
|
|
|
Pressure = aggregatedValues.Pressure,
|
2024-01-30 16:27:24 +05:00
|
|
|
|
PressureIdle = span[0].PressureIdle,
|
|
|
|
|
PressureSp = span[0].PressureSp,
|
2024-02-01 13:44:27 +05:00
|
|
|
|
AxialLoad = aggregatedValues.AxialLoad,
|
2024-01-30 16:27:24 +05:00
|
|
|
|
AxialLoadSp = span[0].AxialLoadSp,
|
|
|
|
|
AxialLoadLimitMax = span[0].AxialLoadLimitMax,
|
2024-02-01 13:44:27 +05:00
|
|
|
|
RotorTorque = aggregatedValues.RotorTorque,
|
2024-01-30 16:27:24 +05:00
|
|
|
|
RotorTorqueSp = span[0].RotorTorqueSp,
|
|
|
|
|
RotorTorqueLimitMax = span[0].RotorTorqueLimitMax,
|
|
|
|
|
IdFeedRegulator = span[0].IdFeedRegulator,
|
2024-02-01 13:44:27 +05:00
|
|
|
|
RotorSpeed = aggregatedValues.RotorSpeed,
|
|
|
|
|
IdCategory = operation.IdCategory,
|
2024-01-30 16:27:24 +05:00
|
|
|
|
EnabledSubsystems = operation.EnabledSubsystems,
|
2024-01-31 10:47:00 +05:00
|
|
|
|
HasOscillation = hasOscillation,
|
|
|
|
|
IdTelemetry = operation.IdTelemetry,
|
2024-02-01 13:44:27 +05:00
|
|
|
|
Flow = aggregatedValues.Flow
|
2024-01-30 16:27:24 +05:00
|
|
|
|
};
|
|
|
|
|
return processMapDrillingCacheItem;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 13:44:27 +05:00
|
|
|
|
private static (
|
2024-01-31 15:20:14 +05:00
|
|
|
|
double Pressure,
|
|
|
|
|
double AxialLoad,
|
|
|
|
|
double RotorTorque,
|
2024-01-31 16:05:57 +05:00
|
|
|
|
double RotorSpeed,
|
|
|
|
|
double Flow
|
2024-02-01 13:44:27 +05:00
|
|
|
|
) CalcAggregate(Span<TelemetryDataSaub> span)
|
2024-01-30 16:27:24 +05:00
|
|
|
|
{
|
|
|
|
|
var sumPressure = 0.0;
|
|
|
|
|
var sumAxialLoad = 0.0;
|
|
|
|
|
var sumRotorTorque = 0.0;
|
|
|
|
|
var sumRotorSpeed = 0.0;
|
2024-01-31 16:05:57 +05:00
|
|
|
|
var flow = span[0].Flow ?? 0.0;
|
2024-01-30 17:28:41 +05:00
|
|
|
|
var diffDepthTotal = span[^1].WellDepth - span[0].WellDepth;
|
2024-01-30 16:27:24 +05:00
|
|
|
|
for (var i = 0; i < span.Length - 1; i++)
|
|
|
|
|
{
|
2024-02-01 17:17:26 +05:00
|
|
|
|
var diffDepth = span[i + 1].WellDepth - span[i].WellDepth;
|
|
|
|
|
sumPressure += diffDepth * span[i].Pressure;
|
|
|
|
|
sumAxialLoad += diffDepth * span[i].AxialLoad;
|
|
|
|
|
sumRotorTorque += diffDepth * span[i].RotorTorque;
|
|
|
|
|
sumRotorSpeed += diffDepth * span[i].RotorSpeed;
|
2024-02-01 13:44:27 +05:00
|
|
|
|
flow = span[i + 1].Flow > flow ? span[i + 1].Flow ?? 0.0 : flow;
|
2024-01-30 16:27:24 +05:00
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
Pressure: sumPressure / diffDepthTotal,
|
|
|
|
|
AxialLoad: sumAxialLoad / diffDepthTotal,
|
|
|
|
|
RotorTorque: sumRotorTorque / diffDepthTotal,
|
2024-01-31 16:05:57 +05:00
|
|
|
|
RotorSpeed: sumRotorSpeed / diffDepthTotal,
|
|
|
|
|
Flow: flow
|
2024-01-30 16:27:24 +05:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-01 13:44:27 +05:00
|
|
|
|
private static bool IsNewCacheItem(TelemetryDataSaub previous, TelemetryDataSaub current)
|
2024-01-30 16:27:24 +05:00
|
|
|
|
{
|
2024-01-31 15:20:14 +05:00
|
|
|
|
return !(current.Mode == previous.Mode)
|
2024-02-02 15:54:56 +05:00
|
|
|
|
|| !(current.WellDepth >= previous.WellDepth)
|
2024-01-31 15:20:14 +05:00
|
|
|
|
|| !(current.BlockSpeedSp == previous.BlockSpeedSp)
|
|
|
|
|
|| !(current.PressureIdle == previous.PressureIdle)
|
|
|
|
|
|| !(current.PressureSp == previous.PressureSp)
|
|
|
|
|
|| !(current.AxialLoadSp == previous.AxialLoadSp)
|
|
|
|
|
|| !(current.AxialLoadLimitMax == previous.AxialLoadLimitMax)
|
|
|
|
|
|| !(current.HookWeightIdle == previous.HookWeightIdle)
|
|
|
|
|
|| !(current.RotorTorqueIdle == previous.RotorTorqueIdle)
|
|
|
|
|
|| !(current.RotorTorqueSp == previous.RotorTorqueSp)
|
|
|
|
|
|| !(current.RotorTorqueLimitMax == previous.RotorTorqueLimitMax)
|
|
|
|
|
|| !(current.IdFeedRegulator == previous.IdFeedRegulator);
|
|
|
|
|
}
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
}
|