forked from ddrilling/AsbCloudServer
slow detection
This commit is contained in:
parent
d0386b0182
commit
9ab41cd24e
@ -19,15 +19,14 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
|
||||
public int StepLength { get => stepLength; set => stepLength = value; }
|
||||
|
||||
public FragmentDetector(
|
||||
Func<DetectableTelemetry[], int, bool> detectStart,
|
||||
IFragmentDetector detector,
|
||||
Func<DetectableTelemetry[], int, bool> 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<DetectableTelemetry[], int, bool> detectStart,
|
||||
Func<DetectableTelemetry[], int, bool> 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<DetectedOperation> operations)
|
||||
{
|
||||
var positionStart = 0;
|
||||
var operationsList = new List<DetectedOperation>();
|
||||
// поиск начала соответствия критерию
|
||||
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<DetectedOperation> result))
|
||||
operationsList.AddRange(result);
|
||||
var innerFragment = telemetryFragment[positionStart..positionEnd];
|
||||
var detectorEnumerator = detectors.GetEnumerator();
|
||||
while (detectorEnumerator.MoveNext())
|
||||
if (detectorEnumerator.Current.TryDetect(idTelemetry, innerFragment, out IEnumerable<DetectedOperation> result))
|
||||
operationsList.AddRange(result);
|
||||
|
||||
break;
|
||||
}
|
||||
positionEnd += stepLength;
|
||||
break;
|
||||
}
|
||||
positionStart = positionEnd;
|
||||
}
|
||||
else
|
||||
{
|
||||
positionStart += stepLength;
|
||||
positionEnd += stepLength;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
operations = Enumerable.Empty<DetectedOperation>();
|
||||
return false;
|
||||
}
|
||||
|
||||
operations = operationsList;
|
||||
return operationsList.Any();
|
||||
}
|
||||
|
@ -13,5 +13,5 @@ namespace AsbCloudInfrastructure.Services.DetectOperations.Detectors
|
||||
{ }
|
||||
|
||||
internal interface IFragmentDetector: IDetector
|
||||
{ }
|
||||
{ }
|
||||
}
|
@ -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<DetectedOperation>(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<DetectedOperation> 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<DetectedOperation> 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)
|
||||
|
Loading…
Reference in New Issue
Block a user