Skip to content

Commit

Permalink
Add more comments to retrieving latest version path with nodes (#50)
Browse files Browse the repository at this point in the history
Co-authored-by: James Kwon <[email protected]>
  • Loading branch information
james03160927 and james03160927 authored Jun 20, 2024
1 parent a4c3f9d commit 231fc69
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 37 deletions.
2 changes: 1 addition & 1 deletion integration-tests/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func withMiddleware[R any, S any](mw drip.StrictMiddlewareFunc, h func(ctx conte
return h(ctx.Request().Context(), request.(R))
}

nameA := strings.Split((runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name()), ".")
nameA := strings.Split(runtime.FuncForPC(reflect.ValueOf(h).Pointer()).Name(), ".")
nameA = strings.Split(nameA[len(nameA)-1], "-")
opname := nameA[0]

Expand Down
24 changes: 6 additions & 18 deletions mapper/node_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,38 +51,26 @@ func DbNodeVersionToApiNodeVersion(dbNodeVersion *ent.NodeVersion) *drip.NodeVer
if dbNodeVersion == nil {
return nil
}
id := dbNodeVersion.ID.String()

if dbNodeVersion.Edges.StorageFile == nil {
return &drip.NodeVersion{
Id: &id,
Version: &dbNodeVersion.Version,
Changelog: &dbNodeVersion.Changelog,
Deprecated: &dbNodeVersion.Deprecated,
Dependencies: &dbNodeVersion.PipDependencies,
CreatedAt: &dbNodeVersion.CreateTime,
Status: DbNodeVersionStatusToApiNodeVersionStatus(dbNodeVersion.Status),
StatusReason: &dbNodeVersion.StatusReason,
}
}

id := dbNodeVersion.ID.String()
var downloadUrl string
status := DbNodeVersionStatusToApiNodeVersionStatus(dbNodeVersion.Status)
downloadUrl := ""
if dbNodeVersion.Status == schema.NodeVersionStatusActive {
if dbNodeVersion.Edges.StorageFile != nil && dbNodeVersion.Status == schema.NodeVersionStatusActive {
downloadUrl = dbNodeVersion.Edges.StorageFile.FileURL
}

return &drip.NodeVersion{
apiVersion := &drip.NodeVersion{
Id: &id,
Version: &dbNodeVersion.Version,
Changelog: &dbNodeVersion.Changelog,
DownloadUrl: &downloadUrl,
Deprecated: &dbNodeVersion.Deprecated,
Dependencies: &dbNodeVersion.PipDependencies,
CreatedAt: &dbNodeVersion.CreateTime,
Status: status,
StatusReason: &dbNodeVersion.StatusReason,
DownloadUrl: &downloadUrl,
}
return apiVersion
}

func CheckValidSemv(version string) bool {
Expand Down
15 changes: 12 additions & 3 deletions server/implementation/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ func (s *DripStrictServerImplementation) ListNodesForPublisher(

func (s *DripStrictServerImplementation) ListAllNodes(
ctx context.Context, request drip.ListAllNodesRequestObject) (drip.ListAllNodesResponseObject, error) {

log.Ctx(ctx).Info().Msg("ListAllNodes request received")

// Set default values for pagination parameters
Expand All @@ -255,18 +256,21 @@ func (s *DripStrictServerImplementation) ListAllNodes(
if request.Params.Limit != nil {
limit = *request.Params.Limit
}
f := &drip_services.NodeFilter{}

// Initialize the node filter
filter := &drip_services.NodeFilter{}
if request.Params.IncludeBanned != nil {
f.IncludeBanned = *request.Params.IncludeBanned
filter.IncludeBanned = *request.Params.IncludeBanned
}

// List nodes from the registry service
nodeResults, err := s.RegistryService.ListNodes(ctx, s.Client, page, limit, f)
nodeResults, err := s.RegistryService.ListNodes(ctx, s.Client, page, limit, filter)
if err != nil {
log.Ctx(ctx).Error().Msgf("Failed to list nodes w/ err: %v", err)
return drip.ListAllNodes500JSONResponse{Message: "Failed to list nodes", Error: err.Error()}, err
}

// Handle case when no nodes are found
if len(nodeResults.Nodes) == 0 {
log.Ctx(ctx).Info().Msg("No nodes found")
return drip.ListAllNodes200JSONResponse{
Expand All @@ -278,9 +282,12 @@ func (s *DripStrictServerImplementation) ListAllNodes(
}, nil
}

// Convert database nodes to API nodes
apiNodes := make([]drip.Node, 0, len(nodeResults.Nodes))
for _, dbNode := range nodeResults.Nodes {
apiNode := mapper.DbNodeToApiNode(dbNode)

// Fetch the latest version if available
if dbNode.Edges.Versions != nil && len(dbNode.Edges.Versions) > 0 {
latestVersion, err := s.RegistryService.GetLatestNodeVersion(ctx, s.Client, dbNode.ID)
if err == nil {
Expand All @@ -289,6 +296,8 @@ func (s *DripStrictServerImplementation) ListAllNodes(
log.Ctx(ctx).Error().Msgf("Failed to get latest version for node %s w/ err: %v", dbNode.ID, err)
}
}

// Map publisher information
apiNode.Publisher = mapper.DbPublisherToApiPublisher(dbNode.Edges.Publisher, false)
apiNodes = append(apiNodes, *apiNode)
}
Expand Down
50 changes: 35 additions & 15 deletions services/registry/registry_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,49 +92,63 @@ type ListNodeVersionsResult struct {

// ListNodes retrieves a paginated list of nodes with optional filtering.
func (s *RegistryService) ListNodes(ctx context.Context, client *ent.Client, page, limit int, filter *NodeFilter) (*ListNodesResult, error) {
// Ensure valid pagination parameters
if page < 1 {
page = 1
}
if limit < 1 {
limit = 10
}

query := client.Node.Query().WithPublisher().WithVersions(
func(q *ent.NodeVersionQuery) {
// Initialize the query with relationships
query := client.Node.Query().
WithPublisher().
WithVersions(func(q *ent.NodeVersionQuery) {
q.Order(ent.Desc(nodeversion.FieldCreateTime))
},
)
})

// Apply filters if provided
if filter != nil {
var p []predicate.Node
var predicates []predicate.Node

// Filter by PublisherID
if filter.PublisherID != "" {
p = append(p, node.PublisherID(filter.PublisherID))
predicates = append(predicates, node.PublisherID(filter.PublisherID))
}

// Filter by search term across multiple fields
if filter.Search != "" {
p = append(p, node.Or(
predicates = append(predicates, node.Or(
node.IDContainsFold(filter.Search),
node.NameContainsFold(filter.Search),
node.DescriptionContainsFold(filter.Search),
node.AuthorContainsFold(filter.Search),
))
}

// Exclude banned nodes if not requested
if !filter.IncludeBanned {
p = append(p, node.StatusNEQ(schema.NodeStatusBanned))
predicates = append(predicates, node.StatusNEQ(schema.NodeStatusBanned))
}

if len(p) > 1 {
query.Where(node.And(p...))
} else {
query.Where(p...)
// Apply predicates to the query
if len(predicates) > 1 {
query.Where(node.And(predicates...))
} else if len(predicates) == 1 {
query.Where(predicates[0])
}
}

// Calculate pagination offset
offset := (page - 1) * limit

// Count total nodes
total, err := query.Count(ctx)
if err != nil {
return nil, fmt.Errorf("failed to count nodes: %w", err)
}

// Fetch nodes with pagination
nodes, err := query.
Offset(offset).
Limit(limit).
Expand All @@ -143,11 +157,13 @@ func (s *RegistryService) ListNodes(ctx context.Context, client *ent.Client, pag
return nil, fmt.Errorf("failed to list nodes: %w", err)
}

// Calculate total pages
totalPages := total / limit
if total%limit != 0 {
totalPages += 1
totalPages++
}

// Return the result
return &ListNodesResult{
Total: total,
Nodes: nodes,
Expand Down Expand Up @@ -470,7 +486,7 @@ func (s *RegistryService) UpdateNodeVersion(ctx context.Context, client *ent.Cli
}

func (s *RegistryService) GetLatestNodeVersion(ctx context.Context, client *ent.Client, nodeId string) (*ent.NodeVersion, error) {
log.Ctx(ctx).Info().Msgf("getting latest version of node: %v", nodeId)
log.Ctx(ctx).Info().Msgf("Getting latest version of node: %v", nodeId)
nodeVersion, err := client.NodeVersion.
Query().
Where(nodeversion.NodeIDEQ(nodeId)).
Expand All @@ -481,11 +497,15 @@ func (s *RegistryService) GetLatestNodeVersion(ctx context.Context, client *ent.

if err != nil {
if ent.IsNotFound(err) {

log.Ctx(ctx).Info().Msgf("No versions found for node %v", nodeId)
return nil, nil
}

log.Ctx(ctx).Error().Msgf("Error fetching latest version for node %v: %v", nodeId, err)
return nil, err
}

log.Ctx(ctx).Info().Msgf("Found latest version for node %v: %v", nodeId, nodeVersion)
return nodeVersion, nil
}

Expand Down

0 comments on commit 231fc69

Please sign in to comment.