增加总流量

This commit is contained in:
老九 2021-10-30 23:35:10 +08:00
parent d27ac92b36
commit 977694eaa8
5 changed files with 75 additions and 3 deletions

View File

@ -1,12 +1,15 @@
using System; using System;
using System.Collections.Concurrent; using System.Collections.Concurrent;
using System.Linq; using System.Linq;
using System.Threading;
namespace FastGithub.FlowAnalyze namespace FastGithub.FlowAnalyze
{ {
sealed class FlowAnalyzer : IFlowAnalyzer sealed class FlowAnalyzer : IFlowAnalyzer
{ {
private const int INTERVAL_SECONDS = 5; private const int INTERVAL_SECONDS = 5;
private long totalRead = 0;
private long totalWrite = 0;
private readonly ConcurrentQueue<QueueItem> readQueue = new(); private readonly ConcurrentQueue<QueueItem> readQueue = new();
private readonly ConcurrentQueue<QueueItem> writeQueue = new(); private readonly ConcurrentQueue<QueueItem> writeQueue = new();
@ -21,10 +24,12 @@ namespace FastGithub.FlowAnalyze
{ {
if (flowType == FlowType.Read) if (flowType == FlowType.Read)
{ {
Interlocked.Add(ref this.totalRead, length);
Add(this.readQueue, length); Add(this.readQueue, length);
} }
else else
{ {
Interlocked.Add(ref this.totalWrite, length);
Add(this.writeQueue, length); Add(this.writeQueue, length);
} }
} }
@ -71,7 +76,13 @@ namespace FastGithub.FlowAnalyze
Flush(this.writeQueue); Flush(this.writeQueue);
var writeRate = (double)this.writeQueue.Sum(item => item.Length) / INTERVAL_SECONDS; 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
};
} }
} }
} }

View File

@ -2,6 +2,16 @@
{ {
public record FlowRate public record FlowRate
{ {
/// <summary>
/// 获取总读上行
/// </summary>
public long TotalRead { get; init; }
/// <summary>
/// 获取总下行
/// </summary>
public long TotalWrite { get; init; }
public double ReadRate { get; init; } public double ReadRate { get; init; }
public double WriteRate { get; init; } public double WriteRate { get; init; }

View File

@ -5,9 +5,17 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf" xmlns:lvc="clr-namespace:LiveCharts.Wpf;assembly=LiveCharts.Wpf"
mc:Ignorable="d" mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300"> d:DesignHeight="100" d:DesignWidth="300">
<Grid> <Grid>
<lvc:CartesianChart Name="flowChart" Series="{Binding Series}" LegendLocation="None" > <Grid.ColumnDefinitions>
<ColumnDefinition Width=".7*"></ColumnDefinition>
<ColumnDefinition Width=".3*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
</Grid.RowDefinitions>
<lvc:CartesianChart Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Name="flowChart" Series="{Binding Series}" LegendLocation="None" >
<lvc:CartesianChart.AxisY> <lvc:CartesianChart.AxisY>
<lvc:Axis Foreground="#222" MinValue="0" LabelFormatter="{Binding YFormatter}"></lvc:Axis> <lvc:Axis Foreground="#222" MinValue="0" LabelFormatter="{Binding YFormatter}"></lvc:Axis>
</lvc:CartesianChart.AxisY> </lvc:CartesianChart.AxisY>
@ -15,5 +23,12 @@
<lvc:Axis Foreground="#222" Labels="{Binding Labels}"></lvc:Axis> <lvc:Axis Foreground="#222" Labels="{Binding Labels}"></lvc:Axis>
</lvc:CartesianChart.AxisX> </lvc:CartesianChart.AxisX>
</lvc:CartesianChart> </lvc:CartesianChart>
<StackPanel Grid.Row="0" Grid.RowSpan="2" Grid.Column="1" VerticalAlignment="Top">
<TextBlock FontSize="12" FontWeight="Light" Foreground="DodgerBlue" HorizontalAlignment="Center" Margin="0 5">上行流量</TextBlock>
<TextBlock x:Name="textBlockRead" Text="0B" Foreground="DodgerBlue" HorizontalAlignment="Center" Margin="0 0 0 8"/>
<TextBlock FontSize="12" FontWeight="Light" Foreground="IndianRed" HorizontalAlignment="Center" Margin="0 5">下行流量</TextBlock>
<TextBlock x:Name="textBlockWrite" Text="0B" Foreground="IndianRed" HorizontalAlignment="Center" Margin="0 0 0 8"/>
</StackPanel>
</Grid> </Grid>
</UserControl> </UserControl>

View File

@ -69,6 +69,9 @@ namespace FastGithub.UI
var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync(); var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
var flowRate = Newtonsoft.Json.JsonConvert.DeserializeObject<FlowRate>(json); var flowRate = Newtonsoft.Json.JsonConvert.DeserializeObject<FlowRate>(json);
this.textBlockRead.Text = flowRate.ToTotalReadString();
this.textBlockWrite.Text = flowRate.ToTotalWriteString();
this.readSeries.Values.Add(flowRate.ReadRate / 1024); this.readSeries.Values.Add(flowRate.ReadRate / 1024);
this.writeSeries.Values.Add(flowRate.WriteRate / 1024); this.writeSeries.Values.Add(flowRate.WriteRate / 1024);
this.Labels.Add(DateTime.Now.ToString("HH:mm:ss")); this.Labels.Add(DateTime.Now.ToString("HH:mm:ss"));

View File

@ -2,8 +2,41 @@
{ {
public class FlowRate public class FlowRate
{ {
/// <summary>
/// 获取总读上行
/// </summary>
public long TotalRead { get; set; }
/// <summary>
/// 获取总下行
/// </summary>
public long TotalWrite { get; set; }
public double ReadRate { get; set; } public double ReadRate { get; set; }
public double WriteRate { 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";
}
} }
} }