diff --git a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs index fb5a0b70..0c39a960 100644 --- a/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs +++ b/src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs @@ -31,5 +31,5 @@ public enum AgentTaskField Description, Enabled, Content, - DirectAgentId + Status } diff --git a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs index 55e6da2c..ba8a3044 100644 --- a/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs +++ b/src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs @@ -14,13 +14,13 @@ limitations under the License. ******************************************************************************/ +using BotSharp.Abstraction.Agents.Models; using BotSharp.Abstraction.Repositories; using BotSharp.Abstraction.Repositories.Filters; using BotSharp.Abstraction.Tasks; using BotSharp.Abstraction.Tasks.Models; using BotSharp.Abstraction.Utilities; using BotSharp.Core.Infrastructures; -using Microsoft.EntityFrameworkCore.Metadata.Internal; using Microsoft.Extensions.Logging; using System.Text.RegularExpressions; @@ -60,23 +60,22 @@ public class CrontabService : ICrontabService, ITaskFeeder public async Task> GetTasks() { - var agentService = _services.GetRequiredService(); var tasks = new List(); + var agentService = _services.GetRequiredService(); var cronsources = _services.GetServices(); + + // Get all agent subscribed to this cron + var agents = await agentService.GetAgents(new AgentFilter + { + Pager = new Pagination + { + Size = 1000 + } + }); + foreach (var source in cronsources) { var cron = source.GetCrontabItem(); - - // Get all agent subscribed to this cron - - var agents = await agentService.GetAgents(new AgentFilter - { - Pager = new Pagination - { - Size = 1000 - } - }); - var preFilteredAgents = agents.Items.Where(x => x.Rules.Exists(r => r.TriggerName == cron.Title)).ToList(); @@ -84,7 +83,7 @@ public class CrontabService : ICrontabService, ITaskFeeder { Id = Guid.Empty.ToString(), AgentId = x.Id, - Agent = new BotSharp.Abstraction.Agents.Models.Agent + Agent = new Agent { Name = x.Name, Description = x.Description diff --git a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs index e1466d4f..6d47c4cc 100644 --- a/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs +++ b/src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs @@ -50,6 +50,11 @@ public partial class FileRepository matched = matched && task.Enabled == filter.Enabled; } + if (!string.IsNullOrEmpty(filter?.Status)) + { + matched = matched && task.Status == filter.Status; + } + if (!matched) continue; totalCount++; diff --git a/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs b/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs index d4458c82..5ad0637e 100644 --- a/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs +++ b/src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs @@ -21,11 +21,18 @@ public class AgentTaskService : IAgentTaskService if (filter.Status == TaskStatus.Scheduled) { var taskFeeders = _services.GetServices(); - var items = taskFeeders.SelectMany(x => x.GetTasks().Result); + var items = new List(); + + foreach (var feeder in taskFeeders) + { + var tasks = await feeder.GetTasks(); + items.AddRange(tasks); + } return new PagedItems { - Items = items, + Items = items.OrderByDescending(x => x.UpdatedDateTime) + .Skip(filter.Pager.Offset).Take(filter.Pager.Size), Count = items.Count() }; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentTaskController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentTaskController.cs index 1ff61f45..2322dae3 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentTaskController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentTaskController.cs @@ -39,11 +39,11 @@ public class AgentTaskController : ControllerBase public async Task> GetAgentTasks([FromQuery] AgentTaskFilter filter) { filter.Status = TaskStatus.Scheduled; - var tasks = await _agentTaskService.GetTasks(filter); + var page = await _agentTaskService.GetTasks(filter); return new PagedItems { - Items = tasks.Items.Select(AgentTaskViewModel.From), - Count = tasks.Count + Items = page.Items.Select(AgentTaskViewModel.From), + Count = page.Count }; } diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentTaskViewModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentTaskViewModel.cs index 6613e3dc..cf051544 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentTaskViewModel.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentTaskViewModel.cs @@ -15,10 +15,13 @@ public class AgentTaskViewModel [JsonPropertyName("created_datetime")] public DateTime CreatedDateTime { get; set; } + [JsonPropertyName("updated_datetime")] public DateTime UpdatedDateTime { get; set; } + [JsonPropertyName("agent_id")] public string AgentId { get; set; } = null!; + [JsonPropertyName("agent_name")] public string AgentName { get; set; } = null!; diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentTaskDocument.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentTaskDocument.cs index 6acc4f5b..9259f688 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentTaskDocument.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentTaskDocument.cs @@ -1,3 +1,5 @@ +using BotSharp.Abstraction.Tasks.Models; + namespace BotSharp.Plugin.MongoStorage.Collections; public class AgentTaskDocument : MongoBase @@ -7,7 +9,37 @@ public class AgentTaskDocument : MongoBase public string Content { get; set; } public bool Enabled { get; set; } public string AgentId { get; set; } - public string? DirectAgentId { get; set; } + public string Status { get; set; } public DateTime CreatedTime { get; set; } public DateTime UpdatedTime { get; set; } + + public static AgentTask ToDomainModel(AgentTaskDocument model) + { + return new AgentTask + { + Id = model.Id, + Description = model.Description, + Content = model.Content, + Enabled = model.Enabled, + AgentId = model.AgentId, + Status = model.Status, + CreatedDateTime = model.CreatedTime, + UpdatedDateTime = model.UpdatedTime + }; + } + + public static AgentTaskDocument ToMongoModel(AgentTask model) + { + return new AgentTaskDocument + { + Id = model.Id, + Description = model.Description, + Content = model.Content, + Enabled = model.Enabled, + AgentId = model.AgentId, + Status = model.Status, + CreatedTime = model.CreatedDateTime, + UpdatedTime = model.UpdatedDateTime + }; + } } diff --git a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs index 0c8e66e1..db3cdfc6 100644 --- a/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs +++ b/src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs @@ -22,6 +22,11 @@ public partial class MongoRepository filters.Add(builder.Eq(x => x.AgentId, filter.AgentId)); } + if (!string.IsNullOrEmpty(filter.Status)) + { + filters.Add(builder.Eq(x => x.Status, filter.Status)); + } + if (filter.Enabled.HasValue) { filters.Add(builder.Eq(x => x.Enabled, filter.Enabled.Value)); @@ -35,17 +40,11 @@ public partial class MongoRepository var agentIds = taskDocs.Select(x => x.AgentId).Distinct().ToList(); var agents = GetAgents(new AgentFilter { AgentIds = agentIds }); - var tasks = taskDocs.Select(x => new AgentTask + var tasks = taskDocs.Select(x => { - Id = x.Id, - Name = x.Name, - Description = x.Description, - Enabled = x.Enabled, - AgentId = x.AgentId, - Content = x.Content, - CreatedDateTime = x.CreatedTime, - UpdatedDateTime = x.UpdatedTime, - Agent = agents.FirstOrDefault(a => a.Id == x.AgentId) + var task = AgentTaskDocument.ToDomainModel(x); + task.Agent = agents.FirstOrDefault(a => a.Id == x.AgentId); + return task; }).ToList(); return new PagedItems @@ -65,36 +64,15 @@ public partial class MongoRepository var agentDoc = _dc.Agents.AsQueryable().FirstOrDefault(x => x.Id == taskDoc.AgentId); var agent = TransformAgentDocument(agentDoc); - var task = new AgentTask - { - Id = taskDoc.Id, - Name = taskDoc.Name, - Description = taskDoc.Description, - Enabled = taskDoc.Enabled, - AgentId = taskDoc.AgentId, - Content = taskDoc.Content, - CreatedDateTime = taskDoc.CreatedTime, - UpdatedDateTime = taskDoc.UpdatedTime, - Agent = agent - }; - + var task = AgentTaskDocument.ToDomainModel(taskDoc); + task.Agent = agent; return task; } public void InsertAgentTask(AgentTask task) { - var taskDoc = new AgentTaskDocument - { - Id = Guid.NewGuid().ToString(), - Name = task.Name, - Description = task.Description, - Enabled = task.Enabled, - AgentId = task.AgentId, - Content = task.Content, - CreatedTime = DateTime.UtcNow, - UpdatedTime = DateTime.UtcNow - }; - + var taskDoc = AgentTaskDocument.ToMongoModel(task); + taskDoc.Id = Guid.NewGuid().ToString(); _dc.AgentTasks.InsertOne(taskDoc); } @@ -102,16 +80,11 @@ public partial class MongoRepository { if (tasks.IsNullOrEmpty()) return; - var taskDocs = tasks.Select(x => new AgentTaskDocument + var taskDocs = tasks.Select(x => { - Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid().ToString() : x.Id, - Name = x.Name, - Description = x.Description, - Enabled = x.Enabled, - AgentId = x.AgentId, - Content = x.Content, - CreatedTime = x.CreatedDateTime, - UpdatedTime = x.UpdatedDateTime + var task = AgentTaskDocument.ToMongoModel(x); + task.Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString(); + return task; }).ToList(); _dc.AgentTasks.InsertMany(taskDocs); @@ -139,11 +112,15 @@ public partial class MongoRepository case AgentTaskField.Content: taskDoc.Content = task.Content; break; + case AgentTaskField.Status: + taskDoc.Status = task.Status; + break; case AgentTaskField.All: taskDoc.Name = task.Name; taskDoc.Description = task.Description; taskDoc.Enabled = task.Enabled; taskDoc.Content = task.Content; + taskDoc.Status = task.Status; break; }