DD.WellWorkover.Cloud/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs

197 lines
8.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using AsbCloudApp.Data.SAUB;
using AsbCloudApp.Repositories;
using AsbCloudApp.Requests;
using AsbCloudDb.Model;
using AsbCloudInfrastructure.Services.DetectOperations.Detectors;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace AsbCloudInfrastructure.Background.PeriodicWorks
{
/// <summary>
/// задача по добавлению данных в таблицу DataSaubStat, которая используется дл построения РТК-отчета
/// </summary>
internal class WorkDataSaubStat : Work
{
private int MechanicalDrillingCategoryId = 4001;
private int Gap = 60;
public WorkDataSaubStat() : base("Generate DataSaubStat entries and save them into Db")
{
Timeout = TimeSpan.FromMinutes(10);
}
protected override async Task Action(string id, IServiceProvider services, Action<string, double?> onProgressCallback, CancellationToken token)
{
using var db = services.GetRequiredService<IAsbCloudDbContext>();
var telemetryDataCache = services.GetRequiredService<ITelemetryDataCache<TelemetryDataSaubDto>>();
var telemetryIds = telemetryDataCache.GetIds(new TelemetryDataRequest()
{
GeDate = DateTime.UtcNow.AddDays(-Gap)
});
if (telemetryIds == null || !telemetryIds.Any())
return;
var dateEnd = db.Set<DataSaubStat>()
.OrderByDescending(c => c.DateEnd)
.FirstOrDefault()?.DateEnd
?? DateTimeOffset.MinValue;
var detectedOperations = await db.Set<DetectedOperation>()
.Where(o => o.DateStart > dateEnd)
.Where(o => o.OperationCategory.IdParent == MechanicalDrillingCategoryId)
.Where(o => telemetryIds.Contains(o.IdTelemetry))
.OrderBy(o => o.DateStart)
.Take(500)
.ToArrayAsync(token);
if (!detectedOperations.Any())
return;
var minDate = detectedOperations.FirstOrDefault()?.DateStart;
var maxDate = detectedOperations.OrderByDescending(d => d.DateEnd).FirstOrDefault()?.DateEnd;
var telemetryDataSaub = (await db.Set<TelemetryDataSaub>()
.Where(t => t.DateTime >= minDate)
.Where(t => t.DateTime <= maxDate)
.Where(t => Math.Abs(t.BitDepth - t.WellDepth) < 0.0001)
.Where(t => telemetryIds.Contains(t.IdTelemetry))
.ToArrayAsync(token))
.GroupBy(t => t.IdTelemetry)
.ToDictionary(g => g.Key, g => g.OrderBy(t => t.DateTime).ToArray());
CreateDataSaubStat(db, detectedOperations, telemetryDataSaub);
}
private static void CreateDataSaubStat(IAsbCloudDbContext db, DetectedOperation[] detectedOperations, Dictionary<int, TelemetryDataSaub[]> telemetryDataSaub)
{
var indexStart = 0;
var indexEnd = 0;
foreach (var operation in detectedOperations)
{
if (!telemetryDataSaub.TryGetValue(operation.IdTelemetry, out TelemetryDataSaub[]? telemetryDataSaubPart))
break;
indexStart = Array.FindIndex(telemetryDataSaubPart, indexEnd, t => t.DateTime >= operation.DateStart);
indexEnd = Array.FindIndex(telemetryDataSaubPart, indexStart, t => t.DateTime > operation.DateEnd) - 1;
if (indexStart >= 0 && indexEnd >= indexStart)
{
var length = indexEnd - indexStart;
var subset = telemetryDataSaubPart.AsSpan(indexStart, length + 1);
CalcStats(operation, subset, db);
db.SaveChanges();
}
}
}
private static void CalcStats(DetectedOperation operation, Span<TelemetryDataSaub> telemetryDataSaub, IAsbCloudDbContext db)
{
var indexStart = 0;
for (var i = 1; i < telemetryDataSaub.Length; i++)
{
var previous = telemetryDataSaub[i - 1];
var current = telemetryDataSaub[i];
if (IsNewCacheItem(previous, current) || i == telemetryDataSaub.Length - 1)
{
var length = i - indexStart;
var span = telemetryDataSaub.Slice(indexStart, length);
indexStart = i;
var dataSaubStatItem = CalcStat(operation, span);
db.Set<DataSaubStat>().Add(dataSaubStatItem);
}
}
}
private static DataSaubStat CalcStat(DetectedOperation operation, Span<TelemetryDataSaub> span)
{
var hasOscillation = operation.ExtraData.TryGetValue(DetectorDrilling.ExtraDataKeyHasOscillation, out object? hasOscillationObject)
&& hasOscillationObject is true;
var aggregatedValues = CalcAggregate(span);
var processMapDrillingCacheItem = new DataSaubStat
{
DateStart = operation.DateStart,
DateEnd = operation.DateEnd,
DepthStart = operation.DepthStart,
DepthEnd = operation.DepthEnd,
Speed = (operation.DepthEnd - operation.DepthStart) / ((operation.DateEnd - operation.DateStart).TotalHours),
BlockSpeedSp = span[0].BlockSpeedSp,
Pressure = aggregatedValues.Pressure,
PressureIdle = span[0].PressureIdle,
PressureSp = span[0].PressureSp,
AxialLoad = aggregatedValues.AxialLoad,
AxialLoadSp = span[0].AxialLoadSp,
AxialLoadLimitMax = span[0].AxialLoadLimitMax,
RotorTorque = aggregatedValues.RotorTorque,
RotorTorqueSp = span[0].RotorTorqueSp,
RotorTorqueLimitMax = span[0].RotorTorqueLimitMax,
IdFeedRegulator = span[0].IdFeedRegulator,
RotorSpeed = aggregatedValues.RotorSpeed,
IdCategory = operation.IdCategory,
EnabledSubsystems = operation.EnabledSubsystems,
HasOscillation = hasOscillation,
IdTelemetry = operation.IdTelemetry,
Flow = aggregatedValues.Flow
};
return processMapDrillingCacheItem;
}
private static (
double Pressure,
double AxialLoad,
double RotorTorque,
double RotorSpeed,
double Flow
) CalcAggregate(Span<TelemetryDataSaub> span)
{
var sumPressure = 0.0;
var sumAxialLoad = 0.0;
var sumRotorTorque = 0.0;
var sumRotorSpeed = 0.0;
var flow = span[0].Flow ?? 0.0;
var diffDepthTotal = span[^1].WellDepth - span[0].WellDepth;
for (var i = 0; i < span.Length - 1; i++)
{
var weigth = span[i + 1].WellDepth - span[i].WellDepth;
sumPressure += weigth * span[i].Pressure;
sumAxialLoad += weigth * span[i].AxialLoad;
sumRotorTorque += weigth * span[i].RotorTorque;
sumRotorSpeed += weigth * span[i].RotorSpeed;
flow = span[i + 1].Flow > flow ? span[i + 1].Flow ?? 0.0 : flow;
}
return (
Pressure: sumPressure / diffDepthTotal,
AxialLoad: sumAxialLoad / diffDepthTotal,
RotorTorque: sumRotorTorque / diffDepthTotal,
RotorSpeed: sumRotorSpeed / diffDepthTotal,
Flow: flow
);
}
private static 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);
}
}
}