slow detection

This commit is contained in:
ngfrolov 2022-06-27 12:43:55 +05:00
parent d0386b0182
commit 9ab41cd24e
3 changed files with 49 additions and 50 deletions

View File

@ -25,9 +25,8 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
{ {
this.detectStart = detectStart; this.detectStart = detectStart;
this.detectors = JoinDetectors(detector, detectors); this.detectors = JoinDetectors(detector, detectors);
detectEnd = (f, i) => !detectStart(f,i); detectEnd = (f, i) => !detectStart(f, i);
} }
public FragmentDetector( public FragmentDetector(
Func<DetectableTelemetry[], int, bool> detectStart, Func<DetectableTelemetry[], int, bool> detectStart,
Func<DetectableTelemetry[], int, bool> detectEnd, Func<DetectableTelemetry[], int, bool> detectEnd,
@ -92,24 +91,11 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
return detect; 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<DetectedOperation> operations) public bool TryDetect(int idTelemetry, DetectableTelemetry[] telemetryFragment, out IEnumerable<DetectedOperation> operations)
{ {
var positionStart = 0; var positionStart = 0;
var operationsList = new List<DetectedOperation>(); var operationsList = new List<DetectedOperation>();
// поиск начала соответствия критерию // проверка соответствия критерию начала операции
while (telemetryFragment.Length > positionStart + FragmentLength + stepLength)
{
if (detectStart(telemetryFragment, positionStart)) if (detectStart(telemetryFragment, positionStart))
{ {
// Поиск окончания соответствия критерию // Поиск окончания соответствия критерию
@ -120,7 +106,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
{ {
var innerFragment = telemetryFragment[positionStart..positionEnd]; var innerFragment = telemetryFragment[positionStart..positionEnd];
var detectorEnumerator = detectors.GetEnumerator(); var detectorEnumerator = detectors.GetEnumerator();
while(detectorEnumerator.MoveNext()) while (detectorEnumerator.MoveNext())
if (detectorEnumerator.Current.TryDetect(idTelemetry, innerFragment, out IEnumerable<DetectedOperation> result)) if (detectorEnumerator.Current.TryDetect(idTelemetry, innerFragment, out IEnumerable<DetectedOperation> result))
operationsList.AddRange(result); operationsList.AddRange(result);
@ -128,13 +114,13 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
} }
positionEnd += stepLength; positionEnd += stepLength;
} }
positionStart = positionEnd;
} }
else else
{ {
positionStart += stepLength; operations = Enumerable.Empty<DetectedOperation>();
} return false;
} }
operations = operationsList; operations = operationsList;
return operationsList.Any(); return operationsList.Any();
} }

View File

@ -136,7 +136,7 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
}) })
.OrderBy(d => d.DateTime); .OrderBy(d => d.DateTime);
var take = 4 * 86_400;// 4 дня var take = 4 * 86_400; // 4 дня
var startDate = begin; var startDate = begin;
var detectedOperations = new List<DetectedOperation>(8); var detectedOperations = new List<DetectedOperation>(8);
@ -151,25 +151,38 @@ namespace AsbCloudInfrastructure.Services.DetectOperations
break; break;
var isDetected = false; var isDetected = false;
var positionStart = 0;
while (data.Length > positionStart + minFragmentLength + minStepLength)
{
var telemetryFragment = data[positionStart..];
var isDetected1 = false;
foreach (var detector in detectors) foreach (var detector in detectors)
{ {
if (detector is FragmentDetector fragmentDetector) if (detector is FragmentDetector fragmentDetector)
{ {
var minLengthToDetect = fragmentDetector.StepLength + fragmentDetector.FragmentLength; var minLengthToDetect = fragmentDetector.StepLength + fragmentDetector.FragmentLength;
if (data.Length < minLengthToDetect) if (telemetryFragment.Length < minLengthToDetect)
continue; continue;
if (fragmentDetector.TryDetect(idTelemetry, data, out IEnumerable<DetectedOperation> operations)) if (fragmentDetector.TryDetect(idTelemetry, data, out IEnumerable<DetectedOperation> operations))
{ {
isDetected = true; isDetected = true;
isDetected1 = true;
detectedOperations.AddRange(operations); detectedOperations.AddRange(operations);
startDate = operations.Last().DateEnd; startDate = operations.Last().DateEnd;
positionStart = 0;
data = data
.Where(d => d.DateTime > startDate)
.ToArray();
break; break;
} }
} }
} }
if (!isDetected1)
positionStart += minStepLength;
}
if (!isDetected) if (!isDetected)
{ {
if (data.Length < take) if (data.Length < take)