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() @router.post('/create') async def create_experiment_parameters(body: ExperimentParametersBody): try: await create(ExperimentParameters, outer_blades_count=body.outer_blades_count, outer_blades_length=body.outer_blades_length, outer_blades_angle=body.outer_blades_angle, middle_blades_count=body.middle_blades_count, load_id=body.load_id, recycling_id=body.recycling_id, experiment_hash=body.experiment_hash, experiment_category_id=body.experiment_category_id ) return {"message": "Новая запись успешно добавлена"} except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") @router.get('/all') async def get_all_experiment_parameters(): try: result = await get_all(ExperimentParameters) if result is not None: return result else: return {"message": "Нет записей в "} except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") @router.get('/{id}') async def get_by_id_experiment_parameters(id: int): try: result = await get_by_id(ExperimentParameters, id) if result is not None: return result else: return {"message": "Запись не найдена"} except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") @router.get('/by_category/{id}') async def get_experiment_parameters_by_exp_category(id: int): try: result = await get_exp_parameters_by_category(id) if result is not None: return result else: return {"message": f' с идентификатором категории - {id} - не найдены'} except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") @router.get('/by_exp_hash/{hash}') async def get_experiment_parameters_by_exp_category(hash: str): try: result = await get_exp_parameters_by_exp_hash(hash) if result is not None: return result else: return {"message": f' с experiment_hash = {id} - не найдены'} except Exception as e: raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") @router.delete('/{id}/delete') async def delete_experiment_parameters(id: int): try: is_deleted = await delete(ExperimentParameters, id) if is_deleted: return {"message": "Запись успешно удалена"} else: 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, 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)}")