diff --git a/src/calculation_time_constrains/flows.py b/src/calculation_time_constrains/flows.py index e40dff4..fca2b95 100644 --- a/src/calculation_time_constrains/flows.py +++ b/src/calculation_time_constrains/flows.py @@ -3,9 +3,11 @@ from src.workorder.model import MasterWorkOrder from src.scope_equipment.model import ScopeEquipment from src.scope.model import Scope from src.database.core import DbSession +from src.scope.service import get_all +from .schema import CalculationTimeConstrainsParametersRead +from .service import get_overhaul_cost_by_time_chart - -async def get_cost_per_failure(*, db_session: DbSession): +async def get_create_calculation_parameters(*, db_session: DbSession): stmt = ( select( ScopeEquipment.scope_id, @@ -17,11 +19,32 @@ async def get_cost_per_failure(*, db_session: DbSession): ) results = await db_session.execute(stmt) + costFailure = results.all() - return results.all() + avaiableScopes = {scope.id: scope.scope_name for scope in await get_all(db_session=db_session)} + costFailurePerScope = {avaiableScopes.get( + costPerFailure[0]): costPerFailure[1] for costPerFailure in await costFailure} + + return CalculationTimeConstrainsParametersRead( + costPerFailure=costFailurePerScope, + availableScopes=avaiableScopes.values(), + recommendedScope="B", + historicalData={ + "averageOverhaulCost": 10000000, + "lastCalculation": { + "id": "calc_122", + "date": "2024-10-15", + "scope": "B", + }, + }, + ) async def create_calculation(*, db_session: DbSession): + + ## + + return { "id": "calc_123", "result": { diff --git a/src/calculation_time_constrains/router.py b/src/calculation_time_constrains/router.py index f424fe7..35f3056 100644 --- a/src/calculation_time_constrains/router.py +++ b/src/calculation_time_constrains/router.py @@ -4,8 +4,7 @@ from fastapi import APIRouter, HTTPException, status from .schema import CalculationTimeConstrainsParametersRead, CalculationTimeConstrainsRead, CalculationTimeConstrainsCreate from src.database.core import DbSession from src.models import StandardResponse -from .flows import get_cost_per_failure, create_calculation -from src.scope.service import get_all +from .flows import get_create_calculation_parameters, create_calculation router = APIRouter() @@ -14,24 +13,10 @@ router = APIRouter() async def get_calculation_parameters(db_session: DbSession): """Get all calculation parameter pagination.""" - avaiableScopes = {scope.id: scope.scope_name for scope in await get_all(db_session=db_session)} - costFailurePerScope = {avaiableScopes.get( - costPerFailure[0]): costPerFailure[1] for costPerFailure in await get_cost_per_failure(db_session=db_session)} + parameters = await get_create_calculation_parameters(db_session=db_session) return StandardResponse( - data=CalculationTimeConstrainsParametersRead( - costPerFailure=costFailurePerScope, - availableScopes=avaiableScopes.values(), - recommendedScope="B", - historicalData={ - "averageOverhaulCost": 10000000, - "lastCalculation": { - "id": "calc_122", - "date": "2024-10-15", - "scope": "B", - }, - }, - ), + data=parameters, message="Data retrieved successfully", ) @@ -40,7 +25,6 @@ async def get_calculation_parameters(db_session: DbSession): async def create_calculation_time_constrains(db_session: DbSession, calculation_time_constrains_in: CalculationTimeConstrainsCreate): """Calculate Here""" calculation_result = await create_calculation(db_session=db_session) - return StandardResponse(data=CalculationTimeConstrainsRead( id=calculation_result["id"], diff --git a/src/calculation_time_constrains/schema.py b/src/calculation_time_constrains/schema.py index f45e3ad..7d1c2c0 100644 --- a/src/calculation_time_constrains/schema.py +++ b/src/calculation_time_constrains/schema.py @@ -7,20 +7,6 @@ from uuid import UUID from pydantic import Field from src.models import DefultBase -# { -# "costPerFailure": 733.614, -# "availableScopes": ["A", "B"], -# "recommendedScope": "B", -# "historicalData": { -# "averageOverhaulCost": 10000000, -# "lastCalculation": { -# "id": "calc_122", -# "date": "2024-10-15", -# "scope": "B" -# } -# } -# } - class CalculationTimeConstrainsBase(DefultBase): pass @@ -33,47 +19,23 @@ class CalculationTimeConstrainsParametersRead(CalculationTimeConstrainsBase): historicalData: Dict[str, Any] = Field(..., description="Historical data") -# { -# "overhaulCost": 10000000, -# "scopeOH": "B", -# "costPerFailure": 733.614, -# "metadata": { -# "unit": "PLTU1", -# "calculatedBy": "user123", -# "timestamp": "2024-11-28T10:00:00Z" -# } -# } - class CalculationTimeConstrainsCreate(CalculationTimeConstrainsBase): overhaulCost: float = Field(..., description="Overhaul cost") scopeOH: str = Field(..., description="Scope OH") costPerFailure: float = Field(..., description="Cost per failure") metadata: Dict[str, Any] = Field(..., description="Metadata") -# { -# "calculationId": "calc_123", -# "result": { -# "summary": { -# "scope": "B", -# "numberOfFailures": 59, -# "optimumOHTime": 90, -# "optimumTotalCost": 500000000 -# }, -# "chartData": {/* ... */}, -# "comparisons": { -# "vsLastCalculation": { -# "costDifference": -50000000, -# "timeChange": "+15 days" -# } -# } -# }, -# "simulationLimits": { -# "minInterval": 30, -# "maxInterval": 180 -# } -# } - class CalculationTimeConstrainsRead(CalculationTimeConstrainsBase): id: Union[UUID, str] result: Dict[str, Any] + + +{ + "calculationId": "calc_123", + "intervalDays": 45, +} + +class CalculationTimeConstrainsSimulationRead(CalculationTimeConstrainsBase): + simulation: Dict[str, Any] + comparison: Dict[str, Any] \ No newline at end of file diff --git a/src/calculation_time_constrains/service.py b/src/calculation_time_constrains/service.py new file mode 100644 index 0000000..d9ee229 --- /dev/null +++ b/src/calculation_time_constrains/service.py @@ -0,0 +1,6 @@ +import numpy as np + + +def get_overhaul_cost_by_time_chart(overhaul_cost: float, days: int) -> list: + exponents = np.arange(1, days + 1) + return overhaul_cost / (2 ** exponents) diff --git a/src/scope_equipment/router.py b/src/scope_equipment/router.py index 2e24f42..c57d7ef 100644 --- a/src/scope_equipment/router.py +++ b/src/scope_equipment/router.py @@ -54,9 +54,9 @@ async def get_scope_equipment(db_session: DbSession, scope_equipment_id: str): return StandardResponse(data=scope, message="Data retrieved successfully") -@router.post("", response_model=StandardResponse[ScopeEquipmentRead]) -async def create_scope_equipment(db_session: DbSession, scope__equipment_in: ScopeEquipmentCreate): - scope = await create(db_session=db_session, scope__equipment_in=scope__equipment_in) +@router.post("", response_model=StandardResponse[List[str]]) +async def create_scope_equipment(db_session: DbSession, scope_equipment_in: ScopeEquipmentCreate): + scope = await create(db_session=db_session, scope_equipment_in=scope_equipment_in) return StandardResponse(data=scope, message="Data created successfully") diff --git a/src/scope_equipment/service.py b/src/scope_equipment/service.py index 548b7e3..d5fc417 100644 --- a/src/scope_equipment/service.py +++ b/src/scope_equipment/service.py @@ -1,8 +1,8 @@ from fastapi import HTTPException, status -from sqlalchemy import Select, Delete, desc, func, not_, insert, or_ - +from sqlalchemy import Select, Delete, desc, func, not_, or_ +from sqlalchemy.dialects.postgresql import insert from src.workorder.model import MasterWorkOrder from .model import ScopeEquipment, MasterEquipment from src.scope.service import get_by_scope_name as get_scope_by_name_service @@ -43,7 +43,7 @@ async def create(*, db_session: DbSession, scope_equipment_in: ScopeEquipmentCre """Creates a new document.""" # scope_equipment = ScopeEquipment(**scope_equipment_in.model_dump()) assetnums = scope_equipment_in.assetnums - scope = get_scope_by_name_service( + scope = await get_scope_by_name_service( db_session=db_session, scope_name=scope_equipment_in.scope_name) if not scope: @@ -58,14 +58,16 @@ async def create(*, db_session: DbSession, scope_equipment_in: ScopeEquipmentCre stmt = insert(ScopeEquipment).values( assetnum=assetnum, scope_id=scope.id - ).on_conflict_do_update( + ) + + stmt = stmt.on_conflict_do_update( index_elements=["assetnum"], set_={ "scope_id": scope.id } ) - db_session.execute(stmt) + await db_session.execute(stmt) results.append(assetnum) await db_session.commit()