Сделал новый метод, который связывает с другими таблицами (load и recyle)

This commit is contained in:
maksim 2024-11-20 00:11:58 +04:00
parent d986305ee1
commit 7eea3cea40
4 changed files with 56 additions and 3 deletions

View File

@ -7,7 +7,7 @@ from db.models.base import Base
class LoadParameters(Base): class LoadParameters(Base):
__tablename__ = 'load_parameters' __tablename__ = 'load_parameters'
id: Mapped[int] = mapped_column(Identity(start=6, cycle=True), id: Mapped[int] = mapped_column(Identity(start=1000, cycle=True),
primary_key=True) primary_key=True)
load: Mapped[int] load: Mapped[int]
primary_air_consumption: Mapped[float] primary_air_consumption: Mapped[float]

View File

@ -9,7 +9,7 @@ from db.models.base import Base
class RecyclingParameters(Base): class RecyclingParameters(Base):
__tablename__ = 'recycling_parameters' __tablename__ = 'recycling_parameters'
id: Mapped[int] = mapped_column(Identity(start=6, cycle=True), id: Mapped[int] = mapped_column(Identity(start=1000, cycle=True),
primary_key=True) primary_key=True)
load_id: Mapped[Optional[int]] = mapped_column(ForeignKey('load_parameters.id', ondelete='SET NULL')) load_id: Mapped[Optional[int]] = mapped_column(ForeignKey('load_parameters.id', ondelete='SET NULL'))

View File

@ -73,7 +73,7 @@ async def init_db_data(background_tasks: BackgroundTasks):
# "oxidizer_temp": [471, 493] # "oxidizer_temp": [471, 493]
# }, # },
# "count_exp": 1440, # "count_exp": 1440,
# "round_rules": [0, 1, 1, 0, 1, 1, 1] # "round_rules": [0, 1, 1, 0, 0, 0, 0]
# } # }
@app.post("/pyDOE3_screening_design") @app.post("/pyDOE3_screening_design")

View File

@ -89,3 +89,56 @@ async def delete_experiment_parameters(id: int):
return {"message": "Запись <ExperimentParameters> не найдена"} return {"message": "Запись <ExperimentParameters> не найдена"}
except Exception as e: except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@router.post('/process_and_save/{id}')
async def process_and_save_experiment_data(id: int):
try:
# Получаем данные из ExperimentParameters по id
experiment = await get_by_id(ExperimentParameters, id)
if experiment is None:
raise HTTPException(status_code=404, detail=f"ExperimentParameters с id {id} не найден.")
# Пример обработки данных
load_value = experiment.outer_blades_length * experiment.middle_blades_count
primary_air_consumption = load_value * 0.1 # Условный коэффициент
secondary_air_consumption = load_value * 0.05
gas_inlet_consumption = load_value * 0.03
recycling_level = experiment.outer_blades_count * 0.5
co2 = recycling_level * 0.02
n2 = recycling_level * 0.01
h2o = recycling_level * 0.005
o2 = recycling_level * 0.015
# Сохраняем данные в LoadParameters
load_params = await create(
LoadParameters,
load=int(load_value),
primary_air_consumption=primary_air_consumption,
secondary_air_consumption=secondary_air_consumption,
gas_inlet_consumption=gas_inlet_consumption
)
# Сохраняем данные в RecyclingParameters
recycling_params = await create(
RecyclingParameters,
load_id=load_params.id,
recycling_level=int(recycling_level),
co2=co2,
n2=n2,
h2o=h2o,
o2=o2
)
# Обновляем ExperimentParameters, чтобы сохранить связи
experiment.load_id = load_params.id
experiment.recycling_id = recycling_params.id
return {
"message": "Данные успешно обработаны и сохранены.",
"load_parameters": load_params,
"recycling_parameters": recycling_params
}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")