|
21 | 21 | normalize_us_region, |
22 | 22 | ) |
23 | 23 | from policyengine_api.data.places import validate_place_code |
| 24 | +from policyengine_api.utils import budget_window as budget_window_utils |
24 | 25 | from policyengine.simulation import SimulationOptions |
25 | 26 | from policyengine.utils.data.datasets import get_default_dataset |
26 | 27 | import json |
@@ -73,9 +74,9 @@ class ImpactStatus(Enum): |
73 | 74 |
|
74 | 75 | COMPLETE_STATUSES = [ImpactStatus.OK.value, ImpactStatus.ERROR.value] |
75 | 76 | COMPUTING_STATUS = ImpactStatus.COMPUTING.value |
76 | | -BUDGET_WINDOW_MAX_ACTIVE_YEARS = 20 |
77 | | -BUDGET_WINDOW_MAX_YEARS = 75 |
78 | | -BUDGET_WINDOW_MAX_END_YEAR = 2099 |
| 77 | +BUDGET_WINDOW_MAX_ACTIVE_YEARS = budget_window_utils.BUDGET_WINDOW_MAX_ACTIVE_YEARS |
| 78 | +BUDGET_WINDOW_MAX_YEARS = budget_window_utils.BUDGET_WINDOW_MAX_YEARS |
| 79 | +BUDGET_WINDOW_MAX_END_YEAR = budget_window_utils.BUDGET_WINDOW_MAX_END_YEAR |
79 | 80 |
|
80 | 81 |
|
81 | 82 | class EconomicImpactSetupOptions(BaseModel): |
@@ -296,36 +297,21 @@ def get_budget_window_economic_impact( |
296 | 297 | if country_id == "us": |
297 | 298 | region = normalize_us_region(region) |
298 | 299 |
|
299 | | - if target != "general": |
300 | | - raise ValueError( |
301 | | - "Budget-window calculations only support target='general'" |
302 | | - ) |
303 | | - |
304 | | - start_year_int = int(start_year) |
305 | | - if not 1 <= window_size <= BUDGET_WINDOW_MAX_YEARS: |
306 | | - raise ValueError( |
307 | | - f"window_size must be between 1 and {BUDGET_WINDOW_MAX_YEARS}" |
308 | | - ) |
309 | | - end_year = start_year_int + window_size - 1 |
310 | | - if end_year > BUDGET_WINDOW_MAX_END_YEAR: |
311 | | - raise ValueError( |
312 | | - f"budget-window end_year must be {BUDGET_WINDOW_MAX_END_YEAR} or earlier" |
313 | | - ) |
314 | | - |
315 | | - start_year = str(start_year_int) |
316 | | - years = self._build_budget_window_years( |
| 300 | + budget_window_setup = budget_window_utils.build_budget_window_request_setup( |
317 | 301 | start_year=start_year, |
318 | 302 | window_size=window_size, |
| 303 | + target=target, |
319 | 304 | ) |
320 | | - setup_options = self._build_budget_window_setup_options( |
| 305 | + start_year = budget_window_setup.start_year |
| 306 | + years = budget_window_setup.years |
| 307 | + setup_options = self._build_economic_impact_setup_options( |
321 | 308 | country_id=country_id, |
322 | 309 | policy_id=policy_id, |
323 | 310 | baseline_policy_id=baseline_policy_id, |
324 | 311 | region=region, |
325 | 312 | dataset=dataset, |
326 | | - start_year=start_year, |
327 | | - window_size=window_size, |
328 | | - options=options, |
| 313 | + time_period=budget_window_setup.time_period, |
| 314 | + options=dict(options), |
329 | 315 | api_version=api_version, |
330 | 316 | target=target, |
331 | 317 | ) |
@@ -378,52 +364,6 @@ def get_budget_window_economic_impact( |
378 | 364 | print(f"Error getting budget-window economic impact: {str(e)}") |
379 | 365 | raise e |
380 | 366 |
|
381 | | - def _build_budget_window_years( |
382 | | - self, |
383 | | - *, |
384 | | - start_year: str, |
385 | | - window_size: int, |
386 | | - ) -> list[str]: |
387 | | - start_year_int = int(start_year) |
388 | | - return [str(start_year_int + index) for index in range(window_size)] |
389 | | - |
390 | | - def _build_budget_window_time_period( |
391 | | - self, |
392 | | - *, |
393 | | - start_year: str, |
394 | | - window_size: int, |
395 | | - ) -> str: |
396 | | - return f"budget_window:{start_year}:{window_size}" |
397 | | - |
398 | | - def _build_budget_window_setup_options( |
399 | | - self, |
400 | | - *, |
401 | | - country_id: str, |
402 | | - policy_id: int, |
403 | | - baseline_policy_id: int, |
404 | | - region: str, |
405 | | - dataset: str, |
406 | | - start_year: str, |
407 | | - window_size: int, |
408 | | - options: dict, |
409 | | - api_version: str, |
410 | | - target: Literal["general", "cliff"], |
411 | | - ) -> EconomicImpactSetupOptions: |
412 | | - return self._build_economic_impact_setup_options( |
413 | | - country_id=country_id, |
414 | | - policy_id=policy_id, |
415 | | - baseline_policy_id=baseline_policy_id, |
416 | | - region=region, |
417 | | - dataset=dataset, |
418 | | - time_period=self._build_budget_window_time_period( |
419 | | - start_year=start_year, |
420 | | - window_size=window_size, |
421 | | - ), |
422 | | - options=dict(options), |
423 | | - api_version=api_version, |
424 | | - target=target, |
425 | | - ) |
426 | | - |
427 | 367 | def _build_budget_window_cache_key( |
428 | 368 | self, |
429 | 369 | setup_options: EconomicImpactSetupOptions, |
|
0 commit comments