add start time in conv filter

This commit is contained in:
Jicheng Lu 2024-03-25 01:28:20 -05:00
parent 743a9024bf
commit 2bcbfcb394
4 changed files with 60 additions and 14 deletions

View file

@ -11,6 +11,7 @@ public class ConversationFilter
public string? Status { get; set; }
public string? Channel { get; set; }
public string? UserId { get; set; }
public DateTime? StartTime { get; set; }
/// <summary>
/// Agent task id

View file

@ -256,12 +256,34 @@ 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?.TaskId != null) matched = matched && record.TaskId == filter.TaskId;
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?.TaskId != null)
{
matched = matched && record.TaskId == filter.TaskId;
}
if (filter?.StartTime != null)
{
matched = matched && record.CreatedTime >= filter.StartTime.Value;
}
// Check states
if (filter != null && !filter.States.IsNullOrEmpty())

View file

@ -52,7 +52,8 @@ public class RateLimitConversationHook : ConversationHookBase
var convService = _services.GetRequiredService<IConversationService>();
var results = await convService.GetConversations(new ConversationFilter
{
UserId = user.Id
UserId = user.Id,
StartTime = DateTime.UtcNow.AddHours(-24),
});
if (results.Count > rateLimit.MaxConversationPerDay)

View file

@ -232,15 +232,37 @@ public partial class MongoRepository
var builder = Builders<ConversationDocument>.Filter;
var filters = new List<FilterDefinition<ConversationDocument>>() { builder.Empty };
if (!string.IsNullOrEmpty(filter.Id)) filters.Add(builder.Eq(x => x.Id, filter.Id));
if (!string.IsNullOrEmpty(filter.AgentId)) filters.Add(builder.Eq(x => x.AgentId, filter.AgentId));
if (!string.IsNullOrEmpty(filter.Status)) filters.Add(builder.Eq(x => x.Status, filter.Status));
if (!string.IsNullOrEmpty(filter.Channel)) filters.Add(builder.Eq(x => x.Channel, filter.Channel));
if (!string.IsNullOrEmpty(filter.UserId)) filters.Add(builder.Eq(x => x.UserId, filter.UserId));
if (!string.IsNullOrEmpty(filter.TaskId)) filters.Add(builder.Eq(x => x.TaskId, filter.TaskId));
if (!string.IsNullOrEmpty(filter?.Id))
{
filters.Add(builder.Eq(x => x.Id, filter.Id));
}
if (!string.IsNullOrEmpty(filter?.AgentId))
{
filters.Add(builder.Eq(x => x.AgentId, filter.AgentId));
}
if (!string.IsNullOrEmpty(filter?.Status))
{
filters.Add(builder.Eq(x => x.Status, filter.Status));
}
if (!string.IsNullOrEmpty(filter?.Channel))
{
filters.Add(builder.Eq(x => x.Channel, filter.Channel));
}
if (!string.IsNullOrEmpty(filter?.UserId))
{
filters.Add(builder.Eq(x => x.UserId, filter.UserId));
}
if (!string.IsNullOrEmpty(filter?.TaskId))
{
filters.Add(builder.Eq(x => x.TaskId, filter.TaskId));
}
if (filter?.StartTime != null)
{
filters.Add(builder.Gte(x => x.CreatedTime, filter.StartTime.Value));
}
// Check states
if (!filter.States.IsNullOrEmpty())
if (filter != null && !filter.States.IsNullOrEmpty())
{
var targetConvIds = new List<string>();