Skip to content

refactor: pagination system #978

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions internal/api/bulking/mocks_ledger_controller_test.go

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

8 changes: 4 additions & 4 deletions internal/api/common/mocks_ledger_controller_test.go

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

2 changes: 1 addition & 1 deletion internal/api/common/mocks_system_controller_test.go

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

21 changes: 13 additions & 8 deletions internal/api/v1/controllers_accounts_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package v1

import (
"github.com/formancehq/go-libs/v3/bun/bunpaginate"
storagecommon "github.com/formancehq/ledger/internal/storage/common"
"net/http"

"errors"
Expand All @@ -12,19 +14,22 @@ import (
func listAccounts(w http.ResponseWriter, r *http.Request) {
l := common.LedgerFromContext(r.Context())

rq, err := getOffsetPaginatedQuery[any](r)
rq, err := getPaginatedQuery(
r,
"address",
bunpaginate.OrderAsc,
func(resourceQuery *storagecommon.ResourceQuery[any]) error {
var err error
resourceQuery.Builder, err = buildAccountsFilterQuery(r)
return err
},
)
if err != nil {
api.BadRequest(w, common.ErrValidation, err)
return
}

rq.Options.Builder, err = buildAccountsFilterQuery(r)
if err != nil {
api.BadRequest(w, common.ErrValidation, err)
return
}

cursor, err := l.ListAccounts(r.Context(), *rq)
cursor, err := l.ListAccounts(r.Context(), rq)
if err != nil {
switch {
case errors.Is(err, ledgercontroller.ErrMissingFeature{}):
Expand Down
39 changes: 30 additions & 9 deletions internal/api/v1/controllers_accounts_list_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"github.com/formancehq/go-libs/v3/pointer"
"github.com/formancehq/ledger/internal/api/common"
storagecommon "github.com/formancehq/ledger/internal/storage/common"
"net/http"
Expand All @@ -26,7 +27,7 @@ func TestAccountsList(t *testing.T) {
type testCase struct {
name string
queryParams url.Values
expectQuery storagecommon.OffsetPaginatedQuery[any]
expectQuery storagecommon.PaginatedQuery[any]
expectStatusCode int
expectedErrorCode string
expectBackendCall bool
Expand All @@ -37,8 +38,10 @@ func TestAccountsList(t *testing.T) {
{
name: "nominal",
expectBackendCall: true,
expectQuery: storagecommon.OffsetPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Column: "address",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderAsc)),
},
},
{
Expand All @@ -47,11 +50,13 @@ func TestAccountsList(t *testing.T) {
"metadata[roles]": []string{"admin"},
},
expectBackendCall: true,
expectQuery: storagecommon.OffsetPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Options: storagecommon.ResourceQuery[any]{
Builder: query.Match("metadata[roles]", "admin"),
},
Column: "address",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderAsc)),
},
},
{
Expand All @@ -60,20 +65,30 @@ func TestAccountsList(t *testing.T) {
"address": []string{"foo"},
},
expectBackendCall: true,
expectQuery: storagecommon.OffsetPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Options: storagecommon.ResourceQuery[any]{
Builder: query.Match("address", "foo"),
},
Column: "address",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderAsc)),
},
},
{
name: "using empty cursor",
queryParams: url.Values{
"cursor": []string{bunpaginate.EncodeCursor(storagecommon.OffsetPaginatedQuery[any]{})},
"cursor": []string{bunpaginate.EncodeCursor(storagecommon.ColumnPaginatedQuery[any]{
InitialPaginatedQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: bunpaginate.QueryDefaultPageSize,
},
})},
},
expectBackendCall: true,
expectQuery: storagecommon.OffsetPaginatedQuery[any]{},
expectQuery: storagecommon.ColumnPaginatedQuery[any]{
InitialPaginatedQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
},
},
},
{
name: "using invalid cursor",
Expand All @@ -97,8 +112,10 @@ func TestAccountsList(t *testing.T) {
"pageSize": []string{"1000000"},
},
expectBackendCall: true,
expectQuery: storagecommon.OffsetPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: MaxPageSize,
Column: "address",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderAsc)),
},
},
{
Expand All @@ -108,11 +125,13 @@ func TestAccountsList(t *testing.T) {
"balanceOperator": []string{"e"},
},
expectBackendCall: true,
expectQuery: storagecommon.OffsetPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Options: storagecommon.ResourceQuery[any]{
Builder: query.Match("balance", int64(100)),
},
Column: "address",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderAsc)),
},
},
{
Expand All @@ -121,8 +140,10 @@ func TestAccountsList(t *testing.T) {
expectedErrorCode: common.ErrValidation,
returnErr: ledgercontroller.ErrMissingFeature{},
expectBackendCall: true,
expectQuery: storagecommon.OffsetPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Column: "address",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderAsc)),
},
},
}
Expand Down
22 changes: 13 additions & 9 deletions internal/api/v1/controllers_balances_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
storagecommon "github.com/formancehq/ledger/internal/storage/common"
"math/big"
"net/http"

