Skip to content

Commit

Permalink
llbsolver: add time based filter support to history
Browse files Browse the repository at this point in the history
Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Feb 5, 2025
1 parent 83cd1c5 commit 8767767
Show file tree
Hide file tree
Showing 2 changed files with 254 additions and 18 deletions.
132 changes: 114 additions & 18 deletions solver/llbsolver/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
digest "github.com/opencontainers/go-digest"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/tonistiigi/go-csvvalue"
bolt "go.etcd.io/bbolt"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/codes"
Expand Down Expand Up @@ -1044,14 +1045,18 @@ func filterHistoryEvents(in []*controlapi.BuildHistoryEvent, filters []string, l

out := make([]*controlapi.BuildHistoryEvent, 0, len(in))

loop0:
for _, ev := range in {
for _, fn := range f {
if !fn(ev) {
continue loop0
if len(f) == 0 {
out = in
} else {
loop0:
for _, ev := range in {
for _, fn := range f {
if fn(ev) {
out = append(out, ev)
continue loop0
}
}
}
out = append(out, ev)
}

if limit != 0 {
Expand Down Expand Up @@ -1079,16 +1084,102 @@ func parseFilters(in []string) ([]func(*controlapi.BuildHistoryEvent) bool, erro
return nil, nil
}

// ref == ~= !=
// createdAt > < >= <=
// completedAt > < >= <=
// status == ~= != (running, complete, error)
// duration > < >= <=
// repository == ~= !=
var out []func(*controlapi.BuildHistoryEvent) bool
for _, in := range in {
fns, err := parseFilter(in)
if err != nil {
return nil, err
}
out = append(out, func(ev *controlapi.BuildHistoryEvent) bool {
for _, fn := range fns {
if !fn(ev) {
return false
}
}
return true
})
}
return out, nil
}

func timeBasedFilter(f string) (func(*controlapi.BuildHistoryEvent) bool, error) {
key, sep, value, _ := cutAny(f, []string{">=", "<=", ">", "<"})
var cmp int64
switch key {
case "startedAt", "completedAt":
v, err := time.ParseDuration(value)
if err == nil {
tm := time.Now().Add(-v)
cmp = tm.Unix()
} else {
tm, err := time.Parse(time.RFC3339, value)
if err != nil {
return nil, errors.Errorf("invalid time %s", value)
}
cmp = tm.Unix()
}
case "duration":
v, err := time.ParseDuration(value)
if err != nil {
return nil, errors.Errorf("invalid duration %s", value)
}
cmp = int64(v)
default:
return nil, nil
}

return func(ev *controlapi.BuildHistoryEvent) bool {
if ev.Record == nil {
return false
}
var val int64
switch key {
case "startedAt":
val = ev.Record.CreatedAt.AsTime().Unix()
case "completedAt":
if ev.Record.CompletedAt != nil {
val = ev.Record.CompletedAt.AsTime().Unix()
}
case "duration":
if ev.Record.CompletedAt != nil {
val = int64(ev.Record.CompletedAt.AsTime().Sub(ev.Record.CreatedAt.AsTime()))
}
}
switch sep {
case ">=":
return val >= cmp
case "<=":
return val <= cmp
case ">":
return val > cmp
default:
return val < cmp
}
}, nil
}

func parseFilter(in string) ([]func(*controlapi.BuildHistoryEvent) bool, error) {
var out []func(*controlapi.BuildHistoryEvent) bool

filter, err := filters.ParseAll(in...)
fields, err := csvvalue.Fields(in, nil)
if err != nil {
return nil, err
}
var staticFilters []string

for _, f := range fields {
fn, err := timeBasedFilter(f)
if err != nil {
return nil, err
}
if fn == nil {
staticFilters = append(staticFilters, f)
continue
}
out = append(out, fn)
}

filter, err := filters.ParseAll(strings.Join(staticFilters, ","))
if err != nil {
return nil, errors.Wrapf(err, "failed to parse history filters %v", in)
}
Expand Down Expand Up @@ -1118,12 +1209,10 @@ func adaptHistoryRecord(rec *controlapi.BuildHistoryRecord) filters.Adaptor {
return "canceled", true
}
return "error", true
} else {
return "complete", true
}
} else {
return "running", true
return "complete", true
}
return "running", true
case "repository":
v, ok := rec.FrontendAttrs["vcs:source"]
return v, ok
Expand All @@ -1132,7 +1221,14 @@ func adaptHistoryRecord(rec *controlapi.BuildHistoryRecord) filters.Adaptor {
})
}

type constFilter[T comparable] struct{}
func cutAny(in string, opt []string) (before string, sep string, after string, found bool) {
for _, s := range opt {
if i := strings.Index(in, s); i >= 0 {
return in[:i], s, in[i+len(s):], true
}
}
return "", "", "", false
}

type pubsub[T any] struct {
mu sync.Mutex
Expand Down
140 changes: 140 additions & 0 deletions solver/llbsolver/history_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package llbsolver

import (
"testing"
"time"

controlapi "github.com/moby/buildkit/api/services/control"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/types/known/timestamppb"
)

func TestHistoryFilters(t *testing.T) {
epoch := time.Now().Add(-24 * time.Hour)
testRecords := []*controlapi.BuildHistoryEvent{
{
Record: &controlapi.BuildHistoryRecord{
Ref: "foo123",
CreatedAt: timestamppb.New(epoch),
CompletedAt: timestamppb.New(epoch.Add(time.Minute)),
},
},
{
Record: &controlapi.BuildHistoryRecord{
Ref: "bar456",
CreatedAt: timestamppb.New(epoch.Add(time.Hour)),
},
},
{
Record: &controlapi.BuildHistoryRecord{
Ref: "foo789",
FrontendAttrs: map[string]string{
"vcs:source": "testrepo",
},
CreatedAt: timestamppb.New(epoch.Add(2 * time.Hour)),
CompletedAt: timestamppb.New(epoch.Add(2 * time.Hour).Add(10 * time.Minute)),
},
},
}

tcases := []struct {
name string
filters []string
limit int32
expected []string
err string
}{
{
name: "no match",
filters: []string{"ref==foo"},
limit: 2,
},
{
name: "ref prefix",
filters: []string{"ref~=foo"},
limit: 2,
expected: []string{"foo789", "foo123"},
},
{
name: "repo",
filters: []string{"repository!=testrepo"},
limit: 2,
expected: []string{"bar456", "foo123"},
},
{
name: "limit",
filters: []string{"ref!=notexist"},
limit: 2,
expected: []string{"foo789", "bar456"},
},
{
name: "invalid filter",
filters: []string{"ref+foo"},
err: "parse error",
},
{
name: "multi",
filters: []string{"repository!=testrepo,ref~=foo"},
limit: 2,
expected: []string{"foo123"},
},
{
name: "latest",
filters: []string{"completedAt>23h"},
expected: []string{"foo789"},
},
{
name: "oldest",
filters: []string{"startedAt<23h"},
expected: []string{"foo123"},
},
{
name: "oldestoreq",
filters: []string{"startedAt<=23h"},
expected: []string{"foo123", "bar456"},
},
{
name: "longest",
filters: []string{"duration>5m"},
expected: []string{"foo789"},
},
{
name: "multitime",
filters: []string{"startedAt>24h,completedAt>22h"},
expected: []string{"foo789"},
},
{
name: "multitime or",
filters: []string{"startedAt>=23h", "completedAt>22h"},
expected: []string{"bar456", "foo789"},
},
{
name: "timeandref",
filters: []string{"startedAt>24h,ref~=foo"},
expected: []string{"foo789"},
},
{
name: "nofilters",
limit: 2,
expected: []string{"foo789", "bar456"},
},
}

for _, tcase := range tcases {
t.Run(tcase.name, func(t *testing.T) {
out, err := filterHistoryEvents(testRecords, tcase.filters, tcase.limit)
if tcase.err != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tcase.err)
return
}
require.NoError(t, err)
require.Len(t, out, len(tcase.expected))
var refs []string
for _, r := range out {
refs = append(refs, r.Record.Ref)
}
require.Equal(t, tcase.expected, refs)
})
}
}

0 comments on commit 8767767

Please sign in to comment.