From 93f5e8f40f6fbf522263678500d6ba64c4715ef0 Mon Sep 17 00:00:00 2001 From: lilla28 Date: Thu, 20 Apr 2023 11:54:06 +0200 Subject: [PATCH] Renaming variables, ProcessTerminated method usage fix --- .../Processes/IProcessInfoMonitor.cs | 49 ++++++++++++++++++- .../Processes/ProcessInfoMonitor.cs | 2 +- .../Factories/ProcessAggregatorFactory.cs | 6 +-- .../Factories/ProcessMonitorFactory.cs | 2 +- .../ProcessInfoAggregator.cs | 26 +++++----- .../ProcessInfoAggregator.Tests.cs | 14 +++--- 6 files changed, 73 insertions(+), 26 deletions(-) diff --git a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/IProcessInfoMonitor.cs b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/IProcessInfoMonitor.cs index 0b795b6ef..70a406c6b 100644 --- a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/IProcessInfoMonitor.cs +++ b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/IProcessInfoMonitor.cs @@ -14,20 +14,67 @@ namespace ProcessExplorer.Abstractions.Processes; -//TODO(Lilla): Add description, should be async +//TODO(Lilla): should be async? public interface IProcessInfoMonitor : IDisposable { + /// + /// Clears the currently initilialzed processes. + /// void ClearProcessIds(); + + /// + /// Returns the CPU usage of the given process. + /// + /// + /// + /// float GetCpuUsage(int processId, string processName); + + /// + /// Returns the memory usage of the given process. + /// + /// + /// + /// float GetMemoryUsage(int processId, string processName); + + /// + /// Returns the parent id of the given process. + /// + /// + /// + /// int? GetParentId(int processId, string processName); + + /// + /// Returns the initialized/watched process ids. + /// + /// ReadOnlySpan GetProcessIds(); + + /// + /// Sets the behaviors of the process changed events. + /// + /// + /// + /// void SetHandlers( ProcessModifiedHandler processModifiedHandler, ProcessTerminatedHandler processTerminatedHandler, ProcessCreatedHandler processCreatedHandler, ProcessesModifiedHandler processesModifiedHandler, ProcessStatusChangedHandler processStatusChangedHandler); + + /// + /// Sets the watchable process list. + /// + /// + /// void SetProcessIds(int mainProcessId, ReadOnlySpan processIds); + + /// + /// Enables watching processes. + /// + /// void WatchProcesses(int mainProcessId); } diff --git a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/ProcessInfoMonitor.cs b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/ProcessInfoMonitor.cs index 6678f0540..aca38d904 100644 --- a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/ProcessInfoMonitor.cs +++ b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Abstractions/Processes/ProcessInfoMonitor.cs @@ -54,7 +54,7 @@ private void ProcessIdsChanged(object? sender, NotifyCollectionChangedEventArgs { if (e.OldItems != null) foreach (int pid in e.OldItems) - _processTerminatedHandler?.Invoke(pid); + ProcessTerminated(pid); break; } diff --git a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessAggregatorFactory.cs b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessAggregatorFactory.cs index 7330c2489..3bd480f80 100644 --- a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessAggregatorFactory.cs +++ b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessAggregatorFactory.cs @@ -21,11 +21,11 @@ namespace ProcessExplorer.Core.Factories; public static class ProcessAggregatorFactory { public static IProcessInfoAggregator CreateProcessInfoAggregator( - ProcessInfoMonitor processInfoManager, + ProcessInfoMonitor processInfoMonitor, IUiHandler handler, ISubsystemController? subsystemController = null, - ILogger logger = null) + ILogger? logger = null) { - return new ProcessInfoAggregator(processInfoManager, handler, subsystemController, logger); + return new ProcessInfoAggregator(processInfoMonitor, handler, subsystemController, logger); } } diff --git a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessMonitorFactory.cs b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessMonitorFactory.cs index fbc1d2b3e..2b43fd174 100644 --- a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessMonitorFactory.cs +++ b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/Factories/ProcessMonitorFactory.cs @@ -17,7 +17,7 @@ namespace ProcessExplorer.Core.Factories; public static class ProcessMonitorFactory { - public static ProcessInfoMonitor CreateProcessInfoGeneratorWindows(ILogger logger) + public static ProcessInfoMonitor CreateProcessInfoMonitorWindows(ILogger logger) { #pragma warning disable CA1416 // Validate platform compatibility return new WindowsProcessInfoMonitor(logger); diff --git a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/ProcessInfoAggregator.cs b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/ProcessInfoAggregator.cs index b22f22346..b31f799e5 100644 --- a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/ProcessInfoAggregator.cs +++ b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/src/ProcessExplorer.Core/ProcessInfoAggregator.cs @@ -34,7 +34,7 @@ internal class ProcessInfoAggregator : IProcessInfoAggregator private readonly object _processsInformationLock = new(); //putting subsystem change messages to the queue and remove it if it has been sent ~ FIFO private readonly ConcurrentQueue> _subsystemStateChanges = new(); - private readonly IProcessInfoMonitor _processInfoManager; + private readonly IProcessInfoMonitor _processInfoMonitor; private readonly IUiHandler _handler; private bool _disposed; @@ -53,7 +53,7 @@ public ProcessInfoAggregator( _handler = handler; UiHandler = _handler; - _processInfoManager = processInfoManager; + _processInfoMonitor = processInfoManager; SubsystemController = subsystemController; if (SubsystemController != null) @@ -64,7 +64,7 @@ public ProcessInfoAggregator( private void DisposeCore() { - _processInfoManager.Dispose(); + _processInfoMonitor.Dispose(); } private Task UpdateInfoOnUI(Func handlerAction) @@ -90,7 +90,7 @@ private void UpdateProcessInfoCollectorData(string assemblyId, ProcessInfoCollec private void SetUiCommunicatorsToWatchProcessChanges() { - _processInfoManager.SetHandlers( + _processInfoMonitor.SetHandlers( ProcessModified, ProcessTerminated, ProcessCreated, @@ -104,7 +104,7 @@ private IEnumerable GetProcesses(ReadOnlySpan processIds) foreach (var id in processIds) { - var process = ProcessInformation.GetProcessInfoWithCalculatedData(Process.GetProcessById(id), _processInfoManager); + var process = ProcessInformation.GetProcessInfoWithCalculatedData(Process.GetProcessById(id), _processInfoMonitor); processes.Add(process.ProcessInfo); } @@ -127,7 +127,7 @@ await Task.Run(() => Task.Delay(TerminatingProcessDelay); }); - var processes = GetProcesses(_processInfoManager.GetProcessIds()); + var processes = GetProcesses(_processInfoMonitor.GetProcessIds()); await _handler.TerminateProcess(processId); await _handler.AddProcesses(processes); @@ -146,7 +146,7 @@ private async void ProcessCreated(int processId) private ProcessInfoData? GetProcess(int processId) { - var process = GetProcesses(_processInfoManager.GetProcessIds()).FirstOrDefault(proc => proc.ProcessId == processId); + var process = GetProcesses(_processInfoMonitor.GetProcessIds()).FirstOrDefault(proc => proc.ProcessId == processId); if (process == null) return null; @@ -190,7 +190,7 @@ public async Task RunSubsystemStateQueue(CancellationToken cancellationToken) public IEnumerable GetProcesses() { - var processIds = _processInfoManager.GetProcessIds(); + var processIds = _processInfoMonitor.GetProcessIds(); return GetProcesses(processIds); } @@ -331,18 +331,18 @@ public async Task UpdateOrAddModuleInfo(string assemblyId, IEnumerable processIds) { - _processInfoManager.ClearProcessIds(); - _processInfoManager.SetProcessIds(MainProcessId, processIds); + _processInfoMonitor.ClearProcessIds(); + _processInfoMonitor.SetProcessIds(MainProcessId, processIds); } public void SetSubsystemController(ISubsystemController subsystemController) @@ -380,7 +380,7 @@ public void ScheduleSubsystemStateChanged(Guid instanceId, string state) public Task AddProcesses(ReadOnlySpan processes) { - _processInfoManager.SetProcessIds(MainProcessId, processes); + _processInfoMonitor.SetProcessIds(MainProcessId, processes); return Task.CompletedTask; } } diff --git a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/test/ProcessExplorer.Core.Tests/ProcessInfoAggregator.Tests.cs b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/test/ProcessExplorer.Core.Tests/ProcessInfoAggregator.Tests.cs index 9965d8e9e..a6a1d60b9 100644 --- a/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/test/ProcessExplorer.Core.Tests/ProcessInfoAggregator.Tests.cs +++ b/Tryouts/Plugins/ApplicationPlugins/MorganStanley.ComposeUI.ProcessExplorer/dotnet/test/ProcessExplorer.Core.Tests/ProcessInfoAggregator.Tests.cs @@ -44,10 +44,10 @@ public async Task RunSubsystemQueue_will_cancel_after_timeout() //Creating mocks to handle method var mockUiHandler = new Mock(); var mockSubsystemController = new Mock(); - var mockProcessInfoManager = new Mock(NullLogger.Instance); + var mockProcessInfoMonitor = new Mock(NullLogger.Instance); var clientMock = new Mock>(); var processInfoAggregator = new ProcessInfoAggregator( - mockProcessInfoManager.Object, + mockProcessInfoMonitor.Object, mockUiHandler.Object, mockSubsystemController.Object, NullLogger.Instance); @@ -323,16 +323,16 @@ public async Task UpdateModuleInfo_will_update_modules() public void EnableWatchingSavedProcesses_will_begin_to_watch_processes() { var mockSubsystemController = new Mock(); - var mockProcessInfoManager = new Mock(); + var mockProcessInfoMonitor = new Mock(); var mockUiHandler = new Mock(); var processInfoAggregator = new ProcessInfoAggregator( - mockProcessInfoManager.Object, + mockProcessInfoMonitor.Object, mockUiHandler.Object, mockSubsystemController.Object, NullLogger.Instance); processInfoAggregator.EnableWatchingSavedProcesses(); - mockProcessInfoManager.Verify(x => x.WatchProcesses(processInfoAggregator.MainProcessId), Times.Once); + mockProcessInfoMonitor.Verify(x => x.WatchProcesses(processInfoAggregator.MainProcessId), Times.Once); } @@ -361,10 +361,10 @@ public void ScheduleSubsystemStateChanged_will_put_items_to_the_queue() private IProcessInfoAggregator CreateProcessInfoAggregator() { var mockSubsystemController = new Mock(); - var mockProcessInfoManager = new Mock(); + var mockProcessInfoMonitor = new Mock(); var mockUiHandler = new Mock(); var processInfoAggregator = new ProcessInfoAggregator( - mockProcessInfoManager.Object, + mockProcessInfoMonitor.Object, mockUiHandler.Object, mockSubsystemController.Object, NullLogger.Instance);