utils

package
v0.0.0-...-4fa6c80 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const CursorDirectionAsc = "asc"

CursorDirectionAsc marks an ascending cursor as the token's third component. Descending cursors keep the original two-part form, so tokens minted before ordering existed stay readable and tokens minted for the default order stay readable by instances that predate it.

View Source
const (
	MaxSandboxID = "zzzzzzzzzzzzzzzzzzzz"
)
View Source
const (
	// MaxTemplateID sorts lexically after any real template ID. It is used as
	// the default cursor ID for the first page of a descending template listing
	// (mirrors MaxSandboxID).
	MaxTemplateID = "zzzzzzzzzzzzzzzzzzzz"
)

Variables

This section is empty.

Functions

func ErrorHandler

func ErrorHandler(c *gin.Context, message string, statusCode int)

ErrorHandler answers a request the validator refused. It returns and records the validator's message, never the request body.

func MultiErrorHandler

func MultiErrorHandler(me openapi3.MultiError) error

MultiErrorHandler handles wrapped SecurityRequirementsError, so there are no multiple errors returned to the user.

func ParseMetadata

func ParseMetadata(ctx context.Context, metadata *string) (*map[string]string, error)

func SandboxChangingStateMsg

func SandboxChangingStateMsg(sandboxID string, state sandbox.State) string

func SandboxLifecycleHeldMsg

func SandboxLifecycleHeldMsg(sandboxID string) string

func SandboxNotFoundMsg

func SandboxNotFoundMsg(sandboxID string) string

func ShortID

func ShortID(compositeID string) (string, error)

func SortPaginatedSandboxes

func SortPaginatedSandboxes(sandboxes []PaginatedSandbox, order SortDirection)

SortPaginatedSandboxes sorts the sandboxes by PaginationTimestamp then SandboxID for stable pagination. It uses PaginationTimestamp (the microsecond-aligned keyset value), not the public StartedAt, so the order matches the cursor filter and SQL predicate. Descending order is timestamp DESC, SandboxID ASC; ascending order is the exact reverse (timestamp ASC, SandboxID DESC) so it maps onto a backward scan of the (team_id, sandbox_started_at DESC, sandbox_id) index.

func SortPaginatedSandboxesDesc

func SortPaginatedSandboxesDesc(sandboxes []PaginatedSandbox)

SortPaginatedSandboxesDesc preserves the descending-only entry point used by the legacy (v1) list endpoint.

func UnwrapGRPCError

func UnwrapGRPCError(err error) error

Types

type Cursor

type Cursor struct {
	Time time.Time
	ID   string
}

Cursor represents a parsed pagination cursor

type NoOpWriter

type NoOpWriter struct{}

NoOpWriter is an io.Writer implementation that discards anything written to it.

func (*NoOpWriter) Write

func (nw *NoOpWriter) Write(p []byte) (n int, err error)

type PaginatedSandbox

type PaginatedSandbox struct {
	api.ListedSandbox

	PaginationTimestamp time.Time `json:"-"`
}

PaginatedSandbox extends the api.ListedSandbox with a timestamp to use for pagination

func FilterBasedOnCursor

func FilterBasedOnCursor(sandboxes []PaginatedSandbox, cursorTime time.Time, cursorID string, order SortDirection) []PaginatedSandbox

FilterBasedOnCursor keeps only the sandboxes that fall after the cursor in the requested order. It compares on PaginationTimestamp (the microsecond-aligned keyset value), not the public StartedAt, so running and paused sandboxes share the same precision as the SQL predicate. Descending order pages through (timestamp DESC, sandbox_id ASC); ascending order is the exact reverse (timestamp ASC, sandbox_id DESC).

func FilterSandboxesOnMetadata

func FilterSandboxesOnMetadata(sandboxes []PaginatedSandbox, metadata *map[string]string) []PaginatedSandbox

func FilterSandboxesOnStartedAtAndTemplate

func FilterSandboxesOnStartedAtAndTemplate(sandboxes []PaginatedSandbox, startedAfter time.Time, templateID *string) []PaginatedSandbox

FilterSandboxesOnStartedAtAndTemplate applies the startedAfter lower bound (inclusive) and the exact-template filter.

