ListTaskExecutionLogs

This commit is contained in:
2025-10-18 15:39:47 +08:00
parent df0dfd62c6
commit 6a93346e87
4 changed files with 183 additions and 1 deletions

View File

@@ -166,3 +166,52 @@ func (c *Controller) ListPlanExecutionLogs(ctx *gin.Context) {
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total) c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划执行日志成功", resp, actionType, "获取计划执行日志成功", req) controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取计划执行日志成功", resp, actionType, "获取计划执行日志成功", req)
} }
// ListTaskExecutionLogs godoc
// @Summary 获取任务执行日志列表
// @Description 根据提供的过滤条件,分页获取任务执行日志
// @Tags 数据监控
// @Security BearerAuth
// @Produce json
// @Param query query dto.ListTaskExecutionLogRequest true "查询参数"
// @Success 200 {object} controller.Response{data=dto.ListTaskExecutionLogResponse}
// @Router /api/v1/monitor/task-execution-logs [get]
func (c *Controller) ListTaskExecutionLogs(ctx *gin.Context) {
const actionType = "获取任务执行日志列表"
var req dto.ListTaskExecutionLogRequest
if err := ctx.ShouldBindQuery(&req); err != nil {
c.logger.Errorf("%s: 参数绑定失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的查询参数: "+err.Error(), actionType, "参数绑定失败", req)
return
}
opts := repository.TaskExecutionLogListOptions{
PlanExecutionLogID: req.PlanExecutionLogID,
TaskID: req.TaskID,
OrderBy: req.OrderBy,
StartTime: req.StartTime,
EndTime: req.EndTime,
}
if req.Status != nil {
status := models.ExecutionStatus(*req.Status)
opts.Status = &status
}
data, total, err := c.monitorService.ListTaskExecutionLogs(opts, req.Page, req.PageSize)
if err != nil {
if errors.Is(err, repository.ErrInvalidPagination) {
c.logger.Warnf("%s: 无效的分页参数: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeBadRequest, "无效的分页参数: "+err.Error(), actionType, "无效分页参数", req)
return
}
c.logger.Errorf("%s: 服务层查询失败: %v", actionType, err)
controller.SendErrorWithAudit(ctx, controller.CodeInternalError, "获取任务执行日志失败: "+err.Error(), actionType, "服务层查询失败", req)
return
}
resp := dto.NewListTaskExecutionLogResponse(data, total, req.Page, req.PageSize)
c.logger.Infof("%s: 成功, 获取到 %d 条记录, 总计 %d 条", actionType, len(data), total)
controller.SendSuccessWithAudit(ctx, controller.CodeSuccess, "获取任务执行日志成功", resp, actionType, "获取任务执行日志成功", req)
}

View File

@@ -174,3 +174,76 @@ func NewListPlanExecutionLogResponse(data []models.PlanExecutionLog, total int64
}, },
} }
} }
// --- TaskExecutionLog ---
// ListTaskExecutionLogRequest 定义了获取任务执行日志列表的请求参数
type ListTaskExecutionLogRequest struct {
Page int `form:"page,default=1"`
PageSize int `form:"pageSize,default=10"`
PlanExecutionLogID *uint `form:"plan_execution_log_id"`
TaskID *int `form:"task_id"`
Status *string `form:"status"`
StartTime *time.Time `form:"start_time" time_format:"rfc3339"`
EndTime *time.Time `form:"end_time" time_format:"rfc3339"`
OrderBy string `form:"order_by"`
}
// TaskDTO 是用于API响应的简化版任务结构
type TaskDTO struct {
ID uint `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
// TaskExecutionLogDTO 是用于API响应的任务执行日志结构
type TaskExecutionLogDTO struct {
ID uint `json:"id"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
PlanExecutionLogID uint `json:"plan_execution_log_id"`
TaskID int `json:"task_id"`
Task TaskDTO `json:"task"` // 嵌套的任务信息
Status models.ExecutionStatus `json:"status"`
Output string `json:"output"`
StartedAt time.Time `json:"started_at"`
EndedAt time.Time `json:"ended_at"`
}
// ListTaskExecutionLogResponse 是获取任务执行日志列表的响应结构
type ListTaskExecutionLogResponse struct {
List []TaskExecutionLogDTO `json:"list"`
Pagination PaginationDTO `json:"pagination"`
}
// NewListTaskExecutionLogResponse 从模型数据创建列表响应 DTO
func NewListTaskExecutionLogResponse(data []models.TaskExecutionLog, total int64, page, pageSize int) *ListTaskExecutionLogResponse {
dtos := make([]TaskExecutionLogDTO, len(data))
for i, item := range data {
dtos[i] = TaskExecutionLogDTO{
ID: item.ID,
CreatedAt: item.CreatedAt,
UpdatedAt: item.UpdatedAt,
PlanExecutionLogID: item.PlanExecutionLogID,
TaskID: item.TaskID,
Task: TaskDTO{
ID: uint(item.Task.ID),
Name: item.Task.Name,
Description: item.Task.Description,
},
Status: item.Status,
Output: item.Output,
StartedAt: item.StartedAt,
EndedAt: item.EndedAt,
}
}
return &ListTaskExecutionLogResponse{
List: dtos,
Pagination: PaginationDTO{
Total: total,
Page: page,
PageSize: pageSize,
},
}
}

