You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
136 lines
4.5 KiB
Python
136 lines
4.5 KiB
Python
from typing import Optional
|
|
from uuid import UUID
|
|
|
|
import numpy as np
|
|
from fastapi import HTTPException, status
|
|
from sqlalchemy import Select, func, select
|
|
from sqlalchemy.orm import joinedload
|
|
|
|
from src.auth.service import Token
|
|
from src.config import TC_RBD_ID
|
|
from src.database.core import DbSession
|
|
from src.overhaul_scope.service import get_all
|
|
from src.standard_scope.model import StandardScope
|
|
from src.workorder.model import MasterWorkOrder
|
|
|
|
from .schema import (CalculationTimeConstrainsParametersCreate,
|
|
CalculationTimeConstrainsParametersRead,
|
|
CalculationTimeConstrainsParametersRetrive,
|
|
CalculationTimeConstrainsRead)
|
|
from .service import (create_calculation_result_service, create_param_and_data,
|
|
get_avg_cost_by_asset,
|
|
get_calculation_by_reference_and_parameter,
|
|
get_calculation_data_by_id, get_calculation_result,
|
|
run_simulation_with_spareparts)
|
|
from src.database.core import CollectorDbSession
|
|
|
|
|
|
async def get_create_calculation_parameters(
|
|
*, db_session: DbSession, calculation_id: Optional[str] = None
|
|
):
|
|
|
|
|
|
if calculation_id is not None:
|
|
calculation = await get_calculation_data_by_id(
|
|
calculation_id=calculation_id, db_session=db_session
|
|
)
|
|
|
|
if not calculation:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="A data with this id does not exist.",
|
|
)
|
|
|
|
return CalculationTimeConstrainsParametersRead(
|
|
costPerFailure=calculation.parameter.avg_failure_cost,
|
|
overhaulCost=calculation.parameter.overhaul_cost,
|
|
reference=calculation,
|
|
)
|
|
|
|
stmt = (
|
|
select(
|
|
StandardScope,
|
|
func.avg(MasterWorkOrder.total_cost_max).label("average_cost"),
|
|
)
|
|
.outerjoin(MasterWorkOrder, StandardScope.location_tag == MasterWorkOrder.location_tag)
|
|
.group_by(StandardScope.id)
|
|
)
|
|
|
|
results = await db_session.execute(stmt)
|
|
costFailure = results.all()
|
|
scopes = await get_all(db_session=db_session)
|
|
avaiableScopes = {scope.id: scope.scope_name for scope in scopes}
|
|
costFailurePerScope = {
|
|
avaiableScopes.get(costPerFailure[0]): costPerFailure[1]
|
|
for costPerFailure in costFailure
|
|
}
|
|
|
|
return CalculationTimeConstrainsParametersRetrive(
|
|
costPerFailure=costFailurePerScope,
|
|
availableScopes=avaiableScopes.values(),
|
|
recommendedScope="A",
|
|
# historicalData={
|
|
# "averageOverhaulCost": 10000000,
|
|
# "lastCalculation": {
|
|
# "id": "calc_122",
|
|
# "date": "2024-10-15",
|
|
# "scope": "B",
|
|
# },
|
|
# },
|
|
)
|
|
|
|
|
|
async def create_calculation(
|
|
*,
|
|
token: str,
|
|
db_session: DbSession,
|
|
collector_db_session: CollectorDbSession,
|
|
calculation_time_constrains_in: CalculationTimeConstrainsParametersCreate,
|
|
created_by: str,
|
|
simulation_id
|
|
):
|
|
calculation_data = await create_param_and_data(
|
|
db_session=db_session,
|
|
calculation_param_in=calculation_time_constrains_in,
|
|
created_by=created_by,
|
|
)
|
|
|
|
rbd_simulation_id = simulation_id or TC_RBD_ID
|
|
|
|
# results = await create_calculation_result_service(
|
|
# db_session=db_session, calculation=calculation_data, token=token
|
|
# )
|
|
results = await run_simulation_with_spareparts(
|
|
db_session=db_session, calculation=calculation_data, token=token, collector_db_session=collector_db_session, simulation_id=rbd_simulation_id
|
|
)
|
|
|
|
return results
|
|
|
|
|
|
async def get_or_create_scope_equipment_calculation(
|
|
*,
|
|
db_session: DbSession,
|
|
scope_calculation_id,
|
|
calculation_time_constrains_in: Optional[CalculationTimeConstrainsParametersCreate]
|
|
):
|
|
scope_calculation = await get_calculation_data_by_id(
|
|
db_session=db_session, calculation_id=scope_calculation_id
|
|
)
|
|
|
|
if not scope_calculation:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_404_NOT_FOUND,
|
|
detail="A data with this id does not exist.",
|
|
)
|
|
|
|
return scope_calculation.id
|
|
|
|
# Check if calculation already exist
|
|
# return CalculationTimeConstrainsRead(
|
|
# id=scope_calculation.id,
|
|
# reference=scope_calculation.overhaul_session_id,
|
|
# results=scope_calculation.results,
|
|
# optimum_oh=scope_calculation.optimum_oh_day,
|
|
# equipment_results=scope_calculation.equipment_results,
|
|
# )
|