Skip to content

Commit

Permalink
add action needed and pending counts
Browse files Browse the repository at this point in the history
  • Loading branch information
Sledro committed Nov 11, 2024
1 parent 3542024 commit f7b9e59
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 8 deletions.
10 changes: 6 additions & 4 deletions database/models/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ type Filter struct {
}

type PaginatedResult struct {
Items interface{} `json:"items"`
TotalCount int64 `json:"total_count"`
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
Items interface{} `json:"items"`
TotalCount int64 `json:"total_count"`
Page int64 `json:"page"`
PageSize int64 `json:"page_size"`
ActionNeeded int64 `json:"action_needed_count"`
Pending int64 `json:"pending_count"`
}
43 changes: 39 additions & 4 deletions database/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ func (db *Database) GetTransactions(ctx context.Context, filter models.Filter, p
return nil, fmt.Errorf("failed to get total count: %w", err)
}

actionNeededFilter := bson.D{
{Key: "$and", Value: bson.A{
mongoFilter,
bson.D{{Key: "status", Value: bson.M{
"$in": []string{
string(types.ReadyToProve),
string(types.ReadyForRelay),
},
}},
}},
}}
actionNeededCount, err := collection.CountDocuments(ctx, actionNeededFilter)
if err != nil {
return nil, fmt.Errorf("failed to get action needed count: %w", err)
}

pendingFilter := bson.D{
{Key: "$and", Value: bson.A{
mongoFilter,
bson.D{{Key: "status", Value: bson.M{
"$in": []string{
string(types.UnconfirmedL1ToL2Message),
string(types.StateRootNotPublished),
string(types.InChallengePeriod),
},
}},
}},
}}
pendingCount, err := collection.CountDocuments(ctx, pendingFilter)
if err != nil {
return nil, fmt.Errorf("failed to get pending count: %w", err)
}

pipeline := mongo.Pipeline{
{{Key: "$match", Value: mongoFilter}},
{{Key: "$lookup", Value: bson.D{
Expand Down Expand Up @@ -77,10 +110,12 @@ func (db *Database) GetTransactions(ctx context.Context, filter models.Filter, p
}

return &models.PaginatedResult{
Items: transactions,
TotalCount: totalCount,
Page: page,
PageSize: pageSize,
Items: transactions,
TotalCount: totalCount,
Page: page,
PageSize: pageSize,
ActionNeeded: actionNeededCount,
Pending: pendingCount,
}, nil
}

Expand Down

0 comments on commit f7b9e59

Please sign in to comment.