Skip to content

Commit

Permalink
add infinite scroll to dashboards
Browse files Browse the repository at this point in the history
Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri committed Feb 5, 2024
1 parent 8ad76e4 commit 7494403
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 110 deletions.
23 changes: 11 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
module github.com/absmach/magistrala-ui

go 1.21.0
go 1.21.5

require (
github.com/absmach/agent v0.0.0-20240112151723-2e9676fadd46
github.com/absmach/magistrala v0.0.0-20240110171157-9f573850fc4b
github.com/absmach/agent v0.0.0-20240202075640-cc619e6685c8
github.com/absmach/magistrala v0.14.0
github.com/absmach/senml v1.0.5
github.com/caarlos0/env/v9 v9.0.0
github.com/eclipse/paho.mqtt.golang v1.4.3
Expand All @@ -13,7 +13,7 @@ require (
github.com/go-zoo/bone v1.3.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/prometheus/client_golang v1.18.0
golang.org/x/exp v0.0.0-20231006140011-7918f672742d
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a
golang.org/x/sync v0.6.0
google.golang.org/grpc v1.61.0
)
Expand All @@ -28,19 +28,18 @@ require (
github.com/go-logfmt/logfmt v0.6.0 // indirect
github.com/gofrs/uuid v4.4.0+incompatible // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.4.0 // indirect
github.com/gorilla/websocket v1.5.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
github.com/mainflux/export v0.1.1-0.20230724124847-67d0bc7f38cb // indirect
github.com/mainflux/mainflux v0.12.0 // indirect
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 // indirect
github.com/pelletier/go-toml v1.9.5 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.45.0 // indirect
github.com/prometheus/common v0.46.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
github.com/x448/float16 v0.8.4 // indirect
golang.org/x/net v0.18.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20231106174013-bbf56f31fb17 // indirect
google.golang.org/protobuf v1.31.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240116215550-a9fa1716bcac // indirect
google.golang.org/protobuf v1.32.0 // indirect
)
116 changes: 58 additions & 58 deletions go.sum

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions ui/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2033,6 +2033,25 @@ func dashboardsEndpoint(svc ui.Service) endpoint.Endpoint {
}
}

func viewDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(viewDashboardReq)
if err := req.validate(); err != nil {
return nil, err
}

res, err := svc.ViewDashboard(req.token)
if err != nil {
return nil, err
}

return uiRes{
code: http.StatusOK,
html: res,
}, nil
}
}

func extractTokenExpiry(token string) (time.Time, error) {
jwtToken, _, err := new(jwt.Parser).ParseUnverified(token, jwt.MapClaims{})
if err != nil {
Expand Down
32 changes: 14 additions & 18 deletions ui/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,24 +285,6 @@ func (lm *loggingMiddleware) UpdateUserIdentity(token string, user sdk.User) (er
return lm.svc.UpdateUserIdentity(token, user)
}

// UpdateUserOwner adds logging middleware to update user owner method.
func (lm *loggingMiddleware) UpdateUserOwner(token string, user sdk.User) (err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
slog.Group("user", slog.String("id", user.ID), slog.String("owner", user.Owner)),
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn("Update user owner failed to complete successfully", args...)
return
}
lm.logger.Info("Update user owner completed successfully", args...)
}(time.Now())

return lm.svc.UpdateUserOwner(token, user)
}

