Skip to content

filter: Add FilterTime #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions adapters/adaptertest/recordstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,56 @@ func testList(t *testing.T, factory func() workflow.RecordStore) {
}
}
})

t.Run("List - Filter by Created At", func(t *testing.T) {
store := factory()
ctx := context.Background()

now := time.Now()
n := 100
for range n {
newRecord := dummyWireRecord(t, workflowName)
newRecord.CreatedAt = now
err := store.Store(ctx, newRecord)
require.Nil(t, err)
}

t0 := now.Add(-time.Minute)
t1 := now.Add(time.Minute)
ls, err := store.List(
ctx,
workflowName,
0,
n,
workflow.OrderTypeAscending,
workflow.FilterByCreatedAtAfter(t0),
workflow.FilterByCreatedAtBefore(t1),
)
require.Nil(t, err)
require.Equal(t, n, len(ls))

ls2, err := store.List(
ctx,
workflowName,
0,
n,
workflow.OrderTypeAscending,
workflow.FilterByCreatedAtAfter(t1),
)
require.Nil(t, err)
require.Equal(t, 0, len(ls2))
ls3, err := store.List(
ctx,
workflowName,
0,
n,
workflow.OrderTypeAscending,
workflow.FilterByCreatedAtBefore(t0),
)
require.Nil(t, err)
require.Equal(t, 0, len(ls3))
})

}

func dummyWireRecord(t *testing.T, workflowName string) *workflow.Record {
Expand Down
7 changes: 7 additions & 0 deletions adapters/memrecordstore/memrecordstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,13 @@ func (s *Store) List(
continue
}

if filter.ByCreatedAtAfter().Enabled && !filter.ByCreatedAtAfter().Matches(record.CreatedAt) {
continue
}
if filter.ByCreatedAtBefore().Enabled && !filter.ByCreatedAtBefore().Matches(record.CreatedAt) {
continue
}

filteredStore[increment] = record
increment++
}
Expand Down
15 changes: 15 additions & 0 deletions adapters/sqlstore/sqlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlstore
import (
"context"
"database/sql"
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -169,6 +170,14 @@ func (s *SQLStore) List(
}
}

if filter.ByCreatedAtAfter().Enabled {
wb.AddCondition("created_at", ">", filter.ByCreatedAtAfter().Value())
}

if filter.ByCreatedAtBefore().Enabled {
wb.AddCondition("created_at", "<", filter.ByCreatedAtBefore().Value())
}

if limit == 0 {
limit = defaultListLimit
}
Expand All @@ -195,6 +204,12 @@ func (fq *whereBuilder) WhereNotNull(field string) {
fq.conditions = append(fq.conditions, field+" is not null")
}

func (fq *whereBuilder) AddCondition(field string, comparison string, value any) {
condition := fmt.Sprintf("(%s %s?)", field, comparison)
fq.conditions = append(fq.conditions, condition)
fq.params = append(fq.params, value)
}

func (fq *whereBuilder) Where(field string, values ...string) {
condition := " ( "
for i, value := range values {
Expand Down
52 changes: 49 additions & 3 deletions filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workflow
import (
"strconv"
"strings"
"time"
)

const multiValueDelimiter = ","
Expand All @@ -17,9 +18,11 @@ func MakeFilter(filters ...RecordFilter) *recordFilters {
}

type recordFilters struct {
byForeignID Filter
byStatus Filter
byRunState Filter
byForeignID Filter
byStatus Filter
byRunState Filter
byCreatedAtAfter FilterTime
byCreatedAtBefore FilterTime
}

func (r recordFilters) ByForeignID() Filter {
Expand All @@ -34,6 +37,13 @@ func (r recordFilters) ByRunState() Filter {
return r.byRunState
}

func (r recordFilters) ByCreatedAtAfter() FilterTime {
return r.byCreatedAtAfter
}
func (r recordFilters) ByCreatedAtBefore() FilterTime {
return r.byCreatedAtBefore
}

func makeFilterValue(value string, isMultiMatch bool) Filter {
return Filter{
Enabled: true,
Expand Down Expand Up @@ -70,6 +80,30 @@ func (f Filter) Value() string {
return f.value
}

func makeFilterTime(compare int, value time.Time) FilterTime {
return FilterTime{
Enabled: true,
compare: compare,
value: value,
}
}

type FilterTime struct {
Enabled bool
compare int
value time.Time
}

func (f FilterTime) Matches(findValue time.Time) bool {
if f.value.Compare(findValue) == f.compare {
return true
}
return false
}
func (f FilterTime) Value() time.Time {
return f.value
}

type RecordFilter func(filters *recordFilters)

func FilterByForeignID(foreignIDs ...string) RecordFilter {
Expand Down Expand Up @@ -130,3 +164,15 @@ func FilterByRunState(runStates ...RunState) RecordFilter {
filters.byRunState = makeFilterValue(val, true)
}
}

func FilterByCreatedAtAfter(after time.Time) RecordFilter {
return func(filters *recordFilters) {
filters.byCreatedAtAfter = makeFilterTime(-1, after)
}
}

func FilterByCreatedAtBefore(before time.Time) RecordFilter {
return func(filters *recordFilters) {
filters.byCreatedAtBefore = makeFilterTime(1, before)
}
}
26 changes: 25 additions & 1 deletion filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package workflow_test

import (
"testing"
"time"

"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -45,9 +46,32 @@ func TestMakeFilter(t *testing.T) {
require.Equal(t, []string{"9", "12"}, filter.ByStatus().MultiValues(), "Expected status filter value to be {'9', '12'}")
})

t.Run("Filter by Created At After", func(t *testing.T) {
d := time.Date(2025, time.August, 1, 0, 0, 0, 0, time.UTC)
filter := workflow.MakeFilter(workflow.FilterByCreatedAtAfter(d))
require.True(t, filter.ByCreatedAtAfter().Enabled, "Expected created at after filter to be enabled")
require.Equal(t, d, filter.ByCreatedAtAfter().Value(), "Expected created at after filter value to be 1st of august 2025")
})

t.Run("Filter by Created At Before", func(t *testing.T) {
d := time.Date(2025, time.August, 1, 0, 0, 0, 0, time.UTC)
filter := workflow.MakeFilter(workflow.FilterByCreatedAtBefore(d))
require.True(t, filter.ByCreatedAtBefore().Enabled, "Expected created at before filter to be enabled")
require.Equal(t, d, filter.ByCreatedAtBefore().Value(), "Expected created at before filter value to be 1st of august 2025")
})

t.Run("Matches", func(t *testing.T) {
filter := workflow.MakeFilter(workflow.FilterByStatus(9))
t0 := time.Date(2025, time.August, 1, 0, 0, 0, 0, time.UTC)
t1 := time.Date(2025, time.August, 10, 0, 0, 0, 0, time.UTC)

filter := workflow.MakeFilter(workflow.FilterByStatus(9),
workflow.FilterByCreatedAtAfter(t0),
workflow.FilterByCreatedAtBefore(t1))
require.True(t, filter.ByStatus().Matches("9"))

match := time.Date(2025, time.August, 5, 0, 0, 0, 0, time.UTC)
require.True(t, filter.ByCreatedAtAfter().Matches(match))
require.True(t, filter.ByCreatedAtBefore().Matches(match))
})

t.Run("Matches - Multi", func(t *testing.T) {
Expand Down