From b1442787815c1ad916b166a5d333a7fcfdce8acd Mon Sep 17 00:00:00 2001 From: ianmuchyri Date: Mon, 15 Jan 2024 16:48:21 +0300 Subject: [PATCH] rename api endpoint Signed-off-by: ianmuchyri --- ui/api/endpoint.go | 4 +- ui/api/logging.go | 8 ++-- ui/api/metrics.go | 10 ++-- ui/api/transport.go | 21 ++++----- ui/service.go | 96 +++++++++++++++++++------------------- ui/web/template/thing.html | 2 +- 6 files changed, 68 insertions(+), 73 deletions(-) diff --git a/ui/api/endpoint.go b/ui/api/endpoint.go index 2ab75bac..de5dad0a 100644 --- a/ui/api/endpoint.go +++ b/ui/api/endpoint.go @@ -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 } diff --git a/ui/api/logging.go b/ui/api/logging.go index 05f6c3ed..3ff7737b 100644 --- a/ui/api/logging.go +++ b/ui/api/logging.go @@ -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. diff --git a/ui/api/metrics.go b/ui/api/metrics.go index 5ae2d98d..1fdda13a 100644 --- a/ui/api/metrics.go +++ b/ui/api/metrics.go @@ -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. diff --git a/ui/api/transport.go b/ui/api/transport.go index dc6a158a..666c6fea 100644 --- a/ui/api/transport.go +++ b/ui/api/transport.go @@ -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) { @@ -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)) @@ -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, } diff --git a/ui/service.go b/ui/service.go index a2ff8011..08bc5cd4 100644 --- a/ui/service.go +++ b/ui/service.go @@ -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 @@ -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 { @@ -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{ diff --git a/ui/web/template/thing.html b/ui/web/template/thing.html index 875ada54..cf0a48aa 100644 --- a/ui/web/template/thing.html +++ b/ui/web/template/thing.html @@ -23,7 +23,7 @@ Share - + Events