2024-01-29 11:51:09 +05:00
|
|
|
|
using AsbCloudDb.Model;
|
2024-01-30 16:27:24 +05:00
|
|
|
|
using AsbCloudInfrastructure.Services.DetectOperations.Detectors;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
using Microsoft.Extensions.Caching.Memory;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace AsbCloudInfrastructure.Background.PeriodicWorks
|
|
|
|
|
{
|
2024-01-29 12:50:31 +05:00
|
|
|
|
internal class WorkProcessMapDrillingCache : Work
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
|
|
|
|
private int MechanicalDrillingCategoryId = 4001;
|
2024-01-29 12:50:31 +05:00
|
|
|
|
public WorkProcessMapDrillingCache() : base("Generate process map drilling cache table")
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected override Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
|
|
|
|
|
{
|
|
|
|
|
//1 найти последнюю запись в кеш-таблице (по полям "Дата до")
|
|
|
|
|
//2 определяем дату с которой начинаем анализ
|
|
|
|
|
//3 находим автоопределнные операции по скважине, начиная с начальной даты из пункта 2
|
|
|
|
|
// 3.1 только те, которые связанные с бурением (idParent = 4001)
|
|
|
|
|
// 3.2 выбираем 500 операций
|
|
|
|
|
// 3.3. выбираем минимальную дату начала и максимальную дату окончания
|
|
|
|
|
//4 по полученным в пункте выше данным выбираем записи из telemetry_data_saub
|
|
|
|
|
// 4.1.выбираем те у которых (bit_depth == well_depth)
|
|
|
|
|
//5. для каждой операции из 3.2 находим соответствующий ей кусок телеметрии (по дате начала и окончания)
|
|
|
|
|
// и храним индексы этого куска в виде span
|
|
|
|
|
//6. кусок телеметрии, полученный в пункте 5 (span) и соответствующая ей операция направляется на разбивку диапазонов
|
|
|
|
|
|
|
|
|
|
//7/ разбивка диапазонов:
|
|
|
|
|
// параметры метода: массив из
|
|
|
|
|
|
|
|
|
|
using var db = services.GetRequiredService<IAsbCloudDbContext>();
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var dateFrom = db.ProcessMapDrillingCache
|
|
|
|
|
.OrderByDescending(c => c.DateTo)
|
|
|
|
|
.FirstOrDefault()?.DateTo
|
|
|
|
|
?? DateTimeOffset.MinValue;
|
2024-01-29 12:50:31 +05:00
|
|
|
|
|
2024-01-29 11:51:09 +05:00
|
|
|
|
|
|
|
|
|
//а остальные операции, которые не вошли в 500 первых?
|
|
|
|
|
var detectedOperations = db.DetectedOperations
|
2024-01-29 12:50:31 +05:00
|
|
|
|
.Where(o => o.DateStart > dateFrom)
|
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-01-29 11:51:09 +05:00
|
|
|
|
.Take(500)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var minDate = detectedOperations.FirstOrDefault()?.DateStart;
|
|
|
|
|
var maxDate = detectedOperations.OrderByDescending(d =>d.DateEnd).FirstOrDefault()?.DateEnd;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
|
|
|
|
|
var telemetryDataSaub = db.TelemetryDataSaub
|
|
|
|
|
.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-01-29 11:51:09 +05:00
|
|
|
|
.OrderBy(t => t.DateTime)
|
|
|
|
|
.ToArray();
|
|
|
|
|
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var indexStart = 0;
|
|
|
|
|
var indexEnd = 0;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
foreach (var operation in detectedOperations)
|
|
|
|
|
{
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var telemetryDataSaubItemStart = telemetryDataSaub
|
|
|
|
|
.FirstOrDefault(t => t.DateTime >= operation.DateStart);
|
|
|
|
|
var telemetryDataSaubItemEnd = telemetryDataSaub
|
|
|
|
|
.Where(t => t.DateTime <= operation.DateEnd)
|
|
|
|
|
.LastOrDefault();
|
|
|
|
|
|
|
|
|
|
indexStart = Array.IndexOf(telemetryDataSaub, telemetryDataSaubItemStart, indexEnd);
|
|
|
|
|
indexEnd = Array.IndexOf(telemetryDataSaub, telemetryDataSaubItemEnd, indexStart);
|
|
|
|
|
|
|
|
|
|
if(indexStart >= 0 && indexEnd >= indexStart)
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var length = indexEnd - indexStart;
|
|
|
|
|
|
|
|
|
|
var subset = telemetryDataSaub.AsSpan(indexStart, length + 1);
|
|
|
|
|
var result = CalcStats(operation, subset);
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
2024-01-30 16:27:24 +05:00
|
|
|
|
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-29 12:50:31 +05:00
|
|
|
|
return Task.CompletedTask;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 16:27:24 +05:00
|
|
|
|
private object CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub)
|
2024-01-29 11:51:09 +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];
|
|
|
|
|
|
|
|
|
|
if(IsNewCacheItem(previous, current))
|
2024-01-29 11:51:09 +05:00
|
|
|
|
{
|
2024-01-30 16:27:24 +05:00
|
|
|
|
var span = telemetryDataSaub.Slice(indexStart, i - indexStart);
|
|
|
|
|
indexStart = i;
|
|
|
|
|
var processMapDrillingCacheItem = CalcStat(operation, span);
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-01-30 16:27:24 +05:00
|
|
|
|
return null;
|
2024-01-29 11:51:09 +05:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-30 16:27:24 +05:00
|
|
|
|
private ProcessMapDrillingCache CalcStat(DetectedOperation operation, Span<TelemetryDataSaub> span)
|
|
|
|
|
{
|
|
|
|
|
var depthStart = span[0].WellDepth;
|
|
|
|
|
var depthEnd = span[span.Length - 1].WellDepth;
|
|
|
|
|
|
|
|
|
|
var hasOscillation = false;
|
|
|
|
|
if (operation.ExtraData.TryGetValue(DetectorDrilling.ExtraDataKeyHasOscillation, out object? HasOscillation))
|
|
|
|
|
hasOscillation = Convert.ToBoolean(HasOscillation);
|
|
|
|
|
|
|
|
|
|
var processMapDrillingCacheItem = new ProcessMapDrillingCache
|
|
|
|
|
{
|
|
|
|
|
DateDrillingStart = operation.DateStart,
|
|
|
|
|
DateDrillingEnd = operation.DateEnd,
|
|
|
|
|
DepthStart = depthStart,
|
|
|
|
|
DepthEnd = depthEnd,
|
|
|
|
|
Speed = (depthEnd - depthStart) / ((operation.DateEnd - operation.DateStart).TotalHours),
|
|
|
|
|
BlockSpeedSp = span[0].BlockSpeedSp,
|
|
|
|
|
Pressure = CalcValue(span, depthEnd - depthStart).Pressure,
|
|
|
|
|
PressureIdle = span[0].PressureIdle,
|
|
|
|
|
PressureSp = span[0].PressureSp,
|
|
|
|
|
AxialLoad = CalcValue(span, depthEnd - depthStart).AxialLoad,
|
|
|
|
|
AxialLoadSp = span[0].AxialLoadSp,
|
|
|
|
|
AxialLoadLimitMax = span[0].AxialLoadLimitMax,
|
|
|
|
|
RotorTorque = CalcValue(span, depthEnd - depthStart).RotorTorque,
|
|
|
|
|
RotorTorqueSp = span[0].RotorTorqueSp,
|
|
|
|
|
RotorTorqueLimitMax = span[0].RotorTorqueLimitMax,
|
|
|
|
|
//Значение столбца id_feed_regulator из таблицы “Фильтрованная телеметрия”
|
|
|
|
|
//в границах временного интервала (диапазоне).
|
|
|
|
|
IdFeedRegulator = span[0].IdFeedRegulator,
|
|
|
|
|
RotorSpeed = CalcValue(span, depthEnd - depthStart).RotorSpeed,
|
|
|
|
|
//?
|
|
|
|
|
DetectedOperationCategoryId = operation.IdCategory,
|
|
|
|
|
EnabledSubsystems = operation.EnabledSubsystems,
|
|
|
|
|
HasOscillation = hasOscillation
|
|
|
|
|
};
|
|
|
|
|
return processMapDrillingCacheItem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private (
|
|
|
|
|
double Pressure,
|
|
|
|
|
double AxialLoad,
|
|
|
|
|
double RotorTorque,
|
|
|
|
|
double RotorSpeed
|
|
|
|
|
) CalcValue(Span<TelemetryDataSaub> span, float diffDepthTotal)
|
|
|
|
|
{
|
|
|
|
|
var sumPressure = 0.0;
|
|
|
|
|
var sumAxialLoad = 0.0;
|
|
|
|
|
var sumRotorTorque = 0.0;
|
|
|
|
|
var sumRotorSpeed = 0.0;
|
|
|
|
|
for (var i = 0; i < span.Length - 1; i++)
|
|
|
|
|
{
|
|
|
|
|
sumPressure += (span[i + 1].WellDepth - span[i].WellDepth) * span[i].Pressure;
|
|
|
|
|
sumAxialLoad += (span[i + 1].WellDepth - span[i].WellDepth) * span[i].AxialLoad;
|
|
|
|
|
sumRotorTorque += (span[i + 1].WellDepth - span[i].WellDepth) * span[i].RotorTorque;
|
|
|
|
|
sumRotorSpeed += (span[i + 1].WellDepth - span[i].WellDepth) * span[i].RotorSpeed;
|
|
|
|
|
}
|
|
|
|
|
return (
|
|
|
|
|
Pressure: sumPressure / diffDepthTotal,
|
|
|
|
|
AxialLoad: sumAxialLoad / diffDepthTotal,
|
|
|
|
|
RotorTorque: sumRotorTorque / diffDepthTotal,
|
|
|
|
|
RotorSpeed: sumRotorSpeed / diffDepthTotal
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private bool IsNewCacheItem(TelemetryDataSaub previous, TelemetryDataSaub current)
|
|
|
|
|
{
|
|
|
|
|
return !(current.Mode == previous.Mode)
|
|
|
|
|
|| !(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
|
|
|
|
}
|
|
|
|
|
}
|