ChartsWindow.xaml.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. using LiveCharts;
  2. using NTMiner.Core.MinerServer;
  3. using NTMiner.MinerStudio.Vms;
  4. using NTMiner.Views;
  5. using NTMiner.Vms;
  6. using System;
  7. using System.Linq;
  8. using System.Windows;
  9. using System.Windows.Input;
  10. namespace NTMiner.MinerStudio.Views {
  11. public partial class ChartsWindow : BlankWindow {
  12. private static ChartsWindow _sWindow = null;
  13. public static void ShowWindow() {
  14. if (_sWindow == null) {
  15. _sWindow = new ChartsWindow();
  16. }
  17. _sWindow.Show();
  18. if (_sWindow.WindowState == WindowState.Minimized) {
  19. _sWindow.WindowState = WindowState.Normal;
  20. }
  21. _sWindow.Activate();
  22. }
  23. public ChartsWindowViewModel Vm { get; private set; }
  24. private ChartsWindow() {
  25. if (WpfUtil.IsInDesignMode) {
  26. return;
  27. }
  28. this.Vm = new ChartsWindowViewModel();
  29. this.DataContext = this.Vm;
  30. Width = SystemParameters.FullPrimaryScreenWidth * 0.95;
  31. Height = SystemParameters.FullPrimaryScreenHeight * 0.95;
  32. InitializeComponent();
  33. this.TbUcName.Text = nameof(ChartsWindow);
  34. NotiCenterWindow.Bind(this);
  35. #region 总算力
  36. this.AddEventPath<Per10SecondEvent>("周期刷新总算力图", LogEnum.DevConsole,
  37. action: message => {
  38. RefreshTotalSpeedChart(limit: 1);
  39. }, location: this.GetType());
  40. RefreshTotalSpeedChart(limit: 60);
  41. #endregion
  42. }
  43. protected override void OnClosed(EventArgs e) {
  44. _sWindow = null;
  45. base.OnClosed(e);
  46. }
  47. #region 刷新总算力图表
  48. private void RefreshTotalSpeedChart(int limit) {
  49. MinerStudioService.Instance.GetLatestSnapshotsAsync(
  50. limit,
  51. (response, exception) => {
  52. if (response == null) {
  53. return;
  54. }
  55. if (!response.IsSuccess()) {
  56. VirtualRoot.Out.ShowError(response.ReadMessage(exception), autoHideSeconds: 4);
  57. return;
  58. }
  59. UIThread.Execute(() => {
  60. bool isOnlyOne = limit == 1;
  61. Vm.TotalMiningCount = response.TotalMiningCount;
  62. Vm.TotalOnlineCount = response.TotalOnlineCount;
  63. foreach (var chartVm in Vm.ChartVms) {
  64. var list = response.Data.Where(a => a.CoinCode == chartVm.CoinVm.Code).ToList();
  65. if (list.Count == 0) {
  66. list.Add(new CoinSnapshotData {
  67. CoinCode = chartVm.CoinVm.Code,
  68. DualCoinOnlineCount = 0,
  69. DualCoinMiningCount = 0,
  70. MainCoinOnlineCount = 0,
  71. MainCoinMiningCount = 0,
  72. RejectShareDelta = 0,
  73. ShareDelta = 0,
  74. Speed = 0,
  75. Timestamp = DateTime.Now.AddSeconds(-5)
  76. });
  77. }
  78. CoinSnapshotData one = null;
  79. if (isOnlyOne) {
  80. one = list.Last();
  81. }
  82. else {
  83. list = list.OrderBy(a => a.Timestamp).ToList();
  84. }
  85. CoinSnapshotData latestData = list.Last();
  86. chartVm.SnapshotDataVm.Update(latestData);
  87. foreach (var riser in chartVm.Series) {
  88. if (riser.Title == "speed") {
  89. if (list.Count > 0) {
  90. if (isOnlyOne) {
  91. riser.Values.Add(new MeasureModel() {
  92. DateTime = one.Timestamp,
  93. Value = one.Speed
  94. });
  95. }
  96. else {
  97. foreach (var item in list) {
  98. riser.Values.Add(new MeasureModel() {
  99. DateTime = item.Timestamp,
  100. Value = item.Speed
  101. });
  102. }
  103. }
  104. }
  105. }
  106. else if (riser.Title == "onlineCount") {
  107. if (isOnlyOne) {
  108. riser.Values.Add(new MeasureModel() {
  109. DateTime = one.Timestamp,
  110. Value = one.MainCoinOnlineCount + one.DualCoinOnlineCount
  111. });
  112. }
  113. else {
  114. foreach (var item in list) {
  115. riser.Values.Add(new MeasureModel() {
  116. DateTime = item.Timestamp,
  117. Value = item.MainCoinOnlineCount + item.DualCoinOnlineCount
  118. });
  119. }
  120. }
  121. }
  122. else if (riser.Title == "miningCount") {
  123. if (isOnlyOne) {
  124. riser.Values.Add(new MeasureModel() {
  125. DateTime = one.Timestamp,
  126. Value = one.MainCoinMiningCount + one.DualCoinMiningCount
  127. });
  128. }
  129. else {
  130. foreach (var item in list) {
  131. riser.Values.Add(new MeasureModel() {
  132. DateTime = item.Timestamp,
  133. Value = item.MainCoinMiningCount + item.DualCoinMiningCount
  134. });
  135. }
  136. }
  137. }
  138. else if (riser.Title == "rejectShare") {
  139. if (isOnlyOne) {
  140. riser.Values.Add(new MeasureModel() {
  141. DateTime = one.Timestamp,
  142. Value = one.RejectShareDelta
  143. });
  144. }
  145. else {
  146. foreach (var item in list) {
  147. riser.Values.Add(new MeasureModel() {
  148. DateTime = item.Timestamp,
  149. Value = item.RejectShareDelta
  150. });
  151. }
  152. }
  153. }
  154. else if (riser.Title == "acceptShare") {
  155. if (isOnlyOne) {
  156. riser.Values.Add(new MeasureModel() {
  157. DateTime = one.Timestamp,
  158. Value = one.ShareDelta
  159. });
  160. }
  161. else {
  162. foreach (var item in list) {
  163. riser.Values.Add(new MeasureModel() {
  164. DateTime = item.Timestamp,
  165. Value = item.ShareDelta
  166. });
  167. }
  168. }
  169. }
  170. }
  171. DateTime now = DateTime.Now.AddSeconds(10);
  172. foreach (var riser in chartVm.Series) {
  173. IChartValues valuesTotal = riser.Values;
  174. if (valuesTotal.Count > 0 && ((MeasureModel)valuesTotal[0]).DateTime.AddMinutes(NTMinerContext.SpeedHistoryLengthByMinute) < now) {
  175. valuesTotal.RemoveAt(0);
  176. }
  177. }
  178. chartVm.SetAxisLimits(now);
  179. }
  180. });
  181. });
  182. }
  183. #endregion
  184. private void ScrollViewer_PreviewMouseDown(object sender, MouseButtonEventArgs e) {
  185. WpfUtil.ScrollViewer_PreviewMouseDown(sender, e);
  186. }
  187. private void IsShowCheckBox_Click(object sender, RoutedEventArgs e) {
  188. Vm.OnPropertyChanged(nameof(Vm.IsShowAll));
  189. }
  190. }
  191. }