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)
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) {
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)
}