Рефакторинг WorkOperationDetection избавился от двух циклов внутри метода Action

This commit is contained in:
Степанов Дмитрий 2024-02-21 07:29:01 +03:00
parent 3e84b1591f
commit 615771395d

View File

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
@ -34,31 +33,19 @@ public class WorkOperationDetection: Work
var detectedOperationService = services.GetRequiredService<IDetectedOperationService>();
var telemetryIds = (await telemetryRepository.GetAllAsync(token))
.Select(t => t.Id);
.Select(t => t.Id)
.ToArray();
var lastDetectedDates = await detectedOperationRepository.GetLastDetectedDatesAsync(token);
var beginDatesDetectOperations = new List<(int TelemetryId, DateTimeOffset? BeginDate)>();
foreach (var telemetryId in telemetryIds)
for (var i = 0; i < telemetryIds.Length; i++)
{
if (lastDetectedDates.TryGetValue(telemetryId, out var beginDate))
{
beginDatesDetectOperations.Add((telemetryId, beginDate));
continue;
}
var telemetryId = telemetryIds[i];
beginDatesDetectOperations.Add((telemetryId, null));
}
var count = beginDatesDetectOperations.Count;
for (var i = 0; i < count; i++)
{
var (idTelemetry, beginDate) = beginDatesDetectOperations[i];
var beginDate = lastDetectedDates.TryGetValue(telemetryId, out var date) ? date : (DateTimeOffset?)null;
onProgressCallback($"Start detecting telemetry: {idTelemetry} from {beginDate}", i++ / count);
var detectedOperations = await detectedOperationService.DetectOperationsAsync(idTelemetry, beginDate, token);
onProgressCallback($"Start detecting telemetry: {telemetryId} from {beginDate}", i++ / telemetryIds.Length);
var detectedOperations = await detectedOperationService.DetectOperationsAsync(telemetryId, beginDate, token);
if (detectedOperations.Any())
await detectedOperationRepository.InsertRangeAsync(detectedOperations, token);