nit Add detectedOperations diagnostic message;

add basic telemetry validation filter before analisys;
simplify Avg calc;
fix dbcontext refCount;
This commit is contained in:
ngfrolov 2022-12-20 11:57:29 +05:00
parent 8094c8b1b8
commit f6e79e51d7
6 changed files with 50 additions and 20 deletions

View File

@ -61,27 +61,27 @@ namespace AsbCloudDb.Model
public DbSet<WITS.Record60> Record60 => Set<WITS.Record60>(); public DbSet<WITS.Record60> Record60 => Set<WITS.Record60>();
public DbSet<WITS.Record61> Record61 => Set<WITS.Record61>(); public DbSet<WITS.Record61> Record61 => Set<WITS.Record61>();
public static int ReferenceCount { get; private set; } private static int referenceCount;
public static int ReferenceCount => referenceCount;
public AsbCloudDbContext() : base() public AsbCloudDbContext() : base()
{ {
ReferenceCount++; Interlocked.Increment(ref referenceCount);
} }
public AsbCloudDbContext(DbContextOptions<AsbCloudDbContext> options) public AsbCloudDbContext(DbContextOptions<AsbCloudDbContext> options)
: base(options) : base(options)
{ {
ReferenceCount++; Interlocked.Increment(ref referenceCount);
} }
~AsbCloudDbContext() ~AsbCloudDbContext()
{ {
ReferenceCount--; Interlocked.Decrement(ref referenceCount);
} }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{ {
if (!optionsBuilder.IsConfigured) if (!optionsBuilder.IsConfigured)
optionsBuilder.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True" optionsBuilder.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True"
//, builder=>builder.EnableRetryOnFailure(2, System.TimeSpan.FromMinutes(1)) //, builder=>builder.EnableRetryOnFailure(2, System.TimeSpan.FromMinutes(1))

View File

@ -98,19 +98,33 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
protected abstract double CalcValue(DetectableTelemetry[] telemetry, int begin, int end); protected abstract double CalcValue(DetectableTelemetry[] telemetry, int begin, int end);
public static InterpolationLine MakeInterpolationLine( /// <summary>
/// Среднее арифметическое
/// </summary>
/// <param name="yGetter"></param>
/// <param name="telemetry"></param>
/// <param name="begin"></param>
/// <param name="fragmentLength"></param>
/// <returns></returns>
public static double CalcAvgAppr(
Func<DetectableTelemetry, double> yGetter, Func<DetectableTelemetry, double> yGetter,
DetectableTelemetry[] telemetry, DetectableTelemetry[] telemetry,
int begin, int begin,
int fragmentLength) int fragmentLength)
{ {
DetectableTelemetry[] data; var end = begin + fragmentLength;
if (fragmentLength > 0 && (begin + fragmentLength) < telemetry.Length) end = end < telemetry.Length
data = telemetry[begin..(begin + fragmentLength)]; ? end
else : telemetry.Length;
data = telemetry[begin..]; var subData = telemetry[begin..end].Select(yGetter);
var line = new InterpolationLine(data.Select(d => (yGetter(d), (d.DateTime - telemetry[0].DateTime).TotalHours))); if (end - begin > 10)
return line; {
var ratio = (end - begin) / 5;
subData = subData.Where((_,i) => i % ratio > 0);
}
var avg = subData.Average();
return avg;
} }
/// <summary> /// <summary>

View File

@ -1,4 +1,5 @@
using AsbCloudDb.Model; using AsbCloudDb.Model;
using System.Linq;
namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
{ {
@ -38,9 +39,9 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
if (point0.Pressure < 25) if (point0.Pressure < 25)
return IdReasonOfEnd_PressureIsLo; return IdReasonOfEnd_PressureIsLo;
var lineRotorSpeed = MakeInterpolationLine(d => d.RotorSpeed, telemetry, position, 60); var avgRotorSpeed = CalcAvgAppr(d => d.RotorSpeed, telemetry, position, 60);
if (lineRotorSpeed.IsAverageYLessThan(10)) if (avgRotorSpeed < 10)
return IdReasonOfEnd_AvgRotorSpeedIsLo; return IdReasonOfEnd_AvgRotorSpeedIsLo;
if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003)) if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003))

