mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-05-07 00:39:00 +08:00
- 清理 GFramework.Core 与 GFramework.Cqrs 中的大量低风险 Meziantou 警告 - 修复 GFramework.Godot 运行时中的 ConfigureAwait、StringComparison 与参数校验告警 - 调整 Core SourceGenerators 中的字符串比较、文件命名与局部长方法问题 - 拆分部分配置与缓存辅助类型文件以消除 file/type mismatch 告警 - 更新 warning reduction 跟踪与执行记录,保留下一批结构性告警的恢复点
166 lines
4.7 KiB
C#
166 lines
4.7 KiB
C#
using System.Text;
|
||
using GFramework.Core.Abstractions.Events;
|
||
|
||
namespace GFramework.Core.Events;
|
||
|
||
/// <summary>
|
||
/// 事件统计信息实现类
|
||
/// 线程安全:使用 Interlocked 操作确保计数器的原子性
|
||
/// </summary>
|
||
public sealed class EventStatistics : IEventStatistics
|
||
{
|
||
private readonly Dictionary<string, int> _listenerCountByType = new(StringComparer.Ordinal);
|
||
private readonly object _lock = new();
|
||
private readonly Dictionary<string, long> _publishCountByType = new(StringComparer.Ordinal);
|
||
private long _totalFailed;
|
||
private long _totalHandled;
|
||
private long _totalPublished;
|
||
|
||
/// <inheritdoc />
|
||
public long TotalPublished => Interlocked.Read(ref _totalPublished);
|
||
|
||
/// <inheritdoc />
|
||
public long TotalHandled => Interlocked.Read(ref _totalHandled);
|
||
|
||
/// <inheritdoc />
|
||
public long TotalFailed => Interlocked.Read(ref _totalFailed);
|
||
|
||
/// <inheritdoc />
|
||
public int ActiveEventTypes
|
||
{
|
||
get
|
||
{
|
||
lock (_lock)
|
||
{
|
||
return _publishCountByType.Count;
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public int ActiveListeners
|
||
{
|
||
get
|
||
{
|
||
lock (_lock)
|
||
{
|
||
return _listenerCountByType.Values.Sum();
|
||
}
|
||
}
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public long GetPublishCount(string eventType)
|
||
{
|
||
lock (_lock)
|
||
{
|
||
return _publishCountByType.TryGetValue(eventType, out var count) ? count : 0;
|
||
}
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public int GetListenerCount(string eventType)
|
||
{
|
||
lock (_lock)
|
||
{
|
||
return _listenerCountByType.TryGetValue(eventType, out var count) ? count : 0;
|
||
}
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public void Reset()
|
||
{
|
||
Interlocked.Exchange(ref _totalPublished, 0);
|
||
Interlocked.Exchange(ref _totalHandled, 0);
|
||
Interlocked.Exchange(ref _totalFailed, 0);
|
||
|
||
lock (_lock)
|
||
{
|
||
_publishCountByType.Clear();
|
||
_listenerCountByType.Clear();
|
||
}
|
||
}
|
||
|
||
/// <inheritdoc />
|
||
public string GenerateReport()
|
||
{
|
||
var sb = new StringBuilder();
|
||
sb.AppendLine(FormattableString.Invariant($"=== 事件统计报告 ==="));
|
||
sb.AppendLine(FormattableString.Invariant($"总发布数: {TotalPublished}"));
|
||
sb.AppendLine(FormattableString.Invariant($"总处理数: {TotalHandled}"));
|
||
sb.AppendLine(FormattableString.Invariant($"总失败数: {TotalFailed}"));
|
||
sb.AppendLine(FormattableString.Invariant($"活跃事件类型: {ActiveEventTypes}"));
|
||
sb.AppendLine(FormattableString.Invariant($"活跃监听器: {ActiveListeners}"));
|
||
|
||
lock (_lock)
|
||
{
|
||
if (_publishCountByType.Count > 0)
|
||
{
|
||
sb.AppendLine(FormattableString.Invariant($"\n按事件类型统计(发布次数):"));
|
||
foreach (var kvp in _publishCountByType.OrderByDescending(x => x.Value))
|
||
sb.AppendLine(FormattableString.Invariant($" {kvp.Key}: {kvp.Value}"));
|
||
}
|
||
|
||
if (_listenerCountByType.Count > 0)
|
||
{
|
||
sb.AppendLine(FormattableString.Invariant($"\n按事件类型统计(监听器数量):"));
|
||
foreach (var kvp in _listenerCountByType.OrderByDescending(x => x.Value))
|
||
sb.AppendLine(FormattableString.Invariant($" {kvp.Key}: {kvp.Value}"));
|
||
}
|
||
}
|
||
|
||
return sb.ToString();
|
||
}
|
||
|
||
/// <summary>
|
||
/// 记录事件发布
|
||
/// </summary>
|
||
/// <param name="eventType">事件类型名称</param>
|
||
public void RecordPublish(string eventType)
|
||
{
|
||
Interlocked.Increment(ref _totalPublished);
|
||
|
||
lock (_lock)
|
||
{
|
||
_publishCountByType.TryGetValue(eventType, out var count);
|
||
_publishCountByType[eventType] = count + 1;
|
||
}
|
||
}
|
||
|
||
/// <summary>
|
||
/// 记录事件处理
|
||
/// </summary>
|
||
public void RecordHandle()
|
||
{
|
||
Interlocked.Increment(ref _totalHandled);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 记录事件处理失败
|
||
/// </summary>
|
||
public void RecordFailure()
|
||
{
|
||
Interlocked.Increment(ref _totalFailed);
|
||
}
|
||
|
||
/// <summary>
|
||
/// 更新事件类型的监听器数量
|
||
/// </summary>
|
||
/// <param name="eventType">事件类型名称</param>
|
||
/// <param name="count">监听器数量</param>
|
||
public void UpdateListenerCount(string eventType, int count)
|
||
{
|
||
lock (_lock)
|
||
{
|
||
if (count > 0)
|
||
{
|
||
_listenerCountByType[eventType] = count;
|
||
}
|
||
else
|
||
{
|
||
_listenerCountByType.Remove(eventType);
|
||
}
|
||
}
|
||
}
|
||
}
|