Coverage for mpcforces_extractor\api\routes\extractor.py: 39%
31 statements
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-06 21:34 +0100
« prev ^ index » next coverage.py v7.6.4, created at 2024-11-06 21:34 +0100
1import os
2from fastapi import APIRouter, HTTPException, Request
3from mpcforces_extractor.api.db.schemas import RunExtractorRequest
4from mpcforces_extractor.api.config import UPLOAD_FOLDER, OUTPUT_FOLDER
5from mpcforces_extractor.force_extractor import MPCForceExtractor
6from mpcforces_extractor.datastructure.entities import Node, Element1D, Element
7from mpcforces_extractor.datastructure.subcases import Subcase
8from mpcforces_extractor.datastructure.rigids import MPC
9from mpcforces_extractor.api.db.database import MPCDatabase
12router = APIRouter()
15@router.post("/run-extractor")
16async def run_extractor(request: Request, file_request: RunExtractorRequest):
17 """
18 Run the extractor. This is the main endpoint to run the program
19 """
20 fem_file = file_request.fem_filename
21 mpcf_file = file_request.mpcf_filename
23 print(f"Running extractor with files: {fem_file}, {mpcf_file}")
25 # Clear all Instances
26 Node.reset()
27 Element1D.reset()
28 Element.reset_graph()
29 Subcase.reset()
30 MPC.reset()
32 blocksize = 8
33 model_output_folder = str(OUTPUT_FOLDER) + os.sep + f"{fem_file.split('.')[0]}"
35 mpc_force_extractor = MPCForceExtractor(
36 str(UPLOAD_FOLDER) + os.sep + fem_file,
37 str(UPLOAD_FOLDER) + os.sep + mpcf_file,
38 model_output_folder,
39 )
41 # Write Summary
42 mpc_force_extractor.build_fem_and_subcase_data(blocksize)
43 app = request.app
44 app.db = MPCDatabase(model_output_folder + "/db.db")
45 app.db.populate_database()
47 # Implement your logic here to run the extractor using the provided filenames
48 # For example, call your main routine here
49 try:
50 # Assuming you have a function called run_extractor_function
51 # run_extractor_function(fem_file, mpcf_file)
52 return {"message": "Extractor run successfully!"}
53 except Exception as e:
54 raise HTTPException(status_code=500, detail=str(e)) from e