Skip to content

Commit

Permalink
feat: enable aggregations for transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
catalyst17 committed Oct 23, 2024
1 parent 2df2142 commit 803f833
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 19 deletions.
6 changes: 3 additions & 3 deletions internal/handlers/logs_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,14 @@ func handleLogsRequest(c *gin.Context, contractAddress, signature string) {
Page: queryParams.Page,
Limit: queryParams.Limit,
TotalItems: 0,
TotalPages: 0, // Implement total pages count if needed
TotalPages: 0, // TODO: Implement total pages count
},
Data: nil,
Aggregations: nil,
}

// If aggregates are specified, retrieve them
if len(queryParams.Aggregates) > 0 || len(queryParams.GroupBy) > 0 {
// If aggregates or groupings are specified, retrieve them
if len(queryParams.Aggregates) > 0 || len(queryParams.GroupBy) > 0 {
qf.Aggregates = queryParams.Aggregates
qf.GroupBy = queryParams.GroupBy

Expand Down
52 changes: 36 additions & 16 deletions internal/handlers/transactions_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,37 +134,57 @@ func handleTransactionsRequest(c *gin.Context, contractAddress, signature string
return
}

result, err := mainStorage.GetTransactions(storage.QueryFilter{
// Prepare the QueryFilter
qf := storage.QueryFilter{
FilterParams: queryParams.FilterParams,
GroupBy: queryParams.GroupBy,
ContractAddress: contractAddress,
Signature: signatureHash,
ChainId: chainId,
SortBy: queryParams.SortBy,
SortOrder: queryParams.SortOrder,
Page: queryParams.Page,
Limit: queryParams.Limit,
Aggregates: queryParams.Aggregates,
ContractAddress: contractAddress,
Signature: signatureHash,
ChainId: chainId,
})
if err != nil {
log.Error().Err(err).Msg("Error querying transactions")
api.InternalErrorHandler(c)
return
}

response := api.QueryResponse{
// Initialize the QueryResult
queryResult := api.QueryResponse{
Meta: api.Meta{
ChainId: chainId.Uint64(),
ContractAddress: contractAddress,
Signature: signature,
Signature: signatureHash,
Page: queryParams.Page,
Limit: queryParams.Limit,
TotalItems: 0, // TODO: Implement total items count
TotalItems: 0,
TotalPages: 0, // TODO: Implement total pages count
},
Data: result.Data,
Data: nil,
Aggregations: nil,
}

c.JSON(http.StatusOK, response)
// If aggregates or groupings are specified, retrieve them
if len(queryParams.Aggregates) > 0 || len(queryParams.GroupBy) > 0 {
qf.Aggregates = queryParams.Aggregates
qf.GroupBy = queryParams.GroupBy

aggregatesResult, err := mainStorage.GetAggregations("transactions", qf)
if err != nil {
log.Error().Err(err).Msg("Error querying aggregates")
api.InternalErrorHandler(c)
return
}
queryResult.Aggregations = aggregatesResult.Aggregates
queryResult.Meta.TotalItems = len(aggregatesResult.Aggregates)
} else {
// Retrieve logs data
transactionsResult, err := mainStorage.GetTransactions(qf)
if err != nil {
log.Error().Err(err).Msg("Error querying tran")
api.InternalErrorHandler(c)
return
}
queryResult.Data = transactionsResult.Data
queryResult.Meta.TotalItems = len(transactionsResult.Data)
}

c.JSON(http.StatusOK, queryResult)
}

0 comments on commit 803f833

Please sign in to comment.