From a589775ceb3314ee4cb827de5d44397d464a667e Mon Sep 17 00:00:00 2001 From: KharchenkoVV Date: Thu, 30 Sep 2021 16:41:00 +0500 Subject: [PATCH] Fixed telemetry analytics calculation logic --- AsbCloudApp/Services/ISaubDataCache.cs | 12 -- .../Services/ITelemetryAnalyticsService.cs | 2 +- AsbCloudDbDemoData/ControllerLoadTester.cs | 2 + AsbCloudInfrastructure/DependencyInjection.cs | 1 - .../Services/InterpolationLine.cs | 74 +++---- .../Services/SaubDataCache.cs | 32 --- .../TelemetryAnalyticsBackgroundService.cs | 25 +-- .../Services/TelemetryAnalyticsService.cs | 196 ++++++++++-------- .../Services/TelemetryOperationDetector.cs | 5 +- .../TelemetryOperationDetectorService.cs | 21 +- 10 files changed, 166 insertions(+), 204 deletions(-) delete mode 100644 AsbCloudApp/Services/ISaubDataCache.cs delete mode 100644 AsbCloudInfrastructure/Services/SaubDataCache.cs diff --git a/AsbCloudApp/Services/ISaubDataCache.cs b/AsbCloudApp/Services/ISaubDataCache.cs deleted file mode 100644 index aa3c86bb..00000000 --- a/AsbCloudApp/Services/ISaubDataCache.cs +++ /dev/null @@ -1,12 +0,0 @@ -using AsbCloudApp.Data; -using System.Collections.Generic; - -namespace AsbCloudApp.Services -{ - public interface ISaubDataCache - { - TelemetryAnalysisDto CurrentAnalysis { get; set; } - IEnumerable GetOrCreateCache(int telemetryId); - void AddData(TelemetryDataSaubDto data); - } -} diff --git a/AsbCloudApp/Services/ITelemetryAnalyticsService.cs b/AsbCloudApp/Services/ITelemetryAnalyticsService.cs index 8b4f86bf..9851cf4a 100644 --- a/AsbCloudApp/Services/ITelemetryAnalyticsService.cs +++ b/AsbCloudApp/Services/ITelemetryAnalyticsService.cs @@ -23,7 +23,7 @@ namespace AsbCloudApp.Services Task> GetOperationsToIntervalAsync(int idWell, int intervalHoursTimestamp, int workBeginTimestamp, CancellationToken token = default); - void SaveAnalytics(); + void CalculateAnalytics(); Task GetOperationsDateRangeAsync(int idWell, CancellationToken token = default); } diff --git a/AsbCloudDbDemoData/ControllerLoadTester.cs b/AsbCloudDbDemoData/ControllerLoadTester.cs index 793efbaa..8f8e63b1 100644 --- a/AsbCloudDbDemoData/ControllerLoadTester.cs +++ b/AsbCloudDbDemoData/ControllerLoadTester.cs @@ -13,6 +13,8 @@ namespace AsbCloudDevOperations { private readonly HttpClient client = new(); + //Пример роута: "api/telemetryDataSaub/123123_"; + public void TestControllerRoute(string route, CancellationToken token = default) { var host = "http://localhost:5000"; diff --git a/AsbCloudInfrastructure/DependencyInjection.cs b/AsbCloudInfrastructure/DependencyInjection.cs index 408f9c72..148defec 100644 --- a/AsbCloudInfrastructure/DependencyInjection.cs +++ b/AsbCloudInfrastructure/DependencyInjection.cs @@ -24,7 +24,6 @@ namespace AsbCloudInfrastructure services.AddSingleton(new CacheDb()); services.AddSingleton(); services.AddSingleton(); - services.AddSingleton(); services.AddTransient(); services.AddTransient(); diff --git a/AsbCloudInfrastructure/Services/InterpolationLine.cs b/AsbCloudInfrastructure/Services/InterpolationLine.cs index 75a36e75..456aeeb1 100644 --- a/AsbCloudInfrastructure/Services/InterpolationLine.cs +++ b/AsbCloudInfrastructure/Services/InterpolationLine.cs @@ -1,62 +1,48 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace AsbCloudInfrastructure.Services { public class InterpolationLine { - private readonly decimal xSum; - private readonly decimal ySum; - private readonly decimal xySum; - private readonly decimal x2Sum; - private readonly IEnumerable<(double value, double timestamp)> rawData = new List<(double value, double timestamp)>(); + private readonly double xSum; + private readonly double ySum; + private readonly double xySum; + private readonly double x2Sum; + private readonly int count; - public InterpolationLine(IEnumerable<(double value, double timestamp)> rawData) + public InterpolationLine(IEnumerable<(double Y, double X)> rawData) { - var data = rawData.Select((d) => new - { - X = d.timestamp, - Y = d.value - }); - xSum = (decimal)data.Sum(d => d.X); - ySum = (decimal)data.Sum(d => d.Y); - xySum = (decimal)data.Sum(d => d.X * d.Y); - x2Sum = (decimal)data.Sum(d => d.X * d.X); - - this.rawData = rawData; - } - public double GetAForLinearFormula() - { - var result = (xSum * ySum - rawData.Count() * xySum) / - (xSum * xSum - rawData.Count() * x2Sum); - - return (double)result; + xSum = rawData.Sum(d => d.X); + ySum = rawData.Sum(d => d.Y); + xySum = rawData.Sum(d => d.X * d.Y); + x2Sum = rawData.Sum(d => d.X * d.X); + count = rawData.Count(); } - public double GetBForLinearFormula() - { - var result = (xSum * xySum - x2Sum * ySum) / - (xSum * xSum - rawData.Count() * x2Sum); + public double A => + (xSum * ySum - count * xySum) / + (xSum * xSum - count * x2Sum); - return (double)result; - } + public double B => + (xSum * xySum - x2Sum * ySum) / + (xSum * xSum - count * x2Sum); - public static bool IsValueNotChanges(double value, - (double upperBound, double lowerBound) bounds) => - value < bounds.upperBound && value > bounds.lowerBound; - public static bool IsValueIncreases(double value, double bound) => - value > bound; + public bool IsYNotChanges(double upperBound = 0d, double lowerBound = 0d) => + A < upperBound && A > lowerBound; - public static bool IsValueDecreases(double value, double bound) => - value < bound; + public bool IsYIncreases(double bound = 0d) => + A > bound; - public static bool IsAverageLessThanBound(IEnumerable<(double Value, double TotalSeconds)> values, - double bound) => - (values.Sum(s => s.Value) / values.Count()) < bound; + public bool IsYDecreases(double bound = 0d) => + A < bound; - public static bool IsAverageMoreThanBound(IEnumerable<(double Value, double TotalSeconds)> values, - double bound) => - (values.Sum(s => s.Value) / values.Count()) >= bound; + public bool IsAverageYLessThanBound(double bound) => + (ySum / count) < bound; + + public bool IsAverageYMoreThanBound(double bound) => + (ySum / count) >= bound; } } diff --git a/AsbCloudInfrastructure/Services/SaubDataCache.cs b/AsbCloudInfrastructure/Services/SaubDataCache.cs deleted file mode 100644 index 29278925..00000000 --- a/AsbCloudInfrastructure/Services/SaubDataCache.cs +++ /dev/null @@ -1,32 +0,0 @@ -using AsbCloudApp.Data; -using AsbCloudApp.Services; -using System.Collections.Generic; - -namespace AsbCloudInfrastructure.Services -{ - public class SaubDataCache : ISaubDataCache - { - public TelemetryAnalysisDto CurrentAnalysis { get; set; } - - private readonly Dictionary> saubData = - new Dictionary>(); - - public IEnumerable GetOrCreateCache(int telemetryId) - { - if (!saubData.ContainsKey(telemetryId)) - saubData[telemetryId] = new List(); - - return saubData[telemetryId]; - } - - public void AddData(TelemetryDataSaubDto data) - { - GetOrCreateCache(data.IdTelemetry); - - saubData[data.IdTelemetry].Add(data); - - if (saubData[data.IdTelemetry].Count > 10) - saubData[data.IdTelemetry].RemoveAt(1); - } - } -} diff --git a/AsbCloudInfrastructure/Services/TelemetryAnalyticsBackgroundService.cs b/AsbCloudInfrastructure/Services/TelemetryAnalyticsBackgroundService.cs index 33594ddb..dd5afd0c 100644 --- a/AsbCloudInfrastructure/Services/TelemetryAnalyticsBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryAnalyticsBackgroundService.cs @@ -12,27 +12,15 @@ namespace AsbCloudInfrastructure.Services { public class TelemetryAnalyticsBackgroundService : BackgroundService { - private static readonly DbContextOptions options = - new DbContextOptionsBuilder() - .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") - .Options; - private readonly AsbCloudDbContext context = new AsbCloudDbContext(options); - private readonly CacheDb cacheDb; - private readonly ISaubDataCache saubDataCache; - public TelemetryAnalyticsBackgroundService(ISaubDataCache saubDataCache, CacheDb cacheDb) + public TelemetryAnalyticsBackgroundService(CacheDb cacheDb) { - this.saubDataCache = saubDataCache; this.cacheDb = cacheDb; } protected override async Task ExecuteAsync(CancellationToken token = default) { - var telemetryService = new TelemetryService(context, cacheDb); - var analyticsService = new TelemetryAnalyticsService(context, - telemetryService, saubDataCache, cacheDb); - var timeToStartAnalysis = DateTime.Now; while (!token.IsCancellationRequested) @@ -41,9 +29,18 @@ namespace AsbCloudInfrastructure.Services { try { + var options = new DbContextOptionsBuilder() + .UseNpgsql("Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True") + .Options; + using var context = new AsbCloudDbContext(options); + + var telemetryService = new TelemetryService(context, cacheDb); + var analyticsService = new TelemetryAnalyticsService(context, + telemetryService, cacheDb); + timeToStartAnalysis = DateTime.Now.AddHours(1); - await Task.Run(() => analyticsService.SaveAnalytics(), token) + await Task.Run(() => analyticsService.CalculateAnalytics(), token) .ConfigureAwait(false); } catch (Exception ex) diff --git a/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs b/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs index 986d4d3b..62f0ce8a 100644 --- a/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryAnalyticsService.cs @@ -2,7 +2,6 @@ using AsbCloudApp.Services; using AsbCloudDb.Model; using AsbCloudInfrastructure.Services.Cache; -using Mapster; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; @@ -16,17 +15,17 @@ namespace AsbCloudInfrastructure.Services { private readonly IAsbCloudDbContext db; private readonly ITelemetryService telemetryService; - private readonly ISaubDataCache saubDataCache; private readonly CacheTable cacheOperations; private readonly TelemetryOperationDetectorService operationDetectorService; private readonly IEnumerable operations; + private const int intervalHours = 12; + public TelemetryAnalyticsService(IAsbCloudDbContext db, ITelemetryService telemetryService, - ISaubDataCache saubDataCache, CacheDb cacheDb) + CacheDb cacheDb) { this.db = db; this.telemetryService = telemetryService; - this.saubDataCache = saubDataCache; cacheOperations = cacheDb.GetCachedTable((AsbCloudDbContext)db); operations = cacheOperations.Where(); operationDetectorService = new TelemetryOperationDetectorService(operations); @@ -231,7 +230,7 @@ namespace AsbCloudInfrastructure.Services return groupedOperationsList; } - public void SaveAnalytics() + public void CalculateAnalytics() { var allTelemetryIds = (from telemetry in db.TelemetryDataSaub select telemetry.IdTelemetry) @@ -242,47 +241,58 @@ namespace AsbCloudInfrastructure.Services { var analyzeStartDate = GetAnalyzeStartDate(idTelemetry); - if(analyzeStartDate != default) + if (analyzeStartDate == default) + continue; + + TelemetryAnalysis currentAnalysis = null; + + while (TryGetDataSaubPart(idTelemetry, analyzeStartDate, + out var dataSaubPart)) { - while (GetDataSaubsToAnalyze(idTelemetry, analyzeStartDate).Any()) + analyzeStartDate = dataSaubPart.Last().Date; + + var count = dataSaubPart.Count(); + var skip = 0; + var step = 10; + var take = step * 2; + + for(;skip < count; skip += step) { - var dataSaubsToAnalyze = GetDataSaubsToAnalyze(idTelemetry, analyzeStartDate); + var analyzingSaubs = dataSaubPart.Skip(skip).Take(take); - analyzeStartDate = dataSaubsToAnalyze.Last().Date; - - foreach (var dataSaub in dataSaubsToAnalyze) + if (analyzingSaubs.Count() <= 1) { - var dataSaubDto = dataSaub.Adapt(); - - saubDataCache.AddData(dataSaubDto); - - if (saubDataCache.GetOrCreateCache(dataSaubDto.IdTelemetry).Count() > 1) - { - var dataSaubs = saubDataCache.GetOrCreateCache(dataSaubDto.IdTelemetry) - .OrderBy(d => d.Date); - - var telemetryAnalysisDto = GetDrillingAnalysis(dataSaubs); - - if (saubDataCache.CurrentAnalysis is null) - saubDataCache.CurrentAnalysis = telemetryAnalysisDto; - - if (saubDataCache.CurrentAnalysis.IdOperation == telemetryAnalysisDto.IdOperation) - { - saubDataCache.CurrentAnalysis.DurationSec += - telemetryAnalysisDto.DurationSec; - saubDataCache.CurrentAnalysis.OperationEndDepth = dataSaubDto.WellDepth; - } - else - { - db.TelemetryAnalysis.Add(saubDataCache.CurrentAnalysis.Adapt()); - saubDataCache.CurrentAnalysis = telemetryAnalysisDto; - saubDataCache.CurrentAnalysis.OperationStartDepth = dataSaubDto.WellDepth; - } - } + continue; } - db.SaveChanges(); + var dataSaub = analyzingSaubs.First(); + + var telemetryAnalysis = new TelemetryAnalysis(); + + telemetryAnalysis = GetDrillingAnalysis(analyzingSaubs); + + if (currentAnalysis is null) + { + currentAnalysis = telemetryAnalysis; + currentAnalysis.OperationStartDepth = dataSaub.WellDepth; + } + + if (currentAnalysis.IdOperation == telemetryAnalysis.IdOperation) + { + currentAnalysis.DurationSec += telemetryAnalysis.DurationSec; + currentAnalysis.OperationEndDepth = dataSaub.WellDepth; + } + else + { + db.TelemetryAnalysis.Add(currentAnalysis); + currentAnalysis = telemetryAnalysis; + currentAnalysis.OperationStartDepth = dataSaub.WellDepth; + } } + + db.SaveChanges(); + + GC.Collect(); } } } @@ -332,24 +342,36 @@ namespace AsbCloudInfrastructure.Services ? DateTimeOffset.MinValue : DateTimeOffset.FromUnixTimeSeconds(lastAnalysisUnixDate); - var firstDataSaub = (from ds in db.TelemetryDataSaub - where ds.IdTelemetry == idTelemetry && - ds.Date >= analyzeStartDate - select ds).FirstOrDefault(); + var firstDataSaub = GetFirstSaub(analyzeStartDate.DateTime, idTelemetry); var firstSaubUtcTime = firstDataSaub?.Date.ToUniversalTime() ?? default; return firstSaubUtcTime; } - private IEnumerable GetDataSaubsToAnalyze(int idTelemetry, - DateTime analyzeStartDate) + private bool TryGetDataSaubPart(int idTelemetry, DateTime analyzeStartDate, + out IEnumerable dataSaubPart) + { + var query = (from ds in db.TelemetryDataSaub + where ds.IdTelemetry == idTelemetry + && ds.Date > analyzeStartDate + orderby ds.Date + select ds) + .Take(12 * 60* 60) + .AsNoTracking(); + + dataSaubPart = query.ToList(); + + return dataSaubPart.Any(); + } + + private TelemetryDataSaub GetFirstSaub(DateTime analyzeStartDate, int idTelemetry) { return (from ds in db.TelemetryDataSaub where ds.IdTelemetry == idTelemetry - && ds.Date > analyzeStartDate - && ds.Date < analyzeStartDate.AddHours(12) - select ds).ToList(); + && ds.Date > analyzeStartDate.AddHours(intervalHours) + orderby ds.Date + select ds).FirstOrDefault(); } private static IEnumerable<(long IntervalStart, string OperationName, int OperationDuration)> DivideOperationsByIntervalLength( @@ -449,64 +471,66 @@ namespace AsbCloudInfrastructure.Services return groupedOperationsList; } - private TelemetryAnalysisDto GetDrillingAnalysis(IEnumerable dataSaubBases) + private TelemetryAnalysis GetDrillingAnalysis(IEnumerable dataSaubBases) { var lastSaubDate = dataSaubBases.Last().Date; var saubWellDepths = dataSaubBases.Where(sw => sw.WellDepth is not null) - .Select(s => (Value: (double)s.WellDepth, - (s.Date - dataSaubBases.First().Date).TotalSeconds)); + .Select(s => (Y: (double)s.WellDepth, + X: (s.Date - dataSaubBases.First().Date).TotalSeconds)); var saubBitDepths = dataSaubBases.Where(sw => sw.BitDepth is not null) - .Select(s => (Value: (double)s.BitDepth, - (s.Date - dataSaubBases.First().Date).TotalSeconds)); + .Select(s => (Y: (double)s.BitDepth, + X: (s.Date - dataSaubBases.First().Date).TotalSeconds)); var saubBlockPositions = dataSaubBases.Where(sw => sw.BlockPosition is not null) - .Select(s => (Value: (double)s.BlockPosition, - (s.Date - dataSaubBases.First().Date).TotalSeconds)); + .Select(s => (Y: (double)s.BlockPosition, + X: (s.Date - dataSaubBases.First().Date).TotalSeconds)); var saubRotorSpeeds = dataSaubBases.Where(sw => sw.RotorSpeed is not null) - .Select(s => (Value: (double)s.RotorSpeed, - (s.Date - dataSaubBases.First().Date).TotalSeconds)); + .Select(s => (Y: (double)s.RotorSpeed, + X: (s.Date - dataSaubBases.First().Date).TotalSeconds)); var saubPressures = dataSaubBases.Where(sw => sw.Pressure is not null) - .Select(s => (Value: (double)s.Pressure, - (s.Date - dataSaubBases.First().Date).TotalSeconds)); + .Select(s => (Y: (double)s.Pressure, + X: (s.Date - dataSaubBases.First().Date).TotalSeconds)); var saubHookWeights = dataSaubBases.Where(sw => sw.HookWeight is not null) - .Select(s => (Value: (double)s.HookWeight, - (s.Date - dataSaubBases.First().Date).TotalSeconds)); + .Select(s => (Y: (double)s.HookWeight, + X: (s.Date - dataSaubBases.First().Date).TotalSeconds)); - var wellDepthChangingIndex = new InterpolationLine(saubWellDepths).GetAForLinearFormula(); - var bitPositionChangingIndex = new InterpolationLine(saubBitDepths).GetAForLinearFormula(); - var blockPositionChangingIndex = new InterpolationLine(saubBlockPositions).GetAForLinearFormula(); - var rotorSpeedChangingIndex = new InterpolationLine(saubRotorSpeeds).GetAForLinearFormula(); - var pressureChangingIndex = new InterpolationLine(saubPressures).GetAForLinearFormula(); - var hookWeightChangingIndex = new InterpolationLine(saubHookWeights).GetAForLinearFormula(); + var wellDepthLine = new InterpolationLine(saubWellDepths); + var bitPositionLine = new InterpolationLine(saubBitDepths); + var blockPositionLine = new InterpolationLine(saubBlockPositions); + var rotorSpeedLine = new InterpolationLine(saubRotorSpeeds); + var pressureLine = new InterpolationLine(saubPressures); + var hookWeightLine = new InterpolationLine(saubHookWeights); - var IsBlockRising = InterpolationLine.IsValueDecreases(blockPositionChangingIndex, -0.0001); - var IsBlockGoesDown = InterpolationLine.IsValueIncreases(blockPositionChangingIndex, 0.0001); - var IsBlockStandsStill = InterpolationLine.IsValueNotChanges(blockPositionChangingIndex, (0.0001, -0.0001)); + var IsBlockRising = blockPositionLine.IsYDecreases(-0.0001); + var IsBlockGoesDown = blockPositionLine.IsYIncreases(0.0001); + var IsBlockStandsStill = blockPositionLine.IsYNotChanges(0.0001, -0.0001); - var drillingAnalysis = new TelemetryAnalysisDto + var drillingAnalysis = new TelemetryAnalysis { IdTelemetry = dataSaubBases.First().IdTelemetry, UnixDate = (long)(lastSaubDate - new DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds, - DurationSec = (int)(dataSaubBases.Last().Date - dataSaubBases.ElementAt(dataSaubBases.Count() - 2).Date).TotalSeconds, + DurationSec = (int)(dataSaubBases.Last().Date - + dataSaubBases.ElementAt(dataSaubBases.Count() - 2).Date).TotalSeconds, OperationStartDepth = null, OperationEndDepth = null, - IsWellDepthDecreasing = InterpolationLine.IsValueDecreases(wellDepthChangingIndex, -0.0001), - IsWellDepthIncreasing = InterpolationLine.IsValueIncreases(wellDepthChangingIndex, 0.0001), - IsBitPositionDecreasing = InterpolationLine.IsValueDecreases(bitPositionChangingIndex, -0.0001), - IsBitPositionIncreasing = InterpolationLine.IsValueIncreases(bitPositionChangingIndex, 0.0001), - IsBitPositionLt20 = InterpolationLine.IsAverageLessThanBound(saubBitDepths, 20), - IsBlockPositionDecreasing = InterpolationLine.IsValueDecreases(blockPositionChangingIndex, -0.0001), - IsBlockPositionIncreasing = InterpolationLine.IsValueIncreases(blockPositionChangingIndex, 0.0001), - IsRotorSpeedLt3 = InterpolationLine.IsAverageLessThanBound(saubRotorSpeeds, 3), - IsRotorSpeedGt3 = InterpolationLine.IsAverageMoreThanBound(saubRotorSpeeds, 3), - IsPressureLt20 = InterpolationLine.IsAverageLessThanBound(saubPressures, 20), - IsPressureGt20 = InterpolationLine.IsAverageMoreThanBound(saubPressures, 20), - IsHookWeightNotChanges = InterpolationLine.IsValueNotChanges(hookWeightChangingIndex, (0.0001, -0.0001)), - IsHookWeightLt3 = InterpolationLine.IsAverageLessThanBound(saubHookWeights, 3), + IsWellDepthDecreasing = wellDepthLine.IsYDecreases(-0.0001), + IsWellDepthIncreasing = wellDepthLine.IsYIncreases( 0.0001), + IsBitPositionDecreasing = bitPositionLine.IsYDecreases(-0.0001), + IsBitPositionIncreasing = bitPositionLine.IsYIncreases(0.0001), + IsBitPositionLt20 = bitPositionLine.IsAverageYLessThanBound(20), + IsBlockPositionDecreasing = blockPositionLine.IsYDecreases(-0.0001), + IsBlockPositionIncreasing = blockPositionLine.IsYIncreases(0.0001), + IsRotorSpeedLt3 = rotorSpeedLine.IsAverageYLessThanBound(3), + IsRotorSpeedGt3 = rotorSpeedLine.IsAverageYMoreThanBound(3), + IsPressureLt20 = pressureLine.IsAverageYLessThanBound(20), + IsPressureGt20 = pressureLine.IsAverageYMoreThanBound(20), + IsHookWeightNotChanges = hookWeightLine.IsYNotChanges(0.0001, -0.0001), + IsHookWeightLt3 = hookWeightLine.IsAverageYLessThanBound(3), IdOperation = 1 }; - drillingAnalysis.IdOperation = operationDetectorService.DetectOperation(drillingAnalysis).Id; + drillingAnalysis.IdOperation = + operationDetectorService.DetectOperation(drillingAnalysis).Id; return drillingAnalysis; } diff --git a/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs b/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs index c618b0f1..73c90af6 100644 --- a/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs +++ b/AsbCloudInfrastructure/Services/TelemetryOperationDetector.cs @@ -1,5 +1,4 @@ -using AsbCloudApp.Data; -using AsbCloudDb.Model; +using AsbCloudDb.Model; using System; namespace AsbCloudInfrastructure.Services @@ -8,6 +7,6 @@ namespace AsbCloudInfrastructure.Services { public int Order { get; set; } public WellOperationCategory Operation { get; set; } - public Func Detect { get; set; } + public Func Detect { get; set; } } } diff --git a/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs b/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs index c48d21b7..0c39a3c4 100644 --- a/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs +++ b/AsbCloudInfrastructure/Services/TelemetryOperationDetectorService.cs @@ -19,8 +19,7 @@ namespace AsbCloudInfrastructure.Services Operation = operations.FirstOrDefault(o => o.Name.Equals("На поверхности")), Detect = (data) => { - return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing - && data.IsBitPositionLt20 && data.IsHookWeightLt3; + return data.IsBitPositionLt20 && data.IsHookWeightLt3; } }, new TelemetryOperationDetector @@ -42,7 +41,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBlockPositionDecreasing && + data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && data.IsRotorSpeedGt3 && data.IsPressureGt20; } }, @@ -53,7 +52,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBitPositionIncreasing && + data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && data.IsRotorSpeedGt3 && data.IsPressureGt20; } }, @@ -64,7 +63,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBitPositionDecreasing && + data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && data.IsRotorSpeedLt3 && data.IsPressureGt20; } }, @@ -75,7 +74,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBlockPositionIncreasing && + data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && data.IsRotorSpeedLt3 && data.IsPressureGt20; } }, @@ -86,7 +85,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBlockPositionIncreasing && + data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && data.IsRotorSpeedLt3 && data.IsPressureLt20; } }, @@ -97,7 +96,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionIncreasing && data.IsBlockPositionIncreasing && + data.IsBitPositionIncreasing && data.IsBlockPositionDecreasing && data.IsRotorSpeedGt3 && data.IsPressureLt20; } }, @@ -108,7 +107,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBlockPositionDecreasing && + data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && data.IsRotorSpeedLt3 && data.IsPressureLt20; } }, @@ -119,7 +118,7 @@ namespace AsbCloudInfrastructure.Services Detect = (data) => { return !data.IsWellDepthDecreasing && !data.IsWellDepthIncreasing && - data.IsBitPositionDecreasing && data.IsBlockPositionDecreasing && + data.IsBitPositionDecreasing && data.IsBlockPositionIncreasing && data.IsRotorSpeedGt3 && data.IsPressureLt20; } }, @@ -180,7 +179,7 @@ namespace AsbCloudInfrastructure.Services }; } - public WellOperationCategory DetectOperation(TelemetryAnalysisDto data) => + public WellOperationCategory DetectOperation(TelemetryAnalysis data) => detectors.OrderBy(d => d.Order).First(o => o.Detect(data)).Operation ?? new WellOperationCategory { Id = 1, Name = "Невозможно определить операцию" }; }