From 4efbbe8a4e452539c1272fac2ba5c5ca12343be7 Mon Sep 17 00:00:00 2001 From: Pat Hartl Date: Fri, 17 Feb 2023 00:33:32 -0600 Subject: [PATCH] Added basic dashboard --- LANCommander/LANCommander.csproj | 1 + LANCommander/Models/PerformanceChartData.cs | 35 +++++ LANCommander/Pages/Dashboard.razor | 146 ++++++++++++++++++++ 3 files changed, 182 insertions(+) create mode 100644 LANCommander/Models/PerformanceChartData.cs create mode 100644 LANCommander/Pages/Dashboard.razor diff --git a/LANCommander/LANCommander.csproj b/LANCommander/LANCommander.csproj index 05532e1..e041303 100644 --- a/LANCommander/LANCommander.csproj +++ b/LANCommander/LANCommander.csproj @@ -43,6 +43,7 @@ + diff --git a/LANCommander/Models/PerformanceChartData.cs b/LANCommander/Models/PerformanceChartData.cs new file mode 100644 index 0000000..10984d6 --- /dev/null +++ b/LANCommander/Models/PerformanceChartData.cs @@ -0,0 +1,35 @@ +using MudBlazor; +using System.Diagnostics; + +namespace LANCommander.Models +{ + public class PerformanceChartData + { + public PerformanceCounterData ProcessorUtilization { get; set; } + public Dictionary NetworkUploadRate { get; set; } + public Dictionary NetworkDownloadRate { get; set; } + } + + public class PerformanceCounterData + { + public PerformanceCounter PerformanceCounter { get; set; } + public double[] Data { get; set; } + + public ChartSeries ToSeries(string name) + { + return new ChartSeries + { + Name = name, + Data = Data + }; + } + + public List ToSeriesList(string name) + { + return new List + { + ToSeries(name) + }; + } + } +} diff --git a/LANCommander/Pages/Dashboard.razor b/LANCommander/Pages/Dashboard.razor new file mode 100644 index 0000000..0eb02b5 --- /dev/null +++ b/LANCommander/Pages/Dashboard.razor @@ -0,0 +1,146 @@ +@page "/Dashboard" + +@using System.Diagnostics; +@using LANCommander.Models; + + + + + + + CPU Utilization (%) + + + + @if (PerformanceChartData.ProcessorUtilization != null && PerformanceChartData.ProcessorUtilization.PerformanceCounter != null) + { + + } + + + + + + + + + Upload Rate (MB/s) + + + + + + + + + + + + + Download Rate (MB/s) + + + + + + + + + +@code { + int cpuTime = 0; + + int RecordTime = 60; + + private PerformanceChartData PerformanceChartData = new PerformanceChartData() + { + ProcessorUtilization = new PerformanceCounterData(), + NetworkUploadRate = new Dictionary(), + NetworkDownloadRate = new Dictionary() + }; + + protected override async Task OnInitializedAsync() + { + var timer = new System.Timers.Timer(); + + timer.Interval = 1000; + + timer.Elapsed += async (s, e) => + { + RefreshLiveData(); + + await InvokeAsync(StateHasChanged); + }; + + timer.Start(); + } + + private void RefreshLiveData() + { + RefreshProcessorUtilization(); + RefreshNetworkUsage(); + } + + private void RefreshProcessorUtilization() + { + if (PerformanceChartData.ProcessorUtilization.PerformanceCounter == null) + PerformanceChartData.ProcessorUtilization.PerformanceCounter = new PerformanceCounter("Processor", "% Processor Time", "_Total"); + + PerformanceChartData.ProcessorUtilization.Data = ShiftArrayAndInsert(PerformanceChartData.ProcessorUtilization.Data, PerformanceChartData.ProcessorUtilization.PerformanceCounter.NextValue()); + } + + private void RefreshNetworkUsage() + { + var category = new PerformanceCounterCategory("Network Interface"); + + foreach (var instance in category.GetInstanceNames()) + { + if (!PerformanceChartData.NetworkUploadRate.ContainsKey(instance)) + PerformanceChartData.NetworkUploadRate[instance] = new PerformanceCounterData() + { + PerformanceCounter = new PerformanceCounter("Network Interface", "Bytes Sent/sec", instance), + Data = new double[RecordTime] + }; + + if (!PerformanceChartData.NetworkDownloadRate.ContainsKey(instance)) + PerformanceChartData.NetworkDownloadRate[instance] = new PerformanceCounterData() + { + PerformanceCounter = new PerformanceCounter("Network Interface", "Bytes Received/sec", instance), + Data = new double[RecordTime] + }; + + PerformanceChartData.NetworkUploadRate[instance].Data = ShiftArrayAndInsert(PerformanceChartData.NetworkUploadRate[instance].Data, (double)PerformanceChartData.NetworkUploadRate[instance].PerformanceCounter.NextValue() / (1024 * 1024)); + PerformanceChartData.NetworkDownloadRate[instance].Data = ShiftArrayAndInsert(PerformanceChartData.NetworkDownloadRate[instance].Data, (double)PerformanceChartData.NetworkDownloadRate[instance].PerformanceCounter.NextValue() / (1024 * 1024)); + } + } + + private T[] ShiftArrayAndInsert(T[] array, T input) + { + if (array == null || array.Length < RecordTime) + { + array = new T[RecordTime]; + } + + Array.Copy(array, 1, array, 0, array.Length - 1); + + array[array.Length - 1] = input; + + return array; + } + + private PerformanceCounter[] GetCounters(string categoryName) + { + try + { + var category = PerformanceCounterCategory.GetCategories().First(c => c.CategoryName == categoryName); + + var instanceName = Process.GetCurrentProcess().ProcessName; + + return category.GetCounters(instanceName); + } + catch + { + return new PerformanceCounter[] { }; + } + } +}