From 9ab41cd24efa8a99ad917f1d6d0f8738370a5ed0 Mon Sep 17 00:00:00 2001 From: ngfrolov Date: Mon, 27 Jun 2022 12:43:55 +0500 Subject: [PATCH] slow detection --- .../Detectors/FragmentDetector.cs | 58 +++++++------------ .../DetectOperations/Detectors/IDetector.cs | 2 +- .../OperationDetectionBackgroundService.cs | 39 ++++++++----- 3 files changed, 49 insertions(+), 50 deletions(-) diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/FragmentDetector.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/FragmentDetector.cs index ca6115e9..5cb663e3 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/FragmentDetector.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/FragmentDetector.cs @@ -19,15 +19,14 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors public int StepLength { get => stepLength; set => stepLength = value; } public FragmentDetector( - Func detectStart, - IFragmentDetector detector, + Func detectStart, + IFragmentDetector detector, params IFragmentDetector[] detectors) { this.detectStart = detectStart; this.detectors = JoinDetectors(detector, detectors); - detectEnd = (f, i) => !detectStart(f,i); + detectEnd = (f, i) => !detectStart(f, i); } - public FragmentDetector( Func detectStart, Func detectEnd, @@ -92,49 +91,36 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors return detect; } - public IFragmentDetector AddChildDetector(params IFragmentDetector[] detectors) - { - throw new NotImplementedException(); - } - - public IDetector AddOperationDetector(IOperationDetector operation) - { - detectors = new IOperationDetector[] { operation}; - return this; - } - public bool TryDetect(int idTelemetry, DetectableTelemetry[] telemetryFragment, out IEnumerable operations) { var positionStart = 0; var operationsList = new List(); - // поиск начала соответствия критерию - while (telemetryFragment.Length > positionStart + FragmentLength + stepLength) + // проверка соответствия критерию начала операции + if (detectStart(telemetryFragment, positionStart)) { - if (detectStart(telemetryFragment, positionStart)) + // Поиск окончания соответствия критерию + var positionEnd = positionStart + stepLength; + while (telemetryFragment.Length > positionEnd + FragmentLength) { - // Поиск окончания соответствия критерию - var positionEnd = positionStart + stepLength; - while (telemetryFragment.Length > positionEnd + FragmentLength) + if (detectEnd(telemetryFragment, positionEnd)) { - if (detectEnd(telemetryFragment, positionEnd)) - { - var innerFragment = telemetryFragment[positionStart..positionEnd]; - var detectorEnumerator = detectors.GetEnumerator(); - while(detectorEnumerator.MoveNext()) - if (detectorEnumerator.Current.TryDetect(idTelemetry, innerFragment, out IEnumerable result)) - operationsList.AddRange(result); + var innerFragment = telemetryFragment[positionStart..positionEnd]; + var detectorEnumerator = detectors.GetEnumerator(); + while (detectorEnumerator.MoveNext()) + if (detectorEnumerator.Current.TryDetect(idTelemetry, innerFragment, out IEnumerable result)) + operationsList.AddRange(result); - break; - } - positionEnd += stepLength; + break; } - positionStart = positionEnd; - } - else - { - positionStart += stepLength; + positionEnd += stepLength; } } + else + { + operations = Enumerable.Empty(); + return false; + } + operations = operationsList; return operationsList.Any(); } diff --git a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/IDetector.cs b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/IDetector.cs index 96640006..5db7005d 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/Detectors/IDetector.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/Detectors/IDetector.cs @@ -13,5 +13,5 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors { } internal interface IFragmentDetector: IDetector - { } + { } } \ No newline at end of file diff --git a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs index bc22de01..b0a2f97d 100644 --- a/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs +++ b/AsbCloudInfrastructure/Services/DetectOperations/OperationDetectionBackgroundService.cs @@ -136,7 +136,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations }) .OrderBy(d => d.DateTime); - var take = 4 * 86_400;// 4 дня + var take = 4 * 86_400; // 4 дня var startDate = begin; var detectedOperations = new List(8); @@ -151,23 +151,36 @@ namespace AsbCloudInfrastructure.Services.DetectOperations break; var isDetected = false; - - foreach (var detector in detectors) + var positionStart = 0; + while (data.Length > positionStart + minFragmentLength + minStepLength) { - if (detector is FragmentDetector fragmentDetector) + var telemetryFragment = data[positionStart..]; + var isDetected1 = false; + foreach (var detector in detectors) { - var minLengthToDetect = fragmentDetector.StepLength + fragmentDetector.FragmentLength; - if (data.Length < minLengthToDetect) - continue; - - if (fragmentDetector.TryDetect(idTelemetry, data, out IEnumerable operations)) + if (detector is FragmentDetector fragmentDetector) { - isDetected = true; - detectedOperations.AddRange(operations); - startDate = operations.Last().DateEnd; - break; + var minLengthToDetect = fragmentDetector.StepLength + fragmentDetector.FragmentLength; + if (telemetryFragment.Length < minLengthToDetect) + continue; + + if (fragmentDetector.TryDetect(idTelemetry, data, out IEnumerable operations)) + { + isDetected = true; + isDetected1 = true; + detectedOperations.AddRange(operations); + startDate = operations.Last().DateEnd; + positionStart = 0; + data = data + .Where(d => d.DateTime > startDate) + .ToArray(); + break; + } } } + + if (!isDetected1) + positionStart += minStepLength; } if (!isDetected)