Skip to content

Commit

Permalink
feat: add direction parameter
Browse files Browse the repository at this point in the history
Signed-off-by: Rodney Osodo <[email protected]>
  • Loading branch information
rodneyosodo committed Apr 4, 2024
1 parent 1717398 commit 757a22c
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 10 deletions.
20 changes: 15 additions & 5 deletions api/openapi/event-logs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ paths:
- $ref: "#/components/parameters/with_payload"
- $ref: "#/components/parameters/from"
- $ref: "#/components/parameters/to"
- $ref: "#/components/parameters/dir"
security:
- bearerAuth: []
responses:
Expand Down Expand Up @@ -179,16 +180,13 @@ components:

type:
name: type
description: Type of entity, e.g. user, group, domain, etc.
description: Type of entity, e.g. user, group, thing, etc.
in: path
schema:
type: string
enum:
- user
- group
- domain
- thing
- platform
- thing
required: true
example: user
Expand Down Expand Up @@ -224,7 +222,7 @@ components:
type: string
required: false
example: users.create

with_payload:
name: with_payload
description: Include event payload.
Expand Down Expand Up @@ -254,6 +252,18 @@ components:
required: false
example: 1704974066014408296

dir:
name: dir
description: Sort direction.
in: query
schema:
type: string
enum:
- asc
- desc
required: false
example: desc

