From 2d387b6d318767fffb47dd467daab0c34f2618a7 Mon Sep 17 00:00:00 2001 From: "nick.yi" Date: Thu, 16 Oct 2025 10:20:47 +0800 Subject: [PATCH] add UpdateInstructionLogStates --- .../Loggers/Services/ILoggerService.cs | 2 ++ .../Repositories/IBotSharpRepository.cs | 3 ++ .../Models/UpdateInstructionLogStatesModel.cs | 15 ++++++++++ .../Services/LoggerService.Instruction.cs | 10 +++++++ .../Controllers/LoggerController.cs | 9 ++++++ .../Repository/MongoRepository.Log.cs | 28 +++++++++++++++++++ 6 files changed, 67 insertions(+) create mode 100644 src/Infrastructure/BotSharp.Abstraction/Repositories/Models/UpdateInstructionLogStatesModel.cs diff --git a/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs b/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs index 564fcf37..4bf35bdb 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Loggers/Services/ILoggerService.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Repositories.Filters; +using BotSharp.Abstraction.Repositories.Models; namespace BotSharp.Abstraction.Loggers.Services; @@ -13,5 +14,6 @@ public interface ILoggerService #region Instruction Task> GetInstructionLogs(InstructLogFilter filter); Task> GetInstructionLogSearchKeys(InstructLogKeysFilter filter); + Task UpdateInstructionLogStates(UpdateInstructionLogStatesModel request); #endregion } diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs index 63ca3da3..37d9a811 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs @@ -195,6 +195,9 @@ public interface IBotSharpRepository : IHaveServiceProvider List GetInstructionLogSearchKeys(InstructLogKeysFilter filter) => throw new NotImplementedException(); + + Task UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInstructionStates) + => throw new NotImplementedException(); #endregion #region Statistics diff --git a/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/UpdateInstructionLogStatesModel.cs b/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/UpdateInstructionLogStatesModel.cs new file mode 100644 index 00000000..48fd8ddb --- /dev/null +++ b/src/Infrastructure/BotSharp.Abstraction/Repositories/Models/UpdateInstructionLogStatesModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace BotSharp.Abstraction.Repositories.Models +{ + public class UpdateInstructionLogStatesModel + { + public string LogId { get; set; } + public string StateKeyPrefix { get; set; } = "new_"; + public Dictionary States { get; set; } + } +} diff --git a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs index 965212ca..2060b491 100644 --- a/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs +++ b/src/Infrastructure/BotSharp.Core/Loggers/Services/LoggerService.Instruction.cs @@ -1,4 +1,5 @@ using BotSharp.Abstraction.Loggers.Models; +using BotSharp.Abstraction.Repositories.Models; using BotSharp.Abstraction.Users.Models; namespace BotSharp.Core.Loggers.Services; @@ -77,4 +78,13 @@ public partial class LoggerService keys = filter.PreLoad ? keys : keys.Where(x => x.Contains(filter.Query ?? string.Empty, StringComparison.OrdinalIgnoreCase)).ToList(); return keys.OrderBy(x => x).Take(filter.KeyLimit).ToList(); } + + public async Task UpdateInstructionLogStates(UpdateInstructionLogStatesModel request) + { + if(request == null) return false; + if (string.IsNullOrEmpty(request.LogId) || request.States.IsNullOrEmpty()) return false; + + var db = _services.GetRequiredService(); + return await db.UpdateInstructionLogStates(request); + } } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs index 7032bcd3..955d7dd1 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/LoggerController.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Loggers.Services; +using BotSharp.Abstraction.Repositories.Models; using BotSharp.OpenAPI.ViewModels.Instructs; using Microsoft.AspNetCore.Hosting; @@ -76,5 +77,13 @@ public class LoggerController : ControllerBase var keys = await logging.GetInstructionLogSearchKeys(request); return keys; } + + [HttpPost("/logger/instruction/log/{logId}/states")] + public async Task UpdateInstructionLogStates([FromRoute] string logId, UpdateInstructionLogStatesModel request) + { + request.LogId = logId; + var logging = _services.GetRequiredService(); + return await logging.UpdateInstructionLogStates(request); + } #endregion } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs index 9a236277..3eedc005 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Log.cs @@ -1,5 +1,6 @@ using BotSharp.Abstraction.Loggers.Models; using BotSharp.Abstraction.Repositories.Filters; +using BotSharp.Abstraction.Repositories.Models; using MongoDB.Driver; using System.Text.Json; @@ -169,6 +170,33 @@ public partial class MongoRepository return true; } + public async Task UpdateInstructionLogStates(UpdateInstructionLogStatesModel updateInstructionStates) + { + var id = updateInstructionStates.LogId; + + var logDoc = await _dc.InstructionLogs.Find(p => p.Id == id).FirstOrDefaultAsync(); + if (logDoc == null) return false; + + foreach (var pair in updateInstructionStates.States) + { + var key = updateInstructionStates.StateKeyPrefix + pair.Key; + try + { + var jsonStr = JsonSerializer.Serialize(new { Data = JsonDocument.Parse(pair.Value) }, _botSharpOptions.JsonSerializerOptions); + var json = BsonDocument.Parse(jsonStr); + logDoc.States[key] = json; + } + catch + { + var jsonStr = JsonSerializer.Serialize(new { Data = pair.Value }, _botSharpOptions.JsonSerializerOptions); + var json = BsonDocument.Parse(jsonStr); + logDoc.States[key] = json; + } + } + await _dc.InstructionLogs.ReplaceOneAsync(p => p.Id == id, logDoc); + return true; + } + public async ValueTask> GetInstructionLogs(InstructLogFilter filter) { if (filter == null)