FlowChart.xaml.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. using LiveCharts;
  2. using LiveCharts.Configurations;
  3. using LiveCharts.Wpf;
  4. using Newtonsoft.Json;
  5. using System;
  6. using System.Net.Http;
  7. using System.Threading.Tasks;
  8. using System.Windows.Controls;
  9. namespace FastGithub.UI
  10. {
  11. /// <summary>
  12. /// FlowChart.xaml 的交互逻辑
  13. /// </summary>
  14. public partial class FlowChart : UserControl
  15. {
  16. private readonly LineSeries readSeries = new LineSeries
  17. {
  18. Title = "上行速率",
  19. PointGeometry = null,
  20. Values = new ChartValues<RateItem>()
  21. };
  22. private readonly LineSeries writeSeries = new LineSeries()
  23. {
  24. Title = "下行速率",
  25. PointGeometry = null,
  26. Values = new ChartValues<RateItem>()
  27. };
  28. private static DateTime GetDateTime(double timestamp) => new DateTime(1970, 1, 1).Add(TimeSpan.FromMilliseconds(timestamp)).ToLocalTime();
  29. private static double GetTimestamp(DateTime dateTime) => dateTime.ToUniversalTime().Subtract(new DateTime(1970, 1, 1)).TotalMilliseconds;
  30. public SeriesCollection Series { get; } = new SeriesCollection(Mappers.Xy<RateItem>().X(item => item.Timestamp).Y(item => item.Rate));
  31. public Func<double, string> XFormatter { get; } = timestamp => GetDateTime(timestamp).ToString("HH:mm:ss");
  32. public Func<double, string> YFormatter { get; } = value => $"{FlowStatistics.ToNetworkSizeString((long)value)}/s";
  33. public FlowChart()
  34. {
  35. InitializeComponent();
  36. this.Series.Add(this.readSeries);
  37. this.Series.Add(this.writeSeries);
  38. this.DataContext = this;
  39. this.InitFlowChart();
  40. }
  41. private async void InitFlowChart()
  42. {
  43. using var httpClient = new HttpClient();
  44. while (this.Dispatcher.HasShutdownStarted == false)
  45. {
  46. try
  47. {
  48. await this.FlushFlowStatisticsAsync(httpClient);
  49. }
  50. catch (Exception)
  51. {
  52. }
  53. finally
  54. {
  55. await Task.Delay(TimeSpan.FromSeconds(1d));
  56. }
  57. }
  58. }
  59. private async Task FlushFlowStatisticsAsync(HttpClient httpClient)
  60. {
  61. var response = await httpClient.GetAsync("http://127.0.0.1/flowStatistics");
  62. var json = await response.EnsureSuccessStatusCode().Content.ReadAsStringAsync();
  63. var flowStatistics = JsonConvert.DeserializeObject<FlowStatistics>(json);
  64. if (flowStatistics == null)
  65. {
  66. return;
  67. }
  68. this.textBlockRead.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalRead);
  69. this.textBlockWrite.Text = FlowStatistics.ToNetworkSizeString(flowStatistics.TotalWrite);
  70. var timestamp = GetTimestamp(DateTime.Now);
  71. this.readSeries.Values.Add(new RateItem(flowStatistics.ReadRate, timestamp));
  72. this.writeSeries.Values.Add(new RateItem(flowStatistics.WriteRate, timestamp));
  73. if (this.readSeries.Values.Count > 60)
  74. {
  75. this.readSeries.Values.RemoveAt(0);
  76. this.writeSeries.Values.RemoveAt(0);
  77. }
  78. }
  79. private class RateItem
  80. {
  81. public double Rate { get; }
  82. public double Timestamp { get; }
  83. public RateItem(double rate, double timestamp)
  84. {
  85. this.Rate = rate;
  86. this.Timestamp = timestamp;
  87. }
  88. }
  89. }
  90. }