From 48f6c6de7d338480891b881404eedcb94e4f416c Mon Sep 17 00:00:00 2001 From: ifooth Date: Sat, 28 Sep 2024 16:07:55 +0800 Subject: [PATCH] feat: bcs-task manager add ListTask --- bcs-common/common/task/stores/iface/interfaces.go | 3 +++ bcs-common/common/task/stores/mysql/mysql.go | 15 +++++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/bcs-common/common/task/stores/iface/interfaces.go b/bcs-common/common/task/stores/iface/interfaces.go index 7c7b8b6723..7983d9411e 100644 --- a/bcs-common/common/task/stores/iface/interfaces.go +++ b/bcs-common/common/task/stores/iface/interfaces.go @@ -15,6 +15,7 @@ package iface import ( "context" + "time" "github.com/Tencent/bk-bcs/bcs-common/common/task/types" ) @@ -29,6 +30,8 @@ type ListOption struct { CurrentStep string Status string Creator string + StartGte *time.Time // StartGte start time greater or equal to + StartLte *time.Time // StartLte start time less or equal to Sort map[string]int // Sort map for sort list results Offset int64 // Offset offset for list results Limit int64 // Limit limit for list results diff --git a/bcs-common/common/task/stores/mysql/mysql.go b/bcs-common/common/task/stores/mysql/mysql.go index be86020ad5..8174472a6b 100644 --- a/bcs-common/common/task/stores/mysql/mysql.go +++ b/bcs-common/common/task/stores/mysql/mysql.go @@ -107,7 +107,7 @@ func (s *mysqlStore) CreateTask(ctx context.Context, task *types.Task) error { func (s *mysqlStore) ListTask(ctx context.Context, opt *iface.ListOption) (*iface.Pagination[types.Task], error) { tx := s.db.WithContext(ctx) - // 0值gorm自动忽略查询 + // 条件过滤 0值gorm自动忽略查询 tx = tx.Where(&TaskRecord{ TaskID: opt.TaskID, TaskType: opt.TaskType, @@ -117,7 +117,18 @@ func (s *mysqlStore) ListTask(ctx context.Context, opt *iface.ListOption) (*ifac Status: opt.Status, CurrentStep: opt.CurrentStep, Creator: opt.Creator, - }).Order("id desc") + }) + + // mysql store 使用创建时间过滤 + if opt.StartGte != nil { + tx = tx.Where("created_at >= ?", opt.StartGte) + } + if opt.StartLte != nil { + tx = tx.Where("created_at <= ?", opt.StartLte) + } + + // 只使用id排序 + tx = tx.Order("id DESC") result, count, err := FindByPage[TaskRecord](tx, int(opt.Offset), int(opt.Limit)) if err != nil {