From 8dd25063ef3f7805b822f676bd842bdd8ff230d8 Mon Sep 17 00:00:00 2001 From: jkoberg Date: Wed, 26 Jun 2024 16:29:04 +0200 Subject: [PATCH] feat(activitylog): allow sorting results Signed-off-by: jkoberg --- services/activitylog/pkg/service/http.go | 30 +++++++++++++++++------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/services/activitylog/pkg/service/http.go b/services/activitylog/pkg/service/http.go index 9c2388f9fe9..af203113eea 100644 --- a/services/activitylog/pkg/service/http.go +++ b/services/activitylog/pkg/service/http.go @@ -6,6 +6,7 @@ import ( "errors" "net/http" "path/filepath" + "slices" "strconv" "strings" "time" @@ -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())) @@ -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") @@ -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 @@ -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 { @@ -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 { @@ -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 @@ -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