Add cursor pagination flags to proboctl list commands
Replace the limit-driven auto-walking Paginate helper with explicit cursor-pagination flags (--first/--after, --last/--before) that mirror the GraphQL connection arguments. List commands now return a single keyset page with its page info, and emit cursors so callers can page forward and backward. --before no longer requires --last: both --first and --last default to 50 when omitted. Also split the tracker-pattern stats into enriched with and without a description so the enrichment backlog is visible at a glance. Signed-off-by: Émile Ré <emile@probo.com>
This commit is contained in:
@@ -16,63 +16,172 @@ package cmdutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"go.probo.inc/probo/pkg/page"
|
||||
)
|
||||
|
||||
// paginatePageSize is the per-request fetch size used to walk a cursor
|
||||
// connection. It is independent of the caller's --limit, which only
|
||||
// bounds the total returned.
|
||||
const paginatePageSize = 100
|
||||
// defaultPageSize is the forward page size used when neither --first nor
|
||||
// --last is provided.
|
||||
const defaultPageSize = 50
|
||||
|
||||
// Paginate walks a cursor-paginated coredata Load into a single slice,
|
||||
// following the keyset cursor until the caller's limit is reached or the
|
||||
// connection is exhausted. A limit <= 0 returns every matching row. This
|
||||
// is the proboctl-side counterpart of the API's connection resolvers: it
|
||||
// drives page.NewCursor + page.NewPage against the same coredata Load
|
||||
// methods, so a future proboctl API can reuse the data layer untouched.
|
||||
func Paginate[E page.Paginable[F], F page.OrderField](
|
||||
ctx context.Context,
|
||||
orderBy page.OrderBy[F],
|
||||
limit int,
|
||||
load func(ctx context.Context, cursor *page.Cursor[F]) ([]E, error),
|
||||
) ([]E, error) {
|
||||
var (
|
||||
result []E
|
||||
from *page.CursorKey
|
||||
)
|
||||
|
||||
for {
|
||||
size := paginatePageSize
|
||||
|
||||
if limit > 0 {
|
||||
remaining := limit - len(result)
|
||||
if remaining <= 0 {
|
||||
break
|
||||
}
|
||||
|
||||
if remaining < size {
|
||||
size = remaining
|
||||
}
|
||||
}
|
||||
|
||||
cursor := page.NewCursor(size, from, page.Head, orderBy)
|
||||
|
||||
rows, err := load(ctx, cursor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
p := page.NewPage(rows, cursor)
|
||||
result = append(result, p.Data...)
|
||||
|
||||
if !p.Info.HasNext || len(p.Data) == 0 {
|
||||
break
|
||||
}
|
||||
|
||||
key := p.Data[len(p.Data)-1].CursorKey(orderBy.Field)
|
||||
from = &key
|
||||
type (
|
||||
// PageFlags holds the cursor-pagination flag values registered by
|
||||
// AddPageFlags. They mirror the GraphQL connection arguments: --first
|
||||
// with --after walks forward, --last with --before walks backward.
|
||||
PageFlags struct {
|
||||
First int
|
||||
Last int
|
||||
After string
|
||||
Before string
|
||||
}
|
||||
|
||||
return result, nil
|
||||
// PageInfo is the proboctl-side counterpart of the API's PageInfo. The
|
||||
// cursors are the base64 page.CursorKey scalars, so they round-trip
|
||||
// straight back into --after / --before.
|
||||
PageInfo struct {
|
||||
HasNextPage bool `json:"hasNextPage"`
|
||||
HasPreviousPage bool `json:"hasPreviousPage"`
|
||||
StartCursor *string `json:"startCursor"`
|
||||
EndCursor *string `json:"endCursor"`
|
||||
}
|
||||
|
||||
// PageOutput wraps a flat slice of results with its page info for JSON
|
||||
// output.
|
||||
PageOutput struct {
|
||||
Items any `json:"items"`
|
||||
PageInfo PageInfo `json:"pageInfo"`
|
||||
}
|
||||
)
|
||||
|
||||
// AddPageFlags registers the cursor-pagination flags on cmd and returns a
|
||||
// pointer to the bound values.
|
||||
func AddPageFlags(cmd *cobra.Command) *PageFlags {
|
||||
pf := &PageFlags{}
|
||||
|
||||
cmd.Flags().IntVar(&pf.First, "first", 0, fmt.Sprintf("Return the first N rows after --after (default %d)", defaultPageSize))
|
||||
cmd.Flags().IntVar(&pf.Last, "last", 0, fmt.Sprintf("Return the last N rows before --before (default %d)", defaultPageSize))
|
||||
cmd.Flags().StringVar(&pf.After, "after", "", "Cursor to page forward from (use with --first)")
|
||||
cmd.Flags().StringVar(&pf.Before, "before", "", "Cursor to page backward from (use with --last)")
|
||||
|
||||
return pf
|
||||
}
|
||||
|
||||
// NewCursorFromFlags builds a keyset cursor from the pagination flags,
|
||||
// mirroring the GraphQL API's first/after vs last/before semantics.
|
||||
func NewCursorFromFlags[F page.OrderField](
|
||||
pf *PageFlags,
|
||||
orderBy page.OrderBy[F],
|
||||
) (*page.Cursor[F], error) {
|
||||
if pf.First > 0 && pf.Last > 0 {
|
||||
return nil, fmt.Errorf("--first and --last are mutually exclusive")
|
||||
}
|
||||
|
||||
if pf.After != "" && pf.Before != "" {
|
||||
return nil, fmt.Errorf("--after and --before are mutually exclusive")
|
||||
}
|
||||
|
||||
if pf.After != "" && pf.Last > 0 {
|
||||
return nil, fmt.Errorf("--after cannot be combined with --last")
|
||||
}
|
||||
|
||||
var (
|
||||
size int
|
||||
from *page.CursorKey
|
||||
position page.Position
|
||||
)
|
||||
|
||||
switch {
|
||||
case pf.Last > 0 || pf.Before != "":
|
||||
size = pf.Last
|
||||
if size == 0 {
|
||||
size = defaultPageSize
|
||||
}
|
||||
|
||||
position = page.Tail
|
||||
|
||||
if pf.Before != "" {
|
||||
ck, err := page.ParseCursorKey(pf.Before)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid --before cursor: %w", err)
|
||||
}
|
||||
|
||||
from = &ck
|
||||
}
|
||||
default:
|
||||
size = pf.First
|
||||
if size == 0 {
|
||||
size = defaultPageSize
|
||||
}
|
||||
|
||||
position = page.Head
|
||||
|
||||
if pf.After != "" {
|
||||
ck, err := page.ParseCursorKey(pf.After)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("invalid --after cursor: %w", err)
|
||||
}
|
||||
|
||||
from = &ck
|
||||
}
|
||||
}
|
||||
|
||||
return page.NewCursor(size, from, position, orderBy), nil
|
||||
}
|
||||
|
||||
// FetchPage loads a single keyset page using cursor and wraps the rows into a
|
||||
// page.Page, which trims the over-fetch and computes HasNext / HasPrev. It is
|
||||
// the proboctl-side counterpart of the API's connection resolvers.
|
||||
func FetchPage[E page.Paginable[F], F page.OrderField](
|
||||
ctx context.Context,
|
||||
cursor *page.Cursor[F],
|
||||
load func(ctx context.Context, cursor *page.Cursor[F]) ([]E, error),
|
||||
) (*page.Page[E, F], error) {
|
||||
rows, err := load(ctx, cursor)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return page.NewPage(rows, cursor), nil
|
||||
}
|
||||
|
||||
// NewPageInfo derives the proboctl PageInfo (cursors as base64 strings) from a
|
||||
// loaded page.
|
||||
func NewPageInfo[T page.Paginable[F], F page.OrderField](p *page.Page[T, F]) PageInfo {
|
||||
pi := PageInfo{
|
||||
HasNextPage: p.Info.HasNext,
|
||||
HasPreviousPage: p.Info.HasPrev,
|
||||
}
|
||||
|
||||
if len(p.Data) > 0 {
|
||||
start := p.First().CursorKey(p.Cursor.OrderBy.Field).String()
|
||||
end := p.Last().CursorKey(p.Cursor.OrderBy.Field).String()
|
||||
pi.StartCursor = &start
|
||||
pi.EndCursor = &end
|
||||
}
|
||||
|
||||
return pi
|
||||
}
|
||||
|
||||
// PrintPageInfo writes the page info as a footer below a rendered table.
|
||||
func PrintPageInfo(out io.Writer, pi PageInfo) {
|
||||
var start, end string
|
||||
if pi.StartCursor != nil {
|
||||
start = *pi.StartCursor
|
||||
}
|
||||
|
||||
if pi.EndCursor != nil {
|
||||
end = *pi.EndCursor
|
||||
}
|
||||
|
||||
_, _ = fmt.Fprintf(
|
||||
out,
|
||||
"hasNextPage: %t hasPreviousPage: %t\nstartCursor: %s\nendCursor: %s\n",
|
||||
pi.HasNextPage,
|
||||
pi.HasPreviousPage,
|
||||
start,
|
||||
end,
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user