// UpdateUserRole adds logging middleware to update user role method.
func (lm *loggingMiddleware) UpdateUserRole(token string, user sdk.User) (err error) {
defer func(begin time.Time) {
Expand Down Expand Up @@ -1664,3 +1646,17 @@ func (lm *loggingMiddleware) Dashboards(token string) (b []byte, err error) {

return lm.svc.Dashboards(token)
}

// ViewDashboard adds logging middleware to view dashboard method.
func (lm *loggingMiddleware) ViewDashboard(token string) (b []byte, err error) {
defer func(begin time.Time) {
duration := slog.String("duration", time.Since(begin).String())
if err != nil {
lm.logger.Warn("View dashboard failed to complete successfully", slog.Any("error", err), duration)
return
}
lm.logger.Info("View dashboard completed successfully", duration)
}(time.Now())

return lm.svc.ViewDashboard(token)
}
20 changes: 10 additions & 10 deletions ui/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,6 @@ func (mm *metricsMiddleware) UpdateUserIdentity(token string, user sdk.User) err
return mm.svc.UpdateUserIdentity(token, user)
}

// UpdateUserOwner adds metrics middleware to update user owner method.
func (mm *metricsMiddleware) UpdateUserOwner(token string, user sdk.User) error {
defer func(begin time.Time) {
mm.counter.With("method", "update_user_owner").Add(1)
mm.latency.With("method", "update_user_owner").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.UpdateUserOwner(token, user)
}

// UpdateUserRole adds metrics middleware to update user role method.
func (mm *metricsMiddleware) UpdateUserRole(token string, user sdk.User) error {
defer func(begin time.Time) {
Expand Down Expand Up @@ -948,3 +938,13 @@ func (mm *metricsMiddleware) Dashboards(token string) (b []byte, err error) {

return mm.svc.Dashboards(token)
}

// ViewDashboard adds metrics middleware to view dashboard method.
func (mm *metricsMiddleware) ViewDashboard(token string) (b []byte, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "view_dashboard").Add(1)
mm.latency.With("method", "view_dashboard").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.ViewDashboard(token)
}
11 changes: 11 additions & 0 deletions ui/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1100,3 +1100,14 @@ func (req dashboardsReq) validate() error {
}
return nil
}

type viewDashboardReq struct {
token string
}

func (req viewDashboardReq) validate() error {
if req.token == "" {
return errAuthorization
}
return nil
}
19 changes: 19 additions & 0 deletions ui/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ func MakeHandler(svc ui.Service, r *chi.Mux, instanceID string) http.Handler {
opts...,
).ServeHTTP)

r.Get("/dashboard", kithttp.NewServer(
viewDashboardEndpoint(svc),
decodeViewDashboardRequest,
encodeResponse,
opts...,
).ServeHTTP)

r.Get("/entities", kithttp.NewServer(
getEntitiesEndpoint(svc),
decodeGetEntitiesRequest,
Expand Down Expand Up @@ -802,6 +809,18 @@ func decodeDashboardsRequest(_ context.Context, r *http.Request) (interface{}, e
return req, nil
}

func decodeViewDashboardRequest(_ context.Context, r *http.Request) (interface{}, error) {
token, err := tokenFromCookie(r, "token")
if err != nil {
return nil, err
}
req := viewDashboardReq{
token: token,
}

return req, nil
}

func decodeLoginRequest(_ context.Context, _ *http.Request) (interface{}, error) {
return nil, nil
}
Expand Down
51 changes: 40 additions & 11 deletions ui/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const (
statusAll = "all"
homepageActive = "homepage"
dashboardsActive = "dashboards"
dashboardActive = "dashboard"
usersActive = "users"
userActive = "user"
thingsActive = "things"
Expand Down Expand Up @@ -126,6 +127,7 @@ var (
"invitations",

"dashboard",
"dashboards",
}

chartTemplates = []string{
Expand Down Expand Up @@ -209,8 +211,6 @@ type Service interface {
UpdateUserTags(token string, user sdk.User) error
// UpdateUserIdentity updates the identity of the user with the given ID.
UpdateUserIdentity(token string, user sdk.User) error
// UpdateUserOwner updates the owner of the user with the given ID.
UpdateUserOwner(token string, user sdk.User) error
// UpdateUserRole updates the roles of the user with the given ID.
UpdateUserRole(token string, user sdk.User) error
// EnableUser updates the status of a user with the given ID to enabled.
Expand Down Expand Up @@ -366,6 +366,9 @@ type Service interface {

// Dashboards displays the dashboards page.
Dashboards(token string) ([]byte, error)

// ViewDashboard displays a dashboard page.
ViewDashboard(token string) ([]byte, error)
}

var _ Service = (*uiService)(nil)
Expand Down Expand Up @@ -690,14 +693,6 @@ func (us *uiService) UpdateUserIdentity(token string, user sdk.User) error {
return nil
}

func (us *uiService) UpdateUserOwner(token string, user sdk.User) error {
if _, err := us.sdk.UpdateUserIdentity(user, token); err != nil {
return errors.Wrap(err, ErrFailedUpdate)
}

return nil
}

func (us *uiService) UpdateUserRole(token string, user sdk.User) error {
if _, err := us.sdk.UpdateUserRole(user, token); err != nil {
return errors.Wrap(err, ErrFailedUpdate)
Expand Down Expand Up @@ -2033,6 +2028,23 @@ func (us *uiService) GetEntities(token, entity, entityName, domainID, permission
return []byte{}, errors.Wrap(err, ErrFailedRetreive)
}
items["data"] = domains.Domains
case "dashboards":
type Dashboard struct {
ID int
Name string
Description string
}

// Dummy data for dashboards (replace with your actual data retrieval logic)
dashboards := make([]Dashboard, 0)
for i := 1; i <= 28; i++ {
dashboards = append(dashboards, Dashboard{
ID: i,
Name: fmt.Sprintf("Dashboard %d", i),
Description: fmt.Sprintf("Description for Dashboard %d", i),
})
}
items["data"] = dashboards
}

jsonData, err := json.Marshal(items)
Expand Down Expand Up @@ -2350,7 +2362,7 @@ func (us *uiService) DeleteInvitation(token, userID, domainID string) error {
return us.sdk.DeleteInvitation(userID, domainID, token)
}

func (us *uiService) Dashboards(token string) ([]byte, error) {
func (us *uiService) ViewDashboard(token string) ([]byte, error) {
charts := CreateItem()
data := struct {
NavbarActive string
Expand All @@ -2370,6 +2382,23 @@ func (us *uiService) Dashboards(token string) ([]byte, error) {
return btpl.Bytes(), nil
}

func (us *uiService) Dashboards(token string) ([]byte, error) {
data := struct {
NavbarActive string
CollapseActive string
}{
dashboardActive,
dashboardActive,
}

var btpl bytes.Buffer
if err := us.tpls.ExecuteTemplate(&btpl, "dashboards", 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
Loading

0 comments on commit 7494403

Please sign in to comment.