From 096868b2291ae02870e0752bdeb2e44d1f3dd0af Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Sun, 26 Nov 2023 20:24:40 -0600 Subject: [PATCH] add conversation filter --- .../Repositories/IBotSharpRepository.cs | 2 +- .../Services/ConversationService.cs | 3 +- .../Repository/BotSharpDbContext.cs | 2 +- .../Repository/FileRepository.cs | 75 ++++++++----------- .../Collections/ConversationCollection.cs | 1 + .../Repository/MongoRepository.cs | 54 +++++-------- 6 files changed, 53 insertions(+), 84 deletions(-) diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 098fef83..73f81747 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -35,7 +35,7 @@ public interface IBotSharpRepository void UpdateConversationStates(string conversationId, List states); void UpdateConversationStatus(string conversationId, string status); Conversation GetConversation(string conversationId); - List GetConversations(string userId); + List GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null); void UpdateConversationTitle(string conversationId, string title); List GetLastConversations(); void AddExectionLogs(string conversationId, List logs); diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs index ff64a1e6..92084c78 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs @@ -57,7 +57,8 @@ public partial class ConversationService : IConversationService { var db = _services.GetRequiredService(); var user = db.GetUserById(_user.Id); - var conversations = db.GetConversations(user.Role == UserRole.CSR ? null : user?.Id); + var targetUserId = user.Role == UserRole.CSR ? null : user?.Id; + var conversations = db.GetConversations(userId: targetUserId); return conversations.OrderByDescending(x => x.CreatedTime).ToList(); } diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index f4505d68..15946505 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -131,7 +131,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository throw new NotImplementedException(); } - public List GetConversations(string userId) + public List GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null) { throw new NotImplementedException(); } diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index 1bd787ea..e32d0877 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -128,12 +128,7 @@ public class FileRepository : IBotSharpRepository public void Add(object entity) { - if (entity is Conversation conversation) - { - _conversations.Add(conversation); - _changedTableNames.Add(nameof(Conversation)); - } - else if (entity is Agent agent) + if (entity is Agent agent) { _agents.Add(agent); _changedTableNames.Add(nameof(Agent)); @@ -159,20 +154,7 @@ public class FileRepository : IBotSharpRepository // Persist to disk foreach (var table in _changedTableNames) { - if (table == nameof(Conversation)) - { - foreach (var conversation in _conversations) - { - var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir, conversation.Id); - if (!Directory.Exists(dir)) - { - Directory.CreateDirectory(dir); - } - var path = Path.Combine(dir, "conversation.json"); - File.WriteAllText(path, JsonSerializer.Serialize(conversation, _options)); - } - } - else if (table == nameof(Agent)) + if (table == nameof(Agent)) { foreach (var agent in _agents) { @@ -730,32 +712,29 @@ public class FileRepository : IBotSharpRepository public Conversation GetConversation(string conversationId) { var convDir = FindConversationDirectory(conversationId); - if (!string.IsNullOrEmpty(convDir)) + if (!string.IsNullOrEmpty(convDir)) return null; + + var convFile = Path.Combine(convDir, "conversation.json"); + var content = File.ReadAllText(convFile); + var record = JsonSerializer.Deserialize(content, _options); + + var dialogFile = Path.Combine(convDir, "dialogs.txt"); + if (record != null) { - var convFile = Path.Combine(convDir, "conversation.json"); - var content = File.ReadAllText(convFile); - var record = JsonSerializer.Deserialize(content, _options); - - var dialogFile = Path.Combine(convDir, "dialogs.txt"); - if (record != null) - { - record.Dialogs = CollectDialogElements(dialogFile); - } - - var stateFile = Path.Combine(convDir, "state.dict"); - if (record != null) - { - var states = CollectConversationStates(stateFile); - record.States = new ConversationState(states); - } - - return record; + record.Dialogs = CollectDialogElements(dialogFile); } - return null; + var stateFile = Path.Combine(convDir, "state.dict"); + if (record != null) + { + var states = CollectConversationStates(stateFile); + record.States = new ConversationState(states); + } + + return record; } - public List GetConversations(string userId) + public List GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null) { var records = new List(); var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir); @@ -767,10 +746,16 @@ public class FileRepository : IBotSharpRepository var json = File.ReadAllText(path); var record = JsonSerializer.Deserialize(json, _options); - if (record != null && (record.UserId == userId || userId == null)) - { - records.Add(record); - } + if (record == null) continue; + + var matched = true; + if (!string.IsNullOrEmpty(agentId)) matched = matched && record.AgentId == agentId; + if (!string.IsNullOrEmpty(status)) matched = matched && record.Status == status; + if (!string.IsNullOrEmpty(channel)) matched = matched && record.Channel == channel; + if (!string.IsNullOrEmpty(userId)) matched = matched && record.UserId == userId; + + if (!matched) continue; + records.Add(record); } return records; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs index 3eace7ee..8a6ed4f8 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ConversationCollection.cs @@ -7,6 +7,7 @@ public class ConversationCollection : MongoBase public string AgentId { get; set; } public string UserId { get; set; } public string Title { get; set; } + public string Channel { get; set; } public string Status { get; set; } public List States { get; set; } public DateTime CreatedTime { get; set; } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 71c59f94..cc55794b 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -32,12 +32,7 @@ public class MongoRepository : IBotSharpRepository public void Add(object entity) { - if (entity is Conversation conversation) - { - _conversations.Add(conversation); - _changedTableNames.Add(nameof(Conversation)); - } - else if (entity is Agent agent) + if (entity is Agent agent) { _agents.Add(agent); _changedTableNames.Add(nameof(Agent)); @@ -61,33 +56,7 @@ public class MongoRepository : IBotSharpRepository foreach (var table in _changedTableNames) { - if (table == nameof(Conversation)) - { - var conversations = _conversations.Select(x => new ConversationCollection - { - Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(), - AgentId = x.AgentId, - UserId = !string.IsNullOrEmpty(x.UserId) ? x.UserId : string.Empty, - Title = x.Title, - States = x.States?.ToKeyValueList() ?? new List(), - CreatedTime = x.CreatedTime, - UpdatedTime = x.UpdatedTime - }).ToList(); - - foreach (var conversation in conversations) - { - var filter = Builders.Filter.Eq(x => x.Id, conversation.Id); - var update = Builders.Update - .Set(x => x.AgentId, conversation.AgentId) - .Set(x => x.UserId, conversation.UserId) - .Set(x => x.Title, conversation.Title) - .Set(x => x.States, conversation.States) - .Set(x => x.CreatedTime, conversation.CreatedTime) - .Set(x => x.UpdatedTime, conversation.UpdatedTime); - _dc.Conversations.UpdateOne(filter, update, _options); - } - } - else if (table == nameof(Agent)) + if (table == nameof(Agent)) { var agents = _agents.Select(x => new AgentCollection { @@ -610,6 +579,7 @@ public class MongoRepository : IBotSharpRepository AgentId = conversation.AgentId, UserId = !string.IsNullOrEmpty(conversation.UserId) ? conversation.UserId : string.Empty, Title = conversation.Title, + Channel = conversation.Channel, Status = conversation.Status, States = conversation.States?.ToKeyValueList() ?? new List(), CreatedTime = DateTime.UtcNow, @@ -670,6 +640,7 @@ public class MongoRepository : IBotSharpRepository _dc.ConversationDialogs.UpdateOne(filterDialog, updateDialog); _dc.Conversations.UpdateOne(filterConv, updateConv); } + public void UpdateConversationTitle(string conversationId, string title) { if (string.IsNullOrEmpty(conversationId)) return; @@ -684,6 +655,7 @@ public class MongoRepository : IBotSharpRepository _dc.Conversations.UpdateOne(filterConv, updateConv); } + public List GetConversationStates(string conversationId) { var states = new List(); @@ -745,6 +717,7 @@ public class MongoRepository : IBotSharpRepository AgentId = conv.AgentId.ToString(), UserId = conv.UserId.ToString(), Title = conv.Title, + Channel = conv.Channel, Status = conv.Status, Dialogs = dialogElements, States = new ConversationState(conv.States ?? new List()), @@ -753,13 +726,20 @@ public class MongoRepository : IBotSharpRepository }; } - public List GetConversations(string userId) + public List GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null) { var records = new List(); if (string.IsNullOrEmpty(userId)) return records; - var filterByUserId = Builders.Filter.Eq(x => x.UserId, userId); - var conversations = _dc.Conversations.Find(filterByUserId).ToList(); + var builder = Builders.Filter; + var filters = new List>(); + + if (!string.IsNullOrEmpty(agentId)) filters.Add(builder.Eq(x => x.AgentId, agentId)); + if (!string.IsNullOrEmpty(status)) filters.Add(builder.Eq(x => x.Status, status)); + if (!string.IsNullOrEmpty(channel)) filters.Add(builder.Eq(x => x.Channel, channel)); + if (!string.IsNullOrEmpty(userId)) filters.Add(builder.Eq(x => x.UserId, userId)); + + var conversations = _dc.Conversations.Find(builder.And(filters)).ToList(); foreach (var conv in conversations) { @@ -770,6 +750,7 @@ public class MongoRepository : IBotSharpRepository AgentId = conv.AgentId.ToString(), UserId = conv.UserId.ToString(), Title = conv.Title, + Channel = conv.Channel, Status = conv.Status, CreatedTime = conv.CreatedTime, UpdatedTime = conv.UpdatedTime @@ -791,6 +772,7 @@ public class MongoRepository : IBotSharpRepository AgentId = c.AgentId.ToString(), UserId = c.UserId.ToString(), Title = c.Title, + Channel = c.Channel, Status = c.Status, CreatedTime = c.CreatedTime, UpdatedTime = c.UpdatedTime