From 87062cc943ccf8337ef52f87dc9c7023f85e8be6 Mon Sep 17 00:00:00 2001 From: Yash Bhardwaj Date: Tue, 10 Sep 2024 15:09:51 +0530 Subject: [PATCH] fix: add time filter --- core/scheduler/handler/v1beta1/replay.go | 3 +-- .../store/postgres/scheduler/replay_repository.go | 4 ++-- internal/utils/filter/filter.go | 14 ++++++++++++++ internal/utils/filter/filter_opt.go | 11 +++++++++++ 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/core/scheduler/handler/v1beta1/replay.go b/core/scheduler/handler/v1beta1/replay.go index 2432e482ff..009c4d02c5 100644 --- a/core/scheduler/handler/v1beta1/replay.go +++ b/core/scheduler/handler/v1beta1/replay.go @@ -3,7 +3,6 @@ package v1beta1 import ( "fmt" "strings" - "time" "github.com/google/uuid" "github.com/goto/salt/log" @@ -108,7 +107,7 @@ func (h ReplayHandler) GetReplayDetails(ctx context.Context, req *pb.GetReplayDe replays, err := h.service.GetByFilter(ctx, projectName, filter.WithStringArray(filter.JobNames, req.GetJobNames()), - filter.WithString(filter.ScheduledAt, req.GetScheduledAt().AsTime().Format(time.DateTime)), + filter.WithTime(filter.ScheduledAt, req.GetScheduledAt().AsTime()), filter.WithString(filter.ReplayID, req.GetReplayId()), filter.WithString(filter.ReplayStatus, req.GetStatus()), ) diff --git a/internal/store/postgres/scheduler/replay_repository.go b/internal/store/postgres/scheduler/replay_repository.go index 8a144a669e..41f25b6447 100644 --- a/internal/store/postgres/scheduler/replay_repository.go +++ b/internal/store/postgres/scheduler/replay_repository.go @@ -248,8 +248,8 @@ func (r ReplayRepository) getReplayRequestWithFilters(ctx context.Context, proje } } if f.Contains(filter.ScheduledAt) { - scheduledAt := f.GetStringValue(filter.ScheduledAt) - filterQueryFragments = append(filterQueryFragments, fmt.Sprintf("start_time<='%s' AND '%s'<=end_time", scheduledAt, scheduledAt)) + scheduledAt := f.GetTimeValue(filter.ScheduledAt) + filterQueryFragments = append(filterQueryFragments, fmt.Sprintf("start_time<='%s' AND '%s'<=end_time", scheduledAt.Format(time.DateTime), scheduledAt.Format(time.DateTime))) } if f.Contains(filter.ReplayStatus) { replayStatusString := f.GetStringValue(filter.ReplayStatus) diff --git a/internal/utils/filter/filter.go b/internal/utils/filter/filter.go index 27c7d38ab7..4140c3c586 100644 --- a/internal/utils/filter/filter.go +++ b/internal/utils/filter/filter.go @@ -1,5 +1,7 @@ package filter +import "time" + type filter struct { bits uint64 value map[Operand]interface{} @@ -16,6 +18,18 @@ func NewFilter(opts ...FilterOpt) *filter { return f } +func (f *filter) GetTimeValue(operand Operand) time.Time { + v, ok := f.value[operand] + if !ok { + return time.Time{} + } + val, ok := v.(time.Time) + if !ok { + return time.Time{} + } + return val +} + func (f *filter) GetStringValue(operand Operand) string { v, ok := f.value[operand] if !ok { diff --git a/internal/utils/filter/filter_opt.go b/internal/utils/filter/filter_opt.go index eb9eb8366d..9801aca1e9 100644 --- a/internal/utils/filter/filter_opt.go +++ b/internal/utils/filter/filter_opt.go @@ -1,5 +1,7 @@ package filter +import "time" + type ( FilterOpt func(*filter) Operand uint64 @@ -29,6 +31,15 @@ const ( ReplayID = Operand(bitOnReplayID) ) +func WithTime(operand Operand, value time.Time) FilterOpt { + return func(f *filter) { + if !(value.Equal(time.Unix(0, 0))) { + f.bits |= uint64(operand) + f.value[operand] = value + } + } +} + func WithString(operand Operand, value string) FilterOpt { return func(f *filter) { if value != "" {