62 lines
2.2 KiB
Python
62 lines
2.2 KiB
Python
import hashlib
|
|
import json
|
|
|
|
from fastapi import APIRouter, Depends
|
|
|
|
from B08_wf5_Quantity.B08_wf5_Quantity_Engine_Estimate import calculate_estimate
|
|
from B08_wf5_Quantity.B08_wf5_Quantity_Engine_Final import calculate_final_cost
|
|
from B08_wf5_Quantity.B08_wf5_Quantity_Repository_Calculation import CalculationRepository
|
|
from B08_wf5_Quantity.B08_wf5_Quantity_Repository_CalculationSource import (
|
|
CalculationSourceRepository,
|
|
)
|
|
from B08_wf5_Quantity.B08_wf5_Quantity_Schema_Calculation import (
|
|
FinalCalculationRequest,
|
|
FinalCalculationResponse,
|
|
)
|
|
|
|
|
|
def router_for(connection_provider, user_provider):
|
|
router = APIRouter()
|
|
|
|
@router.post("/{project_id}/calculate/final", response_model=FinalCalculationResponse)
|
|
async def calculate(
|
|
project_id: str,
|
|
data: FinalCalculationRequest,
|
|
db=Depends(connection_provider),
|
|
user_id=Depends(user_provider),
|
|
):
|
|
inputs, policies, snapshots = await CalculationSourceRepository(db).load(
|
|
project_id, data.versions
|
|
)
|
|
lines, totals = calculate_estimate(inputs)
|
|
final = calculate_final_cost(totals, policies)
|
|
hash_source = {
|
|
"versions": data.versions.model_dump(mode="json"),
|
|
"inputs": snapshots,
|
|
"policies": [row.model_dump(mode="json") for row in policies],
|
|
}
|
|
input_hash = hashlib.sha256(
|
|
json.dumps(hash_source, ensure_ascii=False, sort_keys=True).encode()
|
|
).hexdigest()
|
|
run_id = await CalculationRepository(db).save(
|
|
project_id,
|
|
data.versions.model_dump(),
|
|
input_hash,
|
|
snapshots,
|
|
lines,
|
|
totals,
|
|
final,
|
|
user_id,
|
|
)
|
|
return FinalCalculationResponse(
|
|
run_id=run_id, lines=lines, totals=totals, final=final
|
|
)
|
|
|
|
@router.get("/{project_id}/calculation-runs/latest")
|
|
async def latest(project_id: str, db=Depends(connection_provider)):
|
|
return await CalculationRepository(db).latest_detail(project_id)
|
|
@router.get("/{project_id}/calculation-runs")
|
|
async def history(project_id: str, db=Depends(connection_provider)):
|
|
return await CalculationRepository(db).history(project_id)
|
|
|
|
return router |