Skip to content

Commit

Permalink
feat(activitylog): allow sorting results
Browse files Browse the repository at this point in the history
Signed-off-by: jkoberg <[email protected]>
  • Loading branch information
kobergj committed Jun 27, 2024
1 parent 13bba1d commit 17df46f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 22 additions & 8 deletions services/activitylog/pkg/service/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"net/http"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -52,7 +53,7 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
return
}

rid, limit, rawActivityAccepted, activityAccepted, err := s.getFilters(r.URL.Query().Get("kql"))
rid, limit, rawActivityAccepted, activityAccepted, sort, err := s.getFilters(r.URL.Query().Get("kql"))
if err != nil {
s.log.Info().Str("query", r.URL.Query().Get("kql")).Err(err).Msg("error getting filters")
_, _ = w.Write([]byte(err.Error()))
Expand Down Expand Up @@ -180,6 +181,8 @@ func (s *ActivitylogService) HandleGetItemActivities(w http.ResponseWriter, r *h
}()
}

sort(resp.Activities)

b, err := json.Marshal(resp)
if err != nil {
s.log.Error().Err(err).Msg("error marshalling activities")
Expand Down Expand Up @@ -211,15 +214,17 @@ func (s *ActivitylogService) unwrapEvent(e *ehmsg.Event) interface{} {
return einterface
}

func (s *ActivitylogService) getFilters(query string) (*provider.ResourceId, int, func(RawActivity) bool, func(*ehmsg.Event) bool, error) {
func (s *ActivitylogService) getFilters(query string) (*provider.ResourceId, int, func(RawActivity) bool, func(*ehmsg.Event) bool, func([]libregraph.Activity), error) {
qast, err := kql.Builder{}.Build(query)
if err != nil {
return nil, 0, nil, nil, err
return nil, 0, nil, nil, nil, err
}

prefilters := make([]func(RawActivity) bool, 0)
postfilters := make([]func(*ehmsg.Event) bool, 0)

sortby := func(_ []libregraph.Activity) {}

var (
itemID string
limit int
Expand All @@ -234,7 +239,7 @@ func (s *ActivitylogService) getFilters(query string) (*provider.ResourceId, int
case "depth":
depth, err := strconv.Atoi(v.Value)
if err != nil {
return nil, limit, nil, nil, err
return nil, limit, nil, nil, sortby, err
}

prefilters = append(prefilters, func(a RawActivity) bool {
Expand All @@ -243,10 +248,19 @@ func (s *ActivitylogService) getFilters(query string) (*provider.ResourceId, int
case "limit":
l, err := strconv.Atoi(v.Value)
if err != nil {
return nil, limit, nil, nil, err
return nil, limit, nil, nil, sortby, err
}

limit = l
case "sort":
switch v.Value {
case "asc":
// nothing to do - already ascending
case "desc":
sortby = func(activities []libregraph.Activity) {
slices.Reverse(activities)
}
}
}
case *ast.DateTimeNode:
switch v.Operator.Value {
Expand All @@ -261,14 +275,14 @@ func (s *ActivitylogService) getFilters(query string) (*provider.ResourceId, int
}
case *ast.OperatorNode:
if v.Value != "AND" {
return nil, limit, nil, nil, errors.New("only AND operator is supported")
return nil, limit, nil, nil, sortby, errors.New("only AND operator is supported")
}
}
}

rid, err := storagespace.ParseID(itemID)
if err != nil {
return nil, limit, nil, nil, err
return nil, limit, nil, nil, sortby, err
}
if rid.GetOpaqueId() == "" {
// space root requested - fix format
Expand All @@ -290,7 +304,7 @@ func (s *ActivitylogService) getFilters(query string) (*provider.ResourceId, int
}
return true
}
return &rid, limit, pref, postf, nil
return &rid, limit, pref, postf, sortby, nil
}

// returns true if this is just a rename
Expand Down
4 changes: 4 additions & 0 deletions services/activitylog/pkg/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,10 @@ func (a *ActivitylogService) RemoveActivities(rid *provider.ResourceId, toDelete

// RemoveResource removes the resource from the store
func (a *ActivitylogService) RemoveResource(rid *provider.ResourceId) error {
if rid == nil {
return fmt.Errorf("resource id is required")
}

a.lock.Lock()
defer a.lock.Unlock()

Expand Down

0 comments on commit 17df46f

Please sign in to comment.