Skip to content

Commit

Permalink
Merge pull request #185 from gobitfly/NOBIDS/slight-improve-get-valis
Browse files Browse the repository at this point in the history
api: simplify GetvalidatorDashboardValidators a bit
  • Loading branch information
invis-bitfly authored Apr 9, 2024
2 parents 558031a + 21e7f07 commit 6352174
Showing 1 changed file with 28 additions and 21 deletions.
49 changes: 28 additions & 21 deletions backend/pkg/api/data_access/data_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -839,6 +839,11 @@ func (d *DataAccessService) GetValidatorDashboardValidators(dashboardId t.VDBId,
}
}

// no data found (searched for something that does not exist)
if len(data) == 0 {
return nil, &paging, nil
}

// Sort the result
isort.Slice(data, func(i, j int) bool {
switch sort.Column {
Expand Down Expand Up @@ -866,49 +871,51 @@ func (d *DataAccessService) GetValidatorDashboardValidators(dashboardId t.VDBId,
return false
})

// reverse data if direction cursor is opposite of sort direction
if currentCursor.IsValid() && currentDirection != currentCursor.Direction {
slices.Reverse(data)
}

// Find the index for the cursor and limit the data
var cursorIndex uint64
if currentCursor.IsValid() {
for idx, row := range data {
if row.Index == currentCursor.Index {
cursorIndex = uint64(idx + 1)
cursorIndex = uint64(idx)
break
}
}
}
limitCutoff := utilMath.MinU64(cursorIndex+limit+1, uint64(len(data)))
result := data[cursorIndex:limitCutoff]

cursorData := make([]t.ValidatorsCursor, len(result))
for idx, row := range result {
cursorData[idx] = t.ValidatorsCursor{Index: row.Index}
doReverse := currentCursor.IsValid() && currentDirection != currentCursor.Direction
var result []t.VDBManageValidatorsTableRow
if doReverse {
// opposite direction
var limitCutoff uint64
if cursorIndex > limit+1 {
limitCutoff = cursorIndex - limit - 1
}
result = data[limitCutoff:cursorIndex]
} else {
if currentCursor.IsValid() {
cursorIndex++
}
limitCutoff := utilMath.MinU64(cursorIndex+limit+1, uint64(len(data)))
result = data[cursorIndex:limitCutoff]
}

// flag if above limit
moreDataFlag := len(cursorData) > int(limit)
moreDataFlag := len(result) > int(limit)
if !moreDataFlag && !currentCursor.IsValid() {
// no paging required
return result, &paging, nil
}

// remove the last entry from data as it is only required for the check
if moreDataFlag {
result = result[:len(result)-1]
cursorData = cursorData[:len(cursorData)-1]
}

// flip it back if flipped
if currentCursor.IsValid() && currentDirection != currentCursor.Direction {
slices.Reverse(result)
slices.Reverse(cursorData)
if doReverse {
result = result[1:]
} else {
result = result[:len(result)-1]
}
}

p, err := utils.GetPagingFromData(cursorData, currentCursor, currentDirection, moreDataFlag)
p, err := utils.GetPagingFromData(result, currentCursor, currentDirection, moreDataFlag)
if err != nil {
return nil, nil, fmt.Errorf("failed to get paging: %w", err)
}
Expand Down

0 comments on commit 6352174

Please sign in to comment.