Skip to content

Commit

Permalink
List All Node Versions API (#49)
Browse files Browse the repository at this point in the history
* Take list of statuses for list node version.

* List all node versions.

* Lower page size.

* Add maximum comment.

* Fix limit issue.

* Fix test.
  • Loading branch information
robinjhuang authored Jun 19, 2024
1 parent 5410d6b commit 6b27d96
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 90 deletions.
310 changes: 229 additions & 81 deletions drip/api.gen.go

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions mapper/node_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ func DbNodeVersionStatusToApiNodeVersionStatus(status schema.NodeVersionStatus)
return &nodeVersionStatus
}

func ApiNodeVersionStatusesToDbNodeVersionStatuses(status *[]drip.NodeVersionStatus) []schema.NodeVersionStatus {
var nodeVersionStatus []schema.NodeVersionStatus

if status == nil {
return nodeVersionStatus
}

for _, s := range *status {
nodeVersionStatus = append(nodeVersionStatus, ApiNodeVersionStatusToDbNodeVersionStatus(s))
}

return nodeVersionStatus
}

func ApiNodeVersionStatusToDbNodeVersionStatus(status drip.NodeVersionStatus) schema.NodeVersionStatus {
var nodeVersionStatus schema.NodeVersionStatus

Expand Down
78 changes: 78 additions & 0 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1359,6 +1359,13 @@ paths:
required: true
schema:
type: string
- in: query
name: statuses
required: false
schema:
type: array
items:
$ref: '#/components/schemas/NodeVersionStatus'
responses:
'200':
description: List of all node versions
Expand Down Expand Up @@ -1423,6 +1430,77 @@ paths:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/versions:
get:
summary: List all node versions given some filters.
operationId: listAllNodeVersions
tags:
- Versions
parameters:
- in: query
name: nodeId
required: false
schema:
type: string
- in: query
name: statuses
required: false
schema:
type: array
items:
$ref: '#/components/schemas/NodeVersionStatus'
- in: query
name: page
required: false
schema:
type: integer
default: 1
description: The page number to retrieve.
- in: query
name: pageSize
required: false
schema:
type: integer
default: 10
description: The number of items to include per page.
responses:
'200':
description: List of all node versions
content:
application/json:
schema:
type: object
properties:
total:
type: integer
description: Total number of node versions available
versions:
type: array
items:
$ref: '#/components/schemas/NodeVersion'
page:
type: integer
description: Current page number
pageSize:
type: integer
description: Maximum number of node versions per page. Maximum is 100.
totalPages:
type: integer
description: Total number of pages available

'403':
description: Node banned
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'

/admin/nodes/{nodeId}/versions/{versionNumber}:
put:
summary: Admin Update Node Version Status
Expand Down
64 changes: 60 additions & 4 deletions server/implementation/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,17 +432,19 @@ func (s *DripStrictServerImplementation) UpdateNode(

func (s *DripStrictServerImplementation) ListNodeVersions(
ctx context.Context, request drip.ListNodeVersionsRequestObject) (drip.ListNodeVersionsResponseObject, error) {

log.Ctx(ctx).Info().Msgf("ListNodeVersions request received for node ID: %s", request.NodeId)

nodeVersions, err := s.RegistryService.ListNodeVersions(ctx, s.Client, &drip_services.NodeVersionFilter{
apiStatus := mapper.ApiNodeVersionStatusesToDbNodeVersionStatuses(request.Params.Statuses)

nodeVersionsResult, err := s.RegistryService.ListNodeVersions(ctx, s.Client, &drip_services.NodeVersionFilter{
NodeId: request.NodeId,
Status: apiStatus,
})
if err != nil {
log.Ctx(ctx).Error().Msgf("Failed to list node versions for node %s w/ err: %v", request.NodeId, err)
return drip.ListNodeVersions500JSONResponse{Message: "Failed to list node versions", Error: err.Error()}, err
}

nodeVersions := nodeVersionsResult.NodeVersions
apiNodeVersions := make([]drip.NodeVersion, 0, len(nodeVersions))
for _, dbNodeVersion := range nodeVersions {
apiNodeVersions = append(apiNodeVersions, *mapper.DbNodeVersionToApiNodeVersion(dbNodeVersion))
Expand Down Expand Up @@ -893,9 +895,10 @@ func (s *DripStrictServerImplementation) AdminUpdateNodeVersion(

func (s *DripStrictServerImplementation) SecurityScan(
ctx context.Context, request drip.SecurityScanRequestObject) (drip.SecurityScanResponseObject, error) {
nodeVersions, err := s.RegistryService.ListNodeVersions(ctx, s.Client, &drip_services.NodeVersionFilter{
nodeVersionsResult, err := s.RegistryService.ListNodeVersions(ctx, s.Client, &drip_services.NodeVersionFilter{
Status: []schema.NodeVersionStatus{schema.NodeVersionStatusPending},
})
nodeVersions := nodeVersionsResult.NodeVersions

log.Ctx(ctx).Info().Msgf("Found %d node versions to scan", len(nodeVersions))

Expand All @@ -916,3 +919,56 @@ func (s *DripStrictServerImplementation) SecurityScan(
}
return drip.SecurityScan200Response{}, nil
}

func (s *DripStrictServerImplementation) ListAllNodeVersions(
ctx context.Context, request drip.ListAllNodeVersionsRequestObject) (drip.ListAllNodeVersionsResponseObject, error) {
log.Ctx(ctx).Info().Msg("ListAllNodeVersions request received")

page := 1
if request.Params.Page != nil {
page = *request.Params.Page
}
pageSize := 10
if request.Params.PageSize != nil && *request.Params.PageSize < 100 {
pageSize = *request.Params.PageSize
}
f := &drip_services.NodeVersionFilter{
Page: page,
PageSize: pageSize,
}
if request.Params.Statuses != nil {
f.Status = mapper.ApiNodeVersionStatusesToDbNodeVersionStatuses(request.Params.Statuses)
}

// List nodes from the registry service
nodeVersionResults, err := s.RegistryService.ListNodeVersions(ctx, s.Client, f)
if err != nil {
log.Ctx(ctx).Error().Msgf("Failed to list node versions w/ err: %v", err)
return drip.ListAllNodeVersions500JSONResponse{Message: "Failed to list node versions", Error: err.Error()}, nil
}

if len(nodeVersionResults.NodeVersions) == 0 {
log.Ctx(ctx).Info().Msg("No node versions found")
return drip.ListAllNodeVersions200JSONResponse{
Versions: &[]drip.NodeVersion{},
Total: &nodeVersionResults.Total,
Page: &nodeVersionResults.Page,
PageSize: &nodeVersionResults.Limit,
TotalPages: &nodeVersionResults.TotalPages,
}, nil
}

apiNodeVersions := make([]drip.NodeVersion, 0, len(nodeVersionResults.NodeVersions))
for _, dbNodeVersion := range nodeVersionResults.NodeVersions {
apiNodeVersions = append(apiNodeVersions, *mapper.DbNodeVersionToApiNodeVersion(dbNodeVersion))
}

log.Ctx(ctx).Info().Msgf("Found %d node versions", len(apiNodeVersions))
return drip.ListAllNodeVersions200JSONResponse{
Versions: &apiNodeVersions,
Total: &nodeVersionResults.Total,
Page: &nodeVersionResults.Page,
PageSize: &nodeVersionResults.Limit,
TotalPages: &nodeVersionResults.TotalPages,
}, nil
}
1 change: 1 addition & 0 deletions server/middleware/authentication/firebase_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func FirebaseAuthMiddleware(entClient *ent.Client) echo.MiddlewareFunc {
regexp.MustCompile(`^/publishers/[^/]+/nodes$`): {"GET"},
regexp.MustCompile(`^/publishers/[^/]+$`): {"GET"},
regexp.MustCompile(`^/nodes$`): {"GET"},
regexp.MustCompile(`^/versions$`): {"GET"},
regexp.MustCompile(`^/nodes/[^/]+$`): {"GET"},
regexp.MustCompile(`^/nodes/[^/]+/versions$`): {"GET"},
regexp.MustCompile(`^/nodes/[^/]+/install$`): {"GET"},
Expand Down
43 changes: 38 additions & 5 deletions services/registry/registry_svc.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,10 @@ type NodeFilter struct {
}

type NodeVersionFilter struct {
NodeId string
Status []schema.NodeVersionStatus
NodeId string
Status []schema.NodeVersionStatus
PageSize int
Page int
}

type NodeData struct {
Expand All @@ -80,6 +82,14 @@ type ListNodesResult struct {
TotalPages int `json:"totalPages"`
}

type ListNodeVersionsResult struct {
Total int `json:"total"`
NodeVersions []*ent.NodeVersion `json:"nodes"`
Page int `json:"page"`
Limit int `json:"limit"`
TotalPages int `json:"totalPages"`
}

// 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) {
if page < 1 {
Expand Down Expand Up @@ -363,7 +373,7 @@ type NodeVersionCreation struct {
SignedUrl string
}

func (s *RegistryService) ListNodeVersions(ctx context.Context, client *ent.Client, filter *NodeVersionFilter) ([]*ent.NodeVersion, error) {
func (s *RegistryService) ListNodeVersions(ctx context.Context, client *ent.Client, filter *NodeVersionFilter) (*ListNodeVersionsResult, error) {
query := client.NodeVersion.Query().
WithStorageFile().
Order(ent.Desc(nodeversion.FieldCreateTime))
Expand All @@ -373,15 +383,38 @@ func (s *RegistryService) ListNodeVersions(ctx context.Context, client *ent.Clie
query.Where(nodeversion.NodeIDEQ(filter.NodeId))
}

if filter.Status != nil {
if filter.Status != nil && len(filter.Status) > 0 {
query.Where(nodeversion.StatusIn(filter.Status...))
}

if filter.Page > 0 && filter.PageSize > 0 {
query.Offset((filter.Page - 1) * filter.PageSize)
query.Limit(filter.PageSize)
}
total, err := query.Count(ctx)
if err != nil {
return nil, fmt.Errorf("failed to count node versions: %w", err)
}
versions, err := query.All(ctx)
if err != nil {
return nil, fmt.Errorf("failed to list node versions: %w", err)
}
return versions, nil

totalPages := 0
if total > 0 && filter.PageSize > 0 {
totalPages = total / filter.PageSize

if total%filter.PageSize != 0 {
totalPages += 1
}
}
return &ListNodeVersionsResult{
Total: total,
NodeVersions: versions,
Page: filter.Page,
Limit: filter.PageSize,
TotalPages: totalPages,
}, nil
}

func (s *RegistryService) AddNodeReview(ctx context.Context, client *ent.Client, nodeId, userID string, star int) (nv *ent.Node, err error) {
Expand Down

0 comments on commit 6b27d96

Please sign in to comment.