Merge pull request #261 from iceljc/features/add-paged-plugins

add agent pagination
This commit is contained in:
Haiping 2024-01-23 03:24:49 -06:00 committed by GitHub
commit 75d492f36b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 33 additions and 21 deletions

View file

@ -10,7 +10,7 @@ public interface IAgentService
{
Task<Agent> CreateAgent(Agent agent);
Task RefreshAgents();
Task<List<Agent>> GetAgents(AgentFilter filter);
Task<PagedItems<Agent>> GetAgents(AgentFilter filter);
/// <summary>
/// Load agent configurations and trigger hooks
@ -29,7 +29,7 @@ public interface IAgentService
/// <param name="id"></param>
/// <returns>Original agent information</returns>
Task<Agent> GetAgent(string id);
Task<bool> DeleteAgent(string id);
Task UpdateAgent(Agent agent, AgentField updateField);
Task UpdateAgentFromFile(string id);

View file

@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Repositories.Filters;
public class AgentFilter
{
public Pagination Pager { get; set; } = new Pagination();
public string? AgentName { get; set; }
public bool? Disabled { get; set; }
public bool? Installed { get; set; }

View file

@ -9,7 +9,7 @@ public partial class AgentService
#if !DEBUG
[MemoryCache(10 * 60)]
#endif
public async Task<List<Agent>> GetAgents(AgentFilter filter)
public async Task<PagedItems<Agent>> GetAgents(AgentFilter filter)
{
var agents = _db.GetAgents(filter);
@ -22,8 +22,12 @@ public partial class AgentService
}
agents = agents.Where(x => x.Installed).ToList();
return agents;
var pager = filter?.Pager ?? new Pagination();
return new PagedItems<Agent>
{
Items = agents.Skip(pager.Offset).Take(pager.Size),
Count = agents.Count()
};
}
#if !DEBUG

View file

@ -126,7 +126,7 @@ public class NaivePlanner : IPlaner
var agents = agentService.GetAgents(new AgentFilter
{
AllowRouting = true
}).Result;
}).Result.Items.ToList();
var malformed = false;
// Sometimes it populate malformed Function in Agent name

View file

@ -204,10 +204,10 @@ namespace BotSharp.Core.Repository
{
var records = new List<Conversation>();
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
var pager = filter?.Pager ?? new Pagination();
var totalDirs = Directory.GetDirectories(dir);
var dirs = totalDirs.Skip(filter.Pager.Offset).Take(filter.Pager.Size).ToList();
foreach (var d in dirs)
foreach (var d in totalDirs)
{
var path = Path.Combine(d, CONVERSATION_FILE);
if (!File.Exists(path)) continue;
@ -217,20 +217,20 @@ namespace BotSharp.Core.Repository
if (record == null) continue;
var matched = true;
if (filter.Id != null) matched = matched && record.Id == filter.Id;
if (filter.AgentId != null) matched = matched && record.AgentId == filter.AgentId;
if (filter.Status != null) matched = matched && record.Status == filter.Status;
if (filter.Channel != null) matched = matched && record.Channel == filter.Channel;
if (filter.UserId != null) matched = matched && record.UserId == filter.UserId;
if (filter?.Id != null) matched = matched && record.Id == filter.Id;
if (filter?.AgentId != null) matched = matched && record.AgentId == filter.AgentId;
if (filter?.Status != null) matched = matched && record.Status == filter.Status;
if (filter?.Channel != null) matched = matched && record.Channel == filter.Channel;
if (filter?.UserId != null) matched = matched && record.UserId == filter.UserId;
if (!matched) continue;
records.Add(record);
}
return new PagedItems<Conversation>
{
Items = records.OrderByDescending(x => x.CreatedTime),
Count = totalDirs.Count(),
Items = records.OrderByDescending(x => x.CreatedTime).Skip(pager.Offset).Take(pager.Size),
Count = records.Count(),
};
}

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Agents.Models;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
@ -27,11 +29,15 @@ public class AgentController : ControllerBase
return AgentViewModel.FromAgent(agent);
}
[HttpGet("/agents")]
public async Task<List<AgentViewModel>> GetAgents([FromQuery] AgentFilter filter)
[HttpPost("/agents")]
public async Task<PagedItems<AgentViewModel>> GetAgents([FromBody] AgentFilter filter)
{
var agents = await _agentService.GetAgents(filter);
return agents.Select(x => AgentViewModel.FromAgent(x)).ToList();
var pagedAgents = await _agentService.GetAgents(filter);
return new PagedItems<AgentViewModel>
{
Items = pagedAgents.Items.Select(x => AgentViewModel.FromAgent(x)).ToList(),
Count = pagedAgents.Count
};
}
[HttpPost("/agent")]

View file

@ -217,7 +217,8 @@ public partial class MongoRepository
var filterDef = builder.And(filters);
var sortDefinition = Builders<ConversationDocument>.Sort.Descending(x => x.CreatedTime);
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDefinition).Skip(filter.Pager.Offset).Limit(filter.Pager.Size).ToList();
var pager = filter?.Pager ?? new Pagination();
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDefinition).Skip(pager.Offset).Limit(pager.Size).ToList();
var count = _dc.Conversations.CountDocuments(filterDef);
foreach (var conv in conversationDocs)