prevent duplicate events , fix download durations

This commit is contained in:
Oliver Kieselbach
2026-03-06 00:32:03 +01:00
parent de50d3733c
commit 65245b6d62
5 changed files with 37 additions and 5 deletions
@@ -412,7 +412,8 @@ namespace AutopilotMonitor.Agent.Core.Monitoring.Core
imeLogPatterns,
_configuration.ImeLogPathOverride,
imeMatchLogPath: imeMatchLogPath,
espAndHelloTracker: _espAndHelloTracker
espAndHelloTracker: _espAndHelloTracker,
isBootstrapMode: _configuration.UseBootstrapTokenAuth
);
_enrollmentTracker.Start();
}
@@ -35,6 +35,7 @@ namespace AutopilotMonitor.Agent.Core.Monitoring.Tracking
private bool _hasAutoSwitchedToAppsPhase; // Track if we've already auto-switched to apps phase for current ESP phase
private string _enrollmentType = "v1"; // "v1" = Autopilot Classic/ESP, "v2" = Windows Device Preparation
private bool _isWaitingForHello = false; // Track if we're waiting for Hello to complete before sending enrollment_complete
private readonly bool _isBootstrapMode; // Agent started via bootstrap token (pre-MDM)
private bool _enrollmentStartDeviceInfoCollected = false; // Re-collect enrollment-dependent info once at first ESP phase
private bool _finalDeviceInfoCollected = false; // Ensure final device info is emitted only once
private string _lastEmittedSummaryHash; // Track last emitted state-breakdown to avoid redundant summary events
@@ -145,8 +146,10 @@ namespace AutopilotMonitor.Agent.Core.Monitoring.Tracking
bool simulationMode = false,
double speedFactor = 50,
string imeMatchLogPath = null,
Collectors.EspAndHelloTracker espAndHelloTracker = null)
Collectors.EspAndHelloTracker espAndHelloTracker = null,
bool isBootstrapMode = false)
{
_isBootstrapMode = isBootstrapMode;
_sessionId = sessionId;
_tenantId = tenantId;
_emitEvent = emitEvent;
@@ -1295,6 +1298,7 @@ namespace AutopilotMonitor.Agent.Core.Monitoring.Tracking
/// <summary>
/// Re-collects enrollment-dependent device info (AAD join, Autopilot profile, ESP config)
/// that were likely empty when the agent started before enrollment (bootstrap scenario).
/// Only runs in bootstrap mode — in normal mode CollectAll() already has complete data.
/// Called once when the first ESP phase is detected.
/// </summary>
private void CollectDeviceInfoAtEnrollmentStart()
@@ -1303,7 +1307,14 @@ namespace AutopilotMonitor.Agent.Core.Monitoring.Tracking
return;
_enrollmentStartDeviceInfoCollected = true;
_logger.Info("EnrollmentTracker: first ESP phase detected — re-collecting enrollment-dependent device info");
if (!_isBootstrapMode)
{
_logger.Debug("EnrollmentTracker: skipping enrollment-start device info re-collection (not bootstrap mode)");
return;
}
_logger.Info("EnrollmentTracker: first ESP phase detected — re-collecting enrollment-dependent device info (bootstrap mode)");
Task.Run(() =>
{
try
@@ -1 +1 @@
240
242
@@ -873,7 +873,13 @@ namespace AutopilotMonitor.Functions.Functions
if (evt.Data.ContainsKey("doDownloadMode") && int.TryParse(evt.Data["doDownloadMode"]?.ToString(), out var doMode))
summary.DoDownloadMode = doMode;
if (evt.Data.ContainsKey("doDownloadDuration"))
summary.DoDownloadDuration = evt.Data["doDownloadDuration"]?.ToString() ?? string.Empty;
{
var doDurStr = evt.Data["doDownloadDuration"]?.ToString() ?? string.Empty;
summary.DoDownloadDuration = doDurStr;
// Use DO's measured duration for throughput calculation (more accurate than timestamp diff)
if (TimeSpan.TryParse(doDurStr, out var doDurTs) && doDurTs.TotalSeconds >= 1)
summary.DownloadDurationSeconds = Math.Max(summary.DownloadDurationSeconds, (int)doDurTs.TotalSeconds);
}
if (evt.Data.ContainsKey("doBytesFromLanPeers") && long.TryParse(evt.Data["doBytesFromLanPeers"]?.ToString(), out var doLan))
summary.DoBytesFromLanPeers = doLan;
if (evt.Data.ContainsKey("doBytesFromGroupPeers") && long.TryParse(evt.Data["doBytesFromGroupPeers"]?.ToString(), out var doGroup))
@@ -169,6 +169,20 @@ namespace AutopilotMonitor.Functions.Services
}
}
}
// Preserve DownloadDurationSeconds and DownloadBytes from prior batch if current batch has no value
if (summary.DownloadDurationSeconds == 0)
{
var existingDlDuration = existing.Value.GetInt32("DownloadDurationSeconds");
if (existingDlDuration.HasValue && existingDlDuration.Value > 0)
summary.DownloadDurationSeconds = existingDlDuration.Value;
}
if (summary.DownloadBytes == 0)
{
var existingDlBytes = existing.Value.GetInt64("DownloadBytes");
if (existingDlBytes.HasValue && existingDlBytes.Value > 0)
summary.DownloadBytes = existingDlBytes.Value;
}
}
catch (Azure.RequestFailedException ex) when (ex.Status == 404)
{