Files
capitalism-eats-the-world/app/routes/main.py
Markus Busche 97e9e18ee5 Enhanced: Comprehensive production debugging and Nginx proxy support
Debugging Tools:
- Added /debug endpoint to verify deployment and test distribution functionality
- Added browser console test function: testDistributionChart()
- Enhanced SocketIO settings with better transport priorities
- Added comprehensive error tracking and status reporting

Nginx Configuration:
- Created NGINX_CONFIG.md with complete proxy setup guide
- Improved WebSocket transport settings and timeouts
- Better ping/pong settings for connection stability

Production Compatibility:
- Optimized SocketIO for proxy environments
- Enhanced fallback mechanisms
- Production-ready logging and error handling

This should resolve proxy-related issues and provide clear diagnostic tools.
2025-08-24 18:53:09 +00:00

202 lines
5.5 KiB
Python

"""
Main Flask Routes
Handles the primary web interface for the Markov economics simulation,
including the main simulation page and basic navigation.
"""
from flask import Blueprint, render_template, request, jsonify
from app.models import SimulationManager, SimulationParameters
main_bp = Blueprint('main', __name__)
# Import global simulation manager instance
from app import simulation_manager
@main_bp.route('/')
def index():
"""
Main simulation interface page.
Returns:
Rendered HTML template with simulation controls
"""
# Default parameters for display
default_params = {
'r_rate': 0.05, # 5% capital return rate
'g_rate': 0.03, # 3% economic growth rate
'initial_capital': 1000.0,
'initial_consumption': 1000.0,
'num_agents': 100,
'iterations': 1000
}
return render_template('simulation.html',
default_params=default_params,
title="Markov Economics - Capitalism Eats the World")
@main_bp.route('/simulation')
def simulation():
"""
Alternative route to the main simulation page.
Returns:
Rendered HTML template with simulation controls
"""
return index()
@main_bp.route('/about')
def about():
"""
Information page about the economic theory behind the simulation.
Returns:
Rendered HTML template with theoretical background
"""
return render_template('about.html',
title="About - Markov Economics Theory")
@main_bp.route('/results/<simulation_id>')
def results(simulation_id):
"""
Display results for a specific simulation.
Args:
simulation_id: Unique identifier for the simulation
Returns:
Rendered results template or 404 if simulation not found
"""
simulation = simulation_manager.get_simulation(simulation_id)
if not simulation:
return render_template('error.html',
error_message=f"Simulation {simulation_id} not found",
title="Simulation Not Found"), 404
# Get simulation summary data
latest_snapshot = simulation.get_latest_snapshot()
iterations, total_wealth, gini_coefficients = simulation.get_wealth_evolution()
summary_data = {
'simulation_id': simulation_id,
'parameters': {
'r_rate': simulation.parameters.r_rate,
'g_rate': simulation.parameters.g_rate,
'num_agents': simulation.parameters.num_agents,
'iterations': simulation.parameters.iterations
},
'current_iteration': simulation.current_iteration,
'total_iterations': len(simulation.snapshots),
'latest_snapshot': latest_snapshot,
'has_data': len(simulation.snapshots) > 0
}
return render_template('results.html',
simulation_data=summary_data,
title=f"Results - Simulation {simulation_id[:8]}")
@main_bp.route('/debug')
def debug_info():
"""
Debug endpoint to verify deployment and test functionality.
Returns:
JSON response with debug information
"""
import os
from datetime import datetime
from app.models.economic_model import EconomicSimulation, SimulationParameters
# Test distribution functionality
test_params = SimulationParameters(
r_rate=0.05,
g_rate=0.03,
initial_capital=1000,
initial_consumption=1000,
num_agents=10,
iterations=5
)
test_sim = EconomicSimulation(test_params)
# Run a few steps
for _ in range(5):
test_sim.step()
# Get distribution data
labels, counts = test_sim.get_wealth_histogram(5)
debug_data = {
'timestamp': datetime.now().isoformat(),
'environment': os.getenv('FLASK_ENV', 'unknown'),
'config': os.getenv('FLASK_CONFIG', 'unknown'),
'python_version': os.sys.version,
'distribution_test': {
'labels': labels,
'counts': counts,
'total_agents': len(test_sim.agents),
'snapshots': len(test_sim.snapshots)
},
'fixes_deployed': {
'fallback_polling': True,
'enhanced_socketio': True,
'production_debug': True,
'environment_fix': True
},
'version_info': 'v2.1-proxy-fixes'
}
return jsonify(debug_data)
@main_bp.route('/health')
def health_check():
"""
Simple health check endpoint.
Returns:
JSON response indicating service status
"""
return jsonify({
'status': 'healthy',
'service': 'markov-economics',
'active_simulations': len(simulation_manager.active_simulations),
'total_simulations': len(simulation_manager.simulations)
})
@main_bp.errorhandler(404)
def not_found_error(error):
"""
Handle 404 errors with custom template.
Args:
error: The error object
Returns:
Rendered error template
"""
return render_template('error.html',
error_message="Page not found",
title="Page Not Found"), 404
@main_bp.errorhandler(500)
def internal_error(error):
"""
Handle 500 errors with custom template.
Args:
error: The error object
Returns:
Rendered error template
"""
return render_template('error.html',
error_message="Internal server error occurred",
title="Server Error"), 500