add execution log

This commit is contained in:
Jicheng Lu 2023-10-23 15:33:24 -05:00
parent 7fb9440011
commit 9f6f0e2d92
11 changed files with 96 additions and 16 deletions

View file

@ -34,5 +34,7 @@ public interface IBotSharpRepository
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
Conversation GetConversation(string conversationId);
List<Conversation> GetConversations(string userId);
void AddExectionLogs(string conversationId, List<string> logs);
List<string> GetExectionLogs(string conversationId);
#endregion
}

View file

@ -55,7 +55,7 @@ public class ConversationStateService : IConversationStateService, IDisposable
{
_conversationId = conversationId;
_savedStates = _db.GetConversationStates(_conversationId);
_savedStates = _db.GetConversationStates(_conversationId).ToList();
if (!_savedStates.IsNullOrEmpty())
{

View file

@ -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 class ExecutionLogger : IExecutionLogger
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<IBotSharpRepository>();
db.AddExectionLogs(conversationId, new List<string> { content });
}
}

View file

@ -150,6 +150,16 @@ public class BotSharpDbContext : Database, IBotSharpRepository
{
throw new NotImplementedException();
}
public void AddExectionLogs(string conversationId, List<string> logs)
{
throw new NotImplementedException();
}
public List<string> GetExectionLogs(string conversationId)
{
throw new NotImplementedException();
}
#endregion

View file

@ -747,6 +747,33 @@ public class FileRepository : IBotSharpRepository
return records;
}
public void AddExectionLogs(string conversationId, List<string> 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<string> GetExectionLogs(string conversationId)
{
var logs = new List<string>();
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<string>();
return logs;
}
#endregion
#region User

View file

@ -10,7 +10,6 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Aspects.Cache" Version="2.0.4" />
<PackageReference Include="MongoDB.Driver" Version="2.22.0" />
</ItemGroup>

View file

@ -0,0 +1,7 @@
namespace BotSharp.Plugin.MongoStorage.Collections;
public class ExectionLogCollection : MongoBase
{
public string ConversationId { get; set; }
public List<string> Logs { get; set; }
}

View file

@ -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; }
}

View file

@ -37,6 +37,9 @@ public class MongoDbContext
public IMongoCollection<ConversationDialogCollection> ConversationDialogs
=> Database.GetCollection<ConversationDialogCollection>($"{_collectionPrefix}_ConversationDialogs");
public IMongoCollection<ExectionLogCollection> ExectionLogs
=> Database.GetCollection<ExectionLogCollection>($"{_collectionPrefix}_ExectionLogs");
public IMongoCollection<UserCollection> Users
=> Database.GetCollection<UserCollection>($"{_collectionPrefix}_Users");

View file

@ -5,7 +5,6 @@ using BotSharp.Abstraction.Routing.Models;
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 class MongoRepository : IBotSharpRepository
return records;
}
public void AddExectionLogs(string conversationId, List<string> logs)
{
if (string.IsNullOrEmpty(conversationId) || logs.IsNullOrEmpty()) return;
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
var update = Builders<ExectionLogCollection>.Update
.SetOnInsert(x => x.Id, Guid.NewGuid().ToString())
.PushEach(x => x.Logs, logs);
_dc.ExectionLogs.UpdateOne(filter, update, _options);
}
public List<string> GetExectionLogs(string conversationId)
{
var logs = new List<string>();
if (string.IsNullOrEmpty(conversationId)) return logs;
var filter = Builders<ExectionLogCollection>.Filter.Eq(x => x.ConversationId, conversationId);
var logCollection = _dc.ExectionLogs.Find(filter).FirstOrDefault();
logs = logCollection?.Logs ?? new List<string>();
return logs;
}
#endregion
#region User

View file

@ -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());
}
}