Skip to content

Commit

Permalink
Fix issue with full-page navigation when show total is disabled
Browse files Browse the repository at this point in the history
and resolve page count on the last page issue
  • Loading branch information
KinyaElGrande committed Sep 11, 2023
1 parent 553ea00 commit 58e8653
Showing 1 changed file with 43 additions and 17 deletions.
60 changes: 43 additions & 17 deletions server/compose/dalutils/records.go
Expand Up @@ -200,7 +200,13 @@ func drainIterator(ctx context.Context, iter dal.Iterator, mod *types.Module, f
return
}

// generatePageNavigation page nav for given record set using iterator
// generatePageNavigation generates page navigation for a given record set using an iterator and filter limit.
// If the limit is not defined, the page navigation will consist of only one page without a cursor.
// If the limit is defined and is greater than the total number of records in the set,
// the page navigation will consist of only one page without a cursor.
// If the limit is defined and is less than the total number of records in the set,
// the page navigation will have multiple pages with cursor(s) based on the total number of records and the provided limit.
// @todo revisit and clean up this function properly
func generatePageNavigation(ctx context.Context, iter dal.Iterator, mod *types.Module, p types.RecordFilter, set types.RecordSet) (out types.RecordFilter, err error) {
const (
howMuchMore = 1000
Expand All @@ -223,6 +229,7 @@ func generatePageNavigation(ctx context.Context, iter dal.Iterator, mod *types.M
},
}

// generatePage generates pageNavigation for given record set
generatePage = func(last *types.Record) (err error) {
if !p.IncPageNavigation || p.Limit == 0 || len(pageNavigation) == 0 {
return
Expand All @@ -234,25 +241,34 @@ func generatePageNavigation(ctx context.Context, iter dal.Iterator, mod *types.M
return
}

// prep page
if (total % p.Limit) == 0 {
if total < p.Limit {
pageNavigation[lastNavPageNo].Count = total
}

// prepare page
if total != 0 && (total%p.Limit) == 0 {
pageNavigation[lastNavPageNo].Count = p.Limit
page = filter.Page{
Page: uint(len(pageNavigation) + 1),
Count: 0,
Count: p.Limit,
Cursor: nextPage,
}
} else {
page.Count += 1
}

// push page when limit is matched with page item size
if p.Limit == 1 || pageNavigation[lastNavPageNo].Count == p.Limit {
pageNavigation = append(pageNavigation, &filter.Page{
Page: page.Page,
Count: page.Count,
Cursor: page.Cursor,
})
expectedItemCountUpToPage := uint(lastNavPageNo+1) * p.Limit
if p.Limit == 1 {
expectedItemCountUpToPage = uint(lastNavPageNo) * p.Limit
}

if expectedItemCountUpToPage < total {
// push page when limit is matched with the previous page item size
if pageNavigation[lastNavPageNo].Count == p.Limit {
pageNavigation = append(pageNavigation, &filter.Page{
Page: page.Page,
Count: total % p.Limit,
Cursor: page.Cursor, // prev cursor
})
}
}

return
Expand All @@ -261,19 +277,19 @@ func generatePageNavigation(ctx context.Context, iter dal.Iterator, mod *types.M

if setLen == 0 {
return
} else {
first = set[0]
last = set[setLen-1]
}

first = set[0]
last = set[setLen-1]

// Limit
out.Limit = p.Limit

// Sorting
out.Sort = dal.IteratorSorting(iter)

// No need to generate prev/next cursor
// if limit is not defined and set is empty
// if limit is not defined and set is empty
if p.Limit > 0 && len(set) > 0 {
// PrevPage
out.PrevPage, err = dal.PreLoadCursor(ctx, iter, 1, true, first)
Expand Down Expand Up @@ -336,6 +352,16 @@ func generatePageNavigation(ctx context.Context, iter dal.Iterator, mod *types.M

// Page navigation
if p.IncPageNavigation {
// Ensure that the last page count is correct if it's not equal to the limit.
lastPageCount := pageNavigation[len(pageNavigation)-1].Count
if lastPageCount > 0 && lastPageCount != p.Limit && lastPageCount != total%p.Limit {
pageNavigation[len(pageNavigation)-1].Count = total % p.Limit
}

if p.Limit == 1 {
pageNavigation = pageNavigation[:len(pageNavigation)-1]
}

out.PageNavigation = pageNavigation
}

Expand Down

0 comments on commit 58e8653

Please sign in to comment.