123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- using LiveCharts;
- using LiveCharts.Configurations;
- using LiveCharts.Wpf;
- using Newtonsoft.Json;
- using System;
- using System.Net.Http;
- using System.Threading.Tasks;
- using System.Windows.Controls;
- namespace FastGithub.UI
- {
- /// <summary>
- /// FlowChart.xaml 的交互逻辑
- /// </summary>
- public partial class FlowChart : UserControl
- {
- private readonly LineSeries readSeries = new LineSeries
- {
- Title = "上行速率",
- PointGeometry = null,
- LineSmoothness = 1D,
- Values = new ChartValues<RateTick>()
- };
- private readonly LineSeries writeSeries = new LineSeries()
- {
- Title = "下行速率",
- PointGeometry = null,
- LineSmoothness = 1D,
- Values = new ChartValues<RateTick>()
- };
- private static DateTime GetDateTime(double timestamp) => new DateTime(1970, 1, 1).Add(TimeSpan.FromMilliseconds(timestamp)).ToLocalTime();
- private static double GetTimestamp(DateTime dateTime) => dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
- public SeriesCollection Series { get; } = new SeriesCollection(Mappers.Xy<RateTick>().X(item => item.Timestamp).Y(item => item.Rate));
- public Func<double, string> XFormatter { get; } = timestamp => GetDateTime(timestamp).ToString("HH:mm:ss");
- public Func<double, string> YFormatter { get; } = value => $"{FlowStatistics.ToNetworkSizeString((long)value)}/s";
- public FlowChart()
- {
- InitializeComponent();
- this.Series.Add(this.readSeries);
- this.Series.Add(this.writeSeries);
- this.DataContext = this;
- this.InitFlowChartAsync();
- }
- private async void InitFlowChartAsync()
- {
- using var httpClient = new HttpClient();
- while (this.Dispatcher.HasShutdownStarted == false)
- {
- try
- {
- await this.FlushFlowStatisticsAsync(httpClient);
- }
- catch (Exception)
- {
- }
- finally
- {
- await Task.Delay(TimeSpan.FromSeconds(1d));
- }
- }
- }
- private async Task FlushFlowStatisticsAsync(HttpClient httpClient)
- {
- var response = await httpClient.GetAsync("http://localhost/flowStatistics");
- var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
- var flowStatistics = JsonConvert.DeserializeObject<FlowStatistics>(json);
- if (flowStatistics == null)
- {
- return;
- }
- this.textBlockRead.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalRead);
- this.textBlockWrite.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalWrite);
- var timestamp = GetTimestamp(DateTime.Now);
- this.readSeries.Values.Add(new RateTick(flowStatistics.ReadRate, timestamp));
- this.writeSeries.Values.Add(new RateTick(flowStatistics.WriteRate, timestamp));
- if (this.readSeries.Values.Count > 60)
- {
- this.readSeries.Values.RemoveAt(0);
- this.writeSeries.Values.RemoveAt(0);
- }
- }
- private class RateTick
- {
- public double Rate { get; }
- public double Timestamp { get; }
- public RateTick(double rate, double timestamp)
- {
- this.Rate = rate;
- this.Timestamp = timestamp;
- }
- }
- }
- }
|