- Add backend/libs/go-http-errors for consistent JSON errors - REST API: use writeMethodNotAllowed, writeNotFound, writeInternalError - middleware, gateway, search: use httperrors.WriteJSON - SPA: navbar with Explore/Tools/More dropdowns, initNavDropdowns() - Next.js: Navbar component with dropdowns + mobile menu Co-authored-by: Cursor <cursoragent@cursor.com>
63 lines
1.3 KiB
Go
63 lines
1.3 KiB
Go
package rest
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"time"
|
|
)
|
|
|
|
// handleStats handles GET /api/v2/stats
|
|
func (s *Server) handleStats(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
writeMethodNotAllowed(w)
|
|
return
|
|
}
|
|
if !s.requireDB(w) {
|
|
return
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
|
defer cancel()
|
|
|
|
// Get total blocks
|
|
var totalBlocks int64
|
|
err := s.db.QueryRow(ctx,
|
|
`SELECT COUNT(*) FROM blocks WHERE chain_id = $1`,
|
|
s.chainID,
|
|
).Scan(&totalBlocks)
|
|
if err != nil {
|
|
totalBlocks = 0
|
|
}
|
|
|
|
// Get total transactions
|
|
var totalTransactions int64
|
|
err = s.db.QueryRow(ctx,
|
|
`SELECT COUNT(*) FROM transactions WHERE chain_id = $1`,
|
|
s.chainID,
|
|
).Scan(&totalTransactions)
|
|
if err != nil {
|
|
totalTransactions = 0
|
|
}
|
|
|
|
// Get total addresses
|
|
var totalAddresses int64
|
|
err = s.db.QueryRow(ctx,
|
|
`SELECT COUNT(DISTINCT from_address) + COUNT(DISTINCT to_address) FROM transactions WHERE chain_id = $1`,
|
|
s.chainID,
|
|
).Scan(&totalAddresses)
|
|
if err != nil {
|
|
totalAddresses = 0
|
|
}
|
|
|
|
stats := map[string]interface{}{
|
|
"total_blocks": totalBlocks,
|
|
"total_transactions": totalTransactions,
|
|
"total_addresses": totalAddresses,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
json.NewEncoder(w).Encode(stats)
|
|
}
|
|
|