from __future__ import annotations from datetime import datetime from typing import Any from .project import ProjectService from .utils import safe_count, safe_text class SimulationService: def __init__(self, pcpy: Any, project: ProjectService): self._pcpy = pcpy self._project = project def run(self) -> dict[str, Any]: self._project.require_open() started = datetime.now() self._pcpy.SWMM.run() finished = datetime.now() try: graph_file_count = safe_count(self._pcpy.Graph.Files) except Exception: graph_file_count = 0 return { "status": "completed", "started_at": started.isoformat(timespec="seconds"), "finished_at": finished.isoformat(timespec="seconds"), "elapsed_seconds": round((finished - started).total_seconds(), 3), "run_status": safe_text(self._pcpy.SWMM.RunStatus), "graph_file_count": graph_file_count, } def refresh_output(self): self._project.require_open() self._pcpy.SWMM.refresh_output()