Files

179 lines
5.4 KiB
C#
Raw Permalink Normal View History

using System.Text;
namespace SHH.MjpegPlayer
{
/// <summary>
/// 按时间统计
/// </summary>
public class SumByTime
{
#region Defines
/// <summary>最近刷新在哪一秒</summary>
private int LastRefreshSecond = DateTime.Now.Second;
/// <summary>最近刷新在哪一分钟</summary>
private int LastRefreshMinute = DateTime.Now.Minute;
/// <summary>最近刷新在哪一小时</summary>
private int LastRefreshHour = DateTime.Now.Minute;
/// <summary>秒统计</summary>
private Dictionary<string, uint> _second
= new Dictionary<string, uint>();
/// <summary>分钟统计</summary>
private Dictionary<string, uint> _minute
= new Dictionary<string, uint>();
/// <summary>小时统计</summary>
private Dictionary<string, uint> _hour
= new Dictionary<string, uint>();
/// <summary>累计统计</summary>
public Dictionary<string, ulong> All { get; init; }
= new Dictionary<string, ulong>();
#endregion
#region TotalSecond
/// <summary>秒统计</summary>
public Dictionary<string, uint> TotalSecond { get; init; }
= new Dictionary<string, uint>();
#endregion
#region TotalMinute
/// <summary>分统计</summary>
public Dictionary<string, uint> TotalMinute { get; init; }
= new Dictionary<string, uint>();
#endregion
#region TotalHour
/// <summary>小时统计</summary>
public Dictionary<string, uint> TotalHour { get; init; }
= new Dictionary<string, uint>();
#endregion
#region Refresh
/// <summary>
/// 刷新方法调用次数
/// </summary>
/// <param name="methodName"></param>
/// <param name="count"></param>
public void Refresh(string methodName, uint count = 1)
{
try
{
2026-01-22 14:06:44 +08:00
#region
lock (_second)
{
2026-01-22 14:06:44 +08:00
// 确保键存在
if (!_second.ContainsKey(methodName))
_second.Add(methodName, 0);
2026-01-22 14:06:44 +08:00
if (!LastRefreshSecond.Equals(DateTime.Now.Second))
{
LastRefreshSecond = DateTime.Now.Second;
// 获取当前键的快照进行遍历,确保线程安全
var keys = _second.Keys.ToList();
foreach (var key in keys)
{
uint val = _second[key];
if (!TotalSecond.ContainsKey(key))
TotalSecond.Add(key, val);
else
TotalSecond[key] = val;
_second[key] = 0; // 重置当前秒计数值
}
}
2026-01-22 14:06:44 +08:00
_second[methodName] += count;
}
#endregion
2026-01-22 14:06:44 +08:00
#region
2026-01-22 14:06:44 +08:00
lock (_minute)
{
2026-01-22 14:06:44 +08:00
if (!_minute.ContainsKey(methodName))
_minute.Add(methodName, 0);
2026-01-22 14:06:44 +08:00
if (!LastRefreshMinute.Equals(DateTime.Now.Minute))
{
2026-01-22 14:06:44 +08:00
LastRefreshMinute = DateTime.Now.Minute;
var keys = _minute.Keys.ToList();
foreach (var key in keys)
{
uint val = _minute[key];
if (!TotalMinute.ContainsKey(key))
TotalMinute.Add(key, val);
else
TotalMinute[key] = val;
_minute[key] = 0;
}
}
2026-01-22 14:06:44 +08:00
_minute[methodName] += count;
}
2026-01-22 14:06:44 +08:00
#endregion
2026-01-22 14:06:44 +08:00
#region
2026-01-22 14:06:44 +08:00
lock (_hour)
{
2026-01-22 14:06:44 +08:00
if (!_hour.ContainsKey(methodName))
_hour.Add(methodName, 0);
2026-01-22 14:06:44 +08:00
if (!LastRefreshHour.Equals(DateTime.Now.Hour))
{
2026-01-22 14:06:44 +08:00
LastRefreshHour = DateTime.Now.Hour;
var keys = _hour.Keys.ToList();
foreach (var key in keys)
{
uint val = _hour[key];
if (!TotalHour.ContainsKey(key))
TotalHour.Add(key, val);
else
TotalHour[key] = val;
_hour[key] = 0;
}
}
2026-01-22 14:06:44 +08:00
_hour[methodName] += count;
}
#endregion
2026-01-22 14:06:44 +08:00
#region
lock (All)
{
if (!All.ContainsKey(methodName))
All.Add(methodName, 0);
2026-01-22 14:06:44 +08:00
All[methodName] += (ulong)count;
}
#endregion
}
catch (Exception ex)
{
2026-01-22 14:06:44 +08:00
// 可选:利用 Ayay 项目规范记录日志
// _sysLog.Warning("统计刷新异常: {Msg}", ex.Message);
}
}
#endregion
}
}