View File

@@ -40,3 +40,8 @@ func (s *MonitorService) ListDeviceCommandLogs(opts repository.DeviceCommandLogL
func (s *MonitorService) ListPlanExecutionLogs(opts repository.PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) { func (s *MonitorService) ListPlanExecutionLogs(opts repository.PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) {
return s.executionLogRepo.ListPlanExecutionLogs(opts, page, pageSize) return s.executionLogRepo.ListPlanExecutionLogs(opts, page, pageSize)
} }
// ListTaskExecutionLogs 负责处理查询任务执行日志列表的业务逻辑
func (s *MonitorService) ListTaskExecutionLogs(opts repository.TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error) {
return s.executionLogRepo.ListTaskExecutionLogs(opts, page, pageSize)
}

View File

@@ -17,6 +17,16 @@ type PlanExecutionLogListOptions struct {
OrderBy string // 例如 "created_at asc" OrderBy string // 例如 "created_at asc"
} }
// TaskExecutionLogListOptions 定义了查询任务执行日志时的可选参数
type TaskExecutionLogListOptions struct {
PlanExecutionLogID *uint
TaskID *int
Status *models.ExecutionStatus
StartTime *time.Time // 基于 created_at 字段
EndTime *time.Time // 基于 created_at 字段
OrderBy string // 例如 "created_at asc"
}
// ExecutionLogRepository 定义了与执行日志交互的接口。 // ExecutionLogRepository 定义了与执行日志交互的接口。
type ExecutionLogRepository interface { type ExecutionLogRepository interface {
// --- Existing methods --- // --- Existing methods ---
@@ -58,8 +68,9 @@ type ExecutionLogRepository interface {
// CancelIncompleteTasksByPlanLogID 取消一个计划执行中的所有未完成任务 // CancelIncompleteTasksByPlanLogID 取消一个计划执行中的所有未完成任务
CancelIncompleteTasksByPlanLogID(planLogID uint, reason string) error CancelIncompleteTasksByPlanLogID(planLogID uint, reason string) error
// --- New method --- // --- New methods ---
ListPlanExecutionLogs(opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error) ListPlanExecutionLogs(opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error)
ListTaskExecutionLogs(opts TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error)
} }
// gormExecutionLogRepository 是使用 GORM 的具体实现。 // gormExecutionLogRepository 是使用 GORM 的具体实现。
@@ -112,6 +123,50 @@ func (r *gormExecutionLogRepository) ListPlanExecutionLogs(opts PlanExecutionLog
return results, total, err return results, total, err
} }
// ListTaskExecutionLogs 实现了分页和过滤查询任务执行日志的功能
func (r *gormExecutionLogRepository) ListTaskExecutionLogs(opts TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error) {
if page <= 0 || pageSize <= 0 {
return nil, 0, ErrInvalidPagination
}
var results []models.TaskExecutionLog
var total int64
query := r.db.Model(&models.TaskExecutionLog{})
if opts.PlanExecutionLogID != nil {
query = query.Where("plan_execution_log_id = ?", *opts.PlanExecutionLogID)
}
if opts.TaskID != nil {
query = query.Where("task_id = ?", *opts.TaskID)
}
if opts.Status != nil {
query = query.Where("status = ?", *opts.Status)
}
if opts.StartTime != nil {
query = query.Where("created_at >= ?", *opts.StartTime)
}
if opts.EndTime != nil {
query = query.Where("created_at <= ?", *opts.EndTime)
}
if err := query.Count(&total).Error; err != nil {
return nil, 0, err
}
orderBy := "created_at DESC"
if opts.OrderBy != "" {
orderBy = opts.OrderBy
}
// 预加载关联的Task信息
query = query.Order(orderBy).Preload("Task")
offset := (page - 1) * pageSize
err := query.Limit(pageSize).Offset(offset).Find(&results).Error
return results, total, err
}
// --- Existing method implementations --- // --- Existing method implementations ---
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error { func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error {