Skip to content

Commit

Permalink
fix: test new logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Aug 8, 2024
1 parent 2ee9464 commit d047ccd
Show file tree
Hide file tree
Showing 10 changed files with 104 additions and 150 deletions.
2 changes: 1 addition & 1 deletion app/sephirah/cmd/sephirah/wire_gen.go

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

43 changes: 29 additions & 14 deletions app/sephirah/internal/data/tiphereth.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,11 @@ func (t tipherethRepo) UpsertPorters(ctx context.Context, il []*modelsupervisor.
SetBuildVersion(instance.BinarySummary.BuildVersion).
SetBuildDate(instance.BinarySummary.BuildDate).
SetGlobalName(instance.GlobalName).
SetRegion(instance.Region).
SetAddress(instance.Address).
SetStatus(converter.ToEntPorterInstanceStatus(instance.Status)).
SetFeatureSummary(instance.FeatureSummary)
SetFeatureSummary(instance.FeatureSummary).
SetContextJSONSchema(instance.ContextJSONSchema)
}
return t.data.db.PorterInstance.
CreateBulk(instances...).
Expand Down Expand Up @@ -430,6 +432,7 @@ func (t tipherethRepo) CreatePorterContext(
context *modelsupervisor.PorterContext,
) error {
return t.data.db.PorterContext.Create().
SetID(context.ID).
SetOwnerID(userID).
SetGlobalName(context.GlobalName).
SetRegion(context.Region).
Expand Down Expand Up @@ -482,37 +485,49 @@ func (t tipherethRepo) ListPorterGroups(
ctx context.Context,
status []modeltiphereth.UserStatus,
) ([]*modelsupervisor.PorterGroup, error) {
var pi []*ent.PorterInstance
var pg []*modelsupervisor.PorterGroup
pgm := make(map[string]*modelsupervisor.PorterGroup)
var res []struct {
ent.PorterInstance
Min model.InternalID
}
q := t.data.db.PorterInstance.Query()
if len(status) > 0 {
q.Where(porterinstance.StatusIn(converter.ToEntPorterInstanceStatusList(status)...))
}
err := q.GroupBy(
porterinstance.FieldGlobalName,
porterinstance.FieldRegion,
).Scan(ctx, &pi)
).
Aggregate(ent.Min(porterinstance.FieldID)).
Scan(ctx, &res)
if err != nil {
return nil, err
}
var ids []model.InternalID
for _, p := range res {
ids = append(ids, p.Min)
}
pi, err := t.data.db.PorterInstance.Query().Where(
porterinstance.IDIn(ids...),
).All(ctx)
if err != nil {
return nil, err
}
var pg []*modelsupervisor.PorterGroup
pgm := make(map[string]*modelsupervisor.PorterGroup)
for _, p := range pi {
if len(p.ContextJSONSchema) == 0 {
continue
}
if pgm[p.GlobalName] == nil {
pgm[p.GlobalName] = &modelsupervisor.PorterGroup{
BinarySummary: &modelsupervisor.PorterBinarySummary{
Name: p.Name,
Version: p.Version,
Description: p.Description,
SourceCodeAddress: p.SourceCodeAddress,
BuildVersion: p.BuildVersion,
BuildDate: p.BuildDate,
},
BinarySummary: converter.ToBizPorter(p).BinarySummary,
GlobalName: p.GlobalName,
Regions: []string{p.Region},
ContextJSONSchema: p.ContextJSONSchema,
}
} else {
pgm[p.GlobalName].Regions = append(pgm[p.GlobalName].Regions, p.Region)
}
pgm[p.GlobalName].Regions = append(pgm[p.GlobalName].Regions, p.Region)
}
for _, v := range pgm {
pg = append(pg, v)
Expand Down
1 change: 1 addition & 0 deletions app/sephirah/internal/model/converter/biz_to_pb.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type toPBConverter interface { //nolint:unused // used by generator
ToPBInternalIDList([]model.InternalID) []*librarian.InternalID
ToPBServerFeatureSummary(*modelsupervisor.ServerFeatureSummary) *pb.ServerFeatureSummary
ToPBFeatureFlag(*modelsupervisor.FeatureFlag) *librarian.FeatureFlag
ToPBFeatureFlagList([]*modelsupervisor.FeatureFlag) []*librarian.FeatureFlag
ToPBFeatureRequest(*modelsupervisor.FeatureRequest) *librarian.FeatureRequest

// goverter:map ID DeviceId
Expand Down
45 changes: 15 additions & 30 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.

57 changes: 21 additions & 36 deletions app/sephirah/internal/service/librariansephirahservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/tuihub/librarian/internal/conf"
"github.com/tuihub/librarian/internal/lib/libapp"
"github.com/tuihub/librarian/internal/lib/libauth"
"github.com/tuihub/librarian/internal/lib/libcache"
pb "github.com/tuihub/protos/pkg/librarian/sephirah/v1"

"google.golang.org/protobuf/types/known/timestamppb"
Expand All @@ -35,7 +34,7 @@ type LibrarianSephirahServiceService struct {
app *libapp.Settings
auth *libauth.Auth
authFunc func(context.Context) (context.Context, error)
info *libcache.Key[pb.GetServerInformationResponse]
info *pb.ServerInstanceSummary
}

func NewLibrarianSephirahServiceService(
Expand All @@ -51,7 +50,6 @@ func NewLibrarianSephirahServiceService(
auth *libauth.Auth,
authFunc func(context.Context) (context.Context, error),
config *conf.SephirahServer,
store libcache.Store,
) pb.LibrarianSephirahServiceServer {
t.CreateConfiguredAdmin()
if config == nil {
Expand All @@ -74,47 +72,34 @@ func NewLibrarianSephirahServiceService(
authFunc: authFunc,
info: nil,
}
res.info = newServerInfromationCache(res, store, &pb.ServerInstanceSummary{
res.info = &pb.ServerInstanceSummary{
Name: config.GetInfo().GetName(),
Description: config.GetInfo().GetDescription(),
WebsiteUrl: config.GetInfo().GetWebsiteUrl(),
LogoUrl: config.GetInfo().GetLogoUrl(),
BackgroundUrl: config.GetInfo().GetBackgroundUrl(),
})
}
return res
}

func newServerInfromationCache(
s *LibrarianSephirahServiceService,
store libcache.Store,
serverSummary *pb.ServerInstanceSummary,
) *libcache.Key[pb.GetServerInformationResponse] {
return libcache.NewKey[pb.GetServerInformationResponse](
store,
"GetServerInformationResponse",
func(ctx context.Context) (*pb.GetServerInformationResponse, error) {
featureSummary := s.s.GetFeatureSummary()
featureSummary.FeedItemActions = append(featureSummary.FeedItemActions, s.y.GetBuiltInFeedActions()...)
return &pb.GetServerInformationResponse{
ServerBinarySummary: &pb.ServerBinarySummary{
SourceCodeAddress: s.app.SourceCodeAddress,
BuildVersion: s.app.Version,
BuildDate: s.app.BuildDate,
},
ProtocolSummary: &pb.ServerProtocolSummary{
Version: s.app.ProtoVersion,
},
CurrentTime: timestamppb.New(time.Now()),
FeatureSummary: converter.ToPBServerFeatureSummary(featureSummary),
ServerInstanceSummary: serverSummary,
StatusReport: nil,
}, nil
},
libcache.WithExpiration(time.Minute),
)
}

func (s *LibrarianSephirahServiceService) GetServerInformation(ctx context.Context,
_ *pb.GetServerInformationRequest) (*pb.GetServerInformationResponse, error) {
return s.info.Get(ctx)
featureSummary := converter.ToPBServerFeatureSummary(s.s.GetFeatureSummary())
featureSummary.FeedItemActions = append(featureSummary.FeedItemActions,
converter.ToPBFeatureFlagList(s.y.GetBuiltInFeedActions())...,
)
return &pb.GetServerInformationResponse{
ServerBinarySummary: &pb.ServerBinarySummary{
SourceCodeAddress: s.app.SourceCodeAddress,
BuildVersion: s.app.Version,
BuildDate: s.app.BuildDate,
},
ProtocolSummary: &pb.ServerProtocolSummary{
Version: s.app.ProtoVersion,
},
CurrentTime: timestamppb.New(time.Now()),
FeatureSummary: featureSummary,
ServerInstanceSummary: s.info,
StatusReport: nil,
}, nil
}
2 changes: 1 addition & 1 deletion app/sephirah/internal/service/tiphereth.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,9 +268,9 @@ func (s *LibrarianSephirahServiceService) ListPorters(ctx context.Context, req *
res[i] = s.s.GetInstanceController(ctx, porters[i].Address)
if res[i] == nil {
res[i] = new(modelsupervisor.PorterInstanceController)
res[i].PorterInstance = *porters[i]
res[i].ConnectionStatus = modelsupervisor.PorterConnectionStatusDisconnected
}
res[i].PorterInstance = *porters[i]
}
return &pb.ListPortersResponse{
Paging: &librarian.PagingResponse{TotalSize: total},
Expand Down
93 changes: 28 additions & 65 deletions app/sephirah/internal/supervisor/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"

"github.com/tuihub/librarian/app/sephirah/internal/model/modelsupervisor"
"github.com/tuihub/librarian/app/sephirah/internal/model/modeltiphereth"
"github.com/tuihub/librarian/internal/lib/libtype"
)

Expand All @@ -13,95 +12,59 @@ func (s *Supervisor) GetFeatureSummary() *modelsupervisor.ServerFeatureSummary {
defer s.featureSummaryRWMu.RUnlock()
featureSummary := new(modelsupervisor.ServerFeatureSummary)
if s.featureSummary != nil {
_ = libtype.DeepCopyStruct(s.featureSummary, &featureSummary)
_ = libtype.DeepCopyStruct(s.featureSummary, featureSummary)
}
return s.featureSummary
return featureSummary
}

func (s *Supervisor) updateFeatureSummary(ctx context.Context) {
func (s *Supervisor) updateFeatureSummary(_ context.Context) {
s.featureSummaryRWMu.Lock()
defer s.featureSummaryRWMu.Unlock()

var instances []*modelsupervisor.PorterInstance
s.instanceController.Range(func(key string, controller modelsupervisor.PorterInstanceController) bool {
ins, err := s.instanceCache.Get(ctx, key)
if err == nil && ins != nil && ins.Status == modeltiphereth.UserStatusActive {
instances = append(instances, ins)
if controller.ConnectionStatus == modelsupervisor.PorterConnectionStatusActive {
instances = append(instances, &controller.PorterInstance)
}
return true
})

featureSummary, featureSummaryMap := summarize(instances)

s.featureSummaryRWMu.Lock()
defer s.featureSummaryRWMu.Unlock()
s.featureSummary = featureSummary
s.featureSummaryMap = featureSummaryMap
}

func summarize( //nolint:gocognit // how?
func summarize(
instances []*modelsupervisor.PorterInstance,
) (*modelsupervisor.ServerFeatureSummary, *modelsupervisor.ServerFeatureSummaryMap) {
res := new(modelsupervisor.ServerFeatureSummary)
resMap := modelsupervisor.NewServerFeatureSummaryMap()

supportedAccountPlatforms := make(map[string]bool)
supportedAppSources := make(map[string]bool)
supportedFeedSources := make(map[string]bool)
supportedNotifyDestinations := make(map[string]bool)
for _, ins := range instances {
if ins == nil {
continue
}
for _, account := range ins.FeatureSummary.AccountPlatforms {
a, _ := resMap.AccountPlatforms.Load(ins.Address)
if a == nil {
a = []string{}
}
a = append(a, account.ID)
resMap.AccountPlatforms.Store(ins.Address, a)
if supportedAccountPlatforms[account.ID] {
continue
}
res.AccountPlatforms = append(res.AccountPlatforms, account)
supportedAccountPlatforms[account.ID] = true
}
for _, appSource := range ins.FeatureSummary.AppInfoSources {
a, _ := resMap.AppInfoSources.Load(ins.Address)
if a == nil {
a = []string{}
}
a = append(a, appSource.ID)
resMap.AppInfoSources.Store(ins.Address, a)
if supportedAppSources[appSource.ID] {
continue
}
res.AppInfoSources = append(res.AppInfoSources, appSource)
supportedAppSources[appSource.ID] = true
}
for _, feedSource := range ins.FeatureSummary.FeedSources {
a, _ := resMap.FeedSources.Load(ins.Address)
if a == nil {
a = []string{}
}
a = append(a, feedSource.ID)
resMap.FeedSources.Store(ins.Address, a)
if supportedFeedSources[feedSource.ID] {
continue
}
res.FeedSources = append(res.FeedSources, feedSource)
supportedFeedSources[feedSource.ID] = true
}
for _, notifyDestination := range ins.FeatureSummary.NotifyDestinations {
a, _ := resMap.NotifyDestinations.Load(ins.Address)
if a == nil {
a = []string{}
}
a = append(a, notifyDestination.ID)
resMap.NotifyDestinations.Store(ins.Address, a)
if supportedNotifyDestinations[notifyDestination.ID] {
continue
do := func(flags []*modelsupervisor.FeatureFlag, resMap *libtype.SyncMap[string, []string], res []*modelsupervisor.FeatureFlag) {
markMap := make(map[string]bool)
for _, flag := range flags {
a, _ := resMap.Load(flag.ID)
if a == nil {
a = []string{}
}
a = append(a, ins.Address)
resMap.Store(flag.ID, a)
if markMap[flag.ID] {
continue
}
res = append(res, flag)
markMap[flag.ID] = true
}
res.NotifyDestinations = append(res.NotifyDestinations, notifyDestination)
supportedNotifyDestinations[notifyDestination.ID] = true
}
do(ins.FeatureSummary.AccountPlatforms, resMap.AccountPlatforms, res.AccountPlatforms)
do(ins.FeatureSummary.AppInfoSources, resMap.AppInfoSources, res.AppInfoSources)
do(ins.FeatureSummary.FeedSources, resMap.FeedSources, res.FeedSources)
do(ins.FeatureSummary.NotifyDestinations, resMap.NotifyDestinations, res.NotifyDestinations)
do(ins.FeatureSummary.FeedItemActions, resMap.FeedItemActions, res.FeedItemActions)
}
return res, resMap
}
Loading

0 comments on commit d047ccd

Please sign in to comment.