Skip to content

Commit 169cb95

Browse files
author
Jicheng Lu
committed
refine
1 parent d34284e commit 169cb95

File tree

8 files changed

+88
-65
lines changed

8 files changed

+88
-65
lines changed

src/Infrastructure/BotSharp.Abstraction/Agents/Enums/AgentField.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,5 @@ public enum AgentTaskField
3131
Description,
3232
Enabled,
3333
Content,
34-
DirectAgentId
34+
Status
3535
}

src/Infrastructure/BotSharp.Core.Crontab/Services/CrontabService.cs

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17+
using BotSharp.Abstraction.Agents.Models;
1718
using BotSharp.Abstraction.Repositories;
1819
using BotSharp.Abstraction.Repositories.Filters;
1920
using BotSharp.Abstraction.Tasks;
2021
using BotSharp.Abstraction.Tasks.Models;
2122
using BotSharp.Abstraction.Utilities;
2223
using BotSharp.Core.Infrastructures;
23-
using Microsoft.EntityFrameworkCore.Metadata.Internal;
2424
using Microsoft.Extensions.Logging;
2525
using System.Text.RegularExpressions;
2626

@@ -60,31 +60,30 @@ public async Task<List<CrontabItem>> GetCrontable()
6060

6161
public async Task<List<AgentTask>> GetTasks()
6262
{
63-
var agentService = _services.GetRequiredService<IAgentService>();
6463
var tasks = new List<AgentTask>();
64+
var agentService = _services.GetRequiredService<IAgentService>();
6565
var cronsources = _services.GetServices<ICrontabSource>();
66-
foreach (var source in cronsources)
67-
{
68-
var cron = source.GetCrontabItem();
6966

70-
// Get all agent subscribed to this cron
71-
72-
var agents = await agentService.GetAgents(new AgentFilter
67+
// Get all agent subscribed to this cron
68+
var agents = await agentService.GetAgents(new AgentFilter
69+
{
70+
Pager = new Pagination
7371
{
74-
Pager = new Pagination
75-
{
76-
Size = 1000
77-
}
78-
});
72+
Size = 1000
73+
}
74+
});
7975

76+
foreach (var source in cronsources)
77+
{
78+
var cron = source.GetCrontabItem();
8079
var preFilteredAgents = agents.Items.Where(x =>
8180
x.Rules.Exists(r => r.TriggerName == cron.Title)).ToList();
8281

8382
tasks.AddRange(preFilteredAgents.Select(x => new AgentTask
8483
{
8584
Id = Guid.Empty.ToString(),
8685
AgentId = x.Id,
87-
Agent = new BotSharp.Abstraction.Agents.Models.Agent
86+
Agent = new Agent
8887
{
8988
Name = x.Name,
9089
Description = x.Description

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.AgentTask.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter)
5050
matched = matched && task.Enabled == filter.Enabled;
5151
}
5252

53+
if (!string.IsNullOrEmpty(filter?.Status))
54+
{
55+
matched = matched && task.Status == filter.Status;
56+
}
57+
5358
if (!matched) continue;
5459

5560
totalCount++;

src/Infrastructure/BotSharp.Core/Tasks/Services/AgentTaskService.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,18 @@ public async Task<PagedItems<AgentTask>> GetTasks(AgentTaskFilter filter)
2121
if (filter.Status == TaskStatus.Scheduled)
2222
{
2323
var taskFeeders = _services.GetServices<ITaskFeeder>();
24-
var items = taskFeeders.SelectMany(x => x.GetTasks().Result);
24+
var items = new List<AgentTask>();
25+
26+
foreach (var feeder in taskFeeders)
27+
{
28+
var tasks = await feeder.GetTasks();
29+
items.AddRange(tasks);
30+
}
2531

2632
return new PagedItems<AgentTask>
2733
{
28-
Items = items,
34+
Items = items.OrderByDescending(x => x.UpdatedDateTime)
35+
.Skip(filter.Pager.Offset).Take(filter.Pager.Size),
2936
Count = items.Count()
3037
};
3138
}

src/Infrastructure/BotSharp.OpenAPI/Controllers/AgentTaskController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ public async Task<AgentTaskViewModel> GetAgentTask([FromRoute] string agentId, [
3939
public async Task<PagedItems<AgentTaskViewModel>> GetAgentTasks([FromQuery] AgentTaskFilter filter)
4040
{
4141
filter.Status = TaskStatus.Scheduled;
42-
var tasks = await _agentTaskService.GetTasks(filter);
42+
var page = await _agentTaskService.GetTasks(filter);
4343
return new PagedItems<AgentTaskViewModel>
4444
{
45-
Items = tasks.Items.Select(AgentTaskViewModel.From),
46-
Count = tasks.Count
45+
Items = page.Items.Select(AgentTaskViewModel.From),
46+
Count = page.Count
4747
};
4848
}
4949

src/Infrastructure/BotSharp.OpenAPI/ViewModels/Agents/AgentTaskViewModel.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ public class AgentTaskViewModel
1515

1616
[JsonPropertyName("created_datetime")]
1717
public DateTime CreatedDateTime { get; set; }
18+
1819
[JsonPropertyName("updated_datetime")]
1920
public DateTime UpdatedDateTime { get; set; }
21+
2022
[JsonPropertyName("agent_id")]
2123
public string AgentId { get; set; } = null!;
24+
2225
[JsonPropertyName("agent_name")]
2326
public string AgentName { get; set; } = null!;
2427

src/Plugins/BotSharp.Plugin.MongoStorage/Collections/AgentTaskDocument.cs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
using BotSharp.Abstraction.Tasks.Models;
2+
13
namespace BotSharp.Plugin.MongoStorage.Collections;
24

35
public class AgentTaskDocument : MongoBase
@@ -7,7 +9,37 @@ public class AgentTaskDocument : MongoBase
79
public string Content { get; set; }
810
public bool Enabled { get; set; }
911
public string AgentId { get; set; }
10-
public string? DirectAgentId { get; set; }
12+
public string Status { get; set; }
1113
public DateTime CreatedTime { get; set; }
1214
public DateTime UpdatedTime { get; set; }
15+
16+
public static AgentTask ToDomainModel(AgentTaskDocument model)
17+
{
18+
return new AgentTask
19+
{
20+
Id = model.Id,
21+
Description = model.Description,
22+
Content = model.Content,
23+
Enabled = model.Enabled,
24+
AgentId = model.AgentId,
25+
Status = model.Status,
26+
CreatedDateTime = model.CreatedTime,
27+
UpdatedDateTime = model.UpdatedTime
28+
};
29+
}
30+
31+
public static AgentTaskDocument ToMongoModel(AgentTask model)
32+
{
33+
return new AgentTaskDocument
34+
{
35+
Id = model.Id,
36+
Description = model.Description,
37+
Content = model.Content,
38+
Enabled = model.Enabled,
39+
AgentId = model.AgentId,
40+
Status = model.Status,
41+
CreatedTime = model.CreatedDateTime,
42+
UpdatedTime = model.UpdatedDateTime
43+
};
44+
}
1345
}

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.AgentTask.cs

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ public PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter)
2222
filters.Add(builder.Eq(x => x.AgentId, filter.AgentId));
2323
}
2424

25+
if (!string.IsNullOrEmpty(filter.Status))
26+
{
27+
filters.Add(builder.Eq(x => x.Status, filter.Status));
28+
}
29+
2530
if (filter.Enabled.HasValue)
2631
{
2732
filters.Add(builder.Eq(x => x.Enabled, filter.Enabled.Value));
@@ -35,17 +40,11 @@ public PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter)
3540
var agentIds = taskDocs.Select(x => x.AgentId).Distinct().ToList();
3641
var agents = GetAgents(new AgentFilter { AgentIds = agentIds });
3742

38-
var tasks = taskDocs.Select(x => new AgentTask
43+
var tasks = taskDocs.Select(x =>
3944
{
40-
Id = x.Id,
41-
Name = x.Name,
42-
Description = x.Description,
43-
Enabled = x.Enabled,
44-
AgentId = x.AgentId,
45-
Content = x.Content,
46-
CreatedDateTime = x.CreatedTime,
47-
UpdatedDateTime = x.UpdatedTime,
48-
Agent = agents.FirstOrDefault(a => a.Id == x.AgentId)
45+
var task = AgentTaskDocument.ToDomainModel(x);
46+
task.Agent = agents.FirstOrDefault(a => a.Id == x.AgentId);
47+
return task;
4948
}).ToList();
5049

5150
return new PagedItems<AgentTask>
@@ -65,53 +64,27 @@ public PagedItems<AgentTask> GetAgentTasks(AgentTaskFilter filter)
6564
var agentDoc = _dc.Agents.AsQueryable().FirstOrDefault(x => x.Id == taskDoc.AgentId);
6665
var agent = TransformAgentDocument(agentDoc);
6766

68-
var task = new AgentTask
69-
{
70-
Id = taskDoc.Id,
71-
Name = taskDoc.Name,
72-
Description = taskDoc.Description,
73-
Enabled = taskDoc.Enabled,
74-
AgentId = taskDoc.AgentId,
75-
Content = taskDoc.Content,
76-
CreatedDateTime = taskDoc.CreatedTime,
77-
UpdatedDateTime = taskDoc.UpdatedTime,
78-
Agent = agent
79-
};
80-
67+
var task = AgentTaskDocument.ToDomainModel(taskDoc);
68+
task.Agent = agent;
8169
return task;
8270
}
8371

8472
public void InsertAgentTask(AgentTask task)
8573
{
86-
var taskDoc = new AgentTaskDocument
87-
{
88-
Id = Guid.NewGuid().ToString(),
89-
Name = task.Name,
90-
Description = task.Description,
91-
Enabled = task.Enabled,
92-
AgentId = task.AgentId,
93-
Content = task.Content,
94-
CreatedTime = DateTime.UtcNow,
95-
UpdatedTime = DateTime.UtcNow
96-
};
97-
74+
var taskDoc = AgentTaskDocument.ToMongoModel(task);
75+
taskDoc.Id = Guid.NewGuid().ToString();
9876
_dc.AgentTasks.InsertOne(taskDoc);
9977
}
10078

10179
public void BulkInsertAgentTasks(List<AgentTask> tasks)
10280
{
10381
if (tasks.IsNullOrEmpty()) return;
10482

105-
var taskDocs = tasks.Select(x => new AgentTaskDocument
83+
var taskDocs = tasks.Select(x =>
10684
{
107-
Id = string.IsNullOrEmpty(x.Id) ? Guid.NewGuid().ToString() : x.Id,
108-
Name = x.Name,
109-
Description = x.Description,
110-
Enabled = x.Enabled,
111-
AgentId = x.AgentId,
112-
Content = x.Content,
113-
CreatedTime = x.CreatedDateTime,
114-
UpdatedTime = x.UpdatedDateTime
85+
var task = AgentTaskDocument.ToMongoModel(x);
86+
task.Id = !string.IsNullOrEmpty(x.Id) ? x.Id : Guid.NewGuid().ToString();
87+
return task;
11588
}).ToList();
11689

11790
_dc.AgentTasks.InsertMany(taskDocs);
@@ -139,11 +112,15 @@ public void UpdateAgentTask(AgentTask task, AgentTaskField field)
139112
case AgentTaskField.Content:
140113
taskDoc.Content = task.Content;
141114
break;
115+
case AgentTaskField.Status:
116+
taskDoc.Status = task.Status;
117+
break;
142118
case AgentTaskField.All:
143119
taskDoc.Name = task.Name;
144120
taskDoc.Description = task.Description;
145121
taskDoc.Enabled = task.Enabled;
146122
taskDoc.Content = task.Content;
123+
taskDoc.Status = task.Status;
147124
break;
148125
}
149126

0 commit comments

Comments
 (0)