Expand All @@ -12,20 +13,23 @@ import (
func getBalances(w http.ResponseWriter, r *http.Request) {
l := common.LedgerFromContext(r.Context())

rq, err := getOffsetPaginatedQuery[any](r)
rq, err := getPaginatedQuery[any](
r,
"address",
bunpaginate.OrderAsc,
func(resourceQuery *storagecommon.ResourceQuery[any]) error {
var err error
resourceQuery.Expand = append(resourceQuery.Expand, "volumes")
resourceQuery.Builder, err = buildAccountsFilterQuery(r)
return err
},
)
if err != nil {
api.BadRequest(w, common.ErrValidation, err)
return
}

rq.Options.Builder, err = buildAccountsFilterQuery(r)
if err != nil {
api.BadRequest(w, common.ErrValidation, err)
return
}
rq.Options.Expand = []string{"volumes"}

cursor, err := l.ListAccounts(r.Context(), *rq)
cursor, err := l.ListAccounts(r.Context(), rq)
if err != nil {
common.HandleCommonErrors(w, r, err)
return
Expand Down
10 changes: 4 additions & 6 deletions internal/api/v1/controllers_config.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package v1

import (
"context"
_ "embed"
"github.com/formancehq/ledger/internal/api/common"
storagecommon "github.com/formancehq/ledger/internal/storage/common"
"net/http"

ledgercontroller "github.com/formancehq/ledger/internal/controller/ledger"
"github.com/formancehq/ledger/internal/controller/system"

"github.com/formancehq/go-libs/v3/bun/bunpaginate"
Expand Down Expand Up @@ -37,10 +35,10 @@ func GetInfo(systemController system.Controller, version string) func(w http.Res
return func(w http.ResponseWriter, r *http.Request) {

ledgerNames := make([]string, 0)
if err := bunpaginate.Iterate(r.Context(), ledgercontroller.NewListLedgersQuery(100),
func(ctx context.Context, q storagecommon.ColumnPaginatedQuery[any]) (*bunpaginate.Cursor[ledger.Ledger], error) {
return systemController.ListLedgers(ctx, q)
},
if err := storagecommon.Iterate(r.Context(), storagecommon.InitialPaginatedQuery[any]{
PageSize: 100,
},
systemController.ListLedgers,
func(cursor *bunpaginate.Cursor[ledger.Ledger]) error {
ledgerNames = append(ledgerNames, collectionutils.Map(cursor.Data, func(from ledger.Ledger) string {
return from.Name
Expand Down
15 changes: 11 additions & 4 deletions internal/api/v1/controllers_logs_list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
storagecommon "github.com/formancehq/ledger/internal/storage/common"
"net/http"

"github.com/formancehq/go-libs/v3/api"
Expand Down Expand Up @@ -35,15 +36,21 @@ func buildGetLogsQuery(r *http.Request) query.Builder {
func getLogs(w http.ResponseWriter, r *http.Request) {
l := common.LedgerFromContext(r.Context())

paginatedQuery, err := getColumnPaginatedQuery[any](r, "id", bunpaginate.OrderDesc)
paginatedQuery, err := getPaginatedQuery[any](
r,
"id",
bunpaginate.OrderDesc,
func(resourceQuery *storagecommon.ResourceQuery[any]) error {
resourceQuery.Builder = buildGetLogsQuery(r)
return nil
},
)
if err != nil {
api.BadRequest(w, common.ErrValidation, err)
return
}

paginatedQuery.Options.Builder = buildGetLogsQuery(r)

cursor, err := l.ListLogs(r.Context(), *paginatedQuery)
cursor, err := l.ListLogs(r.Context(), paginatedQuery)
if err != nil {
common.HandleCommonErrors(w, r, err)
return
Expand Down
20 changes: 14 additions & 6 deletions internal/api/v1/controllers_logs_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestGetLogs(t *testing.T) {
type testCase struct {
name string
queryParams url.Values
expectQuery storagecommon.ColumnPaginatedQuery[any]
expectQuery storagecommon.PaginatedQuery[any]
expectStatusCode int
expectedErrorCode string
}
Expand All @@ -36,7 +36,7 @@ func TestGetLogs(t *testing.T) {
testCases := []testCase{
{
name: "nominal",
expectQuery: storagecommon.ColumnPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Column: "id",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderDesc)),
Expand All @@ -47,7 +47,7 @@ func TestGetLogs(t *testing.T) {
queryParams: url.Values{
"start_time": []string{now.Format(time.DateFormat)},
},
expectQuery: storagecommon.ColumnPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Column: "id",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderDesc)),
Expand All @@ -61,7 +61,7 @@ func TestGetLogs(t *testing.T) {
queryParams: url.Values{
"end_time": []string{now.Format(time.DateFormat)},
},
expectQuery: storagecommon.ColumnPaginatedQuery[any]{
expectQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
Column: "id",
Order: pointer.For(bunpaginate.Order(bunpaginate.OrderDesc)),
Expand All @@ -73,9 +73,17 @@ func TestGetLogs(t *testing.T) {
{
name: "using empty cursor",
queryParams: url.Values{
"cursor": []string{bunpaginate.EncodeCursor(storagecommon.ColumnPaginatedQuery[any]{})},
"cursor": []string{bunpaginate.EncodeCursor(storagecommon.ColumnPaginatedQuery[any]{
InitialPaginatedQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
},
})},
},
expectQuery: storagecommon.ColumnPaginatedQuery[any]{
InitialPaginatedQuery: storagecommon.InitialPaginatedQuery[any]{
PageSize: DefaultPageSize,
},
},
expectQuery: storagecommon.ColumnPaginatedQuery[any]{},
},
{
name: "using invalid cursor",
Expand Down
Loading
Loading