Files
Ayay/SHH.MjpegPlayer/Core/Models/SumByTime.cs

190 lines
6.2 KiB
C#

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="logger"></param>
/// <param name="methodName"></param>
/// <param name="count"></param>
public void Refresh(string methodName, uint count = 1)
{
try
{
#region
// 加入集合
lock (_second)
{
if (!_second.ContainsKey(methodName))
_second.Add(methodName, 0);
}
// 加入集合
lock (_minute)
{
if (!_minute.ContainsKey(methodName))
_minute.Add(methodName, 0);
}
lock (_hour)
{
if (!_hour.ContainsKey(methodName))
_hour.Add(methodName, 0);
}
// 加入集合
lock (All)
{
if (!All.ContainsKey(methodName))
All.Add(methodName, 0);
}
#endregion
#region
// 秒刷新
if (!LastRefreshSecond.Equals(DateTime.Now.Second))
{
LastRefreshSecond = DateTime.Now.Second;
var sb = new StringBuilder();
foreach (var de in _second)
{
// 更新输出用统计信息
if (!TotalSecond.ContainsKey(de.Key))
TotalSecond.Add(de.Key, de.Value);
else
TotalSecond[de.Key] = de.Value;
sb.Append($"\r\n\t{de.Key} => 执行 {de.Value} 次");
_second[de.Key] = 0;
}
var logMsg = $"统计 => SumBySecond 统计时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}{sb.ToString()}";
//Logs.LogInformation<SumByTime>(EIdSys.TotalBySecond, logMsg);
}
// 分钟刷新
if (!LastRefreshMinute.Equals(DateTime.Now.Minute))
{
LastRefreshMinute = DateTime.Now.Minute;
var sb = new StringBuilder();
foreach (var de in _minute)
{
// 更新输出用统计信息
if (!TotalMinute.ContainsKey(de.Key))
TotalMinute.Add(de.Key, de.Value);
else
TotalMinute[de.Key] = de.Value;
sb.Append($"\r\n\t{de.Key} => 执行 {de.Value} 次, 平均每秒 {Math.Round((double)de.Value / 60, 2)} 次");
_minute[de.Key] = 0;
}
var logMsg = $"统计 => SumByMinute 统计时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}{sb.ToString()}";
//Logs.LogInformation<SumByTime>(EIdSys.TotalByMinute, logMsg);
}
// 小时刷新
if (!LastRefreshHour.Equals(DateTime.Now.Hour))
{
LastRefreshHour = DateTime.Now.Hour;
var sb = new StringBuilder();
foreach (var de in _hour)
{
// 更新输出用统计信息
if (!TotalHour.ContainsKey(de.Key))
TotalHour.Add(de.Key, de.Value);
else
TotalHour[de.Key] = de.Value;
sb.Append($"\r\n\t{de.Key} => 执行 {de.Value} 次, 平均每秒 {Math.Round((double)de.Value / 60, 2)} 次");
_hour[de.Key] = 0;
}
var logMsg = $"统计 => SumByHour 统计时间:{DateTime.Now.ToString("yyyy-MM-dd HH:mm")}{sb.ToString()}";
//Logs.LogInformation<SumByTime>(EIdSys.TotalByHour, logMsg);
}
#endregion
#region
_second[methodName] += count;
_minute[methodName] += count;
_hour[methodName] += count;
All[methodName] += count;
#endregion
}
catch (Exception ex)
{
//Logs.LogWarning<SumByTime>(ex.Message);
}
}
#endregion
}
}