Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions manager/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ func getPropletEndpoint(svc manager.Service) endpoint.Endpoint {
}
}

func getPropletSDFEndpoint(svc manager.Service) endpoint.Endpoint {
return func(ctx context.Context, request any) (any, error) {
req, ok := request.(entityReq)
if !ok {
return propletSDFResponse{}, errors.Join(apiutil.ErrValidation, pkgerrors.ErrInvalidData)
}
if err := req.validate(); err != nil {
return propletSDFResponse{}, errors.Join(apiutil.ErrValidation, err)
}

doc, err := svc.GetPropletSDF(ctx, req.id)
if err != nil {
return propletSDFResponse{}, err
}

return propletSDFResponse{Document: doc}, nil
}
}

func deletePropletEndpoint(svc manager.Service) endpoint.Endpoint {
return func(ctx context.Context, request any) (any, error) {
req, ok := request.(entityReq)
Expand Down
18 changes: 18 additions & 0 deletions manager/api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import (

"github.com/absmach/propeller/manager"
"github.com/absmach/propeller/pkg/proplet"
"github.com/absmach/propeller/pkg/sdf"
"github.com/absmach/propeller/pkg/task"
"github.com/absmach/supermq"
)

var (
_ supermq.Response = (*propletResponse)(nil)
_ supermq.Response = (*propletSDFResponse)(nil)
_ supermq.Response = (*listpropletResponse)(nil)
_ supermq.Response = (*taskResponse)(nil)
_ supermq.Response = (*listTaskResponse)(nil)
Expand Down Expand Up @@ -55,6 +57,22 @@ func (w propletResponse) Empty() bool {
return w.deleted
}

type propletSDFResponse struct {
sdf.Document
}

func (r propletSDFResponse) Code() int {
return http.StatusOK
}

func (r propletSDFResponse) Headers() map[string]string {
return map[string]string{}
}

func (r propletSDFResponse) Empty() bool {
return false
}

type listpropletResponse struct {
proplet.PropletPage
}
Expand Down
6 changes: 6 additions & 0 deletions manager/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ func MakeHandler(svc manager.Service, logger *slog.Logger, instanceID string) ht
api.EncodeResponse,
opts...,
), "delete-proplet").ServeHTTP)
r.Get("/sdf", otelhttp.NewHandler(kithttp.NewServer(
getPropletSDFEndpoint(svc),
decodeEntityReq("propletID"),
api.EncodeResponse,
opts...,
), "get-proplet-sdf").ServeHTTP)
r.Get("/metrics", otelhttp.NewHandler(kithttp.NewServer(
getPropletMetricsEndpoint(svc),
decodeMetricsReq("propletID"),
Expand Down
2 changes: 2 additions & 0 deletions manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"

"github.com/absmach/propeller/pkg/proplet"
"github.com/absmach/propeller/pkg/sdf"
"github.com/absmach/propeller/pkg/task"
)

type Service interface {
GetProplet(ctx context.Context, propletID string) (proplet.Proplet, error)
GetPropletSDF(ctx context.Context, propletID string) (sdf.Document, error)
ListProplets(ctx context.Context, offset, limit uint64) (proplet.PropletPage, error)
SelectProplet(ctx context.Context, task task.Task) (proplet.Proplet, error)
DeleteProplet(ctx context.Context, propletID string) error
Expand Down
21 changes: 21 additions & 0 deletions manager/middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/absmach/propeller/manager"
"github.com/absmach/propeller/pkg/proplet"
"github.com/absmach/propeller/pkg/sdf"
"github.com/absmach/propeller/pkg/task"
)

Expand Down Expand Up @@ -42,6 +43,26 @@ func (lm *loggingMiddleware) GetProplet(ctx context.Context, id string) (resp pr
return lm.svc.GetProplet(ctx, id)
}

func (lm *loggingMiddleware) GetPropletSDF(ctx context.Context, id string) (resp sdf.Document, err error) {
defer func(begin time.Time) {
args := []any{
slog.String("duration", time.Since(begin).String()),
slog.Group("proplet",
slog.String("id", id),
),
}
if err != nil {
args = append(args, slog.Any("error", err))
lm.logger.Warn("Get proplet SDF failed", args...)

return
}
lm.logger.Info("Get proplet SDF completed successfully", args...)
}(time.Now())

return lm.svc.GetPropletSDF(ctx, id)
}

func (lm *loggingMiddleware) ListProplets(ctx context.Context, offset, limit uint64) (resp proplet.PropletPage, err error) {
defer func(begin time.Time) {
args := []any{
Expand Down
10 changes: 10 additions & 0 deletions manager/middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/absmach/propeller/manager"
"github.com/absmach/propeller/pkg/proplet"
"github.com/absmach/propeller/pkg/sdf"
"github.com/absmach/propeller/pkg/task"
"github.com/go-kit/kit/metrics"
)
Expand Down Expand Up @@ -35,6 +36,15 @@ func (mm *metricsMiddleware) GetProplet(ctx context.Context, id string) (proplet
return mm.svc.GetProplet(ctx, id)
}

func (mm *metricsMiddleware) GetPropletSDF(ctx context.Context, id string) (sdf.Document, error) {
defer func(begin time.Time) {
mm.counter.With("method", "get-proplet-sdf").Add(1)
mm.latency.With("method", "get-proplet-sdf").Observe(time.Since(begin).Seconds())
}(time.Now())

return mm.svc.GetPropletSDF(ctx, id)
}

func (mm *metricsMiddleware) ListProplets(ctx context.Context, offset, limit uint64) (proplet.PropletPage, error) {
defer func(begin time.Time) {
mm.counter.With("method", "list-proplets").Add(1)
Expand Down
10 changes: 10 additions & 0 deletions manager/middleware/tracing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/absmach/propeller/manager"
"github.com/absmach/propeller/pkg/proplet"
"github.com/absmach/propeller/pkg/sdf"
"github.com/absmach/propeller/pkg/task"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
Expand All @@ -30,6 +31,15 @@ func (tm *tracing) GetProplet(ctx context.Context, id string) (resp proplet.Prop
return tm.svc.GetProplet(ctx, id)
}

func (tm *tracing) GetPropletSDF(ctx context.Context, id string) (resp sdf.Document, err error) {
ctx, span := tm.tracer.Start(ctx, "get-proplet-sdf", trace.WithAttributes(
attribute.String("id", id),
))
defer span.End()

return tm.svc.GetPropletSDF(ctx, id)
}

func (tm *tracing) ListProplets(ctx context.Context, offset, limit uint64) (resp proplet.PropletPage, err error) {
ctx, span := tm.tracer.Start(ctx, "list-proplets", trace.WithAttributes(
attribute.Int64("offset", int64(offset)),
Expand Down
59 changes: 59 additions & 0 deletions manager/mocks/service.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions manager/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/absmach/propeller/pkg/mqtt"
"github.com/absmach/propeller/pkg/proplet"
"github.com/absmach/propeller/pkg/scheduler"
"github.com/absmach/propeller/pkg/sdf"
"github.com/absmach/propeller/pkg/storage"
"github.com/absmach/propeller/pkg/task"
"github.com/google/uuid"
Expand Down Expand Up @@ -112,6 +113,15 @@ func (svc *service) GetProplet(ctx context.Context, propletID string) (proplet.P
return w, nil
}

func (svc *service) GetPropletSDF(ctx context.Context, propletID string) (sdf.Document, error) {
p, err := svc.GetProplet(ctx, propletID)
if err != nil {
return sdf.Document{}, err
}

return sdf.PropletDocument(p), nil
}

func (svc *service) ListProplets(ctx context.Context, offset, limit uint64) (proplet.PropletPage, error) {
proplets, total, err := svc.propletRepo.List(ctx, offset, limit)
if err != nil {
Expand Down
Loading