Skip to content

Commit 76a4bb9

Browse files
committed
data: actions destribution
1 parent 353382f commit 76a4bb9

File tree

7 files changed

+104
-13
lines changed

7 files changed

+104
-13
lines changed

Diff for: api/transaction.go

+13
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,19 @@ func GetActionVolumesByName(db *sql.DB) gin.HandlerFunc {
172172
}
173173
}
174174

175+
func GetTotalActionCounts(db *sql.DB) gin.HandlerFunc {
176+
return func(c *gin.Context) {
177+
totals, err := models.FetchActionVolumes(db)
178+
if err != nil {
179+
log.Printf("Error fetching action totals: %v", err)
180+
c.JSON(http.StatusInternalServerError, gin.H{"error": "Unable to retrieve action totals"})
181+
return
182+
}
183+
184+
c.JSON(http.StatusOK, totals)
185+
}
186+
}
187+
175188
// GetEstimatedFeeByActionType retrieves the estimated fee for a specific action type
176189
func GetEstimatedFeeByActionType(db *sql.DB) gin.HandlerFunc {
177190
return func(c *gin.Context) {

Diff for: db/db.go

+7
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,12 @@ func CreateSchema(db *sql.DB) error {
148148
last_updated TIMESTAMP NOT NULL
149149
);
150150
151+
CREATE TABLE IF NOT EXISTS action_volumes (
152+
action_type SMALLINT PRIMARY KEY,
153+
action_name TEXT NOT NULL,
154+
total_count BIGINT NOT NULL DEFAULT 0
155+
);
156+
151157
CREATE TABLE IF NOT EXISTS genesis_data (
152158
id SERIAL PRIMARY KEY,
153159
data JSON
@@ -178,6 +184,7 @@ func CreateSchema(db *sql.DB) error {
178184
CREATE INDEX IF NOT EXISTS idx_daily_health_summaries_date ON daily_health_summaries(date);
179185
CREATE INDEX IF NOT EXISTS idx_daily_health_summaries_state ON daily_health_summaries(state);
180186
CREATE INDEX IF NOT EXISTS idx_daily_health_summaries_last_updated ON daily_health_summaries(last_updated);
187+
CREATE INDEX IF NOT EXISTS idx_action_volumes_name ON action_volumes(action_name);
181188
182189
`
183190

Diff for: docs/rest_api/transactions.md

+22
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,28 @@
336336
}
337337
```
338338

339+
## Get Total Action Destribution Counts
340+
341+
- **Endpoint**: `/transactions/volumes/actions/total`
342+
- **Description**: Retrieves the all-time total count for each action type
343+
- **Example**: `curl http://localhost:8080/transactions/volumes/actions/total`
344+
- **Output**:
345+
346+
```json
347+
[
348+
{
349+
"action_type": 0,
350+
"action_name": "Transfer",
351+
"total_count": 8350724
352+
},
353+
{
354+
"action_type": 4,
355+
"action_name": "CreateAsset",
356+
"total_count": 37423
357+
}
358+
]
359+
```
360+
339361
## Get Aggregated Estimated Fees for different Transactions
340362

341363
- **Endpoint**: `/transactions/estimated_fee`

Diff for: main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ func main() {
6868
r.GET("/transactions/user/:user", api.GetTransactionsByUser(database))
6969
r.GET("/transactions/volumes", api.GetAllActionVolumes(database))
7070
r.GET("/transactions/volumes/:action_name", api.GetActionVolumesByName(database))
71-
r.GET("/transactions/volumes/total", api.GetTotalTransferVolume(database)) // Fetch transactions by user with pagination
71+
r.GET("/transactions/volumes/actions/total", api.GetTotalActionCounts(database)) // Fetch alltime actions volume
72+
r.GET("/transactions/volumes/total", api.GetTotalTransferVolume(database)) // Fetch alltime transfer volume
7273
r.GET("/transactions/estimated_fee/action_type/:action_type", api.GetEstimatedFeeByActionType(database)) // Fetch estimated fee by action type
7374
r.GET("/transactions/estimated_fee/action_name/:action_name", api.GetEstimatedFeeByActionName(database)) // Fetch estimated fee by action name
7475
r.GET("/transactions/estimated_fee", api.GetAggregateEstimatedFees(database)) // Fetch aggregate estimated fees

Diff for: models/transaction.go

+44-12
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ import (
1616
)
1717

1818
type Transaction struct {
19-
ID int `json:"ID"`
20-
TxHash string `json:"TxHash"`
21-
BlockHash string `json:"BlockHash"`
22-
BlockHeight int64 `json:"BlockHeight"`
23-
Sponsor string `json:"Sponsor"`
24-
Actors []string `json:"Actors"`
25-
Receivers []string `json:"Receivers"`
26-
MaxFee float64 `json:"MaxFee"`
27-
Success bool `json:"Success"`
28-
Fee uint64 `json:"Fee"`
29-
Actions []map[string]interface{} `json:"Actions"`
30-
Timestamp string `json:"Timestamp"`
19+
ID int `json:"ID"`
20+
TxHash string `json:"TxHash"`
21+
BlockHash string `json:"BlockHash"`
22+
BlockHeight int64 `json:"BlockHeight"`
23+
Sponsor string `json:"Sponsor"`
24+
Actors []string `json:"Actors"`
25+
Receivers []string `json:"Receivers"`
26+
MaxFee float64 `json:"MaxFee"`
27+
Success bool `json:"Success"`
28+
Fee uint64 `json:"Fee"`
29+
Actions []map[string]interface{} `json:"Actions"`
30+
Timestamp string `json:"Timestamp"`
3131
}
3232

3333
type TransactionVolumes struct {
@@ -51,6 +51,12 @@ type TotalVolume struct {
5151
Total uint64 `json:"total"`
5252
}
5353

54+
type ActionVolume struct {
55+
ActionType uint8 `json:"action_type"`
56+
ActionName string `json:"action_name"`
57+
TotalCount int64 `json:"total_count"`
58+
}
59+
5460
// CountFilteredTransactions counts transactions based on optional filters
5561
func CountFilteredTransactions(db *sql.DB, txHash, blockHash, actionType, actionName, user string) (int, error) {
5662
query, args := buildTransactionFilterQuery("COUNT(*)", txHash, blockHash, actionType, actionName, user)
@@ -398,6 +404,32 @@ func FetchActionVolumesByName(db *sql.DB, actionName string) (ActionVolumes, err
398404
return volume, nil
399405
}
400406

407+
// FetchActionVolumes retrieves all action volumes
408+
func FetchActionVolumes(db *sql.DB) ([]ActionVolume, error) {
409+
rows, err := db.Query(`
410+
SELECT action_type, action_name, total_count
411+
FROM action_volumes
412+
ORDER BY total_count DESC`)
413+
if err != nil {
414+
return nil, err
415+
}
416+
defer rows.Close()
417+
418+
var totals []ActionVolume
419+
for rows.Next() {
420+
var total ActionVolume
421+
if err := rows.Scan(
422+
&total.ActionType,
423+
&total.ActionName,
424+
&total.TotalCount,
425+
); err != nil {
426+
return nil, err
427+
}
428+
totals = append(totals, total)
429+
}
430+
return totals, rows.Err()
431+
}
432+
401433
// Helper function to scan transaction rows and unmarshal outputs
402434
func scanTransactions(rows *sql.Rows) ([]Transaction, error) {
403435
var transactions []Transaction

Diff for: server/accept_block.go

+5
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,11 @@ func processBlockData(dbConn *sql.DB, executedBlock *chain.ExecutedBlock) error
187187
log.Printf("Error saving action to database: %v\n", err)
188188
}
189189

190+
// Update the action total
191+
if err := updateActionVolume(dbConn, actionType, actionName); err != nil {
192+
log.Printf("Error updating action total: %v\n", err)
193+
}
194+
190195
// Handle special actions
191196
// Parse actionInputJSON into map[string]interface{}
192197
var actionInput map[string]interface{}

Diff for: server/process_actions.go

+11
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,14 @@ func processRegisterValidatorStakeID(dbConn *sql.DB, actionOutput map[string]int
6767
)
6868
return err
6969
}
70+
71+
// Update the action volume in psql
72+
func updateActionVolume(db *sql.DB, actionType uint8, actionName string) error {
73+
_, err := db.Exec(`
74+
INSERT INTO action_volumes (action_type, action_name, total_count)
75+
VALUES ($1, $2, 1)
76+
ON CONFLICT (action_type) DO UPDATE
77+
SET total_count = action_volumes.total_count + 1`,
78+
actionType, actionName)
79+
return err
80+
}

0 commit comments

Comments
 (0)