Skip to content

Commit

Permalink
NOISSUE - Add Fetch chart data functionality (absmach#199)
Browse files Browse the repository at this point in the history
* set up linechart

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

* update chart axes order

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

* fix lint error

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

* update go mod

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

* add fetch data from reader functionality

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

* update content type to json

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

* wrap in a more suitable error

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

* resolve comments

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

* extract page from offset and limit

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

* error renaming for sdk error

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

* fix middleware method naming

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

---------

Signed-off-by: Musilah <[email protected]>
Signed-off-by: ianmuchyri <[email protected]>
Co-authored-by: ianmuchyri <[email protected]>
  • Loading branch information
Musilah and ianmuchyri authored Mar 6, 2024
1 parent 078a1b3 commit adfeca4
Show file tree
Hide file tree
Showing 12 changed files with 301 additions and 91 deletions.
2 changes: 1 addition & 1 deletion cmd/ui/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type config struct {
Port string `env:"MG_UI_PORT" envDefault:"9095"`
InstanceID string `env:"MG_UI_INSTANCE_ID" envDefault:""`
HTTPAdapterURL string `env:"MG_HTTP_ADAPTER_URL" envDefault:"http://localhost:8008"`
ReaderURL string `env:"MG_READER_URL" envDefault:"http://localhost:9007"`
ReaderURL string `env:"MG_READER_URL" envDefault:"http://localhost:9011"`
ThingsURL string `env:"MG_THINGS_URL" envDefault:"http://localhost:9000"`
UsersURL string `env:"MG_USERS_URL" envDefault:"http://localhost:9002"`
HostURL string `env:"MG_UI_HOST_URL" envDefault:"http://localhost:9095"`
Expand Down
2 changes: 1 addition & 1 deletion docker/.env
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
MG_UI_LOG_LEVEL=debug
MG_UI_PORT=9095
MG_HTTP_ADAPTER_URL=http://http-adapter:8008
MG_READER_URL=http://mongodb-reader:9007
MG_READER_URL=http://timescale-reader:9011
MG_THINGS_URL=http://things:9000
MG_USERS_URL=http://users:9002
MG_INVITATIONS_URL=http://invitations:9020
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.21.5

require (
github.com/absmach/agent v0.0.0-20240202075640-cc619e6685c8
github.com/absmach/magistrala v0.14.0
github.com/absmach/magistrala v0.14.1-0.20240305111255-42d433a92f39
github.com/absmach/senml v1.0.5
github.com/caarlos0/env/v10 v10.0.0
github.com/eclipse/paho.mqtt.golang v1.4.3
Expand All @@ -13,9 +13,9 @@ require (
github.com/go-kit/kit v0.13.0
github.com/go-zoo/bone v1.3.0
github.com/golang-jwt/jwt v3.2.2+incompatible
github.com/gorilla/securecookie v1.1.2
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/gorilla/securecookie v1.1.2
github.com/jackc/pgx/v5 v5.5.3
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jmoiron/sqlx v1.3.5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ github.com/absmach/agent v0.0.0-20240202075640-cc619e6685c8 h1:UpXJGOwJ1JAt93/C+
github.com/absmach/agent v0.0.0-20240202075640-cc619e6685c8/go.mod h1:yv3n0/JnLgZFShQlV+d4D14wA7IhGgQwBL5DG+hmTR0=
github.com/absmach/magistrala v0.14.0 h1:ouIYOFwl0RMumBuXr4lGtfcukLGxFpzGAD4XDgrMcGg=
github.com/absmach/magistrala v0.14.0/go.mod h1:7hyZSXwYbXHRBQTBUZ0lgsPw3HlPw+PBQRe+xsr542M=
github.com/absmach/magistrala v0.14.1-0.20240305111255-42d433a92f39 h1:qzE/GO9n1nfaFZ3XoubIDmddr68LQK5QTmjZGPlCKnI=
github.com/absmach/magistrala v0.14.1-0.20240305111255-42d433a92f39/go.mod h1:IiDX6bEARa1CM6rVRwck38DjCAeS2FkBzY37nlKOs6Y=
github.com/absmach/mproxy v0.4.2 h1:u0ORPxSrUknqbVrC+E1MdsCv/7Q5eWNG7clIwOMV5hk=
github.com/absmach/mproxy v0.4.2/go.mod h1:TeXhbHdjihXLVoohSzxvIEFzWu16WDOa91LNduks/N8=
github.com/absmach/senml v1.0.5 h1:zNPRYpGr2Wsb8brAusz8DIfFqemy1a2dNbmMnegY3GE=
Expand Down
4 changes: 2 additions & 2 deletions ui/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ The service is configured using the environment variables presented in the follo
| MG_UI_LOG_LEVEL | Log level for UI (debug, info, warn, error) | debug |
| MG_UI_PORT | Port where UI service is run | 9095 |
| MG_HTTP_ADAPTER_URL | HTTP adapter URL | <http://localhost:8008> |
| MG_READER_URL | Reader URL | <http://localhost:9007> |
| MG_READER_URL | Reader URL | <http://localhost:9011> |
| MG_THINGS_URL | Things URL | <http://localhost:9000> |
| MG_USERS_URL | Users URL | <http://localhost:9002> |
| MG_INVITATIONS_URL | Invitations URL | <http://localhost:9020> |
Expand Down Expand Up @@ -60,7 +60,7 @@ make install
MG_UI_LOG_LEVEL=debug \
MG_UI_PORT=9095 \
MG_HTTP_ADAPTER_URL="http://localhost:8008" \
MG_READER_URL="http://localhost:9007" \
MG_READER_URL="http://localhost:9011" \
MG_THINGS_URL="http://localhost:9000" \
MG_USERS_URL="http://localhost:9002" \
MG_INVITATIONS_URL="http://localhost:9020" \
Expand Down
21 changes: 20 additions & 1 deletion ui/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -1283,7 +1283,7 @@ func readMessagesEndpoint(svc ui.Service) endpoint.Endpoint {
return nil, err
}

res, err := svc.ReadMessages(req.Session, req.channelID, req.thingKey, req.page, req.limit)
res, err := svc.ReadMessages(req.Session, req.channelID, req.thingKey, req.mpgm)
if err != nil {
return nil, err
}
Expand All @@ -1295,6 +1295,25 @@ func readMessagesEndpoint(svc ui.Service) endpoint.Endpoint {
}
}

func FetchChartDataEndpoint(svc ui.Service) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (response interface{}, err error) {
req := request.(readMessagesReq)
if err := req.validate(); err != nil {
return nil, err
}
res, err := svc.FetchChartData(req.Session.AccessToken, req.channelID, req.mpgm)
if err != nil {
return nil, err
}

return uiRes{
code: http.StatusOK,
html: res,
headers: map[string]string{"Content-Type": jsonContentType},
}, nil
}
}

func createBootstrap(svc ui.Service, prefix string) endpoint.Endpoint {
return func(_ context.Context, request interface{}) (response interface{}, err error) {
req := request.(createBootstrapReq)
Expand Down
7 changes: 6 additions & 1 deletion ui/api/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ package api
import "github.com/absmach/magistrala/pkg/errors"

var (
errAuthorization = errors.New("missing or invalid credentials provided")
errInvalidCredentials = errors.New("missing or invalid credentials provided")
errAuthentication = errors.New("failed to perform authentication over the entity")
errAuthorization = errors.New("failed to perform authorization over the entity")
errMissingSecret = errors.New("missing secret")
errMissingIdentity = errors.New("missing entity identity")
errLimitSize = errors.New("invalid limit size")
Expand Down Expand Up @@ -44,4 +45,8 @@ var (
errMissingRole = errors.New("missing role")
errMissingValue = errors.New("missing value")
errCookieDecryption = errors.New("failed to decrypt the cookie")
errMissingFrom = errors.New("missing from time value")
errMissingTo = errors.New("missing to time value")
errInvalidAggregation = errors.New("invalid aggregation value")
errInvalidInterval = errors.New("invalid interval value")
)
26 changes: 22 additions & 4 deletions ui/api/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -1161,13 +1161,12 @@ func (lm *loggingMiddleware) Publish(channelID, thingKey string, message ui.Mess
}

// ReadMessages adds logging middleware to read messages method.
func (lm *loggingMiddleware) ReadMessages(s ui.Session, channelID, thingKey string, page, limit uint64) (b []byte, err error) {
func (lm *loggingMiddleware) ReadMessages(s ui.Session, channelID, thingKey string, mpgm sdk.MessagePageMetadata) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
slog.String("channel_id", channelID),
slog.Uint64("page", page),
slog.Uint64("limit", limit),
slog.Any("page_metadata", mpgm),
}
if err != nil {
args = append(args, slog.Any("error", err))
Expand All @@ -1177,7 +1176,26 @@ func (lm *loggingMiddleware) ReadMessages(s ui.Session, channelID, thingKey stri
lm.logger.Info("Read messages completed successfully", args...)
}(time.Now())

return lm.svc.ReadMessages(s, channelID, thingKey, page, limit)
return lm.svc.ReadMessages(s, channelID, thingKey, mpgm)
}

// FetchChartData adds logging middleware to fetch chart data method.
func (lm *loggingMiddleware) FetchChartData(token string, channelID string, mpgm sdk.MessagePageMetadata) (b []byte, err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
slog.String("channel_id", channelID),
slog.Any("page_metadata", mpgm),
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn("Fetch chart data failed to complete successfully", args...)
return
}
lm.logger.Info("Fetch chart data completed successfully", args...)
}(time.Now())

return lm.svc.FetchChartData(token, channelID, mpgm)
}

// CreateBootstrap adds logging middleware to create bootstrap method.
Expand Down
14 changes: 12 additions & 2 deletions ui/api/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,13 +670,23 @@ func (mm *metricsMiddleware) Publish(channelID, thingKey string, message ui.Mess
}

// ReadMessages adds metrics middleware to read messages method.
func (mm *metricsMiddleware) ReadMessages(s ui.Session, channelID, thingKey string, page, limit uint64) ([]byte, error) {
func (mm *metricsMiddleware) ReadMessages(s ui.Session, channelID, thingKey string, mpgm sdk.MessagePageMetadata) ([]byte, error) {
defer func(begin time.Time) {
mm.counter.With("method", "read_messages").Add(1)
mm.latency.With("method", "read_messages").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.ReadMessages(s, channelID, thingKey, page, limit)
return mm.svc.ReadMessages(s, channelID, thingKey, mpgm)
}

// FetchChartData adds metrics middleware to fetch chart data method.
func (mm *metricsMiddleware) FetchChartData(token string, channelID string, mpgm sdk.MessagePageMetadata) ([]byte, error) {
defer func(begin time.Time) {
mm.counter.With("method", "fetch_chart_data").Add(1)
mm.latency.With("method", "fetch_chart_data").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.FetchChartData(token, channelID, mpgm)
}

// CreateBootstrap adds metrics middleware to create bootstrap method.
Expand Down
Loading

0 comments on commit adfeca4

Please sign in to comment.