Skip to content

Commit

Permalink
rename api endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri committed Jan 15, 2024
1 parent 898cbca commit b144278
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 73 deletions.
4 changes: 2 additions & 2 deletions ui/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -756,14 +756,14 @@ func listThingMembersEndpoint(svc ui.Service) endpoint.Endpoint {
}
}

func listThingEventsEndpoint(svc ui.Service) endpoint.Endpoint {
func listEntityEventsEndpoint(svc ui.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
req := request.(listEntityEventsReq)
if err := req.validate(); err != nil {
return nil, err
}

res, err := svc.ListEvents(req.token, req.entityType, req.entityID, req.page, req.limit)
res, err := svc.ListEntityEvents(req.token, req.entityType, req.entityID, req.page, req.limit)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions ui/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,17 +498,17 @@ func (lm *loggingMiddleware) ListChannelsByThing(token, thingID string, page, li
return lm.svc.ListChannelsByThing(token, thingID, page, limit)
}

// ListEvents adds logging middleware to list events method.
func (lm *loggingMiddleware) ListEvents(token, entityType, entityID string, page, limit uint64) (b []byte, err error) {
// ListEntityEvents adds logging middleware to list entity events method.
func (lm *loggingMiddleware) ListEntityEvents(token, entityType, entityID string, page, limit uint64) (b []byte, err error) {
defer func(begin time.Time) {
message := fmt.Sprintf("Method list_events for thing %s took %s to complete", entityID, time.Since(begin))
message := fmt.Sprintf("Method list_entity_events for thing %s took %s to complete", entityID, time.Since(begin))
if err != nil {
lm.logger.Warn(fmt.Sprintf("%s with error: %s.", message, err))
return
}
lm.logger.Info(fmt.Sprintf("%s without errors.", message))
}(time.Now())
return lm.svc.ListEvents(token, entityType, entityID, page, limit)
return lm.svc.ListEntityEvents(token, entityType, entityID, page, limit)
}

// CreateChannel adds logging middleware to create channel method.
Expand Down
10 changes: 5 additions & 5 deletions ui/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,14 @@ func (mm *metricsMiddleware) ListChannelsByThing(token, thingID string, page, li
return mm.svc.ListChannelsByThing(token, thingID, page, limit)
}

// ListEvents adds metrics middleware to list events method.
func (lm *metricsMiddleware) ListEvents(token, entityType, entityID string, page, limit uint64) (b []byte, err error) {
// ListEntityEvents adds metrics middleware to list entity events method.
func (lm *metricsMiddleware) ListEntityEvents(token, entityType, entityID string, page, limit uint64) (b []byte, err error) {
defer func(begin time.Time) {
lm.counter.With("method", "list_thing_events").Add(1)
lm.latency.With("method", "list_thing_events").Observe(time.Since(begin).Seconds())
lm.counter.With("method", "list_entity_events").Add(1)
lm.latency.With("method", "list_entity_events").Observe(time.Since(begin).Seconds())
}(time.Now())

return lm.svc.ListEvents(token, entityType, entityID, page, limit)
return lm.svc.ListEntityEvents(token, entityType, entityID, page, limit)
}

// CreateChannel adds metrics middleware to create channel method.
Expand Down
21 changes: 8 additions & 13 deletions ui/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,6 @@ func MakeHandler(svc ui.Service, r *chi.Mux, instanceID string) http.Handler {
encodeResponse,
opts...,
).ServeHTTP)

r.Get("/{id}/events", kithttp.NewServer(
listThingEventsEndpoint(svc),
decodeListEntityEventsRequest,
encodeResponse,
opts...,
).ServeHTTP)
})

r.Route("/channels", func(r chi.Router) {
Expand Down Expand Up @@ -746,6 +739,13 @@ func MakeHandler(svc ui.Service, r *chi.Mux, instanceID string) http.Handler {
opts...,
).ServeHTTP)
})

r.Get("/events/{id}/{entity}", kithttp.NewServer(
listEntityEventsEndpoint(svc),
decodeListEntityEventsRequest,
encodeResponse,
opts...,
).ServeHTTP)
})

r.Get("/health", magistrala.Health("ui", instanceID))
Expand Down Expand Up @@ -1852,15 +1852,10 @@ func decodeListEntityEventsRequest(_ context.Context, r *http.Request) (interfac
return nil, err
}

