diff --git a/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.cs b/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.cs index 657f9cc0..3a8d4d2b 100644 --- a/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.cs +++ b/src/Backend/AutopilotMonitor.Functions/Services/TableStorageService.cs @@ -447,7 +447,15 @@ namespace AutopilotMonitor.Functions.Services if (status == SessionStatus.Succeeded || status == SessionStatus.Failed) { // Use the provided completedAt timestamp (from event) if available, otherwise use current time - session["CompletedAt"] = completedAt ?? DateTime.UtcNow; + var effectiveCompletedAt = completedAt ?? DateTime.UtcNow; + session["CompletedAt"] = effectiveCompletedAt; + + // 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 = effectiveEarliest ?? currentStartedAt; + if (durationStart < effectiveCompletedAt) + session["DurationSeconds"] = (int)(effectiveCompletedAt - durationStart).TotalSeconds; } // Set failure reason if failed @@ -649,9 +657,10 @@ namespace AutopilotMonitor.Functions.Services Status = status, FailureReason = entity.GetString("FailureReason") ?? string.Empty, EventCount = SafeGetInt32(entity, "EventCount") ?? 0, - DurationSeconds = completedAt.HasValue - ? (int)(completedAt.Value - startedAt).TotalSeconds - : (int)(DateTime.UtcNow - startedAt).TotalSeconds, + DurationSeconds = SafeGetInt32(entity, "DurationSeconds") + ?? (completedAt.HasValue + ? (int)(completedAt.Value - startedAt).TotalSeconds + : (int)(DateTime.UtcNow - startedAt).TotalSeconds), EnrollmentType = entity.GetString("EnrollmentType") ?? "v1", DiagnosticsBlobName = entity.GetString("DiagnosticsBlobName") }; diff --git a/src/Web/autopilot-monitor-web/app/sessions/[sessionId]/page.tsx b/src/Web/autopilot-monitor-web/app/sessions/[sessionId]/page.tsx index 4e982d79..f60ea535 100644 --- a/src/Web/autopilot-monitor-web/app/sessions/[sessionId]/page.tsx +++ b/src/Web/autopilot-monitor-web/app/sessions/[sessionId]/page.tsx @@ -1635,10 +1635,10 @@ function DeviceDetailsCard({ events }: { events: EnrollmentEvent[] }) { )} {/* System */} - {(bootTime?.bootTime || agentStarted?.bootTime || agentStarted?.agentVersion || imeVersion || aadJoinStatus?.joinType || deviceLocation?.country || deviceLocation?.Country || deviceLocation?.timezone || deviceLocation?.Timezone) && ( + {(bootTime?.bootTimeUtc || bootTime?.bootTime || agentStarted?.bootTime || agentStarted?.agentVersion || imeVersion || aadJoinStatus?.joinType || deviceLocation?.country || deviceLocation?.Country || deviceLocation?.timezone || deviceLocation?.Timezone) && ( - {(bootTime?.bootTime || agentStarted?.bootTime) && ( - + {(bootTime?.bootTimeUtc || bootTime?.bootTime || agentStarted?.bootTime) && ( + )} {bootTime?.uptimeMinutes && } {agentStarted?.agentVersion && }