BotSharp/src/Infrastructure/BotSharp.Logger/Hooks/GlobalStatsConversationHook.cs
Jicheng Lu dac15cee26 rename
2025-01-28 10:56:45 -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
{
Metric = StatsCategory.AgentCall,
Dimension = message.CurrentAgentId,
Data = [
new StatsKeyValuePair("agent_call_count", 1)
],
RecordTime = DateTime.UtcNow
};
globalStats.UpdateStats("global-agent-call", body);
}
}