From 9f6f0e2d926122b1c34a388e56db36ed2a47ab36 Mon Sep 17 00:00:00 2001 From: Jicheng Lu <103353@smsassist.com> Date: Mon, 23 Oct 2023 15:33:24 -0500 Subject: [PATCH] add execution log --- .../Repositories/IBotSharpRepository.cs | 2 ++ .../Services/ConversationStateService.cs | 2 +- .../Evaluations/ExecutionLogger.cs | 12 ++------- .../Repository/BotSharpDbContext.cs | 10 +++++++ .../Repository/FileRepository.cs | 27 +++++++++++++++++++ .../BotSharp.Plugin.MongoStorage.csproj | 1 - .../Collections/ExectionLogCollection.cs | 7 +++++ .../BotSharp.Plugin.MongoStorage/MongoBase.cs | 7 ++--- .../MongoDbContext.cs | 3 +++ .../Repository/MongoRepository.cs | 25 ++++++++++++++++- .../StringGuidIdGenerator.cs | 16 +++++++++++ 11 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExectionLogCollection.cs create mode 100644 src/Plugins/BotSharp.Plugin.MongoStorage/StringGuidIdGenerator.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 97c336485..4cc6af362 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -34,5 +34,7 @@ List GetAgents(string? name = null, bool? disabled = null, bool? allowRou void UpdateConversationStates(string conversationId, List states); Conversation GetConversation(string conversationId); List GetConversations(string userId); + void AddExectionLogs(string conversationId, List logs); + List GetExectionLogs(string conversationId); #endregion } diff --git a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs index 30754faf5..1f0fe1992 100644 --- a/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs +++ b/src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationStateService.cs @@ -55,7 +55,7 @@ public ConversationState Load(string conversationId) { _conversationId = conversationId; - _savedStates = _db.GetConversationStates(_conversationId); + _savedStates = _db.GetConversationStates(_conversationId).ToList(); if (!_savedStates.IsNullOrEmpty()) { diff --git a/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs b/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs index 7b5054d02..a7e7b1f51 100644 --- a/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs +++ b/src/Infrastructure/BotSharp.Core/Evaluations/ExecutionLogger.cs @@ -1,7 +1,5 @@ using BotSharp.Abstraction.Evaluations; using BotSharp.Abstraction.Repositories; -using BotSharp.Abstraction.Utilities; -using System.IO; namespace BotSharp.Core.Evaluations; @@ -19,13 +17,7 @@ public ExecutionLogger( public void Append(string conversationId, string content) { - var file = GetStorageFile(conversationId); - File.AppendAllLines(file, new[] { content }); - } - - private string GetStorageFile(string conversationId) - { - var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId); - return Path.Combine(dir, "execution.log"); + var db = _services.GetRequiredService(); + db.AddExectionLogs(conversationId, new List { content }); } } diff --git a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs index 9cd8ab757..4b2383bc6 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs @@ -150,6 +150,16 @@ public void UpdateConversationStates(string conversationId, List { throw new NotImplementedException(); } + + public void AddExectionLogs(string conversationId, List logs) + { + throw new NotImplementedException(); + } + + public List GetExectionLogs(string conversationId) + { + throw new NotImplementedException(); + } #endregion diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs index e8f00994d..4d862e7a4 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository.cs @@ -747,6 +747,33 @@ public List GetConversations(string userId) return records; } + + public void AddExectionLogs(string conversationId, List logs) + { + if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return; + + var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId); + if (!Directory.Exists(dir)) + { + Directory.CreateDirectory(dir); + } + + var file = Path.Combine(dir, "execution.log"); + File.AppendAllLines(file, logs); + } + + public List GetExectionLogs(string conversationId) + { + var logs = new List(); + if (string.IsNullOrEmpty(conversationId)) return logs; + + var dir = Path.Combine(_dbSettings.FileRepository, "conversations", conversationId); + if (!Directory.Exists(dir)) return logs; + + var file = Path.Combine(dir, "execution.log"); + logs = File.ReadAllLines(file)?.ToList() ?? new List(); + return logs; + } #endregion #region User diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj index 3a568db7e..b74045480 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/BotSharp.Plugin.MongoStorage.csproj @@ -10,7 +10,6 @@ - diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExectionLogCollection.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExectionLogCollection.cs new file mode 100644 index 000000000..8f111f84b --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/ExectionLogCollection.cs @@ -0,0 +1,7 @@ +namespace BotSharp.Plugin.MongoStorage.Collections; + +public class ExectionLogCollection : MongoBase +{ + public string ConversationId { get; set; } + public List Logs { get; set; } +} diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs index 6fcd93486..6d8842bc3 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoBase.cs @@ -1,11 +1,12 @@ using MongoDB.Bson.Serialization.Attributes; -using MongoDB.Bson.Serialization.IdGenerators; namespace BotSharp.Plugin.MongoStorage; [BsonIgnoreExtraElements(Inherited = true)] -public class MongoBase +public abstract class MongoBase { - [BsonId(IdGenerator = typeof(StringObjectIdGenerator))] + [BsonId(IdGenerator = typeof(StringGuidIdGenerator))] public string Id { get; set; } } + + diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs index 5b92f40a1..c8fb7e281 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/MongoDbContext.cs @@ -37,6 +37,9 @@ public IMongoCollection Conversations public IMongoCollection ConversationDialogs => Database.GetCollection($"{_collectionPrefix}_ConversationDialogs"); + public IMongoCollection ExectionLogs + => Database.GetCollection($"{_collectionPrefix}_ExectionLogs"); + public IMongoCollection Users => Database.GetCollection($"{_collectionPrefix}_Users"); diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs index 108fb111b..0009e0aa3 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.cs @@ -5,7 +5,6 @@ using BotSharp.Abstraction.Users.Models; using BotSharp.Plugin.MongoStorage.Collections; using BotSharp.Plugin.MongoStorage.Models; -using Aspects.Cache; namespace BotSharp.Plugin.MongoStorage.Repository; @@ -727,6 +726,30 @@ public List GetConversations(string userId) return records; } + + public void AddExectionLogs(string conversationId, List logs) + { + if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return; + + var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); + var update = Builders.Update + .SetOnInsert(x => x.Id, Guid.NewGuid().ToString()) + .PushEach(x => x.Logs, logs); + + _dc.ExectionLogs.UpdateOne(filter, update, _options); + } + + public List GetExectionLogs(string conversationId) + { + var logs = new List(); + if (string.IsNullOrEmpty(conversationId)) return logs; + + var filter = Builders.Filter.Eq(x => x.ConversationId, conversationId); + var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault(); + + logs = logCollection?.Logs ?? new List(); + return logs; + } #endregion #region User diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/StringGuidIdGenerator.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/StringGuidIdGenerator.cs new file mode 100644 index 000000000..a80ebd2b1 --- /dev/null +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/StringGuidIdGenerator.cs @@ -0,0 +1,16 @@ +using MongoDB.Bson.Serialization; + +namespace BotSharp.Plugin.MongoStorage; + +public class StringGuidIdGenerator : IIdGenerator +{ + public object GenerateId(object container, object document) + { + return Guid.NewGuid().ToString(); + } + + public bool IsEmpty(object id) + { + return id == null || string.IsNullOrEmpty(id.ToString()); + } +}