
- Create project structure with FastAPI - Add database models for blocks, transactions, arbitrages, pools, and DEXes - Implement Solana RPC client for fetching blockchain data - Create arbitrage detection algorithm - Implement comprehensive API endpoints for analytics - Set up database migrations with Alembic - Add detailed project documentation generated with BackendIM... (backend.im) Co-Authored-By: Claude <noreply@anthropic.com>
97 lines
2.6 KiB
Python
97 lines
2.6 KiB
Python
from typing import Any, Dict, List, Optional
|
|
from datetime import datetime, timedelta
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app import crud
|
|
from app.api import deps
|
|
from app.services.worker import worker
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.get("/", response_model=Dict[str, Any])
|
|
def read_stats(
|
|
db: Session = Depends(deps.get_db),
|
|
) -> Any:
|
|
"""
|
|
Get overall analytics statistics.
|
|
"""
|
|
# Blocks stats
|
|
block_count = crud.block.get_count(db)
|
|
latest_blocks = crud.block.get_latest_blocks(db, limit=1)
|
|
latest_block_height = latest_blocks[0].block_height if latest_blocks else 0
|
|
|
|
# Last 24 hours block count
|
|
now = datetime.utcnow()
|
|
yesterday = now - timedelta(days=1)
|
|
blocks_24h = crud.block.get_block_count_by_time_range(
|
|
db, start_time=yesterday, end_time=now
|
|
)
|
|
|
|
# Transactions stats
|
|
tx_count = crud.transaction.get_count(db)
|
|
|
|
# DEX stats
|
|
dex_count = crud.dex.get_count(db)
|
|
|
|
# Pool stats
|
|
pool_count = crud.pool.get_count(db)
|
|
|
|
# Get arbitrage stats
|
|
arbitrage_stats = crud.arbitrage.get_arbitrage_stats(db)
|
|
|
|
return {
|
|
"block_stats": {
|
|
"total_blocks": block_count,
|
|
"latest_block_height": latest_block_height,
|
|
"blocks_last_24h": blocks_24h
|
|
},
|
|
"transaction_stats": {
|
|
"total_transactions": tx_count
|
|
},
|
|
"dex_stats": {
|
|
"total_dexes": dex_count
|
|
},
|
|
"pool_stats": {
|
|
"total_pools": pool_count
|
|
},
|
|
"arbitrage_stats": arbitrage_stats
|
|
}
|
|
|
|
|
|
@router.post("/worker/start-fetcher", status_code=200)
|
|
def start_blockchain_fetcher() -> Dict[str, Any]:
|
|
"""
|
|
Start the blockchain fetcher background worker.
|
|
"""
|
|
success = worker.start_fetcher()
|
|
return {
|
|
"success": success,
|
|
"message": "Blockchain fetcher started" if success else "Blockchain fetcher already running"
|
|
}
|
|
|
|
|
|
@router.post("/worker/start-detector", status_code=200)
|
|
def start_arbitrage_detector() -> Dict[str, Any]:
|
|
"""
|
|
Start the arbitrage detector background worker.
|
|
"""
|
|
success = worker.start_detector()
|
|
return {
|
|
"success": success,
|
|
"message": "Arbitrage detector started" if success else "Arbitrage detector already running"
|
|
}
|
|
|
|
|
|
@router.post("/worker/stop", status_code=200)
|
|
def stop_workers() -> Dict[str, Any]:
|
|
"""
|
|
Stop all background workers.
|
|
"""
|
|
success = worker.stop()
|
|
return {
|
|
"success": success,
|
|
"message": "All workers stopped" if success else "No workers were running"
|
|
} |