Skip to main content
Glama
jjsteffen23

CME Prediction Markets MCP Server

by jjsteffen23
production_setup.pyβ€’6.35 kB
""" Production deployment and optimization scripts. """ import asyncio import asyncpg from sqlalchemy import create_engine, text from src.config.production import OptimizationConfig, ProductionSettings async def create_production_indexes(): """Create optimized database indexes for production workloads.""" settings = ProductionSettings() # Connect to database engine = create_engine(settings.DATABASE_URL, **OptimizationConfig.DB_POOL_CONFIG) print("πŸ”§ Creating production database indexes...") with engine.connect() as conn: for index_sql in OptimizationConfig.RECOMMENDED_INDEXES: try: print(f"Creating index: {index_sql[:50]}...") conn.execute(text(index_sql)) conn.commit() print("βœ… Success") except Exception as e: print(f"❌ Error: {e}") print("πŸŽ‰ Production indexes created!") async def create_materialized_views(): """Create materialized views for common queries.""" views = [ """ CREATE MATERIALIZED VIEW IF NOT EXISTS mv_daily_trading_stats AS SELECT t.instrument_guid_long, c.description, DATE(t.timestamp) as trade_date, COUNT(*) as trade_count, AVG(t.price) as avg_price, SUM(t.volume) as total_volume, MIN(t.price) as low_price, MAX(t.price) as high_price, (ARRAY_AGG(t.price ORDER BY t.timestamp))[1] as open_price, (ARRAY_AGG(t.price ORDER BY t.timestamp DESC))[1] as close_price FROM trades t JOIN contracts c ON t.contract_id = c.id WHERE t.timestamp >= CURRENT_DATE - INTERVAL '30 days' GROUP BY t.instrument_guid_long, c.description, DATE(t.timestamp); """, """ CREATE MATERIALIZED VIEW IF NOT EXISTS mv_popular_contracts AS SELECT c.id, c.symbol, c.description, c.contract_type, COUNT(t.id) as trade_count, SUM(t.volume) as total_volume, MAX(t.timestamp) as last_trade FROM contracts c LEFT JOIN trades t ON c.id = t.contract_id WHERE c.is_active = true GROUP BY c.id, c.symbol, c.description, c.contract_type ORDER BY trade_count DESC, total_volume DESC LIMIT 100; """, """ CREATE MATERIALIZED VIEW IF NOT EXISTS mv_market_summary AS SELECT c.contract_type, COUNT(DISTINCT c.id) as active_contracts, COUNT(t.id) as total_trades, SUM(t.volume) as total_volume, AVG(t.price) as avg_price, MAX(t.timestamp) as last_activity FROM contracts c LEFT JOIN trades t ON c.id = t.contract_id WHERE c.is_active = true GROUP BY c.contract_type; """ ] settings = ProductionSettings() engine = create_engine(settings.DATABASE_URL, **OptimizationConfig.DB_POOL_CONFIG) print("πŸ“Š Creating materialized views...") with engine.connect() as conn: for view_sql in views: try: conn.execute(text(view_sql)) conn.commit() print("βœ… Materialized view created") except Exception as e: print(f"❌ Error: {e}") # Create refresh schedule refresh_sql = """ -- Create function to refresh materialized views CREATE OR REPLACE FUNCTION refresh_market_views() RETURNS void AS $$ BEGIN REFRESH MATERIALIZED VIEW CONCURRENTLY mv_daily_trading_stats; REFRESH MATERIALIZED VIEW CONCURRENTLY mv_popular_contracts; REFRESH MATERIALIZED VIEW CONCURRENTLY mv_market_summary; END; $$ LANGUAGE plpgsql; -- Schedule refresh every 15 minutes (requires pg_cron extension) -- SELECT cron.schedule('refresh-views', '*/15 * * * *', 'SELECT refresh_market_views();'); """ try: with engine.connect() as conn: conn.execute(text(refresh_sql)) conn.commit() print("βœ… Refresh function created") except Exception as e: print(f"❌ Error creating refresh function: {e}") def optimize_chat_performance(): """Implement chat performance optimizations.""" optimizations = { "response_caching": "βœ… Implemented - 5min cache for responses", "rate_limiting": "βœ… Implemented - 30 req/min per IP", "session_management": "βœ… Implemented - Session tracking", "input_validation": "βœ… Implemented - Length & content validation", "async_processing": "βœ… Implemented - Non-blocking operations", } print("πŸš€ Chat Performance Optimizations:") for feature, status in optimizations.items(): print(f" {status} {feature}") def print_production_checklist(): """Print production deployment checklist.""" checklist = [ "πŸ”’ Security: API keys, HTTPS, input validation", "πŸ“ˆ Monitoring: Prometheus, Grafana, health checks", "πŸ—οΈ Infrastructure: Load balancer, auto-scaling", "πŸ’Ύ Database: Connection pooling, indexes, backups", "⚑ Performance: Caching, materialized views, CDN", "πŸ›‘οΈ Rate Limiting: Per-user, per-endpoint limits", "πŸ“Š Analytics: Request tracking, error monitoring", "πŸ”„ CI/CD: Automated testing, deployment pipeline", "πŸ§ͺ Testing: Load testing, integration tests", "πŸ“š Documentation: API docs, operational runbooks" ] print("\n🎯 **PRODUCTION READINESS CHECKLIST**\n") for item in checklist: print(f" {item}") print("\nπŸš€ **DEPLOYMENT COMMANDS**") print(" docker-compose -f docker-compose.prod.yml up -d") print(" python scripts/production_setup.py") print(" python -m pytest tests/ --cov=src/") async def main(): """Run all production setup tasks.""" print("πŸš€ Setting up production environment...\n") await create_production_indexes() print() await create_materialized_views() print() optimize_chat_performance() print() print_production_checklist() if __name__ == "__main__": asyncio.run(main())

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jjsteffen23/dk_mcp_2'

If you have feedback or need assistance with the MCP directory API, please join our Discord server