71 lines
2.5 KiB
Python
71 lines
2.5 KiB
Python
import asyncio
|
||
from typing import Optional
|
||
from fastapi import FastAPI, HTTPException, Body, Response
|
||
from pydantic import BaseModel
|
||
from new_experiment_planner import run_experiment # Импортируем функцию из твоего скрипта
|
||
from db.csv_to_db import csv_to_db
|
||
from db.repositories.load_parameters_repos import LoadParametersRepository
|
||
from network.bodies import LoadParametersBody
|
||
|
||
app = FastAPI()
|
||
|
||
# Модель данных для передачи параметров
|
||
class ExperimentParameters(BaseModel):
|
||
outer_blades_count: str
|
||
outer_blades_length: str
|
||
outer_blades_angle: str
|
||
middle_blades_count: str
|
||
load: str
|
||
recycling: str
|
||
|
||
|
||
|
||
# Эндпоинт для запуска эксперимента
|
||
@app.post("/run_experiment/")
|
||
def run_experiment_api(params: ExperimentParameters):
|
||
try:
|
||
# Вызываем функцию run_experiment с параметрами
|
||
run_experiment(
|
||
params.outer_blades_count,
|
||
params.outer_blades_length,
|
||
params.outer_blades_angle,
|
||
params.middle_blades_count,
|
||
params.load,
|
||
params.recycling
|
||
)
|
||
return {"status": "success", "message": "Experiment started successfully."}
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
||
|
||
|
||
@app.get('/init_db_data')
|
||
def init_db_data():
|
||
try:
|
||
asyncio.run(csv_to_db())
|
||
return {"status": "success", "message": "База данных инициализирована. Данные из файлов csv успешно добавлены."}
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
||
|
||
|
||
@app.post('/create')
|
||
async def test(data: LoadParametersBody):
|
||
try:
|
||
await LoadParametersRepository.create_from_pydantic(data)
|
||
|
||
return {"status": "success", "message": "Новая запись успешно добавлена"}
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
|
||
|
||
|
||
@app.get('/all')
|
||
async def get_all():
|
||
try:
|
||
result = await LoadParametersRepository.get_all()
|
||
|
||
if result is not None:
|
||
# return {"status": "success", "data": [LoadParametersBody.model_validate(param) for param in result]}
|
||
return result
|
||
else:
|
||
return {"status": "success", "message":"result is not None"}
|
||
except Exception as e:
|
||
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}") |