Skip to content

Commit

Permalink
fix: add time filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Mryashbhardwaj committed Sep 10, 2024
1 parent 1d35e9f commit 87062cc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
3 changes: 1 addition & 2 deletions core/scheduler/handler/v1beta1/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package v1beta1
import (
"fmt"
"strings"
"time"

"github.com/google/uuid"
"github.com/goto/salt/log"
Expand Down Expand Up @@ -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()),
)
Expand Down
4 changes: 2 additions & 2 deletions internal/store/postgres/scheduler/replay_repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 14 additions & 0 deletions internal/utils/filter/filter.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package filter

import "time"

type filter struct {
bits uint64
value map[Operand]interface{}
Expand All @@ -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 {
Expand Down
11 changes: 11 additions & 0 deletions internal/utils/filter/filter_opt.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package filter

import "time"

type (
FilterOpt func(*filter)
Operand uint64
Expand Down Expand Up @@ -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 != "" {
Expand Down

0 comments on commit 87062cc

Please sign in to comment.