Skip to content

Commit

Permalink
MG-99 - Facilitate listing of all dashboards (#108)
Browse files Browse the repository at this point in the history
* add infinite scroll to dashboards

Signed-off-by: ianmuchyri <[email protected]>

* remove duplicate functions

Signed-off-by: ianmuchyri <[email protected]>

* enable creating and saving dashboards

Enable a user to create their dashboard and save
it to the database. The user can also view the
dashboard.

Signed-off-by: ianmuchyri <[email protected]>

* implement infinite scroll for dashboards

Signed-off-by: ianmuchyri <[email protected]>

* add edit dashboard capability

Signed-off-by: ianmuchyri <[email protected]>

* update dashboard page

Signed-off-by: ianmuchyri <[email protected]>

* move scripts to a new file dashboards.js

Signed-off-by: ianmuchyri <[email protected]>

* remove name flag in docker-compose.yml file

Signed-off-by: ianmuchyri <[email protected]>

* fix create dashboard running twice

Signed-off-by: ianmuchyri <[email protected]>

---------

Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri authored and dborovcanin committed Feb 9, 2024
1 parent d0a393f commit 3484286
Show file tree
Hide file tree
Showing 14 changed files with 787 additions and 255 deletions.
26 changes: 23 additions & 3 deletions ui/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2080,7 +2080,7 @@ func viewDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return nil, err
}

res, err := svc.ViewDashboard(req.token, req.ID)
res, err := svc.ViewDashboard(req.token, req.DashboardID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -2109,8 +2109,9 @@ func createDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return nil, err
}
return uiRes{
code: http.StatusCreated,
html: res,
code: http.StatusCreated,
html: res,
headers: map[string]string{"Content-Type": jsonContentType},
}, nil
}
}
Expand All @@ -2135,6 +2136,25 @@ func listDashboardsEndpoint(svc ui.Service) endpoint.Endpoint {
}
}

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

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

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

func updateDashboardEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (interface{}, error) {
req := request.(updateDashboardReq)
Expand Down
24 changes: 19 additions & 5 deletions ui/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -1661,7 +1661,7 @@ func (lm *loggingMiddleware) DeleteInvitation(token, userID, domainID string) (e
return lm.svc.DeleteInvitation(token, userID, domainID)
}

// View Dashboard adds logging middleware to view dashboard method.
// ViewDashboard adds logging middleware to view dashboard method.
func (lm *loggingMiddleware) ViewDashboard(token, dashboardID string) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
Expand All @@ -1679,7 +1679,7 @@ func (lm *loggingMiddleware) ViewDashboard(token, dashboardID string) (b []byte,
return lm.svc.ViewDashboard(token, dashboardID)
}

// Create Dashboard adds logging middleware to create dashboard method.
// CreateDashboard adds logging middleware to create dashboard method.
func (lm *loggingMiddleware) CreateDashboard(token string, dashboardReq ui.DashboardReq) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
Expand All @@ -1698,7 +1698,7 @@ func (lm *loggingMiddleware) CreateDashboard(token string, dashboardReq ui.Dashb
return lm.svc.CreateDashboard(token, dashboardReq)
}

// List Dashboards adds logging middleware to list dashboards method.
// ListDashboards adds logging middleware to list dashboards method.
func (lm *loggingMiddleware) ListDashboards(token string, page, limit uint64) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
Expand All @@ -1717,7 +1717,21 @@ func (lm *loggingMiddleware) ListDashboards(token string, page, limit uint64) (b
return lm.svc.ListDashboards(token, page, limit)
}

// Update Dashboard adds logging middleware to update dashboard method.
// Dashboards adds logging middleware to dashboards method.
func (lm *loggingMiddleware) Dashboards(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("Dashboards failed to complete successfully", slog.Any("error", err), duration)
return
}
lm.logger.Info("Dashboards completed successfully", duration)
}(time.Now())

return lm.svc.Dashboards(token)
}

