BotSharp/src/Infrastructure/BotSharp.Core/Statistics/Services/BotSharpStatsService.cs

133 lines
4.4 KiB
C#
Raw Normal View History

2025-01-24 05:45:43 +00:00
using BotSharp.Abstraction.Infrastructures;
using BotSharp.Abstraction.Statistics.Settings;
namespace BotSharp.Core.Statistics.Services;
2025-01-28 00:23:33 +00:00
public class BotSharpStatsService : IBotSharpStatsService
2025-01-24 05:45:43 +00:00
{
private readonly IServiceProvider _services;
2025-01-28 00:23:33 +00:00
private readonly ILogger<BotSharpStatsService> _logger;
2025-01-24 05:45:43 +00:00
private readonly StatisticsSettings _settings;
private const string GLOBAL_LLM_COST = "global-llm-cost";
private const string GLOBAL_AGENT_CALL = "global-agent-call";
private const int TIMEOUT_SECONDS = 5;
2025-01-28 00:23:33 +00:00
public BotSharpStatsService(
2025-01-24 05:45:43 +00:00
IServiceProvider services,
2025-01-28 00:23:33 +00:00
ILogger<BotSharpStatsService> logger,
2025-01-24 05:45:43 +00:00
StatisticsSettings settings)
{
_services = services;
_logger = logger;
_settings = settings;
}
public bool UpdateLlmCost(BotSharpStats stats)
{
try
{
if (!_settings.Enabled) return false;
var db = _services.GetRequiredService<IBotSharpRepository>();
var locker = _services.GetRequiredService<IDistributedLocker>();
var res = locker.Lock(GLOBAL_LLM_COST, () =>
{
2025-01-24 19:42:17 +00:00
var body = db.GetGlobalStats(stats.Category, stats.Group, stats.RecordTime);
2025-01-24 05:45:43 +00:00
if (body == null)
{
db.SaveGlobalStats(stats);
return;
}
foreach (var item in stats.Data)
{
2025-01-24 19:42:17 +00:00
var curValue = item.Value;
if (body.Data.TryGetValue(item.Key, out var preValue))
2025-01-24 05:45:43 +00:00
{
2025-01-24 19:42:17 +00:00
var preValStr = preValue?.ToString();
var curValStr = curValue?.ToString();
try
2025-01-24 05:45:43 +00:00
{
2025-01-24 19:42:17 +00:00
if (int.TryParse(preValStr, out var count))
{
curValue = int.Parse(curValStr ?? "0") + count;
}
else if (double.TryParse(preValStr, out var num))
{
curValue = double.Parse(curValStr ?? "0") + num;
}
2025-01-24 05:45:43 +00:00
}
2025-01-24 19:42:17 +00:00
catch
2025-01-24 05:45:43 +00:00
{
2025-01-24 19:42:17 +00:00
continue;
2025-01-24 05:45:43 +00:00
}
}
2025-01-24 19:42:17 +00:00
body.Data[item.Key] = curValue;
2025-01-24 05:45:43 +00:00
}
db.SaveGlobalStats(body);
}, TIMEOUT_SECONDS);
return res;
}
catch (Exception ex)
{
_logger.LogError($"Error when updating global llm cost stats {stats}. {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
public bool UpdateAgentCall(BotSharpStats stats)
{
try
{
if (!_settings.Enabled) return false;
var db = _services.GetRequiredService<IBotSharpRepository>();
var locker = _services.GetRequiredService<IDistributedLocker>();
var res = locker.Lock(GLOBAL_AGENT_CALL, () =>
{
2025-01-24 19:42:17 +00:00
var body = db.GetGlobalStats(stats.Category, stats.Group, stats.RecordTime);
2025-01-24 05:45:43 +00:00
if (body == null)
{
db.SaveGlobalStats(stats);
return;
}
foreach (var item in stats.Data)
{
2025-01-24 19:42:17 +00:00
var curValue = item.Value;
if (body.Data.TryGetValue(item.Key, out var preValue))
2025-01-24 05:45:43 +00:00
{
2025-01-24 19:42:17 +00:00
var preValStr = preValue?.ToString();
var curValStr = curValue?.ToString();
try
{
if (int.TryParse(preValStr, out var count))
{
curValue = int.Parse(curValStr ?? "0") + count;
}
}
catch
2025-01-24 05:45:43 +00:00
{
2025-01-24 19:42:17 +00:00
continue;
2025-01-24 05:45:43 +00:00
}
}
2025-01-24 19:42:17 +00:00
body.Data[item.Key] = curValue;
2025-01-24 05:45:43 +00:00
}
db.SaveGlobalStats(body);
}, TIMEOUT_SECONDS);
return res;
}
catch (Exception ex)
{
_logger.LogError($"Error when updating global agent call stats {stats}. {ex.Message}\r\n{ex.InnerException}");
return false;
}
}
}