BotSharp/src/Infrastructure/BotSharp.Logger/Hooks/GlobalStatsConversationHook.cs
2025-01-27 23:10:37 -06:00

44 lines
1.2 KiB
C#

using BotSharp.Abstraction.Statistics.Enums;
using BotSharp.Abstraction.Statistics.Models;
using BotSharp.Abstraction.Statistics.Services;
namespace BotSharp.Logger.Hooks;
public class GlobalStatsConversationHook : ConversationHookBase
{
private readonly IServiceProvider _services;
public GlobalStatsConversationHook(
IServiceProvider services)
{
_services = services;
}
public override async Task OnMessageReceived(RoleDialogModel message)
{
UpdateAgentCall(message);
}
public override async Task OnPostbackMessageReceived(RoleDialogModel message, PostbackMessageModel replyMsg)
{
UpdateAgentCall(message);
}
private void UpdateAgentCall(RoleDialogModel message)
{
// record agent call
var globalStats = _services.GetRequiredService<IBotSharpStatsService>();
var body = new BotSharpStatsInput
{
Category = StatsCategory.AgentCall,
Group = message.CurrentAgentId,
Data = [
new StatsKeyValuePair("agent_call_count", 1)
],
RecordTime = DateTime.UtcNow
};
globalStats.UpdateStats("global-agent-call", body);
}
}