using System; using System.Collections.Generic; using System.Linq; using System.Text; using Protocol376Simulator.Interfaces; namespace Protocol376Simulator.Services { /// /// 统计服务类,用于收集和报告通信统计信息 /// public class StatisticsService { private readonly string _deviceIdentifier; private int _totalMessagesSent = 0; private int _totalMessagesReceived = 0; private int _failedMessages = 0; private readonly Dictionary _messageTypeStats = new Dictionary(); private readonly List _responseTimes = new List(); private DateTime _lastMessageSentTime = DateTime.MinValue; private readonly Dictionary _errorStats = new Dictionary(); /// /// 构造函数 /// /// 设备标识(用于日志和显示) public StatisticsService(string deviceIdentifier) { _deviceIdentifier = deviceIdentifier; // 初始化消息类型统计 foreach (MessageType type in Enum.GetValues(typeof(MessageType))) { _messageTypeStats[type] = 0; } } /// /// 总发送消息数 /// public int TotalMessagesSent => _totalMessagesSent; /// /// 总接收消息数 /// public int TotalMessagesReceived => _totalMessagesReceived; /// /// 失败消息数 /// public int FailedMessages => _failedMessages; /// /// 消息类型统计 /// public Dictionary MessageTypeStats => _messageTypeStats; /// /// 平均响应时间 /// public TimeSpan AverageResponseTime => _responseTimes.Count > 0 ? TimeSpan.FromMilliseconds(_responseTimes.Average(t => t.TotalMilliseconds)) : TimeSpan.Zero; /// /// 最大响应时间 /// public TimeSpan MaxResponseTime => _responseTimes.Count > 0 ? _responseTimes.Max() : TimeSpan.Zero; /// /// 最小响应时间 /// public TimeSpan MinResponseTime => _responseTimes.Count > 0 ? _responseTimes.Min() : TimeSpan.Zero; /// /// 记录消息发送 /// public void RecordMessageSent() { _totalMessagesSent++; _lastMessageSentTime = DateTime.Now; } /// /// 记录消息接收 /// public void RecordMessageReceived() { _totalMessagesReceived++; } /// /// 记录消息失败 /// public void RecordMessageFailed() { _failedMessages++; } /// /// 记录消息类型统计 /// /// 消息类型 public void RecordMessageType(MessageType messageType) { if (_messageTypeStats.ContainsKey(messageType)) { _messageTypeStats[messageType]++; } } /// /// 记录响应时间 /// /// 响应时间 public void RecordResponseTime(TimeSpan responseTime) { if (responseTime.TotalMilliseconds > 0) { _responseTimes.Add(responseTime); // 保持响应时间列表在合理大小范围内,避免内存泄漏 if (_responseTimes.Count > 1000) { _responseTimes.RemoveAt(0); } } } /// /// 记录错误统计 /// /// 错误类型 public void RecordError(string errorType) { if (string.IsNullOrEmpty(errorType)) { return; } if (_errorStats.ContainsKey(errorType)) { _errorStats[errorType]++; } else { _errorStats[errorType] = 1; } } /// /// 获取统计信息的字符串表示 /// /// 格式化的统计信息 public string GetStatisticsReport() { var sb = new StringBuilder(); sb.AppendLine($"设备 {_deviceIdentifier} 通信统计:"); sb.AppendLine($"总发送消息数: {_totalMessagesSent}"); sb.AppendLine($"总接收消息数: {_totalMessagesReceived}"); sb.AppendLine($"失败消息数: {_failedMessages}"); if (_responseTimes.Count > 0) { sb.AppendLine($"平均响应时间: {AverageResponseTime.TotalMilliseconds:F2}毫秒"); sb.AppendLine($"最大响应时间: {MaxResponseTime.TotalMilliseconds:F2}毫秒"); sb.AppendLine($"最小响应时间: {MinResponseTime.TotalMilliseconds:F2}毫秒"); } else { sb.AppendLine("尚未记录响应时间"); } sb.AppendLine("消息类型统计:"); foreach (var type in _messageTypeStats.Where(x => x.Value > 0).OrderByDescending(x => x.Value)) { sb.AppendLine($" {type.Key}: {type.Value}"); } if (_errorStats.Count > 0) { sb.AppendLine("错误统计:"); foreach (var error in _errorStats.OrderByDescending(x => x.Value)) { sb.AppendLine($" {error.Key}: {error.Value}"); } } return sb.ToString(); } /// /// 重置所有统计数据 /// public void ResetStatistics() { _totalMessagesSent = 0; _totalMessagesReceived = 0; _failedMessages = 0; _responseTimes.Clear(); _lastMessageSentTime = DateTime.MinValue; foreach (MessageType type in Enum.GetValues(typeof(MessageType))) { _messageTypeStats[type] = 0; } _errorStats.Clear(); } } }