add conversation filter

This commit is contained in:
Jicheng Lu 2023-11-26 20:24:40 -06:00
parent d2cb9a5ebd
commit 096868b229
6 changed files with 53 additions and 84 deletions

View file

@ -35,7 +35,7 @@ public interface IBotSharpRepository
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
void UpdateConversationStatus(string conversationId, string status);
Conversation GetConversation(string conversationId);
List<Conversation> GetConversations(string userId);
List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null);
void UpdateConversationTitle(string conversationId, string title);
List<Conversation> GetLastConversations();
void AddExectionLogs(string conversationId, List<string> logs);

View file

@ -57,7 +57,8 @@ public partial class ConversationService : IConversationService
{
var db = _services.GetRequiredService<IBotSharpRepository>();
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();
}

View file

@ -131,7 +131,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
throw new NotImplementedException();
}
public List<Conversation> GetConversations(string userId)
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
{
throw new NotImplementedException();
}

View file

@ -128,12 +128,7 @@ public class FileRepository : IBotSharpRepository
public void Add<TTableInterface>(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<Conversation>(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<Conversation>(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<Conversation> GetConversations(string userId)
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
{
var records = new List<Conversation>();
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<Conversation>(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;

View file

@ -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<StateKeyValue> States { get; set; }
public DateTime CreatedTime { get; set; }

View file

@ -32,12 +32,7 @@ public class MongoRepository : IBotSharpRepository
public void Add<TTableInterface>(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<StateKeyValue>(),
CreatedTime = x.CreatedTime,
UpdatedTime = x.UpdatedTime
}).ToList();
foreach (var conversation in conversations)
{
var filter = Builders<ConversationCollection>.Filter.Eq(x => x.Id, conversation.Id);
var update = Builders<ConversationCollection>.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<StateKeyValue>(),
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<StateKeyValue> GetConversationStates(string conversationId)
{
var states = new List<StateKeyValue>();
@ -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<StateKeyValue>()),
@ -753,13 +726,20 @@ public class MongoRepository : IBotSharpRepository
};
}
public List<Conversation> GetConversations(string userId)
public List<Conversation> GetConversations(string? agentId = null, string? status = null, string? channel = null, string? userId = null)
{
var records = new List<Conversation>();
if (string.IsNullOrEmpty(userId)) return records;
var filterByUserId = Builders<ConversationCollection>.Filter.Eq(x => x.UserId, userId);
var conversations = _dc.Conversations.Find(filterByUserId).ToList();
var builder = Builders<ConversationCollection>.Filter;
var filters = new List<FilterDefinition<ConversationCollection>>();
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