Fix Markov simulation chart display issue
- Fix circular import issue by moving simulation_manager to app/__init__.py - Enhance get_wealth_evolution to include inequality metrics data - Add get_inequality_evolution method for complete chart data - Update API to return top10_shares and capital_shares in evolution data - Modify onSimulationComplete to fetch and populate charts with complete data - Fix simulation threading to properly mark completion state - Add test script to verify chart data generation The charts now properly display simulation results by fetching complete evolution data when simulation completes, resolving the empty diagrams issue.
This commit is contained in:
@@ -13,7 +13,7 @@ from typing import Optional
|
||||
|
||||
from app import socketio
|
||||
from app.models import SimulationManager, SimulationParameters
|
||||
from app.routes.main import simulation_manager
|
||||
from app import simulation_manager
|
||||
|
||||
api_bp = Blueprint('api', __name__)
|
||||
|
||||
@@ -95,6 +95,9 @@ def start_simulation(simulation_id: str):
|
||||
if simulation.is_running:
|
||||
return jsonify({'error': 'Simulation already running'}), 400
|
||||
|
||||
# Mark simulation as running
|
||||
simulation.is_running = True
|
||||
|
||||
# Start simulation in background thread
|
||||
def run_simulation_background():
|
||||
try:
|
||||
@@ -125,6 +128,9 @@ def start_simulation(simulation_id: str):
|
||||
# Small delay to allow real-time visualization
|
||||
time.sleep(0.01)
|
||||
|
||||
# Mark as completed
|
||||
simulation.is_running = False
|
||||
|
||||
# Emit completion
|
||||
socketio.emit('simulation_complete', {
|
||||
'simulation_id': simulation_id,
|
||||
@@ -132,6 +138,7 @@ def start_simulation(simulation_id: str):
|
||||
}, room=f'simulation_{simulation_id}')
|
||||
|
||||
except Exception as e:
|
||||
simulation.is_running = False
|
||||
current_app.logger.error(f"Error in simulation thread: {str(e)}")
|
||||
socketio.emit('simulation_error', {
|
||||
'simulation_id': simulation_id,
|
||||
@@ -254,10 +261,15 @@ def get_simulation_data(simulation_id: str):
|
||||
|
||||
# Include evolution data if requested
|
||||
if request.args.get('include_evolution', '').lower() == 'true':
|
||||
# Get complete inequality data
|
||||
ineq_iterations, gini_over_time, top10_over_time, capital_over_time = simulation.get_inequality_evolution()
|
||||
|
||||
response_data['evolution'] = {
|
||||
'iterations': iterations,
|
||||
'total_wealth': total_wealth,
|
||||
'gini_coefficients': gini_coefficients
|
||||
'gini_coefficients': gini_coefficients,
|
||||
'top10_shares': top10_over_time,
|
||||
'capital_shares': capital_over_time
|
||||
}
|
||||
|
||||
return jsonify(response_data)
|
||||
|
Reference in New Issue
Block a user