Skip to content
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

Added some query filters on Job executions #878

Merged
merged 5 commits into from
Jan 6, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion dkron/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,16 +328,28 @@ func (h *HTTPTransport) restoreHandler(c *gin.Context) {
func (h *HTTPTransport) executionsHandler(c *gin.Context) {
jobName := c.Param("job")

sort := c.DefaultQuery("_sort", "")
if sort == "id" {
sort = "started_at"
}
order := c.DefaultQuery("_order", "DESC")

job, err := h.agent.Store.GetJob(jobName, nil)
if err != nil {
c.AbortWithError(http.StatusNotFound, err)
return
}

executions, err := h.agent.Store.GetExecutions(job.Name, job.GetTimeLocation())
executions, err := h.agent.Store.GetExecutions(job.Name, job.GetTimeLocation(),
&ExecutionOptions{
Sort: sort,
Order: order,
},
)
if err != nil {
if err == buntdb.ErrNotFound {
renderJSON(c, http.StatusOK, &[]Execution{})
log.Error(err)
return
}
log.Error(err)
Expand Down
4 changes: 2 additions & 2 deletions dkron/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,14 @@ func TestGRPCExecutionDone(t *testing.T) {

rc := NewGRPCClient(nil, a)
rc.ExecutionDone(a.advertiseRPCAddr(), testExecution)
execs, err := a.Store.GetExecutions("test", nil)
execs, err := a.Store.GetExecutions("test", nil, &ExecutionOptions{})
require.NoError(t, err)

assert.Len(t, execs, 1)
assert.Equal(t, string(testExecution.Output), string(execs[0].Output))

// Test run a dependent job
execs, err = a.Store.GetExecutions("child-test", nil)
execs, err = a.Store.GetExecutions("child-test", nil, &ExecutionOptions{})
require.NoError(t, err)

assert.Len(t, execs, 1)
Expand Down
2 changes: 1 addition & 1 deletion dkron/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type Storage interface {
SetExecutionDone(execution *Execution) (bool, error)
GetJobs(options *JobOptions) ([]*Job, error)
GetJob(name string, options *JobOptions) (*Job, error)
GetExecutions(jobName string, timezone *time.Location) ([]*Execution, error)
GetExecutions(jobName string, timezone *time.Location, opts *ExecutionOptions) ([]*Execution, error)
vcastellm marked this conversation as resolved.
Show resolved Hide resolved
GetExecutionGroup(execution *Execution, timezone *time.Location) ([]*Execution, error)
GetGroupedExecutions(jobName string, timezone *time.Location) (map[int64][]*Execution, []int64, error)
Shutdown() error
Expand Down
54 changes: 34 additions & 20 deletions dkron/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ type JobOptions struct {
Status string
}

// ExecutionOptions additional options like "Sort" will be ready for JSON marshall
type ExecutionOptions struct {
Sort string
Order string
}

type kv struct {
Key string
Value []byte
Expand All @@ -56,6 +62,8 @@ type kv struct {
func NewStore() (*Store, error) {
db, err := buntdb.Open(":memory:")
db.CreateIndex("name", jobsPrefix+":*", buntdb.IndexJSON("name"))
db.CreateIndex("started_at", executionsPrefix+":*", buntdb.IndexJSON("started_at"))
db.CreateIndex("finished_at", executionsPrefix+":*", buntdb.IndexJSON("finished_at"))
db.CreateIndex("displayname", jobsPrefix+":*", buntdb.IndexJSON("displayname"))
db.CreateIndex("schedule", jobsPrefix+":*", buntdb.IndexJSON("schedule"))
db.CreateIndex("success_count", jobsPrefix+":*", buntdb.IndexJSON("success_count"))
Expand Down Expand Up @@ -416,49 +424,55 @@ func (s *Store) DeleteJob(name string) (*Job, error) {
}

// GetExecutions returns the executions given a Job name.
func (s *Store) GetExecutions(jobName string, timezone *time.Location) ([]*Execution, error) {
func (s *Store) GetExecutions(jobName string, timezone *time.Location, opts *ExecutionOptions) ([]*Execution, error) {
prefix := fmt.Sprintf("%s:%s:", executionsPrefix, jobName)

kvs, err := s.list(prefix, true)
kvs, err := s.list(prefix, true, opts)
if err != nil {
return nil, err
}

return s.unmarshalExecutions(kvs, timezone)
}

func (s *Store) list(prefix string, checkRoot bool) ([]kv, error) {
func (s *Store) list(prefix string, checkRoot bool, opts *ExecutionOptions) ([]kv, error) {
var found bool
kvs := []kv{}

err := s.db.View(s.listTxFunc(prefix, &kvs, &found))
err := s.db.View(s.listTxFunc(prefix, &kvs, &found, opts))
if err == nil && !found && checkRoot {
return nil, buntdb.ErrNotFound
}

return kvs, err
}

func (*Store) listTxFunc(prefix string, kvs *[]kv, found *bool) func(tx *buntdb.Tx) error {
return func(tx *buntdb.Tx) error {
err := tx.Ascend("", func(key, value string) bool {
if strings.HasPrefix(key, prefix) {
*found = true
// ignore self in listing
if !bytes.Equal(trimDirectoryKey([]byte(key)), []byte(prefix)) {
kv := kv{Key: key, Value: []byte(value)}
*kvs = append(*kvs, kv)
}
func (*Store) listTxFunc(prefix string, kvs *[]kv, found *bool, opts *ExecutionOptions) func(tx *buntdb.Tx) error {
fnc := func(key, value string) bool {
if strings.HasPrefix(key, prefix) {
*found = true
// ignore self in listing
if !bytes.Equal(trimDirectoryKey([]byte(key)), []byte(prefix)) {
kv := kv{Key: key, Value: []byte(value)}
*kvs = append(*kvs, kv)
}
return true
})
}
return true
}

return func(tx *buntdb.Tx) (err error) {
if opts.Order == "DESC" {
err = tx.Descend(opts.Sort, fnc)
} else {
err = tx.Ascend(opts.Sort, fnc)
}
return err
}
}

// GetExecutionGroup returns all executions in the same group of a given execution
func (s *Store) GetExecutionGroup(execution *Execution, timezone *time.Location) ([]*Execution, error) {
res, err := s.GetExecutions(execution.JobName, timezone)
res, err := s.GetExecutions(execution.JobName, timezone, &ExecutionOptions{})
if err != nil {
return nil, err
}
Expand All @@ -475,7 +489,7 @@ func (s *Store) GetExecutionGroup(execution *Execution, timezone *time.Location)
// GetGroupedExecutions returns executions for a job grouped and with an ordered index
// to facilitate access.
func (s *Store) GetGroupedExecutions(jobName string, timezone *time.Location) (map[int64][]*Execution, []int64, error) {
execs, err := s.GetExecutions(jobName, timezone)
execs, err := s.GetExecutions(jobName, timezone, &ExecutionOptions{})
if err != nil {
return nil, nil, err
}
Expand Down Expand Up @@ -545,7 +559,7 @@ func (s *Store) SetExecution(execution *Execution) (string, error) {
return "", err
}

execs, err := s.GetExecutions(execution.JobName, nil)
execs, err := s.GetExecutions(execution.JobName, nil, &ExecutionOptions{})
if err != nil && err != buntdb.ErrNotFound {
log.WithError(err).
WithField("job", execution.JobName).
Expand Down Expand Up @@ -640,7 +654,7 @@ func (s *Store) computeStatus(jobName string, exGroup int64, tx *buntdb.Tx) (str
found := false
prefix := fmt.Sprintf("%s:%s:", executionsPrefix, jobName)

if err := s.listTxFunc(prefix, &kvs, &found)(tx); err != nil {
if err := s.listTxFunc(prefix, &kvs, &found, &ExecutionOptions{})(tx); err != nil {
return "", err
}

Expand Down
5 changes: 4 additions & 1 deletion dkron/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ func TestStore(t *testing.T) {
_, err = s.SetExecution(testExecution2)
require.NoError(t, err)

execs, err := s.GetExecutions("test", nil)
execs, err := s.GetExecutions("test", nil, &ExecutionOptions{
Sort: "started_at",
Order: "DESC",
})
assert.NoError(t, err)

testExecution.Id = testExecution.Key()
Expand Down