From 2092a1f00c031b10f7a4bc1f9dca2ec61bc7cfb6 Mon Sep 17 00:00:00 2001 From: Oliver Kieselbach Date: Sun, 1 Mar 2026 09:18:08 +0100 Subject: [PATCH] fix duraton in session list --- .../AutopilotMonitor.Agent/buildcounter.txt | 2 +- .../Services/TableStorageService.Sessions.cs | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/Agent/AutopilotMonitor.Agent/buildcounter.txt b/src/Agent/AutopilotMonitor.Agent/buildcounter.txt index 078fa0fe..9289ddce 100644 --- a/src/Agent/AutopilotMonitor.Agent/buildcounter.txt +++ b/src/Agent/AutopilotMonitor.Agent/buildcounter.txt @@ -1 +1 @@ -119 +121 diff --git a/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.Sessions.cs b/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.Sessions.cs index 8880183b..50ef0a60 100644 --- a/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.Sessions.cs +++ b/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.Sessions.cs @@ -419,9 +419,13 @@ namespace AutopilotMonitor.Functions.Services // Store accurate DurationSeconds based on first event timestamp (not registration StartedAt) // The agent registers before events arrive, so registration StartedAt is always earlier // than the actual enrollment start — using the first event gives the true enrollment duration. - var durationStart = earliestEventTimestamp ?? currentStartedAt; - if (durationStart < effectiveCompletedAt) - update["DurationSeconds"] = (int)(effectiveCompletedAt - durationStart).TotalSeconds; + // Use the effective StartedAt (already corrected by earlier batches via the StartedAt + // alignment above), taking the minimum with the current batch's earliest timestamp. + var effectiveStartedAt = earliestEventTimestamp.HasValue && earliestEventTimestamp.Value < currentStartedAt + ? earliestEventTimestamp.Value + : currentStartedAt; + if (effectiveStartedAt < effectiveCompletedAt) + update["DurationSeconds"] = (int)(effectiveCompletedAt - effectiveStartedAt).TotalSeconds; } // Track the most recent event timestamp for excessive data sender detection @@ -704,10 +708,15 @@ namespace AutopilotMonitor.Functions.Services Status = status, FailureReason = entity.GetString("FailureReason") ?? string.Empty, EventCount = SafeGetInt32(entity, "EventCount") ?? 0, - DurationSeconds = SafeGetInt32(entity, "DurationSeconds") - ?? (completedAt.HasValue + DurationSeconds = SafeGetInt32(entity, "DurationSeconds") switch + { + // Stored duration is valid — use it directly + int d when d > 0 => d, + // Duration missing or zero (bug from batch-split race) — recompute from timestamps + _ => completedAt.HasValue ? (int)(completedAt.Value - startedAt).TotalSeconds - : (int)(DateTime.UtcNow - startedAt).TotalSeconds), + : (int)(DateTime.UtcNow - startedAt).TotalSeconds + }, EnrollmentType = entity.GetString("EnrollmentType") ?? "v1", DiagnosticsBlobName = entity.GetString("DiagnosticsBlobName"), LastEventAt = SafeGetDateTime(entity, "LastEventAt"),