USACE / instrumentation

Instrumentation project issue tracking and project planning
MIT License
4 stars 1 forks source link

Discussion - Paginated API #89

Open KevinJJackson opened 3 years ago

KevinJJackson commented 3 years ago

@brettpalmberg and I had a quick discussion on the addition of pagination params to requests to allow for quicker upfront loading times for large bodies of data. A good example of this would be an instruments' alerts. If there are many alerts for a single instrument, the UI could hang while it tries to load in all of the data.

A solution to this problem lies in returning a small subset of the data initially and loading subsequent data when needed.

Our initial proposal is to follow the REST guidelines for offset pagination: https://www.moesif.com/blog/technical/api-design/REST-API-Design-Filtering-Sorting-and-Pagination/#offset-pagination

This proposal would allow the consumer of the API to select a specific sized subset of data. This also means the API should provide consumers with added attributes to make fetching subsequent datasets more simple. The following attributes would be appended to each GET request with default values set for offset and limit if not provided by the consumer, ie: instruments endpoint:

firstLink: "/projects/12345678-1234-1234-1234-123456789012/instruments?offset=0&limit=100",
prevLink: null,
nextLink: "/projects/12345678-1234-1234-1234-123456789012/instruments?offset=100&limit=100",
lastLink: "/projects/12345678-1234-1234-1234-123456789012/instruments?offset=900&limit=100",
totalResults: 962,

This would also allow consumers to pull directly from the API using a custom offset and limit, if so desired and the API would be able do adjust the previous and next links in accordance with the params. For example, if the consumer passed in the request an offset of 540 and a limit of 20, the resulting links would appear as such:

firstLink: "/projects/12345678-1234-1234-1234-123456789012/instruments?offset=0&limit=20",
prevLink: "/projects/12345678-1234-1234-1234-123456789012/instruments?offset=520&limit=20",
nextLink: "/projects/12345678-1234-1234-1234-123456789012/instruments?offset=560&limit=20",
lastLink: "/projects/12345678-1234-1234-1234-123456789012/instruments?offset=960&limit=20",
totalResults: 962,

As seen above, the previous and next links could be null if there is no further data to be retrieved, ie: if you are on the first or last page, or both (for small data sets).

cc: @adamscarberry