feat: add delete log

This commit is contained in:
engigu
2026-02-27 09:02:00 +08:00
parent 789a538557
commit d4f1defdeb
7 changed files with 187 additions and 10 deletions
+38
View File
@@ -97,3 +97,41 @@ func (lc *LogController) GetLogDetail(c *gin.Context) {
utils.Success(c, vo.ToTaskLogVO(&log))
}
func (lc *LogController) ClearLogs(c *gin.Context) {
var req struct {
TaskID *int `json:"task_id"`
}
if err := c.ShouldBindJSON(&req); err != nil {
utils.BadRequest(c, err.Error())
return
}
query := database.DB.Model(&models.TaskLog{})
if req.TaskID != nil && *req.TaskID > 0 {
query = query.Where("task_id = ?", *req.TaskID)
}
if err := query.Delete(&models.TaskLog{}).Error; err != nil {
utils.ServerError(c, "清空日志失败")
return
}
utils.SuccessMsg(c, "日志清空成功")
}
func (lc *LogController) DeleteLog(c *gin.Context) {
id, err := strconv.Atoi(c.Param("id"))
if err != nil {
utils.BadRequest(c, "无效的日志ID")
return
}
if err := database.DB.Delete(&models.TaskLog{}, id).Error; err != nil {
utils.ServerError(c, "删除日志失败")
return
}
utils.SuccessMsg(c, "日志已删除")
}
+2 -1
View File
@@ -109,6 +109,7 @@ func (tc *TaskController) GetTasks(c *gin.Context) {
agentIDStr := c.DefaultQuery("agent_id", "")
tags := c.DefaultQuery("tags", "")
taskType := c.DefaultQuery("type", "")
var agentID *uint
if agentIDStr != "" {
@@ -118,7 +119,7 @@ func (tc *TaskController) GetTasks(c *gin.Context) {
}
}
tasks, total := tc.taskService.GetTasksWithPagination(p.Page, p.PageSize, name, agentID, tags)
tasks, total := tc.taskService.GetTasksWithPagination(p.Page, p.PageSize, name, agentID, tags, taskType)
utils.PaginatedResponse(c, vo.ToTaskVOListFromModels(tasks), total, p)
}
+2
View File
@@ -172,8 +172,10 @@ func Setup(c *Controllers) *gin.Engine {
logs := authorized.Group("/logs")
{
logs.GET("", c.Log.GetLogs)
logs.POST("/clear", c.Log.ClearLogs)
logs.GET("/ws", c.LogWS.StreamLog)
logs.GET("/:id", c.Log.GetLogDetail)
logs.DELETE("/:id", c.Log.DeleteLog)
}
// 终端模块
+4 -1
View File
@@ -49,7 +49,7 @@ func (ts *TaskService) GetTasks() []models.Task {
}
// GetTasksWithPagination 分页获取任务列表
func (ts *TaskService) GetTasksWithPagination(page, pageSize int, name string, agentID *uint, tags string) ([]models.Task, int64) {
func (ts *TaskService) GetTasksWithPagination(page, pageSize int, name string, agentID *uint, tags string, taskType string) ([]models.Task, int64) {
var tasks []models.Task
var total int64
@@ -60,6 +60,9 @@ func (ts *TaskService) GetTasksWithPagination(page, pageSize int, name string, a
if tags != "" {
query = query.Where("tags LIKE ?", "%"+tags+"%")
}
if taskType != "" {
query = query.Where("type = ?", taskType)
}
if agentID != nil {
query = query.Where("agent_id = ?", *agentID)
}