From a4254a2f537e00a574629656b82f57fe555e1ec9 Mon Sep 17 00:00:00 2001 From: Emre KARA Date: Thu, 29 May 2025 03:51:23 +0300 Subject: [PATCH] Refactor TelemetryDeviceInfoEnricher to improve device ID handling by allowing nullable GUIDs, enhancing enrichment logic with dedicated methods for device and software information, and ensuring better null safety in device type detection. --- .../Providers/TelemetryDeviceInfoEnricher.cs | 100 +++++++++--------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Activity/Providers/TelemetryDeviceInfoEnricher.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Activity/Providers/TelemetryDeviceInfoEnricher.cs index 0965330483..bdd53898e5 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Activity/Providers/TelemetryDeviceInfoEnricher.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Telemetry/Activity/Providers/TelemetryDeviceInfoEnricher.cs @@ -21,95 +21,94 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing { private readonly ITelemetryActivityStorage _telemetryActivityStorage; private readonly ISoftwareInfoProvider _softwareInfoProvider; - private readonly Lazy _deviceId; + private readonly Lazy _deviceId; public TelemetryDeviceInfoEnricher(ITelemetryActivityStorage telemetryActivityStorage, ISoftwareInfoProvider softwareInfoProvider) { _telemetryActivityStorage = telemetryActivityStorage; _softwareInfoProvider = softwareInfoProvider; - _deviceId = new Lazy(() => - { - var deviceIdText = File.ReadAllText(TelemetryPaths.ComputerId); - return deviceIdText.To(); - }); - } - - public async Task ShouldEnrichAsync(ActivityData activity) - { - return await _telemetryActivityStorage.ShouldAddDeviceInfoAsync(); + _deviceId = new Lazy(GetDeviceId); } public async Task EnrichAsync(ActivityData activity) { - - activity[ActivityPropertyNames.DeviceId] = _deviceId.Value; + if (_deviceId.Value.HasValue) + { + activity[ActivityPropertyNames.DeviceId] = _deviceId.Value.Value; + } + if (!await _telemetryActivityStorage.ShouldAddDeviceInfoAsync()) { return; } - + + EnrichWithDeviceInfo(activity); + await EnrichWithSoftwareInfoAsync(activity); + await _telemetryActivityStorage.MarkDeviceInfoAsAddedAsync(); + } + + private void EnrichWithDeviceInfo(ActivityData activity) + { activity[ActivityPropertyNames.DeviceType] = GetDeviceType(); activity[ActivityPropertyNames.DeviceLanguage] = GetLanguage(); activity[ActivityPropertyNames.OperatingSystem] = GetOperatingSystem(); activity[ActivityPropertyNames.CountryIsoCode] = GetCountry(); + } + private async Task EnrichWithSoftwareInfoAsync(ActivityData activity) + { var softwareList = await _softwareInfoProvider.GetSoftwareInfoAsync(); activity[ActivityPropertyNames.InstalledSoftwares] = softwareList; + } - await _telemetryActivityStorage.MarkDeviceInfoAsAddedAsync(); + private static Guid? GetDeviceId() + { + if (File.Exists(TelemetryPaths.ComputerId)) + { + var deviceIdText = File.ReadAllText(TelemetryPaths.ComputerId); + return deviceIdText.To(); + } + + return null; } - public OperationSystem GetOperatingSystem() + private static OperationSystem GetOperatingSystem() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { return OperationSystem.Windows; - } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { return OperationSystem.Linux; - } if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { return OperationSystem.MacOS; - } return OperationSystem.Unknown; } - public DeviceType GetDeviceType() + private static DeviceType GetDeviceType() { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) - { return DetectDeviceTypeOnWindows(); - } if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) - { return DetectDeviceTypeOnLinux(); - } if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX)) - { return DetectDeviceTypeOnMacOS(); - } if (RuntimeInformation.OSDescription.ToLower().Contains("ios")) - { return DeviceType.Laptop; - } return DeviceType.Unknown; } - public string GetLanguage() + private static string GetLanguage() { return CultureInfo.CurrentUICulture.Name; } - public string GetCountry() + private static string GetCountry() { var culture = CultureInfo.CurrentUICulture; if (culture.IsNeutralCulture) @@ -121,33 +120,40 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing return region.TwoLetterISORegionName; } - private DeviceType DetectDeviceTypeOnWindows() + private static DeviceType DetectDeviceTypeOnWindows() { +#if WINDOWS try { using var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_SystemEnclosure"); foreach (var obj in searcher.Get()) { - var chassisTypes = obj["ChassisTypes"] as ushort[]; - if (chassisTypes != null && chassisTypes.Any(t => t is 8 or 9 or 10 or 14)) + if (obj["ChassisTypes"] is ushort[] chassisTypes && + chassisTypes.Any(t => t is 8 or 9 or 10 or 14)) { return DeviceType.Laptop; } } } - catch { } - + catch + { + // ignored + } +#endif return DeviceType.Desktop; } - private DeviceType DetectDeviceTypeOnLinux() + private static DeviceType DetectDeviceTypeOnLinux() { try { - var type = File.ReadAllText("/sys/class/dmi/id/chassis_type"); - if (int.TryParse(type.Trim(), out var code) && code is 8 or 9 or 10 or 14) + if (File.Exists("/sys/class/dmi/id/chassis_type")) { - return DeviceType.Laptop; + var type = File.ReadAllText("/sys/class/dmi/id/chassis_type"); + if (int.TryParse(type.Trim(), out var code) && code is 8 or 9 or 10 or 14) + { + return DeviceType.Laptop; + } } } catch @@ -158,11 +164,11 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing return DeviceType.Desktop; } - private DeviceType DetectDeviceTypeOnMacOS() + private static DeviceType DetectDeviceTypeOnMacOS() { try { - var process = new Process + using var process = new Process { StartInfo = new ProcessStartInfo { @@ -179,14 +185,10 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing process.WaitForExit(); if (output.Contains("MacBook")) - { return DeviceType.Laptop; - } if (output.Contains("iMac") || output.Contains("Mac Pro")) - { return DeviceType.Desktop; - } } catch { @@ -195,6 +197,4 @@ public class TelemetryDeviceInfoEnricher : ITelemetryActivityDataEnricher, ISing return DeviceType.Unknown; } - - } \ No newline at end of file