diff --git a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs index d6b4a257..69e510f9 100644 --- a/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs +++ b/AsbCloudInfrastructure/Background/PeriodicWorks/WorkDataSaubStat.cs @@ -40,13 +40,15 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks if (!idTelemetries.Any()) return; - var stats = await db.Set() + var statsQuery = await db.Set() .Where(s => idTelemetries.Contains(s.IdTelemetry)) + .ToArrayAsync(token); + + var stats = statsQuery .GroupBy(s => s.IdTelemetry) - .ToDictionaryAsync( + .ToDictionary( g => g.Key, - g => g.OrderByDescending(s => s.DateEnd).First(), - token + g => g.OrderByDescending(s => s.DateEnd).First() ); foreach ( var idTelemetry in idTelemetries) @@ -92,6 +94,7 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks { var indexStart = 0; var indexEnd = 0; + var result = new List(); foreach (var operation in detectedOperations) { indexStart = Array.FindIndex(telemetryDataSaub, indexEnd, t => t.DateTime >= operation.DateStart); @@ -103,14 +106,21 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks var subset = telemetryDataSaub.AsSpan(indexStart, length + 1); var stats = CalcStats(operation, subset); - foreach (var stat in stats) - yield return stat; + result.AddRange(stats); + } + else + { + indexStart = 0; + indexEnd = 0; } } + return result; } private static IEnumerable CalcStats(DetectedOperation operation, Span telemetryDataSaub) { + var result = new List(); + var indexStart = 0; for (var i = 1; i < telemetryDataSaub.Length; i++) { @@ -123,9 +133,11 @@ namespace AsbCloudInfrastructure.Background.PeriodicWorks var span = telemetryDataSaub.Slice(indexStart, length); indexStart = i; var stat = CalcStat(operation, span); - yield return stat; + result.Add(stat); } } + + return result; } private static DataSaubStat CalcStat(DetectedOperation operation, Span span)