Skip to content

Commit

Permalink
Merge branch 'mongodb'
Browse files Browse the repository at this point in the history
  • Loading branch information
kwx4957 committed Sep 5, 2024
2 parents b74af53 + 31059dd commit b6a9016
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 324 deletions.
31 changes: 17 additions & 14 deletions chaoscenter/authentication/api/utils/project_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,25 +184,28 @@ func CreateSortStage(sort *entities.SortInput) bson.D {
}
}

func CreatePaginationStage(pagination *entities.Pagination) []bson.D {
func CreatePaginationStage(pagination *entities.Pagination) ([]bson.D, int, int) {
var stages []bson.D
skip := 0
limit := 10

if pagination != nil {
page := pagination.Page
limit := pagination.Limit
limit = pagination.Limit

// upper limit of 50 to prevent exceeding max limit 16mb
if pagination.Limit > 50 {
if limit > 50 {
limit = 50
}
stages = append(stages, bson.D{
{"$skip", page * limit},
})
stages = append(stages, bson.D{
{"$limit", limit},
})
} else {
stages = append(stages, bson.D{
{"$limit", 10},
})
skip = page * limit
}
return stages

stages = append(stages, bson.D{
{"$skip", skip},
})
stages = append(stages, bson.D{{
"$limit", limit},
})

return stages, skip, limit
}
42 changes: 25 additions & 17 deletions chaoscenter/authentication/pkg/project/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,29 @@ func (r repository) GetProjectsByUserID(request *entities.ListProjectRequest) (*
pipeline = append(pipeline, sortStage)
}

// Pagination stages
paginationStages := project_utils.CreatePaginationStage(request.Pagination)

// Facet stage to count total projects and paginate results
facetStage := bson.D{
{"$facet", bson.D{
{"totalCount", bson.A{
bson.D{{"$count", "totalNumberOfProjects"}},
}},
{"projects", append(mongo.Pipeline{}, paginationStages...)},
// Pagination stage
_, skip, limit := project_utils.CreatePaginationStage(request.Pagination)

// Count total project and get top-level document to array
countStage := bson.D{
{"$group", bson.D{
{"_id", nil},
{"totalNumberOfProjects", bson.D{{"$sum", 1}}},
{"projects", bson.D{{"$push", "$$ROOT"}}},
}},
}
pipeline = append(pipeline, facetStage)

// Paging results
pagingStage := bson.D{
{"$project", bson.D{
{"_id", 0},
{"totalNumberOfProjects", 1},
{"projects", bson.D{
{"$slice", bson.A{"$projects", skip, limit}},
}},
}}}

pipeline = append(pipeline, countStage, pagingStage)

// Execute the aggregate pipeline
cursor, err := r.Collection.Aggregate(ctx, pipeline)
Expand All @@ -115,10 +125,8 @@ func (r repository) GetProjectsByUserID(request *entities.ListProjectRequest) (*

// Extract results
var result struct {
TotalCount []struct {
TotalNumberOfProjects int64 `bson:"totalNumberOfProjects"`
} `bson:"totalCount"`
Projects []*entities.Project `bson:"projects"`
TotalNumberOfProjects int64 `bson:"totalNumberOfProjects"`
Projects []*entities.Project `bson:"projects"`
}

if cursor.Next(ctx) {
Expand All @@ -128,8 +136,8 @@ func (r repository) GetProjectsByUserID(request *entities.ListProjectRequest) (*
}

var totalNumberOfProjects int64
if len(result.TotalCount) > 0 {
totalNumberOfProjects = result.TotalCount[0].TotalNumberOfProjects
if result.TotalNumberOfProjects > 0 {
totalNumberOfProjects = result.TotalNumberOfProjects
} else {
zero := int64(0)
return &entities.ListProjectResponse{
Expand Down
Loading

0 comments on commit b6a9016

Please sign in to comment.