Skip to content

Commit

Permalink
Fix pagination for zero index flow api (#561)
Browse files Browse the repository at this point in the history
* fix pagination for zero index paging update

* remove debug logging

* add tmp log

* refactor pagination

* remove logging

* add change log
  • Loading branch information
nullpointer0x00 authored Oct 18, 2024
1 parent d3add2d commit 68c19be
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 19 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ Ref: https://keepachangelog.com/en/1.0.0/
* Refactor account processing implementation to be more efficient [#552](https://github.com/provenance-io/explorer-service/issues/552)
* Update keep alive times for flow api grpc calls [#558](https://github.com/provenance-io/explorer-service/pull/558)

### Bug Fixes

* Fix pagination off by one calls to flow grpc [#561](https://github.com/provenance-io/explorer-service/pull/561)

## [v5.11.0](https://github.com/provenance-io/explorer-service/releases/tag/v5.11.0) - 2024-08-27

### Improvements
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ class FlowApiGrpcClient(flowApiChannelUri: URI) {
val allNavEvents = mutableListOf<NavEvent>()
var currentPage = 0
var hasMorePages = true

while (hasMorePages) {
val pagination = PaginationRequest.newBuilder().setPage(currentPage).setPageSize(requestSize).build()
val requestBuilder = NavEventRequest.newBuilder()
Expand All @@ -80,8 +79,7 @@ class FlowApiGrpcClient(flowApiChannelUri: URI) {
logger().debug("getAllMarkerNavByPriceDenoms $request")
val response: NavEventResponse = navService.getNavEvents(request)
allNavEvents.addAll(response.navEventsList)

if (response.pagination.currentPage >= response.pagination.totalPages) {
if (response.pagination.currentPage >= response.pagination.totalPages - 1) {
hasMorePages = false
} else {
currentPage++
Expand All @@ -91,7 +89,6 @@ class FlowApiGrpcClient(flowApiChannelUri: URI) {
hasMorePages = false
}
}

return@runBlocking allNavEvents
}

Expand All @@ -115,15 +112,22 @@ class FlowApiGrpcClient(flowApiChannelUri: URI) {
}
}

fun getAllLatestNavPrices(priceDenom: String, includeMarkers: Boolean = true, includeScopes: Boolean = true, fromDate: DateTime?, requestSize: Int = 10000): List<NavEvent> = runBlocking {
fun getAllLatestNavPrices(
priceDenom: String,
includeMarkers: Boolean = true,
includeScopes: Boolean = true,
fromDate: DateTime?,
requestSize: Int = 10000
): List<NavEvent> = runBlocking {
val fromDateString = fromDate?.toString(DateTimeFormat.forPattern("yyyy-MM-dd")) ?: ""
var currentPage = 1
var totalPages = 1
var currentPage = 0
var hasMorePages = true
val allNavEvents = mutableListOf<NavEvent>()

try {
while (currentPage <= totalPages) {
val pagination = PaginationRequest.newBuilder().setPage(currentPage).setPageSize(requestSize).build()
while (hasMorePages) {
try {
val pagination =
PaginationRequest.newBuilder().setPage(currentPage).setPageSize(requestSize).build()
val request = LatestNavEventRequest.newBuilder()
.setPriceDenom(priceDenom)
.setFromDate(fromDateString)
Expand All @@ -135,17 +139,17 @@ class FlowApiGrpcClient(flowApiChannelUri: URI) {
logger().debug("getLatestNavEvents $request")

val response: NavEventResponse = navService.getLatestNavEvents(request)

allNavEvents.addAll(response.navEventsList)

currentPage++
totalPages = response.pagination.totalPages
if (response.pagination.currentPage >= response.pagination.totalPages - 1) {
hasMorePages = false
} else {
currentPage++
}
} catch (e: Exception) {
logger().error("Error fetching latest Nav Events: ${e.message}", e)
}
logger().debug("getLatestNavEvents finished total pages requested: $currentPage total navs: ${allNavEvents.size}")
return@runBlocking allNavEvents
} catch (e: Exception) {
logger().error("Error fetching latest Nav Events: ${e.message}", e)
emptyList()
}
return@runBlocking allNavEvents
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ class AssetService(
includeScopes = false,
fromDate = assetPricinglastRun?.toDateTime()
)

latestPrices.forEach { price ->
if (price.denom != UTILITY_TOKEN) {
val marker = getAssetRaw(price.denom)
Expand Down

0 comments on commit 68c19be

Please sign in to comment.