From 4a24783377e020cc454f608c340a84a762e55c20 Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Wed, 17 Sep 2025 17:11:43 +0700 Subject: [PATCH] add new to endpoint get plot --- f.py | 171 -------------------------------- src/aeros_simulation/router.py | 4 +- src/aeros_simulation/service.py | 6 +- 3 files changed, 7 insertions(+), 174 deletions(-) delete mode 100644 f.py diff --git a/f.py b/f.py deleted file mode 100644 index 6952ada..0000000 --- a/f.py +++ /dev/null @@ -1,171 +0,0 @@ -async def save_default_simulation_node( - *, db_session: DbSession, project_name: str = "trialapi" -): - session = await create_session() - - tasks = [] - all_results = [] - # Get all schematic - schematics = await get_all_schematic_aeros(db_session=db_session) - - for schematic in schematics: - sim_data = { - "projectName": project_name, - "SchematicName": schematic.schematic_name, - "SimSeed": 1, - "SimDuration": 1, - "DurationUnit": "UMinute", - "SimNumRun": 1, - } - - results = await execute_simulation(db_session=db_session, sim_data=sim_data) - mainSchematicId = uuid4() - mainSchematic = AerosNode( - id=mainSchematicId, - node_name="- TJB - Unit 3 -", - schematic_name="- TJB - Unit 3 -", - schematic_id=None, - node_type="SchematicNode", - aeros_schematic_id=schematic.id - ) - - nodes = await save_recusive_simulation_result_node(db_session=db_session, data=results, schematic_name=mainSchematic.node_name, schematic_id=mainSchematicId, aeros_schematic_id=schematic.id) - nodes.append(mainSchematic) - - all_results.extend(nodes) - - # delete old data - await db_session.execute(delete(AerosNode)) - - db_session.add_all(all_results) - await db_session.commit() - -async def execute_simulation( - *, - db_session: DbSession, - simulation_id: Optional[UUID] = None, - sim_data: dict, - is_saved: bool = False, -): - """Execute the actual simulation call""" - print("Executing simulation with id: %s", simulation_id) - - try: - response = await client.post( - f"{AEROS_BASE_URL}/api/Simulation/RunSimulation", - json=sim_data, - headers={"Content-Type": "application/json"}, - ) - response.raise_for_status() - result = response.json() - - if is_saved: - simulation = await get_simulation_by_id( - db_session=db_session, simulation_id=simulation_id - ) - simulation.status = "proccessing" - simulation.result = result - await db_session.commit() - await save_simulation_result( - db_session=db_session, simulation_id=simulation_id, result=result - ) - - print("Simulation completed with id: %s", simulation_id) - return result - - except Exception as e: - simulation = await get_simulation_by_id( - db_session=db_session, simulation_id=simulation_id - ) - simulation.status = "failed" - simulation.error = str(e) - await db_session.commit() - - log.error("Simulation failed with error: %s", str(e)) - - raise HTTPException( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e) - ) - -async def process_single_schematic(*, db_session: DbSession, sim_data: dict, schematic) -> List[AerosNode]: - """Process a single schematic simulation and return the nodes""" - try: - # Execute simulation for this schematic - results = await execute_simulation(db_session=db_session, sim_data=sim_data) - - # Create main schematic node - mainSchematicId = uuid4() - mainSchematic = AerosNode( - id=mainSchematicId, - node_name="- TJB - Unit 3 -", - schematic_name="- TJB - Unit 3 -", - schematic_id=None, - node_type="SchematicNode", - aeros_schematic_id=schematic.id - ) - - # Process simulation results recursively - nodes = await save_recusive_simulation_result_node( - db_session=db_session, - data=results, - schematic_name=mainSchematic.node_name, - schematic_id=mainSchematicId, - aeros_schematic_id=schematic.id - ) - nodes.append(mainSchematic) - - return nodes - - except Exception as e: - print(f"Error processing schematic {schematic.schematic_name}: {e}") - raise # Re-raise to be caught by asyncio.gather - -async def save_recusive_simulation_result_node(*, db_session: DbSession, data, schematic_name: str, aeros_schematic_id ,schematic_id: Optional[UUID] = None): - ## Get All schematic - - #doing multiple simulation with all schematic - - #1 Record schmatic ID from master schematic, ex - TJB - Unit 3 - = 1 - #2 Get The highest parent from Plot data using nodeName == schematicName - #3 save the highest parent, add master schematic ID, get highest parent_id, - # continue looping through all plot data, check if it regular node and schemmaticName = highest parent schematic ID, save - # If schematicName = Parent schematic name, but not regular node, that mean that node is schematic and should have children - # search for children schematic and save them - - - plotResult = data["plotNodeOuts"] - - results = [] - - for result in plotResult: - - if result["schematicName"] == schematic_name and result["nodeType"] == "RegularNode": - - node = AerosNode( - node_name=result["nodeName"], - schematic_id=schematic_id, - node_type="RegularNode", - schematic_name=schematic_name, - aeros_schematic_id=aeros_schematic_id - ) - - results.append(node) - - elif result["schematicName"] == schematic_name and result["nodeType"] == "SubSchematic": - schematicId = uuid4() - schematic = AerosNode( - id=schematicId, - node_name=result["nodeName"], - schematic_name=schematic_name, - schematic_id=schematic_id, - node_type="SchematicNode", - aeros_schematic_id=aeros_schematic_id - ) - results.append(schematic) - - res = await save_recusive_simulation_result_node(db_session=db_session, data=data, schematic_name=result["nodeName"], schematic_id=schematicId, aeros_schematic_id=aeros_schematic_id) - results.extend(res) - else: - continue - - return results diff --git a/src/aeros_simulation/router.py b/src/aeros_simulation/router.py index 2997245..4ddd0e2 100644 --- a/src/aeros_simulation/router.py +++ b/src/aeros_simulation/router.py @@ -173,10 +173,10 @@ async def get_simulation_result_plot(db_session: DbSession, simulation_id): "/result/plot/{simulation_id}/{node_id}", response_model=StandardResponse[SimulationPlot], ) -async def get_simulation_result_plot_per_node(db_session: DbSession, simulation_id, node_id): +async def get_simulation_result_plot_per_node(db_session: DbSession, simulation_id, node_id, use_location_tag: Optional[int] = Query(0)): """Get simulation result.""" simulation_result = await get_simulation_with_plot_result( - db_session=db_session, simulation_id=simulation_id, node_id=node_id + db_session=db_session, simulation_id=simulation_id, node_id=node_id, use_location_tag=use_location_tag ) diff --git a/src/aeros_simulation/service.py b/src/aeros_simulation/service.py index b666c60..b96b55e 100644 --- a/src/aeros_simulation/service.py +++ b/src/aeros_simulation/service.py @@ -235,7 +235,7 @@ async def get_result_ranking(*, db_session: DbSession, simulation_id: UUID): async def get_simulation_with_plot_result( - *, db_session: DbSession, simulation_id: UUID, node_type: Optional[str] = None, node_id: Optional[str] = None + *, db_session: DbSession, simulation_id: UUID, node_type: Optional[str] = None, node_id: Optional[str] = None, use_location_tag:Optional[int] = 0 ): """Get a simulation by id.""" # query = ( @@ -275,6 +275,10 @@ async def get_simulation_with_plot_result( query = query.join( AerosNode, AerosNode.id == AerosSimulationPlotResult.aeros_node_id ).filter(AerosNode.node_name == '- TJB - Unit 3 -') + elif use_location_tag: + query = query.join( + AerosNode, AerosNode.id == AerosSimulationPlotResult.aeros_node_id + ).filter(AerosNode.node_name == node_id) else: query = query.join( AerosNode, AerosNode.id == AerosSimulationPlotResult.aeros_node_id