2024-11-18 23:16:24 +04:00
|
|
|
|
import hashlib
|
2024-10-27 23:29:52 +04:00
|
|
|
|
from typing import Sequence
|
|
|
|
|
|
2024-11-18 23:16:24 +04:00
|
|
|
|
import pandas as pd
|
2024-11-28 21:52:42 +04:00
|
|
|
|
import yaml
|
2024-11-18 23:16:24 +04:00
|
|
|
|
from fastapi import HTTPException
|
2024-10-25 02:02:31 +04:00
|
|
|
|
from sqlalchemy.future import select
|
2024-10-27 23:29:52 +04:00
|
|
|
|
|
2024-11-28 21:52:42 +04:00
|
|
|
|
from db.crud import create, update, get_by_id, update_exp
|
|
|
|
|
from db.models import LoadParameters, RecyclingParameters
|
2024-10-14 21:18:07 +04:00
|
|
|
|
from db.models.experiment_parameters_model import ExperimentParameters
|
2024-10-28 13:30:02 +04:00
|
|
|
|
from db.postgres_db_connection import async_session_postgres
|
2024-11-28 21:52:42 +04:00
|
|
|
|
from macros_generator import load_calculation, recycling_calculation
|
2024-10-14 21:18:07 +04:00
|
|
|
|
|
2024-10-27 23:29:52 +04:00
|
|
|
|
|
2024-10-28 13:30:02 +04:00
|
|
|
|
async def get_exp_parameters_by_category(category_id: int) -> Sequence[ExperimentParameters]:
|
|
|
|
|
async with async_session_postgres() as session:
|
|
|
|
|
result = await session.execute(
|
2024-10-27 23:29:52 +04:00
|
|
|
|
select(ExperimentParameters).where(ExperimentParameters.experiment_category_id == category_id)
|
|
|
|
|
)
|
|
|
|
|
return result.scalars().all()
|
|
|
|
|
|
2024-10-28 13:30:02 +04:00
|
|
|
|
async def get_exp_parameters_by_exp_hash(exp_hash: str) -> Sequence[ExperimentParameters]:
|
|
|
|
|
async with async_session_postgres() as session:
|
|
|
|
|
result = await session.execute(
|
|
|
|
|
select(ExperimentParameters).where(ExperimentParameters.experiment_hash == exp_hash)
|
2024-10-22 16:46:39 +04:00
|
|
|
|
)
|
2024-11-18 23:16:24 +04:00
|
|
|
|
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
|
2024-11-28 21:52:42 +04:00
|
|
|
|
load = int(row['load'])
|
|
|
|
|
recycling = int(row['recycling_level'])
|
2024-11-18 23:16:24 +04:00
|
|
|
|
|
|
|
|
|
# Генерация хеша для experiment_hash
|
|
|
|
|
experiment_hash = generate_experiment_hash(row)
|
|
|
|
|
|
2024-11-28 21:52:42 +04:00
|
|
|
|
exp = await create(
|
2024-11-18 23:16:24 +04:00
|
|
|
|
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']),
|
2024-12-11 13:41:58 +04:00
|
|
|
|
load_id=None,
|
2024-11-18 23:16:24 +04:00
|
|
|
|
recycling_id=None,
|
2024-11-28 21:52:42 +04:00
|
|
|
|
experiment_hash=experiment_hash,
|
2024-12-11 13:41:58 +04:00
|
|
|
|
oxidizer_temp=float(row['oxidizer_temp']),
|
|
|
|
|
experiment_category_id=experiment_category_id
|
2024-11-18 23:16:24 +04:00
|
|
|
|
)
|
2024-11-28 21:52:42 +04:00
|
|
|
|
|
|
|
|
|
await process_and_save_experiment_data(exp.id, load, recycling)
|
2024-11-18 23:16:24 +04:00
|
|
|
|
except Exception as e:
|
|
|
|
|
print(f"Ошибка при сохранении данных: {e}")
|
2024-11-20 00:21:24 +04:00
|
|
|
|
raise HTTPException(status_code=500, detail=f"Ошибка при сохранении данных: {e}")
|
|
|
|
|
|
2024-12-11 13:41:58 +04:00
|
|
|
|
|
2024-11-28 21:52:42 +04:00
|
|
|
|
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)}")
|