View File

@ -38,9 +38,9 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
if (point0.Pressure < 25) if (point0.Pressure < 25)
return IdReasonOfEnd_PressureIsLo; return IdReasonOfEnd_PressureIsLo;
var lineRotorSpeed = MakeInterpolationLine(d => d.RotorSpeed, telemetry, position, 60); var avgRotorSpeed = CalcAvgAppr(d => d.RotorSpeed, telemetry, position, 60);
if (lineRotorSpeed.IsAverageYGreaterThan(10)) if (avgRotorSpeed > 10)
return IdReasonOfEnd_AvgRotorSpeedIsHi; return IdReasonOfEnd_AvgRotorSpeedIsHi;
if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003)) if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003))

View File

@ -17,6 +17,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
{ {
private const string workId = "Operation detection"; private const string workId = "Operation detection";
private static readonly TimeSpan workPeriod = TimeSpan.FromMinutes(30); private static readonly TimeSpan workPeriod = TimeSpan.FromMinutes(30);
private static string progress = "no progress";
private static readonly DetectorAbstract[] detectors = new DetectorAbstract[] private static readonly DetectorAbstract[] detectors = new DetectorAbstract[]
{ {
@ -34,7 +35,13 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
public static WorkPeriodic MakeWork() public static WorkPeriodic MakeWork()
{ {
var workPeriodic = new WorkPeriodic(workId, WorkAction, workPeriod); var workPeriodic = new WorkPeriodic(workId, WorkAction, workPeriod);
workPeriodic.Timeout = TimeSpan.FromMinutes(30); 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; return workPeriodic;
} }
@ -86,6 +93,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
var query = db.TelemetryDataSaub var query = db.TelemetryDataSaub
.AsNoTracking() .AsNoTracking()
.Where(d => d.IdTelemetry == idTelemetry) .Where(d => d.IdTelemetry == idTelemetry)
.Where(d => d.BlockPosition >= 0)
.Select(d => new DetectableTelemetry .Select(d => new DetectableTelemetry
{ {
DateTime = d.DateTime, DateTime = d.DateTime,
@ -120,20 +128,26 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
var isDetected = false; var isDetected = false;
var positionBegin = 0; var positionBegin = 0;
var positionEnd = data.Length - gap; var positionEnd = data.Length - gap;
var step = 10;
while (positionEnd > positionBegin) while (positionEnd > positionBegin)
{ {
step ++;
for (int i = 0; i < detectors.Length; i++) for (int i = 0; i < detectors.Length; i++)
{ {
progress = $"telemetry:{idTelemetry}, date:{startDate}, pos:{positionBegin}, detector:{detectors[i]}";
if (detectors[i].TryDetect(idTelemetry, data, positionBegin, positionEnd, lastDetectedOperation, out OperationDetectorResult? result)) if (detectors[i].TryDetect(idTelemetry, data, positionBegin, positionEnd, lastDetectedOperation, out OperationDetectorResult? result))
{ {
detectedOperations.Add(result!.Operation); detectedOperations.Add(result!.Operation);
lastDetectedOperation = result.Operation; lastDetectedOperation = result.Operation;
isDetected = true; isDetected = true;
step = 1;
positionBegin = result.TelemetryEnd; positionBegin = result.TelemetryEnd;
break; break;
} }
} }
positionBegin++; if (step > 20)
step = 10;
positionBegin += step;
} }
if (isDetected) if (isDetected)

View File

@ -38,6 +38,7 @@ namespace AsbCloudInfrastructure.Services.SAUB
{ {
using var db = MakeContext(configuration); using var db = MakeContext(configuration);
instance.InitializeCacheFromDB<TEntity>(db); instance.InitializeCacheFromDB<TEntity>(db);
db.Dispose();
}); });
} }
return instance; return instance;