govorov/backend/app/services/calculation_service.py

47 lines
1.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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
from app.fuzzy_solver import solve_cutting_problem
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()