The bound is compared against PaginationTimestamp, the microsecond-aligned keyset value, not the public StartedAt, so a kept row can never sort before the bound it satisfies. Callers pass a bound already truncated onto that same microsecond grid (see parseSandboxListStartedAfter), which is what keeps the boundary inclusive here and in the SQL predicate alike -- a sandbox does not enter or leave a filtered list purely because it paused.

The input is left intact: filtering in place would rewrite the caller's slice, and this one already returns it unmodified when no filter is active, so a destructive contract would only hold for some arguments.

func FilterUpToCursor

func FilterUpToCursor(sandboxes []PaginatedSandbox, cursorTime time.Time, cursorID string, order SortDirection) []PaginatedSandbox

FilterUpToCursor keeps the sandboxes FilterBasedOnCursor drops: those at or before the cursor in the order.

type Pagination

type Pagination[T any] struct {
	// contains filtered or unexported fields
}

Pagination handles pagination logic for list endpoints

func NewPagination

func NewPagination[T any](params PaginationParams, config PaginationConfig) (*Pagination[T], error)

NewPagination creates a new pagination instance from request parameters

func (*Pagination[T]) CursorID

func (p *Pagination[T]) CursorID() string

CursorID returns the cursor ID

func (*Pagination[T]) CursorTime

func (p *Pagination[T]) CursorTime() time.Time

CursorTime returns the cursor timestamp

func (*Pagination[T]) ProcessResultsWithHeader

func (p *Pagination[T]) ProcessResultsWithHeader(c *gin.Context, results []T, getTimestampAndID func(T) (time.Time, string)) []T

ProcessResultsWithHeader handles pagination and sets the X-Next-Token header in one call. This is a convenience method that combines ProcessResults and SetHeader.

func (*Pagination[T]) ProcessResultsWithHeaderContinuing

func (p *Pagination[T]) ProcessResultsWithHeaderContinuing(c *gin.Context, results []T, getTimestampAndID func(T) (time.Time, string), continueAfter *Cursor) []T

ProcessResultsWithHeaderContinuing is ProcessResultsWithHeader for results whose source stopped early at continueAfter: when they do not fill the page, the next token still continues after that position, so the short page does not end pagination.

func (*Pagination[T]) QueryLimit

func (p *Pagination[T]) QueryLimit() int32

QueryLimit returns the limit to use for database queries (limit + 1 to detect more results)

type PaginationConfig

type PaginationConfig struct {
	DefaultLimit int32
	MaxLimit     int32
	DefaultID    string // Default cursor ID when no token is provided (e.g., max UUID or max sandbox ID)
	// Order controls the first-page cursor when no token is provided. For
	// SortDesc the first page starts at "now" (newest first); for SortAsc it
	// starts at the zero time (oldest first).
	Order SortDirection
}

PaginationConfig holds configuration for pagination behavior

type PaginationParams

type PaginationParams struct {
	Limit     *int32
	NextToken *string
}

PaginationParams holds pagination parameters from the API request

type Result

type Result struct {
	Error error
}

type SortDirection

type SortDirection int

SortDirection is the order in which keyset-paginated results are returned. The zero value is SortDesc, preserving the default newest-first behavior.

const (
	SortDesc SortDirection = iota
	SortAsc
)

func ParseCursor

func ParseCursor(cursor string) (time.Time, string, SortDirection, error)

ParseCursor decodes a cursor token into its timestamp, ID and sort direction. A two-part token is a descending cursor (the original format, still emitted for the default order); a three-part token carries CursorDirectionAsc.

type TemplateCounter

type TemplateCounter struct {
	// contains filtered or unexported fields
}

type TemplateSpawnCounter

type TemplateSpawnCounter struct {
	// contains filtered or unexported fields
}

func NewTemplateSpawnCounter

func NewTemplateSpawnCounter(ctx context.Context, tickerDuration time.Duration, dbClient *sqlcdb.Client) *TemplateSpawnCounter

func (*TemplateSpawnCounter) Close

func (t *TemplateSpawnCounter) Close(ctx context.Context)

func (*TemplateSpawnCounter) IncreaseTemplateSpawnCount

func (t *TemplateSpawnCounter) IncreaseTemplateSpawnCount(templateID string, time time.Time)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL