Skip to content
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

feat: add pagination to tasks list #73

Merged
merged 1 commit into from
Dec 27, 2022
Merged
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
22 changes: 21 additions & 1 deletion internal/app/api/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"net/http"
"time"

"github.com/formancehq/payments/internal/app/storage"

"github.com/google/uuid"

"github.com/formancehq/payments/internal/app/models"
Expand Down Expand Up @@ -81,7 +83,25 @@ func listTasks[Config payments.ConnectorConfigObject,
Descriptor payments.TaskDescriptor](connectorManager *integration.ConnectorManager[Config, Descriptor],
) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tasks, err := connectorManager.ListTasksStates(r.Context())
skip, err := integerWithDefault(r, "skip", 0)
if err != nil {
handleValidationError(w, r, err)

return
}

limit, err := integerWithDefault(r, "limit", maxPerPage)
if err != nil {
handleValidationError(w, r, err)

return
}

if limit > maxPerPage {
limit = maxPerPage
}

tasks, err := connectorManager.ListTasksStates(r.Context(), storage.Paginate(skip, limit))
if err != nil {
handleError(w, r, err)

Expand Down
6 changes: 4 additions & 2 deletions internal/app/integration/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package integration
import (
"context"

"github.com/formancehq/payments/internal/app/storage"

"github.com/google/uuid"

"github.com/formancehq/payments/internal/app/models"
Expand Down Expand Up @@ -220,9 +222,9 @@ func (l *ConnectorManager[ConnectorConfig, TaskDescriptor]) IsInstalled(ctx cont
}

func (l *ConnectorManager[ConnectorConfig,
TaskDescriptor]) ListTasksStates(ctx context.Context,
TaskDescriptor]) ListTasksStates(ctx context.Context, pagination storage.Paginator,
) ([]models.Task, error) {
return l.scheduler.ListTasks(ctx)
return l.scheduler.ListTasks(ctx, pagination)
}

func (l *ConnectorManager[Config, TaskDescriptor]) ReadTaskState(ctx context.Context, taskID uuid.UUID) (*models.Task, error) {
Expand Down
11 changes: 7 additions & 4 deletions internal/app/storage/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,20 @@ func (s *Storage) ListTasksByStatus(ctx context.Context, provider models.Connect
return tasks, nil
}

func (s *Storage) ListTasks(ctx context.Context, provider models.ConnectorProvider) ([]models.Task, error) {
func (s *Storage) ListTasks(ctx context.Context, provider models.ConnectorProvider, pagination Paginator) ([]models.Task, error) {
connector, err := s.GetConnector(ctx, provider)
if err != nil {
return nil, e("failed to get connector", err)
}

var tasks []models.Task

err = s.db.NewSelect().Model(&tasks).
Where("connector_id = ?", connector.ID).
Scan(ctx)
q := s.db.NewSelect().Model(&tasks).
Where("connector_id = ?", connector.ID)

pagination.apply(q)

err = q.Scan(ctx)
if err != nil {
return nil, e("failed to get tasks", err)
}
Expand Down
3 changes: 2 additions & 1 deletion internal/app/task/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ type DefaultTaskScheduler[TaskDescriptor payments.TaskDescriptor] struct {
}

func (s *DefaultTaskScheduler[TaskDescriptor]) ListTasks(ctx context.Context,
pagination storage.Paginator,
) ([]models.Task, error) {
return s.store.ListTasks(ctx, s.provider)
return s.store.ListTasks(ctx, s.provider, pagination)
}

func (s *DefaultTaskScheduler[TaskDescriptor]) ReadTask(ctx context.Context, taskID uuid.UUID) (*models.Task, error) {
Expand Down
4 changes: 3 additions & 1 deletion internal/app/task/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"encoding/json"

"github.com/formancehq/payments/internal/app/storage"

"github.com/google/uuid"

"github.com/formancehq/payments/internal/app/models"
Expand All @@ -13,7 +15,7 @@ type Repository interface {
UpdateTaskStatus(ctx context.Context, provider models.ConnectorProvider, descriptor json.RawMessage, status models.TaskStatus, err string) error
FindAndUpsertTask(ctx context.Context, provider models.ConnectorProvider, descriptor json.RawMessage, status models.TaskStatus, err string) (*models.Task, error)
ListTasksByStatus(ctx context.Context, provider models.ConnectorProvider, status models.TaskStatus) ([]models.Task, error)
ListTasks(ctx context.Context, provider models.ConnectorProvider) ([]models.Task, error)
ListTasks(ctx context.Context, provider models.ConnectorProvider, pagination storage.Paginator) ([]models.Task, error)
ReadOldestPendingTask(ctx context.Context, provider models.ConnectorProvider) (*models.Task, error)
GetTask(ctx context.Context, id uuid.UUID) (*models.Task, error)
GetTaskByDescriptor(ctx context.Context, provider models.ConnectorProvider, descriptor json.RawMessage) (*models.Task, error)
Expand Down
3 changes: 2 additions & 1 deletion internal/app/task/storememory.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func (s *InMemoryStore) GetTaskByDescriptor(ctx context.Context, provider models

func (s *InMemoryStore) ListTasks(ctx context.Context,
provider models.ConnectorProvider,
pagination storage.Paginator,
) ([]models.Task, error) {
ret := make([]models.Task, 0)

Expand Down Expand Up @@ -115,7 +116,7 @@ func (s *InMemoryStore) ReadOldestPendingTask(ctx context.Context,
func (s *InMemoryStore) ListTasksByStatus(ctx context.Context,
provider models.ConnectorProvider, taskStatus models.TaskStatus,
) ([]models.Task, error) {
all, err := s.ListTasks(ctx, provider)
all, err := s.ListTasks(ctx, provider, storage.Paginate(0, 0))
if err != nil {
return nil, err
}
Expand Down