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) }