From f6e79e51d7f8d8631bbac42d98472d5394e002bb Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Tue, 20 Dec 2022 11:57:29 +0500 Subject: [PATCH] nit Add detectedOperations diagnostic message; add basic telemetry validation filter before analisys; simplify Avg calc; fix dbcontext refCount; --- AsbCloudDb/Model/AsbCloudDbContext.cs | 12 ++++---- .../Detectors/DetectorAbstract.cs | 30 ++++++++++++++----- .../Detectors/DetectorRotor.cs | 5 ++-- .../Detectors/DetectorSlide.cs | 4 +-- .../OperationDetectionWorkFactory.cs | 18 +++++++++-- .../Services/SAUB/TelemetryDataCache.cs | 1 + 6 files changed, 50 insertions(+), 20 deletions(-) diff --git a/AsbCloudDb/Model/AsbCloudDbContext.cs b/AsbCloudDb/Model/AsbCloudDbContext.cs index 8354d87f..661c8395 100644 --- a/AsbCloudDb/Model/AsbCloudDbContext.cs +++ b/AsbCloudDb/Model/AsbCloudDbContext.cs @@ -61,27 +61,27 @@ namespace AsbCloudDb.Model public DbSet Record60 => Set(); public DbSet Record61 => Set(); - public static int ReferenceCount { get; private set; } + private static int referenceCount; + public static int ReferenceCount => referenceCount; public AsbCloudDbContext() : base() { - ReferenceCount++; + Interlocked.Increment(ref referenceCount); } public AsbCloudDbContext(DbContextOptions options) : base(options) { - ReferenceCount++; + Interlocked.Increment(ref referenceCount); } ~AsbCloudDbContext() { - ReferenceCount--; + Interlocked.Decrement(ref referenceCount); } protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) - { - + { if (!optionsBuilder.IsConfigured) optionsBuilder.UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True" //, builder=>builder.EnableRetryOnFailure(2, System.TimeSpan.FromMinutes(1)) diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs index e3c690a0..ab96181a 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorAbstract.cs @@ -98,19 +98,33 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors protected abstract double CalcValue(DetectableTelemetry[] telemetry, int begin, int end); - public static InterpolationLine MakeInterpolationLine( + /// + /// Среднее арифметическое + /// + /// + /// + /// + /// + /// + public static double CalcAvgAppr( Func yGetter, DetectableTelemetry[] telemetry, int begin, int fragmentLength) { - DetectableTelemetry[] data; - if (fragmentLength > 0 && (begin + fragmentLength) < telemetry.Length) - data = telemetry[begin..(begin + fragmentLength)]; - else - data = telemetry[begin..]; - var line = new InterpolationLine(data.Select(d => (yGetter(d), (d.DateTime - telemetry[0].DateTime).TotalHours))); - return line; + var end = begin + fragmentLength; + end = end < telemetry.Length + ? end + : telemetry.Length; + var subData = telemetry[begin..end].Select(yGetter); + if (end - begin > 10) + { + var ratio = (end - begin) / 5; + subData = subData.Where((_,i) => i % ratio > 0); + } + + var avg = subData.Average(); + return avg; } /// diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorRotor.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorRotor.cs index 78a6806f..8cb2be50 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorRotor.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorRotor.cs @@ -1,4 +1,5 @@ using AsbCloudDb.Model; +using System.Linq; namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors { @@ -38,9 +39,9 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors if (point0.Pressure < 25) 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; if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003)) diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorSlide.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorSlide.cs index 173b6341..1e2bd081 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorSlide.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/DetectorSlide.cs @@ -38,9 +38,9 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors if (point0.Pressure < 25) 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; if (!DeviatesFromBegin(telemetry, t => t.WellDepth, position, 150, 0.003)) diff --git a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionWorkFactory.cs b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionWorkFactory.cs index 8ff6ebb8..0bf4f258 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionWorkFactory.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionWorkFactory.cs @@ -17,6 +17,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations { private const string workId = "Operation detection"; private static readonly TimeSpan workPeriod = TimeSpan.FromMinutes(30); + private static string progress = "no progress"; private static readonly DetectorAbstract[] detectors = new DetectorAbstract[] { @@ -34,7 +35,13 @@ namespace AsbCloudInfrastructure.Services.DetectOperations public static WorkPeriodic MakeWork() { 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; } @@ -86,6 +93,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations var query = db.TelemetryDataSaub .AsNoTracking() .Where(d => d.IdTelemetry == idTelemetry) + .Where(d => d.BlockPosition >= 0) .Select(d => new DetectableTelemetry { DateTime = d.DateTime, @@ -120,20 +128,26 @@ namespace AsbCloudInfrastructure.Services.DetectOperations var isDetected = false; var positionBegin = 0; var positionEnd = data.Length - gap; + var step = 10; while (positionEnd > positionBegin) { + step ++; 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)) { detectedOperations.Add(result!.Operation); lastDetectedOperation = result.Operation; isDetected = true; + step = 1; positionBegin = result.TelemetryEnd; break; } } - positionBegin++; + if (step > 20) + step = 10; + positionBegin += step; } if (isDetected) diff --git a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs index 00c583c1..56e214fd 100644 --- a/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs +++ b/AsbCloudInfrastructure/Services/SAUB/TelemetryDataCache.cs @@ -38,6 +38,7 @@ namespace AsbCloudInfrastructure.Services.SAUB { using var db = MakeContext(configuration); instance.InitializeCacheFromDB(db); + db.Dispose(); }); } return instance;