This repository has been archived on 2025-05-15. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
synchronizator-go/pkg/fetcher.go
aleidk 96af51ee68 feat: auto calculate pagination in fetcher
also refactor a little the worker pool
2024-11-30 17:30:38 -03:00

53 lines
1.2 KiB
Go

package synchronizator
import "fmt"
// StartPagination is the initial pagination configuration.
var StartPagination = Pagination{
Total: 0,
Pages: 0,
HasMore: false,
Limit: 10,
Offset: 0,
}
type FetchResponse[T any] struct {
Pagination
Response T
}
// Pagination represents the pagination information for fetching data.
type Pagination struct {
Total uint64 // Total number of items.
Pages uint64
HasMore bool // Indicates if there are more items to fetch.
Limit uint64 // Number of items to fetch per request.
Offset uint64 // Offset for the next set of items to fetch.
}
func calculatePages(pagination *Pagination, offset uint64) (uint64, error) {
if pagination.Limit == 0 {
return 0, fmt.Errorf("division by zero")
}
dividend := pagination.Total - offset
divisor := pagination.Limit
result := dividend / divisor
if dividend%divisor != 0 {
result++
}
return result, nil
}
func getPageByOffset(pagination *Pagination) (uint64, error) {
if pagination.Pages == 0 {
return 0, fmt.Errorf("division by zero")
}
total := pagination.Pages * pagination.Limit
result := (pagination.Pages * pagination.Offset) / total
return result, nil
}