Skip to content

Commit 6c11919

Browse files
committed
refactor: extract _parse_positive_float to deduplicate budget validation
The try/float()/> 0/except pattern was duplicated 4 times across the two budget endpoints. Extracted into a single module-level helper that validates, converts, or falls back to the current value.
1 parent 23065c3 commit 6c11919

1 file changed

Lines changed: 24 additions & 47 deletions

File tree

src/dashboard/server.py

Lines changed: 24 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,19 @@ def _verify_dashboard_auth(request: Request) -> None:
5959
raise HTTPException(401, error)
6060

6161

62+
def _parse_positive_float(value: Any, field: str, fallback: float) -> float:
63+
"""Validate *value* as a positive float, returning *fallback* if None."""
64+
if value is None:
65+
return fallback
66+
try:
67+
result = float(value)
68+
if result <= 0:
69+
raise ValueError
70+
except (ValueError, TypeError):
71+
raise HTTPException(status_code=400, detail=f"{field} must be a positive number")
72+
return result
73+
74+
6275
def create_dashboard_router(
6376
blackboard: Blackboard,
6477
health_monitor: HealthMonitor | None,
@@ -552,29 +565,12 @@ async def api_update_agent_config(agent_id: str, request: Request) -> dict:
552565
if "budget" in body:
553566
budget_val = body["budget"]
554567
if isinstance(budget_val, dict):
555-
daily = budget_val.get("daily_usd")
556-
monthly = budget_val.get("monthly_usd")
557-
if daily is not None or monthly is not None:
558-
# Read current budget so we don't reset the other limit
559-
current_budget = cost_tracker.check_budget(agent_id)
560-
if daily is not None:
561-
try:
562-
daily = float(daily)
563-
if daily <= 0:
564-
raise ValueError
565-
except (ValueError, TypeError):
566-
raise HTTPException(status_code=400, detail="Budget daily_usd must be a positive number")
567-
else:
568-
daily = current_budget.get("daily_limit", 10.0)
569-
if monthly is not None:
570-
try:
571-
monthly = float(monthly)
572-
if monthly <= 0:
573-
raise ValueError
574-
except (ValueError, TypeError):
575-
raise HTTPException(status_code=400, detail="Budget monthly_usd must be a positive number")
576-
else:
577-
monthly = current_budget.get("monthly_limit", 200.0)
568+
raw_daily = budget_val.get("daily_usd")
569+
raw_monthly = budget_val.get("monthly_usd")
570+
if raw_daily is not None or raw_monthly is not None:
571+
current = cost_tracker.check_budget(agent_id)
572+
daily = _parse_positive_float(raw_daily, "daily_usd", current.get("daily_limit", 10.0))
573+
monthly = _parse_positive_float(raw_monthly, "monthly_usd", current.get("monthly_limit", 200.0))
578574
_update_agent_field(agent_id, "budget", {"daily_usd": daily, "monthly_usd": monthly})
579575
cost_tracker.set_budget(agent_id, daily_usd=daily, monthly_usd=monthly)
580576
updated.append("budget")
@@ -648,32 +644,13 @@ async def api_update_budget(agent_id: str, request: Request) -> dict:
648644
if agent_id not in agent_registry:
649645
raise HTTPException(status_code=404, detail="Agent not found")
650646
body = await request.json()
651-
daily_usd = body.get("daily_usd")
652-
monthly_usd = body.get("monthly_usd")
653-
if daily_usd is None and monthly_usd is None:
647+
raw_daily = body.get("daily_usd")
648+
raw_monthly = body.get("monthly_usd")
649+
if raw_daily is None and raw_monthly is None:
654650
raise HTTPException(status_code=400, detail="Provide daily_usd and/or monthly_usd")
655-
# Read current budget so we don't reset the other limit
656651
current = cost_tracker.check_budget(agent_id)
657-
current_daily = current.get("daily_limit", 10.0)
658-
current_monthly = current.get("monthly_limit", 200.0)
659-
if daily_usd is not None:
660-
try:
661-
daily_usd = float(daily_usd)
662-
if daily_usd <= 0:
663-
raise ValueError
664-
except (ValueError, TypeError):
665-
raise HTTPException(status_code=400, detail="daily_usd must be a positive number")
666-
else:
667-
daily_usd = current_daily
668-
if monthly_usd is not None:
669-
try:
670-
monthly_usd = float(monthly_usd)
671-
if monthly_usd <= 0:
672-
raise ValueError
673-
except (ValueError, TypeError):
674-
raise HTTPException(status_code=400, detail="monthly_usd must be a positive number")
675-
else:
676-
monthly_usd = current_monthly
652+
daily_usd = _parse_positive_float(raw_daily, "daily_usd", current.get("daily_limit", 10.0))
653+
monthly_usd = _parse_positive_float(raw_monthly, "monthly_usd", current.get("monthly_limit", 200.0))
677654
cost_tracker.set_budget(agent_id, daily_usd=daily_usd, monthly_usd=monthly_usd)
678655
from src.cli.config import _update_agent_field
679656
_update_agent_field(agent_id, "budget", {"daily_usd": daily_usd, "monthly_usd": monthly_usd})

0 commit comments

Comments
 (0)