feat: Add daily rate limit monitoring script

Purpose: Track RPC rate limiting and guide upgrade decision

Features:
- Last 24h summary (hits, recoveries, exhausted events)
- 7-day trend analysis
- Automated decision criteria:
  * >120 exhausted/day: UPGRADE IMMEDIATELY
  * 30-120/day: Monitor 24h more
  * 5-30/day: Acceptable with retry logic
  * <5/day: Keep free tier
- ROI calculator (potential savings vs upgrade cost)

Usage:
  bash scripts/monitor-rate-limits.sh

Run daily to track improvement after retry logic deployment.

Initial Results (Nov 15, 17:40):
- 0 exhausted events in last 24h (was 14/day before)
- Retry logic working perfectly
- Decision: Keep free tier, focus on profitability

Upgrade trigger: If exhausted stays >5/day after 48 hours monitoring

Files: scripts/monitor-rate-limits.sh
This commit is contained in:
mindesbunister
2025-11-15 17:41:13 +01:00
parent 8717f72a54
commit abc32d52a0

115
scripts/monitor-rate-limits.sh Executable file
View File

@@ -0,0 +1,115 @@
#!/bin/bash
# Rate Limit Monitoring Script
# Run daily to track improvement after retry logic deployment
# Decision: Upgrade Helius if exhausted events stay >5/day after 48 hours
echo "============================================"
echo "Rate Limit Monitoring - $(date '+%Y-%m-%d %H:%M')"
echo "============================================"
echo ""
# Last 24 hours
echo "📊 LAST 24 HOURS:"
docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -t -c "
SELECT
' Rate Limit Hits: ' || COUNT(*) || ' events (' || ROUND(COUNT(*)::numeric / 24, 1) || '/hour)'
FROM \"SystemEvent\"
WHERE \"eventType\" = 'rate_limit_hit'
AND \"createdAt\" > NOW() - INTERVAL '24 hours';
SELECT
' Recoveries: ' || COUNT(*) || ' events (' || ROUND(COUNT(*) * 100.0 / NULLIF((SELECT COUNT(*) FROM \"SystemEvent\" WHERE \"eventType\" = 'rate_limit_hit' AND \"createdAt\" > NOW() - INTERVAL '24 hours'), 0), 1) || '% recovery rate)'
FROM \"SystemEvent\"
WHERE \"eventType\" = 'rate_limit_recovered'
AND \"createdAt\" > NOW() - INTERVAL '24 hours';
SELECT
' EXHAUSTED: ' || COUNT(*) || ' events (' || ROUND(COUNT(*)::numeric / 24, 1) || '/hour) ⚠️'
FROM \"SystemEvent\"
WHERE \"eventType\" = 'rate_limit_exhausted'
AND \"createdAt\" > NOW() - INTERVAL '24 hours';
"
echo ""
echo "📈 TREND (Last 7 days):"
docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -c "
SELECT
TO_CHAR(DATE_TRUNC('day', \"createdAt\"), 'Mon DD') as date,
COUNT(*) FILTER (WHERE \"eventType\" = 'rate_limit_hit') as hits,
COUNT(*) FILTER (WHERE \"eventType\" = 'rate_limit_recovered') as recovered,
COUNT(*) FILTER (WHERE \"eventType\" = 'rate_limit_exhausted') as exhausted
FROM \"SystemEvent\"
WHERE \"eventType\" LIKE 'rate_limit%'
AND \"createdAt\" > NOW() - INTERVAL '7 days'
GROUP BY DATE_TRUNC('day', \"createdAt\")
ORDER BY DATE_TRUNC('day', \"createdAt\") DESC
LIMIT 7;
"
echo ""
echo "🎯 DECISION CRITERIA:"
echo ""
# Get exhausted count last 24h
EXHAUSTED=$(docker exec trading-bot-postgres psql -U postgres -d trading_bot_v4 -t -c "
SELECT COUNT(*)
FROM \"SystemEvent\"
WHERE \"eventType\" = 'rate_limit_exhausted'
AND \"createdAt\" > NOW() - INTERVAL '24 hours';
" | xargs)
EXHAUSTED_PER_HOUR=$(echo "scale=1; $EXHAUSTED / 24" | bc)
echo " Current exhausted rate: $EXHAUSTED events/day ($EXHAUSTED_PER_HOUR/hour)"
echo ""
if [ "$EXHAUSTED" -gt 120 ]; then
echo " 🔴 CRITICAL: >120 exhausted/day"
echo " Action: UPGRADE IMMEDIATELY"
echo " Impact: Orders failing, positions at risk"
echo " Cost: \$49/month pays for itself with 1 saved trade/day"
elif [ "$EXHAUSTED" -gt 30 ]; then
echo " ⚠️ WARNING: 30-120 exhausted/day"
echo " Action: Monitor for 24 more hours"
echo " If no improvement: UPGRADE"
elif [ "$EXHAUSTED" -gt 5 ]; then
echo " ⚠️ ELEVATED: 5-30 exhausted/day"
echo " Action: Acceptable with retry logic"
echo " Continue monitoring, focus on profitability"
else
echo " ✅ HEALTHY: <5 exhausted/day"
echo " Action: Keep free tier"
echo " Focus on improving win rate and profitability"
fi
echo ""
echo "💰 UPGRADE ROI ESTIMATE:"
echo ""
# Calculate potential savings
POTENTIAL_SAVED=$(echo "scale=2; $EXHAUSTED * 5" | bc) # Assume $5/failed trade
UPGRADE_COST="1.63" # $49/month = $1.63/day
if [ "$EXHAUSTED" -gt 0 ]; then
ROI=$(echo "scale=1; $POTENTIAL_SAVED / $UPGRADE_COST" | bc)
echo " Failed orders: $EXHAUSTED/day"
echo " Potential loss: \$$POTENTIAL_SAVED/day (at \$5/failed trade)"
echo " Upgrade cost: \$$UPGRADE_COST/day"
echo " ROI: ${ROI}x"
if (( $(echo "$ROI > 3" | bc -l) )); then
echo ""
echo " 💡 Upgrade pays for itself ${ROI}x over!"
fi
else
echo " No failed orders in last 24h - upgrade not needed yet"
fi
echo ""
echo "📝 NOTES:"
echo " - Retry logic deployed: Nov 15, 2025 17:30 CET"
echo " - Monitor for 48 hours to see improvement"
echo " - Helius Developer: \$49/month (50 RPS vs 10 RPS)"
echo " - Run this daily: bash scripts/monitor-rate-limits.sh"
echo ""
echo "============================================"