2025-04-07 09:02:54 +02:00
|
|
|
|
from sqlalchemy.orm import Session
|
|
|
|
|
from typing import List, Optional, Dict, Any
|
|
|
|
|
|
|
|
|
|
from app.models.calculation import Calculation
|
|
|
|
|
from app.schemas.calculation import CalculationCreate, CalculationUpdate
|
2025-04-07 09:18:31 +02:00
|
|
|
|
from app.fuzzy_solver import solve_cutting_problem
|
2025-04-07 09:02:54 +02:00
|
|
|
|
|
|
|
|
|
async def create_calculation(db: Session, calc_in: CalculationCreate) -> Calculation:
|
|
|
|
|
"""
|
|
|
|
|
Асинхронно создает запись о расчете, вызывает заглушку и сохраняет результат.
|
|
|
|
|
"""
|
|
|
|
|
print("Calling async solver...")
|
|
|
|
|
output_results = await solve_cutting_problem(calc_in.input_params)
|
|
|
|
|
print("Async solver finished.")
|
|
|
|
|
|
|
|
|
|
objective_score = output_results.get("waste_percentage")
|
|
|
|
|
model_name = calc_in.model_name or "default_fuzzy_model_v1"
|
|
|
|
|
|
|
|
|
|
print("Creating DB object...")
|
|
|
|
|
db_calc = Calculation(
|
|
|
|
|
input_params=calc_in.input_params,
|
|
|
|
|
output_results=output_results,
|
|
|
|
|
objective_score=objective_score,
|
|
|
|
|
model_name=model_name
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
print("Adding to DB session...")
|
|
|
|
|
db.add(db_calc)
|
|
|
|
|
print("Committing DB session...")
|
|
|
|
|
db.commit()
|
|
|
|
|
print("Refreshing DB object...")
|
|
|
|
|
db.refresh(db_calc)
|
|
|
|
|
print("Calculation created and saved.")
|
|
|
|
|
return db_calc
|
|
|
|
|
|
|
|
|
|
def get_calculation_by_id(db: Session, calc_id: int) -> Optional[Calculation]:
|
|
|
|
|
"""
|
|
|
|
|
Получает расчет по его ID.
|
|
|
|
|
"""
|
|
|
|
|
return db.query(Calculation).filter(Calculation.id == calc_id).first()
|
|
|
|
|
|
|
|
|
|
def get_calculations(db: Session, skip: int = 0, limit: int = 100) -> List[Calculation]:
|
|
|
|
|
"""
|
|
|
|
|
Получает список всех расчетов с пагинацией.
|
|
|
|
|
"""
|
|
|
|
|
return db.query(Calculation).offset(skip).limit(limit).all()
|