responses:
EventPageRes:
description: Data retrieved.
Expand Down
25 changes: 23 additions & 2 deletions eventlogs/api/endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,14 +164,35 @@ func TestListEventsEndpoint(t *testing.T) {
{
desc: "with payload",
token: validToken,
url: validID + "/" + auth.UserType + fmt.Sprintf("?payload=%s", strconv.FormatBool(true)),
url: validID + "/" + auth.UserType + fmt.Sprintf("?with_payload=%s", strconv.FormatBool(true)),
status: http.StatusOK,
svcErr: nil,
},
{
desc: "with invalid payload",
token: validToken,
url: validID + "/" + auth.UserType + "?payload=ten",
url: validID + "/" + auth.UserType + "?with_payload=ten",
status: http.StatusBadRequest,
svcErr: nil,
},
{
desc: "with asc direction",
token: validToken,
url: validID + "/" + auth.UserType + "?dir=asc",
status: http.StatusOK,
svcErr: nil,
},
{
desc: "with desc direction",
token: validToken,
url: validID + "/" + auth.UserType + "?dir=desc",
status: http.StatusOK,
svcErr: nil,
},
{
desc: "with invalid direction",
token: validToken,
url: validID + "/" + auth.UserType + "?dir=ten",
status: http.StatusBadRequest,
svcErr: nil,
},
Expand Down
6 changes: 5 additions & 1 deletion eventlogs/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package api
import (
"github.com/absmach/magistrala/auth"
"github.com/absmach/magistrala/eventlogs"
"github.com/absmach/magistrala/internal/api"
"github.com/absmach/magistrala/internal/apiutil"
)

Expand All @@ -26,12 +27,15 @@ func (req listEventsReq) validate() error {
if req.page.EntityType == "" {
return apiutil.ErrMissingEntityType
}
if req.page.EntityType != auth.UserType && req.page.EntityType != auth.GroupType && req.page.EntityType != auth.ThingType && req.page.EntityType != auth.DomainType && req.page.EntityType != auth.PlatformType {
if req.page.EntityType != auth.UserType && req.page.EntityType != auth.GroupType && req.page.EntityType != auth.ThingType {
return apiutil.ErrInvalidEntityType
}
if req.page.Limit > maxLimitSize {
return apiutil.ErrLimitSize
}
if req.page.Direction != "" && req.page.Direction != api.AscDir && req.page.Direction != api.DescDir {
return apiutil.ErrInvalidDirection
}

return nil
}
5 changes: 5 additions & 0 deletions eventlogs/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ func decodeListEventsReq(_ context.Context, r *http.Request) (interface{}, error
if err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, err)
}
dir, err := apiutil.ReadStringQuery(r, api.DirKey, api.DescDir)
if err != nil {
return nil, errors.Wrap(apiutil.ErrValidation, err)
}

req := listEventsReq{
token: apiutil.ExtractBearerToken(r),
Expand All @@ -91,6 +95,7 @@ func decodeListEventsReq(_ context.Context, r *http.Request) (interface{}, error
From: time.Unix(0, from),
To: time.Unix(0, to),
WithPayload: payload,
Direction: dir,
},
}

Expand Down
1 change: 1 addition & 0 deletions eventlogs/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Page struct {
From time.Time `json:"from,omitempty" db:"from,omitempty"`
To time.Time `json:"to,omitempty" db:"to,omitempty"`
WithPayload bool `json:"with_payload,omitempty"`
Direction string `json:"direction,omitempty"`
}

func (page EventsPage) MarshalJSON() ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion eventlogs/postgres/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (repo *repository) RetrieveAll(ctx context.Context, page eventlogs.Page) (e
if page.WithPayload {
sq += ", payload"
}
q := fmt.Sprintf("SELECT %s FROM events %s ORDER BY occurred_at LIMIT :limit OFFSET :offset;", sq, query)
q := fmt.Sprintf("SELECT %s FROM events %s ORDER BY occurred_at %s LIMIT :limit OFFSET :offset;", sq, query, page.Direction)

rows, err := repo.db.NamedQueryContext(ctx, q, page)
if err != nil {
Expand Down
36 changes: 36 additions & 0 deletions eventlogs/postgres/events_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"fmt"
"math/rand"
"sort"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -226,6 +227,12 @@ func TestEventsRetrieveAll(t *testing.T) {
items = append(items, event)
}

reversedItems := make([]eventlogs.Event, len(items))
copy(reversedItems, items)
sort.Slice(reversedItems, func(i, j int) bool {
return reversedItems[i].OccurredAt.After(reversedItems[j].OccurredAt)
})

cases := []struct {
desc string
page eventlogs.Page
Expand Down Expand Up @@ -417,6 +424,34 @@ func TestEventsRetrieveAll(t *testing.T) {
Events: items[:10],
},
},
{
desc: "with asc direction",
page: eventlogs.Page{
Direction: "asc",
Offset: 0,
Limit: 10,
},
response: eventlogs.EventsPage{
Total: uint64(num),
Offset: 0,
Limit: 10,
Events: items[:10],
},
},
{
desc: "with desc direction",
page: eventlogs.Page{
Direction: "desc",
Offset: 0,
Limit: 10,
},
response: eventlogs.EventsPage{
Total: uint64(num),
Offset: 0,
Limit: 10,
Events: reversedItems[:10],
},
},
{
desc: "with all filters",
page: eventlogs.Page{
Expand All @@ -425,6 +460,7 @@ func TestEventsRetrieveAll(t *testing.T) {
From: items[0].OccurredAt,
To: items[num-1].OccurredAt,
WithPayload: true,
Direction: "asc",
Offset: 0,
Limit: 10,
},
Expand Down
3 changes: 2 additions & 1 deletion internal/api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ func EncodeError(_ context.Context, err error, w http.ResponseWriter) {
errors.Contains(err, apiutil.ErrInvalidQueryParams),
errors.Contains(err, apiutil.ErrValidation),
errors.Contains(err, apiutil.ErrMissingEntityType),
errors.Contains(err, apiutil.ErrInvalidEntityType):
errors.Contains(err, apiutil.ErrInvalidEntityType),
errors.Contains(err, apiutil.ErrInvalidDirection):
w.WriteHeader(http.StatusBadRequest)
case errors.Contains(err, svcerr.ErrAuthentication),
errors.Contains(err, svcerr.ErrLogin),
Expand Down

0 comments on commit 757a22c

Please sign in to comment.