entity, err := readStringQuery(r, entityKey, defKey)
if err != nil {
return nil, err
}

req := listEntityEventsReq{
token: token,
entityID: chi.URLParam(r, "id"),
entityType: entity,
entityType: chi.URLParam(r, "entity"),
page: page,
limit: limit,
}
Expand Down
96 changes: 48 additions & 48 deletions ui/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ type Service interface {
// ListChannelsByThing retrieves a list of channels based on the given thing ID.
ListChannelsByThing(token, thingID string, page, limit uint64) ([]byte, error)
// ListEvents retrieves a list of events based on the given ID.
ListEvents(token, entityType, entityID string, page, limit uint64) ([]byte, error)
ListEntityEvents(token, entityType, entityID string, page, limit uint64) ([]byte, error)

// CreateChannel creates a new channel.
CreateChannel(channel sdk.Channel, token string) error
Expand Down Expand Up @@ -975,53 +975,6 @@ func (us *uiService) ListChannelsByThing(token, thingID string, page, limit uint
return btpl.Bytes(), nil
}

func (us *uiService) ListEvents(token, entityType, entityID string, page, limit uint64) ([]byte, error) {
offset := (page - 1) * limit
pgm := sdk.PageMetadata{
Offset: offset,
Limit: limit,
Direction: "desc",
WithPayload: true,
}

eventsPage, err := us.sdk.Events(pgm, entityID, entityType, token)
if err != nil {
return []byte{}, errors.Wrap(err, ErrFailedRetreive)
}

noOfPages := int(math.Ceil(float64(eventsPage.Total) / float64(limit)))

crumb := breadcrumb{
Previous: thingsActive,
Current: entityID,
}

data := struct {
NavbarActive string
CollapseActive string
Events []sdk.Event
CurrentPage int
Pages int
Limit int
Breadcrumb breadcrumb
}{
thingsActive,
thingsActive,
eventsPage.Events,
int(page),
noOfPages,
int(limit),
crumb,
}

var btpl bytes.Buffer
if err := us.tpls.ExecuteTemplate(&btpl, "events", data); err != nil {
return []byte{}, errors.Wrap(err, ErrExecTemplate)
}

return btpl.Bytes(), nil
}

func (us *uiService) CreateChannel(channel sdk.Channel, token string) error {
_, err := us.sdk.CreateChannel(channel, token)
if err != nil {
Expand Down Expand Up @@ -2339,6 +2292,53 @@ func (us *uiService) DeleteInvitation(token, userID, domainID string) error {
return us.sdk.DeleteInvitation(userID, domainID, token)
}

func (us *uiService) ListEntityEvents(token, entityType, entityID string, page, limit uint64) ([]byte, error) {
offset := (page - 1) * limit
pgm := sdk.PageMetadata{
Offset: offset,
Limit: limit,
Direction: "desc",
WithPayload: true,
}

eventsPage, err := us.sdk.Events(pgm, entityID, entityType, token)
if err != nil {
return []byte{}, errors.Wrap(err, ErrFailedRetreive)
}

noOfPages := int(math.Ceil(float64(eventsPage.Total) / float64(limit)))

crumb := breadcrumb{
Previous: thingsActive,
Current: entityID,
}

data := struct {
NavbarActive string
CollapseActive string
Events []sdk.Event
CurrentPage int
Pages int
Limit int
Breadcrumb breadcrumb
}{
thingsActive,
thingsActive,
eventsPage.Events,
int(page),
noOfPages,
int(limit),
crumb,
}

var btpl bytes.Buffer
if err := us.tpls.ExecuteTemplate(&btpl, "events", data); err != nil {
return []byte{}, errors.Wrap(err, ErrExecTemplate)
}

return btpl.Bytes(), nil
}

func parseTemplates(mfsdk sdk.SDK, templates []string) (tpl *template.Template, err error) {
tpl = template.New("mainflux")
tpl = tpl.Funcs(template.FuncMap{
Expand Down
2 changes: 1 addition & 1 deletion ui/web/template/thing.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<a class="btn body-button" href="/things/{{ .Entity.ID }}/users" role="button">
Share
</a>
<a class="btn body-button" href="/things/{{ .Entity.ID }}/events" role="button">
<a class="btn body-button" href="/events/{{ .Entity.ID }}/thing" role="button">
Events
</a>
</div>
Expand Down

0 comments on commit b144278

Please sign in to comment.