// UpdateDashboard adds logging middleware to update dashboard method.
func (lm *loggingMiddleware) UpdateDashboard(token, dashboardID string, dashboardReq ui.DashboardReq) (err error) {
defer func(begin time.Time) {
args := []any{
Expand All @@ -1737,7 +1751,7 @@ func (lm *loggingMiddleware) UpdateDashboard(token, dashboardID string, dashboar
return lm.svc.UpdateDashboard(token, dashboardID, dashboardReq)
}

// Delete Dashboard adds logging middleware to delete dashboard method.
// DeleteDashboard adds logging middleware to delete dashboard method.
func (lm *loggingMiddleware) DeleteDashboard(token, dashboardID string) (err error) {
defer func(begin time.Time) {
args := []any{
Expand Down
20 changes: 15 additions & 5 deletions ui/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -949,7 +949,7 @@ func (mm *metricsMiddleware) DeleteInvitation(token, userID, domainID string) er
return mm.svc.DeleteInvitation(token, userID, domainID)
}

// View Dashboard adds metrics middleware to view dashboard method.
// ViewDashboard adds metrics middleware to view dashboard method.
func (mm *metricsMiddleware) ViewDashboard(token string, dashboardID string) (b []byte, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "view_dashboard").Add(1)
Expand All @@ -959,7 +959,7 @@ func (mm *metricsMiddleware) ViewDashboard(token string, dashboardID string) (b
return mm.svc.ViewDashboard(token, dashboardID)
}

// Create Dashboard adds metrics middleware to create dashboard method.
// CreateDashboard adds metrics middleware to create dashboard method.
func (mm *metricsMiddleware) CreateDashboard(token string, dashboardReq ui.DashboardReq) (b []byte, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "create_dashboard").Add(1)
Expand All @@ -969,7 +969,7 @@ func (mm *metricsMiddleware) CreateDashboard(token string, dashboardReq ui.Dashb
return mm.svc.CreateDashboard(token, dashboardReq)
}

// List Dashboards adds metrics middleware to list dashboards method.
// ListDashboards adds metrics middleware to list dashboards method.
func (mm *metricsMiddleware) ListDashboards(token string, page uint64, limit uint64) (b []byte, err error) {
defer func(begin time.Time) {
mm.counter.With("method", "list_dashboards").Add(1)
Expand All @@ -979,7 +979,17 @@ func (mm *metricsMiddleware) ListDashboards(token string, page uint64, limit uin
return mm.svc.ListDashboards(token, page, limit)
}

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

return mm.svc.Dashboards(token)
}

// UpdateDashboard adds metrics middleware to update dashboard method.
func (mm *metricsMiddleware) UpdateDashboard(token, dashboardID string, dashboardReq ui.DashboardReq) (err error) {
defer func(begin time.Time) {
mm.counter.With("method", "update_dashboard").Add(1)
Expand All @@ -989,7 +999,7 @@ func (mm *metricsMiddleware) UpdateDashboard(token, dashboardID string, dashboar
return mm.svc.UpdateDashboard(token, dashboardID, dashboardReq)
}

// Delete Dashboard adds metrics middleware to delete dashboard method.
// DeleteDashboard adds metrics middleware to delete dashboard method.
func (mm *metricsMiddleware) DeleteDashboard(token string, dashboardID string) (err error) {
defer func(begin time.Time) {
mm.counter.With("method", "delete_dashboard").Add(1)
Expand Down
15 changes: 13 additions & 2 deletions ui/api/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -1091,8 +1091,8 @@ func (req listInvitationsReq) validate() error {
}

type viewDashboardReq struct {
token string
ID string `json:"id"`
token string
DashboardID string `json:"dashboard_id"`
}

func (req viewDashboardReq) validate() error {
Expand Down Expand Up @@ -1132,6 +1132,17 @@ func (req listDashboardsReq) validate() error {
return nil
}

type dashboardsReq struct {
token string
}

func (req dashboardsReq) validate() error {
if req.token == "" {
return errAuthorization
}
return nil
}

type updateDashboardReq struct {
token string
ID string `json:"id"`
Expand Down
54 changes: 36 additions & 18 deletions ui/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,15 @@ func MakeHandler(svc ui.Service, r *chi.Mux, instanceID string) http.Handler {
encodeResponse,
opts...,
).ServeHTTP)
r.Get("/", kithttp.NewServer(
r.Get("/list", kithttp.NewServer(
listDashboardsEndpoint(svc),
decodelistDashboardsRequest,
decodeListDashboardsRequest,
encodeResponse,
opts...,
).ServeHTTP)
r.Get("/", kithttp.NewServer(
dashboardsEndpoint(svc),
decodeDashboardRequest,
encodeResponse,
opts...,
).ServeHTTP)
Expand Down Expand Up @@ -832,20 +838,6 @@ func decodeViewRegistrationRequest(_ context.Context, _ *http.Request) (interfac
return nil, 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,
ID: chi.URLParam(r, "id"),
}

return req, nil
}

func decodeCreateDashboardRequest(_ context.Context, r *http.Request) (interface{}, error) {
token, err := tokenFromCookie(r, "token")
if err != nil {
Expand All @@ -862,13 +854,12 @@ func decodeCreateDashboardRequest(_ context.Context, r *http.Request) (interface
token: token,
Name: data.Name,
Description: data.Description,
Layout: data.Layout,
}

return req, nil
}

func decodelistDashboardsRequest(_ context.Context, r *http.Request) (interface{}, error) {
func decodeListDashboardsRequest(_ context.Context, r *http.Request) (interface{}, error) {
token, err := tokenFromCookie(r, "token")
if err != nil {
return nil, err
Expand All @@ -891,6 +882,19 @@ func decodelistDashboardsRequest(_ context.Context, r *http.Request) (interface{
return req, nil
}

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

req := dashboardsReq{
token: token,
}

return req, nil
}

func decodeUpdateDashboardRequest(_ context.Context, r *http.Request) (interface{}, error) {
token, err := tokenFromCookie(r, "token")
if err != nil {
Expand Down Expand Up @@ -931,6 +935,20 @@ func decodeDeleteDashboardRequest(_ context.Context, r *http.Request) (interface
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,
DashboardID: chi.URLParam(r, "id"),
}

return req, nil
}

func decodeLoginRequest(_ context.Context, _ *http.Request) (interface{}, error) {
return nil, nil
}
Expand Down
2 changes: 1 addition & 1 deletion ui/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ type Dashboard struct {
ID string `json:"id" db:"id"`
CreatedBy string `json:"created_by" db:"created_by"`
Name string `json:"name" db:"name"`
Description string `json:"description,omitempty" db:"description"`
Description string `json:"description" db:"description"`
Layout string `json:"layout" db:"layout"`
CreatedAt time.Time `json:"created_at" db:"created_at"`
UpdatedAt time.Time `json:"updated_at,omitempty" db:"updated_at"`
Expand Down
Loading

0 comments on commit 3484286

Please sign in to comment.