From f66cceeadde2d36cba6aeb193605ce5589e1d35a Mon Sep 17 00:00:00 2001 From: maksim Date: Mon, 18 Nov 2024 21:48:39 +0400 Subject: [PATCH 1/6] =?UTF-8?q?=D0=98=D0=B7=D0=BC=D0=B5=D0=BD=D0=B8=D0=BB?= =?UTF-8?q?=20seq=20=D1=83=201=20=D0=BC=D0=BE=D0=B4=D0=B5=D0=BB=D0=B8=20?= =?UTF-8?q?=D0=B2=20=D0=B1=D0=B4,=20=D1=82=D0=B0=D0=BA=20=D0=BA=D0=B0?= =?UTF-8?q?=D0=BA=20=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D0=B5=D1=82=20create.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/models/experiment_parameters_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/db/models/experiment_parameters_model.py b/db/models/experiment_parameters_model.py index 03e5ae1..324f8f9 100644 --- a/db/models/experiment_parameters_model.py +++ b/db/models/experiment_parameters_model.py @@ -9,7 +9,7 @@ from db.models.base import Base class ExperimentParameters(Base): __tablename__ = 'experiment_parameters' - id: Mapped[int] = mapped_column(Identity(start=11, cycle=True), + id: Mapped[int] = mapped_column(Identity(start=1100, cycle=True), primary_key=True) outer_blades_count: Mapped[int] outer_blades_length: Mapped[float] From 3002be30dda90016921a78ebc3bd24b4108bdea6 Mon Sep 17 00:00:00 2001 From: maksim Date: Mon, 18 Nov 2024 23:16:24 +0400 Subject: [PATCH 2/6] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D1=8F?= =?UTF-8?q?=D0=B5=D1=82=D1=81=D1=8F=20=D0=B2=20=D0=B1=D0=B4,=20=D0=BD?= =?UTF-8?q?=D0=B0=D0=B4=D0=BE=20=D1=81=D0=B4=D0=B5=D0=BB=D1=8F=D1=82=D1=8C?= =?UTF-8?q?=20=D0=B5=D1=89=D0=B5=20=D1=81=D0=B2=D1=8F=D0=B7=D1=8C=20=D0=B8?= =?UTF-8?q?=20=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5?= =?UTF-8?q?=D0=B2=20=D0=B2=20=D0=B4=D1=80=D1=83=D0=B3=D0=B8=D0=B5=20=D1=82?= =?UTF-8?q?=D0=B0=D0=B1=D0=BB=D0=B8=D1=86=D1=8B.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../experiment_parameters_repos.py | 37 ++++++++++++++++++- main.py | 17 +++++++-- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/db/repositories/experiment_parameters_repos.py b/db/repositories/experiment_parameters_repos.py index 0e671d4..9103c18 100644 --- a/db/repositories/experiment_parameters_repos.py +++ b/db/repositories/experiment_parameters_repos.py @@ -1,7 +1,11 @@ +import hashlib from typing import Sequence +import pandas as pd +from fastapi import HTTPException from sqlalchemy.future import select +from db.crud import create from db.models.experiment_parameters_model import ExperimentParameters from db.postgres_db_connection import async_session_postgres @@ -18,4 +22,35 @@ async def get_exp_parameters_by_exp_hash(exp_hash: str) -> Sequence[ExperimentPa result = await session.execute( select(ExperimentParameters).where(ExperimentParameters.experiment_hash == exp_hash) ) - return result.scalars().all() \ No newline at end of file + return result.scalars().all() + + +def generate_experiment_hash(data: dict) -> str: + """Генерация уникального хеша на основе данных эксперимента""" + hash_input = f"{data['outer_blades_count']}_{data['outer_blades_length']}_{data['outer_blades_angle']}_{data['middle_blades_count']}_{data['load']}_{data['recycling_level']}" + return hashlib.sha256(hash_input.encode()).hexdigest() + + +async def save_experiment_to_db(df: pd.DataFrame): + for _, row in df.iterrows(): + try: + # Преобразуем load и recycling_level в соответствующие id + load_id = int(row['load']) + recycling_id = int(row['recycling_level']) + + # Генерация хеша для experiment_hash + experiment_hash = generate_experiment_hash(row) + + await create( + ExperimentParameters, + outer_blades_count=int(row['outer_blades_count']), + outer_blades_length=float(row['outer_blades_length']), + outer_blades_angle=float(row['outer_blades_angle']), + middle_blades_count=int(row['middle_blades_count']), + load_id= None, + recycling_id=None, + experiment_hash=experiment_hash + ) + except Exception as e: + print(f"Ошибка при сохранении данных: {e}") + raise HTTPException(status_code=500, detail=f"Ошибка при сохранении данных: {e}") \ No newline at end of file diff --git a/main.py b/main.py index a74f67c..f1892fb 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from fastapi import FastAPI, HTTPException, BackgroundTasks from pyDOE3 import pbdesign, lhs from db.csv_to_db import csv_to_db +from db.repositories import save_experiment_to_db from network.routes import (ch_experimentdb_experiment_data_router, experiment_data_router, experiment_parameters_router, experiment_category_router) from network.routes import load_parameters_router, recycling_parameters_router @@ -76,7 +77,7 @@ async def init_db_data(background_tasks: BackgroundTasks): # } @app.post("/pyDOE3_screening_design") -def generate_screening_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]: +async def generate_screening_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]: param_ranges = request.param_ranges # Создаем screening design и масштабируем его @@ -84,13 +85,17 @@ def generate_screening_design(request: ExperimentParametersPyDOE3) -> List[Dict[ screening_design = pbdesign(num_factors) scaled_screening_design = scale_design(screening_design, param_ranges) - # Преобразуем в DataFrame и возвращаем результат + # Преобразуем в DataFrame df_screening = pd.DataFrame(scaled_screening_design, columns=param_ranges.keys()) + + # Сохраняем результаты в базу данных + await save_experiment_to_db(df_screening) + return df_screening.to_dict(orient="records") @app.post("/pyDOE3_lhs_design") -def generate_lhs_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]: +async def generate_lhs_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, float]]: param_ranges = request.param_ranges count_exp = request.count_exp round_rules = request.round_rules @@ -103,6 +108,10 @@ def generate_lhs_design(request: ExperimentParametersPyDOE3) -> List[Dict[str, f # Округляем значения round_scaled_lhs_samples = round_by_index(scaled_lhs_samples, round_rules) - # Преобразуем в DataFrame и возвращаем результат + # Преобразуем в DataFrame df_lhs = pd.DataFrame(round_scaled_lhs_samples, columns=param_ranges.keys()) + + # Сохраняем результаты в базу данных + await save_experiment_to_db(df_lhs) + return df_lhs.to_dict(orient="records") \ No newline at end of file From d986305ee11a63f612bdd6b365c6dfa85988ee6c Mon Sep 17 00:00:00 2001 From: HellsSenju Date: Tue, 19 Nov 2024 16:24:05 +0400 Subject: [PATCH 3/6] =?UTF-8?q?=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB?= =?UTF-8?q?=D0=B5=D0=BD=D0=BE=20=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=B4=D0=BB=D1=8F=20ExperimentParameters?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- network/routes/experiment_parameters_router.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/network/routes/experiment_parameters_router.py b/network/routes/experiment_parameters_router.py index c7c08e4..189dfd7 100644 --- a/network/routes/experiment_parameters_router.py +++ b/network/routes/experiment_parameters_router.py @@ -81,7 +81,8 @@ async def get_experiment_parameters_by_exp_category(hash: str): @router.delete('/{id}/delete') async def delete_experiment_parameters(id: int): try: - is_deleted = await delete(LoadParameters, id) + is_deleted = await delete(ExperimentParameters, id) + if is_deleted: return {"message": "Запись успешно удалена"} else: From 7eea3cea407fc6efa115a7e4159a21aaa212f881 Mon Sep 17 00:00:00 2001 From: maksim Date: Wed, 20 Nov 2024 00:11:58 +0400 Subject: [PATCH 4/6] =?UTF-8?q?=D0=A1=D0=B4=D0=B5=D0=BB=D0=B0=D0=BB=20?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D1=8B=D0=B9=20=D0=BC=D0=B5=D1=82=D0=BE=D0=B4?= =?UTF-8?q?,=20=D0=BA=D0=BE=D1=82=D0=BE=D1=80=D1=8B=D0=B9=20=D1=81=D0=B2?= =?UTF-8?q?=D1=8F=D0=B7=D1=8B=D0=B2=D0=B0=D0=B5=D1=82=20=D1=81=20=D0=B4?= =?UTF-8?q?=D1=80=D1=83=D0=B3=D0=B8=D0=BC=D0=B8=20=D1=82=D0=B0=D0=B1=D0=BB?= =?UTF-8?q?=D0=B8=D1=86=D0=B0=D0=BC=D0=B8=20(load=20=D0=B8=20recyle)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/models/load_parameters_model.py | 2 +- db/models/recycling_parameters_model.py | 2 +- main.py | 2 +- .../routes/experiment_parameters_router.py | 53 +++++++++++++++++++ 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/db/models/load_parameters_model.py b/db/models/load_parameters_model.py index ffb5a8a..9f84dac 100644 --- a/db/models/load_parameters_model.py +++ b/db/models/load_parameters_model.py @@ -7,7 +7,7 @@ from db.models.base import Base class LoadParameters(Base): __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) load: Mapped[int] primary_air_consumption: Mapped[float] diff --git a/db/models/recycling_parameters_model.py b/db/models/recycling_parameters_model.py index 507b261..4e22524 100644 --- a/db/models/recycling_parameters_model.py +++ b/db/models/recycling_parameters_model.py @@ -9,7 +9,7 @@ from db.models.base import Base class RecyclingParameters(Base): __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) load_id: Mapped[Optional[int]] = mapped_column(ForeignKey('load_parameters.id', ondelete='SET NULL')) diff --git a/main.py b/main.py index f1892fb..74e5fdd 100644 --- a/main.py +++ b/main.py @@ -73,7 +73,7 @@ async def init_db_data(background_tasks: BackgroundTasks): # "oxidizer_temp": [471, 493] # }, # "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") diff --git a/network/routes/experiment_parameters_router.py b/network/routes/experiment_parameters_router.py index 189dfd7..d79bd89 100644 --- a/network/routes/experiment_parameters_router.py +++ b/network/routes/experiment_parameters_router.py @@ -89,3 +89,56 @@ async def delete_experiment_parameters(id: int): return {"message": "Запись не найдена"} except Exception as 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)}") From f394139d35b40d2e29cd59efa274d428d5e85d47 Mon Sep 17 00:00:00 2001 From: maksim Date: Wed, 20 Nov 2024 00:21:24 +0400 Subject: [PATCH 5/6] =?UTF-8?q?=D0=9D=D0=B0=D1=81=D1=82=D1=80=D0=BE=D0=B9?= =?UTF-8?q?=D0=BB=20=D1=81=D0=B2=D1=8F=D0=B7=D0=B8.=20=D0=94=D0=BE=D0=B1?= =?UTF-8?q?=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BD=D0=B5=D0=BA=D0=BE=D1=82=D0=BE?= =?UTF-8?q?=D1=80=D1=8B=D0=B5,=20=D0=B2=D0=BE=D0=B7=D0=BC=D0=BE=D0=B6?= =?UTF-8?q?=D0=BD=D0=BE=20=D0=BD=D0=B5=20=D0=BD=D1=83=D0=B6=D0=BD=D1=8B?= =?UTF-8?q?=D0=B5=20=D1=84=D1=83=D0=BD=D0=BA=D1=86=D0=B8=D0=B8,=20=D0=BD?= =?UTF-8?q?=D0=BE=20=D1=82=D0=B0=D0=BA=20=D1=82=D0=BE=D1=87=D0=BD=D0=BE=20?= =?UTF-8?q?=D0=B2=D1=81=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE=D1=82=D0=B0=D0=B5?= =?UTF-8?q?=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- db/crud.py | 18 ++++++++++++++++++ db/repositories/experiment_parameters_repos.py | 3 ++- network/routes/experiment_parameters_router.py | 15 +++++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/db/crud.py b/db/crud.py index 25d873f..4411075 100644 --- a/db/crud.py +++ b/db/crud.py @@ -42,6 +42,24 @@ async def update(model_class: Type[T], id: int, updated_data: Dict[str, Any]) -> await session.commit() return await get_by_id(model_class, id) +# Надо переписать/раасмотреть update +async def update_exp(model_class: Type[T], id: int, updated_data: Dict[str, Any]) -> Optional[T]: + async with async_session_postgres() as session: + async with session.begin(): # Явная транзакция + stmt = ( + update_(model_class) + .where(model_class.id == id) + .values(**updated_data) + .returning(model_class) # Возвращаем обновленный объект + ) + result = await session.execute(stmt) + updated_instance = result.scalars().first() + + if not updated_instance: + return None + + return updated_instance # Возвращаем сразу обновленный объект + async def delete(model_class: Type[T], id: int) -> bool: async with async_session_postgres() as session: diff --git a/db/repositories/experiment_parameters_repos.py b/db/repositories/experiment_parameters_repos.py index 9103c18..794b779 100644 --- a/db/repositories/experiment_parameters_repos.py +++ b/db/repositories/experiment_parameters_repos.py @@ -53,4 +53,5 @@ async def save_experiment_to_db(df: pd.DataFrame): ) except Exception as e: print(f"Ошибка при сохранении данных: {e}") - raise HTTPException(status_code=500, detail=f"Ошибка при сохранении данных: {e}") \ No newline at end of file + raise HTTPException(status_code=500, detail=f"Ошибка при сохранении данных: {e}") + diff --git a/network/routes/experiment_parameters_router.py b/network/routes/experiment_parameters_router.py index d79bd89..2216bda 100644 --- a/network/routes/experiment_parameters_router.py +++ b/network/routes/experiment_parameters_router.py @@ -130,14 +130,21 @@ async def process_and_save_experiment_data(id: int): o2=o2 ) - # Обновляем ExperimentParameters, чтобы сохранить связи - experiment.load_id = load_params.id - experiment.recycling_id = recycling_params.id + # Обновляем ExperimentParameters + experiment = await update_exp( + ExperimentParameters, + id=experiment.id, + updated_data={ + "load_id": load_params.id, + "recycling_id": recycling_params.id + } + ) return { "message": "Данные успешно обработаны и сохранены.", "load_parameters": load_params, - "recycling_parameters": recycling_params + "recycling_parameters": recycling_params, + "updated_experiment_parameters": experiment } except Exception as e: From 8cbe236fcf7bf511f4c8d676129f289db1f87fb1 Mon Sep 17 00:00:00 2001 From: AnnZhimol Date: Thu, 28 Nov 2024 20:52:42 +0300 Subject: [PATCH 6/6] added new attribute and one method for new experiments --- db/models/experiment_parameters_model.py | 1 + .../experiment_parameters_repos.py | 80 +++++++++++++++++-- .../routes/experiment_parameters_router.py | 49 +++++++----- 3 files changed, 105 insertions(+), 25 deletions(-) diff --git a/db/models/experiment_parameters_model.py b/db/models/experiment_parameters_model.py index 324f8f9..bd8c38d 100644 --- a/db/models/experiment_parameters_model.py +++ b/db/models/experiment_parameters_model.py @@ -19,6 +19,7 @@ class ExperimentParameters(Base): recycling_id: Mapped[Optional[int]] = mapped_column(ForeignKey('recycling_parameters.id', ondelete='SET NULL')) experiment_hash: Mapped[str] = mapped_column(unique=True) experiment_category_id: Mapped[Optional[int]] = mapped_column(ForeignKey('experiment_category.id', ondelete='SET NULL'), nullable=True) + oxidizer_temp: Mapped[float] = mapped_column(nullable=True) def __repr__(self): return f"" diff --git a/db/repositories/experiment_parameters_repos.py b/db/repositories/experiment_parameters_repos.py index 794b779..67e7e1c 100644 --- a/db/repositories/experiment_parameters_repos.py +++ b/db/repositories/experiment_parameters_repos.py @@ -2,12 +2,15 @@ import hashlib from typing import Sequence import pandas as pd +import yaml from fastapi import HTTPException from sqlalchemy.future import select -from db.crud import create +from db.crud import create, update, get_by_id, update_exp +from db.models import LoadParameters, RecyclingParameters from db.models.experiment_parameters_model import ExperimentParameters from db.postgres_db_connection import async_session_postgres +from macros_generator import load_calculation, recycling_calculation async def get_exp_parameters_by_category(category_id: int) -> Sequence[ExperimentParameters]: @@ -35,13 +38,13 @@ async def save_experiment_to_db(df: pd.DataFrame): for _, row in df.iterrows(): try: # Преобразуем load и recycling_level в соответствующие id - load_id = int(row['load']) - recycling_id = int(row['recycling_level']) + load = int(row['load']) + recycling = int(row['recycling_level']) # Генерация хеша для experiment_hash experiment_hash = generate_experiment_hash(row) - await create( + exp = await create( ExperimentParameters, outer_blades_count=int(row['outer_blades_count']), outer_blades_length=float(row['outer_blades_length']), @@ -49,9 +52,76 @@ async def save_experiment_to_db(df: pd.DataFrame): middle_blades_count=int(row['middle_blades_count']), load_id= None, recycling_id=None, - experiment_hash=experiment_hash + experiment_hash=experiment_hash, + oxidizer_temp=float(row['oxidizer_temp']) ) + + await process_and_save_experiment_data(exp.id, load, recycling) except Exception as e: print(f"Ошибка при сохранении данных: {e}") raise HTTPException(status_code=500, detail=f"Ошибка при сохранении данных: {e}") +async def process_and_save_experiment_data(id: int, load: float, recycling_level: float) -> dict: + try: + experiment = await get_by_id(ExperimentParameters, id) + if experiment is None: + raise HTTPException(status_code=404, detail=f"ExperimentParameters с id {id} не найден.") + + yaml_file_path = "config.yaml" + + with open(yaml_file_path, "r", encoding="utf-8") as file: + data = yaml.safe_load(file) + + diameters = data["parameters"]["diameters"] + + dict_load = load_calculation(load, diameters, None) + + primary_air_consumption = dict_load["primary_air_consumption"] + secondary_air_consumption = dict_load["secondary_air_consumption"] + gas_inlet_consumption = dict_load["gas_inlet_consumption"] + alpha = dict_load["alpha"] + gas_consumption = dict_load["gas_consumption"] + air_consumption = dict_load["air_consumption"] + + dict_recycling = recycling_calculation(alpha, gas_consumption, air_consumption, recycling_level) + + co2 = dict_recycling["CO2"] + n2 = dict_recycling["N2"] + h2o = dict_recycling["H2O"] + o2 = dict_recycling["O2"] + + load_params = await create( + LoadParameters, + load=int(load), + primary_air_consumption=primary_air_consumption, + secondary_air_consumption=secondary_air_consumption, + gas_inlet_consumption=gas_inlet_consumption + ) + + recycling_params = await create( + RecyclingParameters, + load_id=load_params.id, + recycling_level=int(recycling_level), + co2=co2, + n2=n2, + h2o=h2o, + o2=o2 + ) + + await update_exp( + ExperimentParameters, + id=experiment.id, + updated_data={ + "load_id": load_params.id, + "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)}") diff --git a/network/routes/experiment_parameters_router.py b/network/routes/experiment_parameters_router.py index 2216bda..2487c36 100644 --- a/network/routes/experiment_parameters_router.py +++ b/network/routes/experiment_parameters_router.py @@ -1,8 +1,11 @@ +import yaml from fastapi import APIRouter, HTTPException +from scipy.stats import alpha from db.crud import * from db.models import LoadParameters from db.repositories import get_exp_parameters_by_category, get_exp_parameters_by_exp_hash +from macros_generator import load_calculation, recycling_calculation from network.schemas import ExperimentParametersBody router = APIRouter() @@ -90,36 +93,44 @@ async def delete_experiment_parameters(id: int): except Exception as 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): +# @router.post('/process_and_save/{id}') было нужно для проверки +async def process_and_save_experiment_data(id: int, load: float, recycling_level: float) -> dict: 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 + yaml_file_path = "config.yaml" - 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 + with open(yaml_file_path, "r", encoding="utf-8") as file: + data = yaml.safe_load(file) + + diameters = data["parameters"]["diameters"] + + dict_load = load_calculation(load, diameters, None) + + primary_air_consumption = dict_load["primary_air_consumption"] + secondary_air_consumption = dict_load["secondary_air_consumption"] + gas_inlet_consumption = dict_load["gas_inlet_consumption"] + alpha = dict_load["alpha"] + gas_consumption = dict_load["gas_consumption"] + air_consumption = dict_load["air_consumption"] + + dict_recycling = recycling_calculation(alpha, gas_consumption, air_consumption, recycling_level) + + co2 = dict_recycling["CO2"] + n2 = dict_recycling["N2"] + h2o = dict_recycling["H2O"] + o2 = dict_recycling["O2"] - # Сохраняем данные в LoadParameters load_params = await create( LoadParameters, - load=int(load_value), + load=int(load), 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, @@ -130,8 +141,7 @@ async def process_and_save_experiment_data(id: int): o2=o2 ) - # Обновляем ExperimentParameters - experiment = await update_exp( + await update_exp( ExperimentParameters, id=experiment.id, updated_data={ @@ -143,8 +153,7 @@ async def process_and_save_experiment_data(id: int): return { "message": "Данные успешно обработаны и сохранены.", "load_parameters": load_params, - "recycling_parameters": recycling_params, - "updated_experiment_parameters": experiment + "recycling_parameters": recycling_params } except Exception as e: