Skip to content

Generalize pagination logic for queriers #4601

Closed

Description

Summary

Create a generalized pagination function available for module Queriers.

Problem Definition

Currently, there's some code redundancy for pagination logic and not all the endpoints that return batched items use pagination. The goal of this issue is to create a general pagination function and use it all across the modules' REST endpoints. This functionality should live under /types/rest/rest.go.

Additionally, the implementer will have to update the swagger.yaml docs and add the query parameters for pagination and limit to each of the affected endpoints.

Proposal

Idea:

func PaginateResponse(lenItems, limit, page int) (start, end int) {
	if limit== 0 {
		limit = lenItems
	}

	start = (page - 1) * limit
	end = limit + start
	if end >= lenItems {
		end = lenItems
	}

	if start >= lenItems {
		// page is out of bounds
		return -1, -1
	}

	return
}

and on each of the queriers (see each module's querier.go):

...
start, end := PaginateResponse(len(items), params.Limit, params.Page)
if start == -1 && end == -1 {
     items = items{}
} else {
     items = items[start:end]
}
...

For Admin Use

  • Not duplicate issue
  • Appropriate labels applied
  • Appropriate contributors tagged
  • Contributor assigned/self-assigned
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type: Code HygieneGeneral cleanup and restructuring of code to provide clarity, flexibility, and modularity.good first issue

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions