You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

172 lines
5.9 KiB
Python

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