DD.WellWorkover.Cloud/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionWorkFactory.cs

164 lines
6.5 KiB
C#
Raw Normal View History

2022-04-22 17:17:38 +05:00
using AsbCloudDb.Model;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
2022-06-15 14:57:37 +05:00
using System.Diagnostics;
2022-04-22 17:17:38 +05:00
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AsbCloudInfrastructure.Services.DetectOperations.Detectors;
using AsbCloudInfrastructure.Background;
using Microsoft.Extensions.DependencyInjection;
2022-04-22 17:17:38 +05:00
namespace AsbCloudInfrastructure.Services.DetectOperations
{
2022-07-04 17:33:32 +05:00
#nullable enable
public static class OperationDetectionWorkFactory
2022-04-22 17:17:38 +05:00
{
private const string workId = "Operation detection";
private static readonly TimeSpan workPeriod = TimeSpan.FromMinutes(30);
private static string progress = "no progress";
2022-04-22 17:17:38 +05:00
2022-06-30 17:37:57 +05:00
private static readonly DetectorAbstract[] detectors = new DetectorAbstract[]
{
2022-06-30 17:37:57 +05:00
new DetectorRotor(),
new DetectorSlide(),
2023-02-15 18:03:23 +05:00
//new DetectorDevelopment(),
//new DetectorTemplating(),
2022-06-30 17:37:57 +05:00
new DetectorSlipsTime(),
2023-02-15 18:03:23 +05:00
//new DetectorStaticSurveying(),
//new DetectorFlashingBeforeConnection(),
//new DetectorFlashing(),
//new DetectorTemplatingWhileDrilling(),
};
public static WorkPeriodic MakeWork()
{
var workPeriodic = new WorkPeriodic(workId, WorkAction, workPeriod);
workPeriodic.Timeout = TimeSpan.FromSeconds(15 * 60);
workPeriodic.OnErrorAsync = (id, exception, token) =>
{
var text = $"work {id}, when {progress}, throw error:{exception.Message}";
Trace.TraceWarning(text);
return Task.CompletedTask;
};
return workPeriodic;
}
// TODO: Разделить этот акшн на более мелкие части И использовать telemetryServiceData<..> вместо прямого обращения к БД.
private static async Task WorkAction(string _, IServiceProvider serviceProvider, CancellationToken token)
{
using var db = serviceProvider.GetRequiredService<IAsbCloudDbContext>();
2022-04-28 15:04:13 +05:00
var lastDetectedDates = await db.DetectedOperations
.GroupBy(o => o.IdTelemetry)
.Select(g => new
{
IdTelemetry = g.Key,
LastDate = g.Max(o => o.DateEnd)
2022-04-28 15:04:13 +05:00
})
.ToListAsync(token);
var telemetryIds = await db.Telemetries
.Where(t => t.Info != null && t.TimeZone != null)
.Select(t => t.Id)
.ToListAsync(token);
var joinedlastDetectedDates = telemetryIds
2022-04-28 15:04:13 +05:00
.GroupJoin(lastDetectedDates,
t => t,
o => o.IdTelemetry,
2022-06-15 14:57:37 +05:00
(outer, inner) => new
{
2022-04-28 15:04:13 +05:00
IdTelemetry = outer,
2022-07-04 17:50:16 +05:00
inner.SingleOrDefault()?.LastDate,
2022-04-28 15:04:13 +05:00
});
var affected = 0;
foreach (var item in joinedlastDetectedDates)
{
var stopwatch = Stopwatch.StartNew();
2022-06-15 14:57:37 +05:00
var newOperations = await DetectOperationsAsync(item.IdTelemetry, item.LastDate ?? DateTimeOffset.MinValue, db, token);
stopwatch.Stop();
if (newOperations.Any())
{
db.DetectedOperations.AddRange(newOperations);
affected += await db.SaveChangesAsync(token);
2022-06-15 14:57:37 +05:00
}
}
2022-04-22 17:17:38 +05:00
}
2022-07-04 17:50:16 +05:00
private static async Task<IEnumerable<DetectedOperation>> DetectOperationsAsync(int idTelemetry, DateTimeOffset begin, IAsbCloudDbContext db, CancellationToken token)
2022-04-22 17:17:38 +05:00
{
var query = db.TelemetryDataSaub
.AsNoTracking()
.Where(d => d.IdTelemetry == idTelemetry)
.Where(d => d.BlockPosition >= 0)
2022-06-15 14:57:37 +05:00
.Select(d => new DetectableTelemetry
{
2022-04-22 17:17:38 +05:00
DateTime = d.DateTime,
IdUser = d.IdUser,
2022-07-04 17:33:32 +05:00
WellDepth = d.WellDepth ?? float.NaN,
Pressure = d.Pressure ?? float.NaN,
HookWeight = d.HookWeight ?? float.NaN,
BlockPosition = d.BlockPosition ?? float.NaN,
BitDepth = d.BitDepth ?? float.NaN,
RotorSpeed = d.RotorSpeed ?? float.NaN,
2022-04-22 17:17:38 +05:00
})
.OrderBy(d => d.DateTime);
2022-06-27 12:43:55 +05:00
var take = 4 * 86_400; // 4 дня
2022-04-22 17:17:38 +05:00
var startDate = begin;
var detectedOperations = new List<DetectedOperation>(8);
2022-06-30 17:37:57 +05:00
DetectedOperation? lastDetectedOperation = null;
const int minOperationLength = 5;
const int maxDetectorsInterpolationFrameLength = 30;
const int gap = maxDetectorsInterpolationFrameLength + minOperationLength;
2022-04-22 17:17:38 +05:00
while (true)
{
var data = await query
.Where(d => d.DateTime > startDate)
.Take(take)
.ToArrayAsync(token);
2022-06-30 17:37:57 +05:00
if (data.Length < gap)
2022-04-22 17:17:38 +05:00
break;
var isDetected = false;
var positionBegin = 0;
2022-06-30 17:37:57 +05:00
var positionEnd = data.Length - gap;
var step = 10;
2022-06-30 17:37:57 +05:00
while (positionEnd > positionBegin)
2022-04-22 17:17:38 +05:00
{
step ++;
2022-06-30 17:37:57 +05:00
for (int i = 0; i < detectors.Length; i++)
2022-04-22 17:17:38 +05:00
{
progress = $"telemetry:{idTelemetry}, date:{startDate}, pos:{positionBegin}, detector:{detectors[i]}";
2022-07-04 17:33:32 +05:00
if (detectors[i].TryDetect(idTelemetry, data, positionBegin, positionEnd, lastDetectedOperation, out OperationDetectorResult? result))
2022-04-22 17:17:38 +05:00
{
2022-07-04 17:33:32 +05:00
detectedOperations.Add(result!.Operation);
2022-06-30 17:37:57 +05:00
lastDetectedOperation = result.Operation;
isDetected = true;
step = 1;
2022-06-30 17:37:57 +05:00
positionBegin = result.TelemetryEnd;
break;
2022-04-22 17:17:38 +05:00
}
}
if (step > 20)
step = 10;
positionBegin += step;
2022-04-22 17:17:38 +05:00
}
if (isDetected)
2022-07-04 17:33:32 +05:00
startDate = lastDetectedOperation!.DateEnd;
else
2022-06-30 17:37:57 +05:00
startDate = data[positionEnd].DateTime;
2022-04-22 17:17:38 +05:00
}
return detectedOperations;
}
}
2022-07-04 17:33:32 +05:00
#nullable disable
2022-04-22 17:17:38 +05:00
}