Skip to content

Commit e782547

Browse files
committed
feat: enable aggregates and group_by
1 parent a55695d commit e782547

File tree

9 files changed

+56
-19
lines changed

9 files changed

+56
-19
lines changed

api/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ type QueryParams struct {
3030
// @Description Map of filter parameters
3131
FilterParams map[string]string `schema:"-"`
3232
// @Description Field to group results by
33-
GroupBy string `schema:"group_by"`
33+
GroupBy []string `schema:"group_by"`
3434
// @Description Field to sort results by
3535
SortBy string `schema:"sort_by"`
3636
// @Description Sort order (asc or desc)

internal/handlers/logs_handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func handleLogsRequest(c *gin.Context, contractAddress, signature string) {
135135

136136
logs, err := mainStorage.GetLogs(storage.QueryFilter{
137137
FilterParams: queryParams.FilterParams,
138-
GroupBy: []string{queryParams.GroupBy},
138+
GroupBy: queryParams.GroupBy,
139139
SortBy: queryParams.SortBy,
140140
SortOrder: queryParams.SortOrder,
141141
Page: queryParams.Page,

internal/handlers/transactions_handlers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ func handleTransactionsRequest(c *gin.Context, contractAddress, signature string
136136

137137
result, err := mainStorage.GetTransactions(storage.QueryFilter{
138138
FilterParams: queryParams.FilterParams,
139-
GroupBy: []string{queryParams.GroupBy},
139+
GroupBy: queryParams.GroupBy,
140140
SortBy: queryParams.SortBy,
141141
SortOrder: queryParams.SortOrder,
142142
Page: queryParams.Page,

internal/storage/clickhouse.go

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,19 @@ func (c *ClickHouseConnector) GetTransactions(qf QueryFilter) (QueryResult[commo
342342
return executeQuery[common.Transaction](c, "transactions", columns, qf, scanTransaction)
343343
}
344344

345-
func (c *ClickHouseConnector) GetLogs(qf QueryFilter) (QueryResult[common.Log], error) {
346-
columns := "chain_id, block_number, block_hash, block_timestamp, transaction_hash, transaction_index, log_index, address, data, topic_0, topic_1, topic_2, topic_3"
347-
return executeQuery[common.Log](c, "logs", columns, qf, scanLog)
345+
func (c *ClickHouseConnector) GetLogs(qf QueryFilter) (QueryResult[map[string]interface{}], error) {
346+
var columns string
347+
348+
if len(qf.GroupBy) > 0 || len(qf.Aggregates) > 0 {
349+
// Build columns for SELECT when grouping or aggregating
350+
selectColumns := append(qf.GroupBy, qf.Aggregates...)
351+
columns = strings.Join(selectColumns, ", ")
352+
} else {
353+
// Default columns when not grouping
354+
columns = "chain_id, block_number, block_hash, block_timestamp, transaction_hash, transaction_index, log_index, address, data, topic_0, topic_1, topic_2, topic_3"
355+
}
356+
357+
return executeQuery[map[string]interface{}](c, "logs", columns, qf, scanRowToMap)
348358
}
349359

350360
func executeQuery[T any](c *ClickHouseConnector, table, columns string, qf QueryFilter, scanFunc func(driver.Rows) (T, error)) (QueryResult[T], error) {
@@ -397,7 +407,13 @@ func (c *ClickHouseConnector) buildQuery(table, columns string, qf QueryFilter)
397407
query = addFilterParams(key, strings.ToLower(value), query)
398408
}
399409

400-
// Add sort by clause
410+
// Add GROUP BY clause if specified
411+
if len(qf.GroupBy) > 0 {
412+
groupByColumns := strings.Join(qf.GroupBy, ", ")
413+
query += fmt.Sprintf(" GROUP BY %s", groupByColumns)
414+
}
415+
416+
// Add ORDER BY clause
401417
if qf.SortBy != "" {
402418
query += fmt.Sprintf(" ORDER BY %s %s", qf.SortBy, qf.SortOrder)
403419
}
@@ -556,6 +572,27 @@ func scanLog(rows driver.Rows) (common.Log, error) {
556572
return log, nil
557573
}
558574

575+
func scanRowToMap(rows driver.Rows) (map[string]interface{}, error) {
576+
columns := rows.Columns()
577+
values := make([]interface{}, len(columns))
578+
valuePtrs := make([]interface{}, len(columns))
579+
580+
for i := range columns {
581+
valuePtrs[i] = &values[i]
582+
}
583+
584+
if err := rows.Scan(valuePtrs...); err != nil {
585+
return nil, err
586+
}
587+
588+
result := make(map[string]interface{})
589+
for i, col := range columns {
590+
result[col] = values[i]
591+
}
592+
593+
return result, nil
594+
}
595+
559596
func (c *ClickHouseConnector) GetMaxBlockNumber(chainId *big.Int) (maxBlockNumber *big.Int, err error) {
560597
query := fmt.Sprintf("SELECT number FROM %s.blocks WHERE is_deleted = 0", c.cfg.Database)
561598
if chainId.Sign() > 0 {

internal/storage/connector.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type IMainStorage interface {
5454

5555
GetBlocks(qf QueryFilter) (blocks []common.Block, err error)
5656
GetTransactions(qf QueryFilter) (transactions QueryResult[common.Transaction], err error)
57-
GetLogs(qf QueryFilter) (logs QueryResult[common.Log], err error)
57+
GetLogs(qf QueryFilter) (logs QueryResult[map[string]interface{}], err error)
5858
GetTraces(qf QueryFilter) (traces []common.Trace, err error)
5959
GetMaxBlockNumber(chainId *big.Int) (maxBlockNumber *big.Int, err error)
6060
/**

test/mocks/MockIMainStorage.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/mocks/MockIOrchestratorStorage.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/mocks/MockIRPCClient.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/mocks/MockIStagingStorage.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)