diff --git a/FastGithub.FlowAnalyze/FlowAnalyzer.cs b/FastGithub.FlowAnalyze/FlowAnalyzer.cs index dfaa282..e4c9c1b 100644 --- a/FastGithub.FlowAnalyze/FlowAnalyzer.cs +++ b/FastGithub.FlowAnalyze/FlowAnalyzer.cs @@ -1,12 +1,15 @@ using System; using System.Collections.Concurrent; using System.Linq; +using System.Threading; namespace FastGithub.FlowAnalyze { sealed class FlowAnalyzer : IFlowAnalyzer { private const int INTERVAL_SECONDS = 5; + private long totalRead = 0; + private long totalWrite = 0; private readonly ConcurrentQueue readQueue = new(); private readonly ConcurrentQueue writeQueue = new(); @@ -21,10 +24,12 @@ namespace FastGithub.FlowAnalyze { if (flowType == FlowType.Read) { + Interlocked.Add(ref this.totalRead, length); Add(this.readQueue, length); } else { + Interlocked.Add(ref this.totalWrite, length); Add(this.writeQueue, length); } } @@ -71,7 +76,13 @@ namespace FastGithub.FlowAnalyze Flush(this.writeQueue); var writeRate = (double)this.writeQueue.Sum(item => item.Length) / INTERVAL_SECONDS; - return new FlowRate { ReadRate = readRate, WriteRate = writeRate }; + return new FlowRate + { + TotalRead = this.totalRead, + TotalWrite = this.totalWrite, + ReadRate = readRate, + WriteRate = writeRate + }; } } } diff --git a/FastGithub.FlowAnalyze/FlowRate.cs b/FastGithub.FlowAnalyze/FlowRate.cs index edeb855..536a2c2 100644 --- a/FastGithub.FlowAnalyze/FlowRate.cs +++ b/FastGithub.FlowAnalyze/FlowRate.cs @@ -2,6 +2,16 @@ { public record FlowRate { + /// + /// 获取总读上行 + /// + public long TotalRead { get; init; } + + /// + /// 获取总下行 + /// + public long TotalWrite { get; init; } + public double ReadRate { get; init; } public double WriteRate { get; init; } diff --git a/FastGithub.UI/FlowChart.xaml b/FastGithub.UI/FlowChart.xaml index f91b2a4..7871d10 100644 --- a/FastGithub.UI/FlowChart.xaml +++ b/FastGithub.UI/FlowChart.xaml @@ -5,9 +5,17 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf" mc:Ignorable="d" - d:DesignHeight="300" d:DesignWidth="300"> + d:DesignHeight="100" d:DesignWidth="300"> - + + + + + + + + + @@ -15,5 +23,12 @@ + + 上行流量 + + + 下行流量 + + diff --git a/FastGithub.UI/FlowChart.xaml.cs b/FastGithub.UI/FlowChart.xaml.cs index 7199bd6..e1dc7a6 100644 --- a/FastGithub.UI/FlowChart.xaml.cs +++ b/FastGithub.UI/FlowChart.xaml.cs @@ -69,6 +69,9 @@ namespace FastGithub.UI var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); var flowRate = Newtonsoft.Json.JsonConvert.DeserializeObject(json); + this.textBlockRead.Text = flowRate.ToTotalReadString(); + this.textBlockWrite.Text = flowRate.ToTotalWriteString(); + this.readSeries.Values.Add(flowRate.ReadRate / 1024); this.writeSeries.Values.Add(flowRate.WriteRate / 1024); this.Labels.Add(DateTime.Now.ToString("HH:mm:ss")); diff --git a/FastGithub.UI/FlowRate.cs b/FastGithub.UI/FlowRate.cs index 02e5c5f..c20c454 100644 --- a/FastGithub.UI/FlowRate.cs +++ b/FastGithub.UI/FlowRate.cs @@ -2,8 +2,41 @@ { public class FlowRate { + /// + /// 获取总读上行 + /// + public long TotalRead { get; set; } + + /// + /// 获取总下行 + /// + public long TotalWrite { get; set; } + public double ReadRate { get; set; } public double WriteRate { get; set; } + + + public string ToTotalReadString() + { + return ToNetworkString(this.TotalRead); + } + + public string ToTotalWriteString() + { + return ToNetworkString(this.TotalWrite); + } + private static string ToNetworkString(long value) + { + if (value < 1024) + { + return $"{value}B"; + } + if (value < 1024 * 1024) + { + return $"{value / 1024d:0.00}KB"; + } + return $"{value / 1024d / 1024d:0.00}MB"; + } } }