|
| 1 | +""" |
| 2 | +Orchestration logic for national-with-breakdowns simulations. |
| 3 | +
|
| 4 | +This module handles spawning 52 parallel simulations (1 national + 51 states) |
| 5 | +and aggregating the results into a single response with congressional district breakdowns. |
| 6 | +""" |
| 7 | + |
| 8 | +import logfire |
| 9 | +from typing import Any, Callable |
| 10 | + |
| 11 | +from src.modal.utils.state_codes import STATE_CODES, TEST_STATE_CODES |
| 12 | + |
| 13 | +# Re-export for backwards compatibility |
| 14 | +__all__ = ["STATE_CODES", "TEST_STATE_CODES", "run_national_orchestration"] |
| 15 | + |
| 16 | + |
| 17 | +def run_national_orchestration( |
| 18 | + params: dict, |
| 19 | + run_simulation: Callable, |
| 20 | + state_codes: list[str] | None = None, |
| 21 | +) -> dict: |
| 22 | + """ |
| 23 | + Orchestrate parallel simulations and aggregate results. |
| 24 | +
|
| 25 | + Spawns: |
| 26 | + - 1 national ECPS simulation (region="us") |
| 27 | + - State-level simulations for each state in state_codes (or all 51 if not specified) |
| 28 | +
|
| 29 | + Each spawned job runs in its own container via run_simulation. |
| 30 | +
|
| 31 | + Partial failure handling: |
| 32 | + - If ALL states fail, the entire request fails |
| 33 | + - If SOME states fail, the request succeeds with null values for failed states |
| 34 | +
|
| 35 | + Args: |
| 36 | + params: Base simulation parameters (reform, baseline, time_period, etc.) |
| 37 | + run_simulation: The Modal function to spawn for each simulation |
| 38 | + state_codes: Optional list of state codes to run. If None, runs all 51. |
| 39 | +
|
| 40 | + Returns: |
| 41 | + Aggregated result with national metrics + all congressional district breakdowns |
| 42 | + """ |
| 43 | + states_to_run = state_codes if state_codes is not None else STATE_CODES |
| 44 | + |
| 45 | + # Prepare base params (remove the special data flag) |
| 46 | + base_params = {k: v for k, v in params.items() if k != "data"} |
| 47 | + |
| 48 | + # 1. Spawn national ECPS simulation |
| 49 | + logfire.info("Spawning national ECPS simulation") |
| 50 | + national_params = { |
| 51 | + **base_params, |
| 52 | + "region": "us", |
| 53 | + # data=None lets policyengine use default ECPS dataset |
| 54 | + } |
| 55 | + national_call = run_simulation.spawn(national_params) |
| 56 | + |
| 57 | + # 2. Spawn state simulations (each gets its own container) |
| 58 | + logfire.info("Spawning state-level simulations", state_count=len(states_to_run)) |
| 59 | + state_calls: dict[str, Any] = {} |
| 60 | + for state_code in states_to_run: |
| 61 | + state_params = { |
| 62 | + **base_params, |
| 63 | + "region": f"state/{state_code.lower()}", |
| 64 | + # data=None lets get_default_dataset resolve to states/{CODE}.h5 |
| 65 | + } |
| 66 | + state_calls[state_code] = run_simulation.spawn(state_params) |
| 67 | + |
| 68 | + logfire.info( |
| 69 | + "All simulations spawned, waiting for results", |
| 70 | + total_jobs=len(states_to_run) + 1, |
| 71 | + ) |
| 72 | + |
| 73 | + # 3. Wait for national result first |
| 74 | + logfire.info("Waiting for national ECPS result") |
| 75 | + national_result = national_call.get() |
| 76 | + logfire.info("National ECPS simulation complete") |
| 77 | + |
| 78 | + # 4. Wait for all state results and extract district data |
| 79 | + all_districts: list[dict] = [] |
| 80 | + failed_states: list[str] = [] |
| 81 | + successful_states: list[str] = [] |
| 82 | + |
| 83 | + for state_code in states_to_run: |
| 84 | + logfire.info("Waiting for state result", state_code=state_code) |
| 85 | + call = state_calls[state_code] |
| 86 | + |
| 87 | + try: |
| 88 | + state_result = call.get() |
| 89 | + |
| 90 | + # Extract congressional_district_impact.districts from state result |
| 91 | + district_impact = state_result.get("congressional_district_impact", {}) |
| 92 | + districts = district_impact.get("districts", []) |
| 93 | + logfire.info( |
| 94 | + "State result received", |
| 95 | + state_code=state_code, |
| 96 | + districts_extracted=len(districts), |
| 97 | + ) |
| 98 | + all_districts.extend(districts) |
| 99 | + successful_states.append(state_code) |
| 100 | + |
| 101 | + except Exception as e: |
| 102 | + logfire.warn( |
| 103 | + "State simulation failed", |
| 104 | + state_code=state_code, |
| 105 | + error=str(e)[:200], |
| 106 | + ) |
| 107 | + failed_states.append(state_code) |
| 108 | + # Add null placeholder for each district in this state |
| 109 | + # We don't know how many districts, so we skip adding placeholders |
| 110 | + # The response will simply be missing these districts |
| 111 | + |
| 112 | + logfire.info( |
| 113 | + "State simulations complete", |
| 114 | + successful_count=len(successful_states), |
| 115 | + failed_count=len(failed_states), |
| 116 | + ) |
| 117 | + |
| 118 | + # 5. Check if ALL states failed |
| 119 | + if len(failed_states) == len(states_to_run): |
| 120 | + raise RuntimeError( |
| 121 | + f"All {len(states_to_run)} state simulations failed. " |
| 122 | + f"Failed states: {failed_states}" |
| 123 | + ) |
| 124 | + |
| 125 | + if failed_states: |
| 126 | + logfire.warn("Some states failed", failed_states=failed_states) |
| 127 | + |
| 128 | + logfire.info("Total districts collected", total_districts=len(all_districts)) |
| 129 | + |
| 130 | + # 6. Merge: national result + aggregated districts + metadata |
| 131 | + final_result = national_result.copy() |
| 132 | + final_result["congressional_district_impact"] = { |
| 133 | + "districts": all_districts, |
| 134 | + "failed_states": failed_states if failed_states else None, |
| 135 | + "successful_states": successful_states, |
| 136 | + } |
| 137 | + |
| 138 | + return final_result |
0 commit comments