Skip to content

Commit

Permalink
feat: impl porter context control
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Aug 28, 2024
1 parent d047ccd commit 400890b
Show file tree
Hide file tree
Showing 12 changed files with 333 additions and 89 deletions.
29 changes: 15 additions & 14 deletions app/sephirah/cmd/sephirah/wire_gen.go

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

20 changes: 20 additions & 0 deletions app/sephirah/internal/biz/biztiphereth/tiphereth.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package biztiphereth

import (
"context"
"strconv"

"github.com/tuihub/librarian/app/sephirah/internal/client"
"github.com/tuihub/librarian/app/sephirah/internal/model/modelsupervisor"
Expand All @@ -23,6 +24,7 @@ var ProviderSet = wire.NewSet(
NewTiphereth,
NewUserCountCache,
NewPorterInstanceCache,
NewPorterContextCache,
)

type TipherethRepo interface {
Expand All @@ -45,6 +47,7 @@ type TipherethRepo interface {
CreatePorterContext(context.Context, model.InternalID, *modelsupervisor.PorterContext) error
ListPorterContexts(context.Context, model.InternalID, model.Paging) ([]*modelsupervisor.PorterContext, int64, error)
UpdatePorterContext(context.Context, model.InternalID, *modelsupervisor.PorterContext) error
FetchPorterContext(context.Context, model.InternalID) (*modelsupervisor.PorterContext, error)
CreateDevice(context.Context, *modeltiphereth.DeviceInfo) error
ListUserSessions(context.Context, model.InternalID) ([]*modeltiphereth.UserSession, error)
DeleteUserSession(context.Context, model.InternalID, model.InternalID) error
Expand Down Expand Up @@ -185,3 +188,20 @@ func NewPorterInstanceCache(
libcache.WithExpiration(libtime.SevenDays),
)
}

func NewPorterContextCache(
t TipherethRepo,
store libcache.Store,
) *libcache.Map[model.InternalID, modelsupervisor.PorterContext] {
return libcache.NewMap[model.InternalID, modelsupervisor.PorterContext](
store,
"PorterContextCache",
func(k model.InternalID) string {
return strconv.FormatInt(int64(k), 10)
},
func(ctx context.Context, k model.InternalID) (*modelsupervisor.PorterContext, error) {
return t.FetchPorterContext(ctx, k)
},
libcache.WithExpiration(libtime.SevenDays),
)
}
2 changes: 0 additions & 2 deletions app/sephirah/internal/data/internal/converter/ent_to_biz.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,6 @@ type toBizConverter interface { //nolint:unused // used by generator
// goverter:enum:map StatusBlocked UserStatusBlocked
ToBizPorterStatus(porterinstance.Status) modeltiphereth.UserStatus

// goverter:ignore HandleStatus
// goverter:ignore HandleStatusMessage
ToBizPorterContext(*ent.PorterContext) *modelsupervisor.PorterContext
ToBizPorterContextList([]*ent.PorterContext) []*modelsupervisor.PorterContext
// goverter:enum:unknown PorterContextStatusUnspecified
Expand Down
11 changes: 11 additions & 0 deletions app/sephirah/internal/data/tiphereth.go
Original file line number Diff line number Diff line change
Expand Up @@ -534,3 +534,14 @@ func (t tipherethRepo) ListPorterGroups(
}
return pg, nil
}

func (t tipherethRepo) FetchPorterContext(
ctx context.Context,
id model.InternalID,
) (*modelsupervisor.PorterContext, error) {
res, err := t.data.db.PorterContext.Get(ctx, id)
if err != nil {
return nil, err
}
return converter.ToBizPorterContext(res), nil
}
5 changes: 3 additions & 2 deletions app/sephirah/internal/model/converter/biz_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,9 @@ type toPBConverter interface { //nolint:unused // used by generator
// goverter:enum:map PorterConnectionStatusDowngraded PorterConnectionStatus_PORTER_CONNECTION_STATUS_DOWNGRADED
ToPBPorterConnectionStatus(modelsupervisor.PorterConnectionStatus) pb.PorterConnectionStatus

ToPBPorterContext(*modelsupervisor.PorterContext) *pb.PorterContext
ToPBPorterContextList([]*modelsupervisor.PorterContext) []*pb.PorterContext
// goverter:autoMap PorterContext
ToPBPorterContext(*modelsupervisor.PorterContextController) *pb.PorterContext
ToPBPorterContextList([]*modelsupervisor.PorterContextController) []*pb.PorterContext
// goverter:enum:unknown PorterContextStatus_PORTER_CONTEXT_STATUS_UNSPECIFIED
// goverter:enum:map PorterContextStatusUnspecified PorterContextStatus_PORTER_CONTEXT_STATUS_UNSPECIFIED
// goverter:enum:map PorterContextStatusActive PorterContextStatus_PORTER_CONTEXT_STATUS_ACTIVE
Expand Down
20 changes: 9 additions & 11 deletions app/sephirah/internal/model/converter/generated.go

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

24 changes: 15 additions & 9 deletions app/sephirah/internal/model/modelsupervisor/modelsupervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ type PorterInstanceController struct {
ConnectionStatus PorterConnectionStatus
ConnectionStatusMessage string
LastHeartbeat time.Time
LastEnabledContext []model.InternalID
}

type PorterContextController struct {
PorterContext
HandleStatus PorterContextHandleStatus
HandleStatusMessage string
HandlerAddress string
}

type PorterInstance struct {
Expand Down Expand Up @@ -82,15 +90,13 @@ const (
)

type PorterContext struct {
ID model.InternalID
GlobalName string
Region string
ContextJSON string
Name string
Description string
Status PorterContextStatus
HandleStatus PorterContextHandleStatus
HandleStatusMessage string
ID model.InternalID
GlobalName string
Region string
ContextJSON string
Name string
Description string
Status PorterContextStatus
}

type PorterContextStatus int
Expand Down
11 changes: 10 additions & 1 deletion app/sephirah/internal/service/tiphereth.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,18 @@ func (s *LibrarianSephirahServiceService) ListPorterContexts(
if err != nil {
return nil, err
}
res := make([]*modelsupervisor.PorterContextController, len(contexts))
for i := range res {
res[i] = s.s.GetContextController(ctx, contexts[i].ID)
if res[i] == nil {
res[i] = new(modelsupervisor.PorterContextController)
res[i].HandleStatus = modelsupervisor.PorterContextHandleStatusBlocked
}
res[i].PorterContext = *contexts[i]
}
return &pb.ListPorterContextsResponse{
Paging: &librarian.PagingResponse{TotalSize: total},
Contexts: converter.ToPBPorterContextList(contexts),
Contexts: converter.ToPBPorterContextList(res),
}, nil
}

Expand Down
Loading

0 comments on commit 400890b

Please sign in to comment.