using System; using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using Microsoft.Extensions.Logging; using ThreadPilot.Services; namespace ThreadPilot.ViewModels { /// /// ViewModel for the performance monitoring dashboard /// public partial class PerformanceViewModel : BaseViewModel { private readonly IPerformanceMonitoringService _performanceService; private readonly ILogger _logger; [ObservableProperty] private ObservableCollection coreUsages = new(); [ObservableProperty] private double totalCpuUsage; [ObservableProperty] private long totalMemoryUsage; [ObservableProperty] private long availableMemory; [ObservableProperty] private long totalMemory; [ObservableProperty] private double memoryUsagePercentage; [ObservableProperty] private ProcessPerformanceInfo? topCpuProcess; [ObservableProperty] private ProcessPerformanceInfo? topMemoryProcess; [ObservableProperty] private ObservableCollection topCpuProcesses = new(); [ObservableProperty] private ObservableCollection topMemoryProcesses = new(); [ObservableProperty] private int activeProcessCount; [ObservableProperty] private bool isMonitoring; [ObservableProperty] private string monitoringStatusText = "Monitoring Stopped"; [ObservableProperty] private DateTime lastUpdateTime; [ObservableProperty] private ObservableCollection historicalData = new(); [ObservableProperty] private int updateInterval = 1000; // milliseconds [ObservableProperty] private bool showCoreDetails = true; [ObservableProperty] private bool showProcessDetails = true; [ObservableProperty] private string cpuUsageText = "0.0%"; [ObservableProperty] private string memoryUsageText = "0 MB / 0 MB"; [ObservableProperty] private string uptimeText = "00:00:00"; public PerformanceViewModel( IPerformanceMonitoringService performanceService, ILogger logger) : base(logger, null) { _performanceService = performanceService; _logger = logger; // Subscribe to performance updates _performanceService.MetricsUpdated += OnMetricsUpdated; } [RelayCommand] public async Task StartMonitoringAsync() { try { SetStatus("Starting performance monitoring..."); await _performanceService.StartMonitoringAsync(); IsMonitoring = true; MonitoringStatusText = "Monitoring Active"; SetStatus("Performance monitoring started"); _logger.LogInformation("Performance monitoring started"); } catch (Exception ex) { SetError("Failed to start performance monitoring", ex); _logger.LogError(ex, "Error starting performance monitoring"); } } [RelayCommand] public async Task StopMonitoringAsync() { try { SetStatus("Stopping performance monitoring..."); await _performanceService.StopMonitoringAsync(); IsMonitoring = false; MonitoringStatusText = "Monitoring Stopped"; SetStatus("Performance monitoring stopped"); _logger.LogInformation("Performance monitoring stopped"); } catch (Exception ex) { SetError("Failed to stop performance monitoring", ex); _logger.LogError(ex, "Error stopping performance monitoring"); } } [RelayCommand] public async Task RefreshMetricsAsync() { try { SetStatus("Refreshing performance metrics..."); var metrics = await _performanceService.GetSystemMetricsAsync(); UpdateMetrics(metrics); SetStatus("Performance metrics refreshed"); } catch (Exception ex) { SetError("Failed to refresh performance metrics", ex); _logger.LogError(ex, "Error refreshing performance metrics"); } } [RelayCommand] public async Task ClearHistoricalDataAsync() { try { await _performanceService.ClearHistoricalDataAsync(); HistoricalData.Clear(); SetStatus("Historical data cleared"); _logger.LogInformation("Historical performance data cleared"); } catch (Exception ex) { SetError("Failed to clear historical data", ex); _logger.LogError(ex, "Error clearing historical data"); } } [RelayCommand] public async Task LoadHistoricalDataAsync() { try { SetStatus("Loading historical data..."); var data = await _performanceService.GetHistoricalDataAsync(TimeSpan.FromHours(1)); // Marshal ObservableCollection assignment to UI thread to prevent cross-thread access exceptions await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => { HistoricalData = new ObservableCollection(data); SetStatus($"Loaded {data.Count} historical data points"); }); } catch (Exception ex) { SetError("Failed to load historical data", ex); _logger.LogError(ex, "Error loading historical data"); } } [RelayCommand] public void ToggleCoreDetails() { ShowCoreDetails = !ShowCoreDetails; } [RelayCommand] public void ToggleProcessDetails() { ShowProcessDetails = !ShowProcessDetails; } private void OnMetricsUpdated(object? sender, PerformanceMetricsUpdatedEventArgs e) { try { // Update on UI thread App.Current.Dispatcher.Invoke(() => { UpdateMetrics(e.Metrics); }); } catch (Exception ex) { _logger.LogError(ex, "Error updating performance metrics in UI"); } } private void UpdateMetrics(SystemPerformanceMetrics metrics) { try { TotalCpuUsage = metrics.TotalCpuUsage; TotalMemoryUsage = metrics.TotalMemoryUsage; AvailableMemory = metrics.AvailableMemory; TotalMemory = metrics.TotalMemory; MemoryUsagePercentage = metrics.MemoryUsagePercentage; TopCpuProcess = metrics.TopCpuProcess; TopMemoryProcess = metrics.TopMemoryProcess; ActiveProcessCount = metrics.ActiveProcessCount; LastUpdateTime = metrics.Timestamp; // Update core usages CoreUsages.Clear(); foreach (var coreUsage in metrics.CpuCoreUsages) { CoreUsages.Add(coreUsage); } // Update formatted text CpuUsageText = $"{TotalCpuUsage:F1}%"; MemoryUsageText = $"{TotalMemoryUsage / (1024 * 1024):N0} MB / {TotalMemory / (1024 * 1024):N0} MB"; UptimeText = (DateTime.UtcNow - System.Diagnostics.Process.GetCurrentProcess().StartTime).ToString(@"hh\:mm\:ss"); // Add to historical data if monitoring if (IsMonitoring) { HistoricalData.Add(metrics); // Keep only last 300 entries (5 minutes at 1-second intervals) while (HistoricalData.Count > 300) { HistoricalData.RemoveAt(0); } } } catch (Exception ex) { _logger.LogError(ex, "Error updating metrics display"); } } public override async Task InitializeAsync() { try { SetStatus("Initializing performance monitoring..."); // Load initial metrics await RefreshMetricsAsync(); // Load top processes await LoadTopProcessesAsync(); SetStatus("Performance monitoring initialized"); } catch (Exception ex) { SetError("Failed to initialize performance monitoring", ex); _logger.LogError(ex, "Error initializing performance monitoring"); } } private async Task LoadTopProcessesAsync() { try { var topCpu = await _performanceService.GetTopCpuProcessesAsync(10); var topMemory = await _performanceService.GetTopMemoryProcessesAsync(10); // PERFORMANCE OPTIMIZATION: Update existing collections instead of creating new ones await System.Windows.Application.Current.Dispatcher.InvokeAsync(() => { UpdateObservableCollection(TopCpuProcesses, topCpu); UpdateObservableCollection(TopMemoryProcesses, topMemory); }); } catch (Exception ex) { _logger.LogError(ex, "Error loading top processes"); } } /// /// PERFORMANCE OPTIMIZATION: Updates an ObservableCollection efficiently by only modifying changed items /// private void UpdateObservableCollection(ObservableCollection collection, IEnumerable newItems) where T : class { var newList = newItems.ToList(); // Remove items that are no longer in the new list for (int i = collection.Count - 1; i >= 0; i--) { if (!newList.Contains(collection[i])) { collection.RemoveAt(i); } } // Add or update items for (int i = 0; i < newList.Count; i++) { if (i < collection.Count) { // Update existing item if different if (!collection[i].Equals(newList[i])) { collection[i] = newList[i]; } } else { // Add new item collection.Add(newList[i]); } } } protected override void OnDispose() { _performanceService.MetricsUpdated -= OnMetricsUpdated; // Stop monitoring if active if (IsMonitoring) { _ = Task.Run(async () => await StopMonitoringAsync()); } base.OnDispose(); } } }