ListTaskExecutionLogs
This commit is contained in:
@@ -17,6 +17,16 @@ type PlanExecutionLogListOptions struct {
|
||||
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 定义了与执行日志交互的接口。
|
||||
type ExecutionLogRepository interface {
|
||||
// --- Existing methods ---
|
||||
@@ -58,8 +68,9 @@ type ExecutionLogRepository interface {
|
||||
// CancelIncompleteTasksByPlanLogID 取消一个计划执行中的所有未完成任务
|
||||
CancelIncompleteTasksByPlanLogID(planLogID uint, reason string) error
|
||||
|
||||
// --- New method ---
|
||||
// --- New methods ---
|
||||
ListPlanExecutionLogs(opts PlanExecutionLogListOptions, page, pageSize int) ([]models.PlanExecutionLog, int64, error)
|
||||
ListTaskExecutionLogs(opts TaskExecutionLogListOptions, page, pageSize int) ([]models.TaskExecutionLog, int64, error)
|
||||
}
|
||||
|
||||
// gormExecutionLogRepository 是使用 GORM 的具体实现。
|
||||
@@ -112,6 +123,50 @@ func (r *gormExecutionLogRepository) ListPlanExecutionLogs(opts PlanExecutionLog
|
||||
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 ---
|
||||
|
||||
func (r *gormExecutionLogRepository) UpdateTaskExecutionLogStatusByIDs(logIDs []uint, status models.ExecutionStatus) error {
|
||||
|
||||
Reference in New Issue
Block a user