From 290b74d827ed977a1dfc348b9c8a7904d0d1f108 Mon Sep 17 00:00:00 2001 From: Cizz22 Date: Thu, 14 Aug 2025 15:12:28 +0700 Subject: [PATCH] fix --- src/calculation_time_constrains/service.py | 1 + src/maximo/service.py | 36 ++++++++++++++++++++++ src/overhaul_activity/schema.py | 1 + src/overhaul_activity/service.py | 9 ++++-- 4 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/calculation_time_constrains/service.py b/src/calculation_time_constrains/service.py index 1a725fe..e8e6fdd 100644 --- a/src/calculation_time_constrains/service.py +++ b/src/calculation_time_constrains/service.py @@ -1219,6 +1219,7 @@ class OptimumCostModel: for equipment in equipments: location_tag = equipment.location_tag cost_per_failure = equipment.material_cost + overhaul_cost = equipment.overhaul_cost # Get pre-fetched reliability data failure_rate = all_reliabilities.get(location_tag, {}) diff --git a/src/maximo/service.py b/src/maximo/service.py index ac618c3..ec90ca9 100644 --- a/src/maximo/service.py +++ b/src/maximo/service.py @@ -41,3 +41,39 @@ async def get_cm_cost_summary(collector_db: CollectorDbSession, last_oh_date:dat return { data.location: data.avg_cost for data in data } + + +async def get_oh_cost_summary(collector_db: CollectorDbSession, last_oh_date:datetime, upcoming_oh_date:datetime): + query = select( + WorkOrderData.location, + (func.sum(WorkOrderData.total_cost_max).cast(Numeric) / func.count(WorkOrderData.wonum)).label('avg_cost') + ).where( + and_( + # WorkOrderData.wo_start >= last_oh_date, + # WorkOrderData.wo_start <= upcoming_oh_date, + WorkOrderData.worktype.in_(['OH']), + WorkOrderData.system_tag.in_(['HPB', 'AH', 'APC', 'SCR', 'CL', 'DM', 'CRH', 'ASH', 'BAD', 'DS', 'WTP', + 'MT', 'SUP', 'DCS', 'FF', 'EG', 'AI', 'SPS', 'EVM', 'SCW', 'KLH', 'CH', + 'TUR', 'LOT', 'HRH', 'ESP', 'CAE', 'GMC', 'BFT', 'LSH', 'CHB', 'BSS', + 'LOS', 'LPB', 'SAC', 'CP', 'EHS', 'RO', 'GG', 'MS', 'CW', 'SO', 'ATT', + 'AFG', 'EHB', 'RP', 'FO', 'PC', 'APE', 'AF', 'DMW', 'BRS', 'GEN', 'ABS', + 'CHA', 'TR', 'H2', 'BDW', 'LOM', 'ACR', 'AL', 'FW', 'COND', 'CCCW', 'IA', + 'GSS', 'BOL', 'SSB', 'CO', 'OA', 'CTH-UPD', 'AS', 'DP']), + WorkOrderData.reportdate.is_not(None), + WorkOrderData.actstart.is_not(None), + WorkOrderData.actfinish.is_not(None), + WorkOrderData.unit.in_([3, 0]), + WorkOrderData.reportdate >= datetime.strptime('2015-01-01', '%Y-%m-%d'), + not_(WorkOrderData.wonum.like('T%')) + ) + ).group_by( + WorkOrderData.location + ).order_by( + func.count(WorkOrderData.wonum).desc() + ) + result = await collector_db.execute(query) + data = result.all() + + return { + data.location: data.avg_cost for data in data + } diff --git a/src/overhaul_activity/schema.py b/src/overhaul_activity/schema.py index 061e563..f4d9ccf 100644 --- a/src/overhaul_activity/schema.py +++ b/src/overhaul_activity/schema.py @@ -41,6 +41,7 @@ class OverhaulActivityRead(OverhaulActivityBase): location_tag: str equipment_name: Optional[str] oh_scope: str + overhaul_cost: Optional[float] = Field(0) # equipment: MasterEquipmentTree # overhaul_scope: OverhaulScope # overhaul_jobs: Optional[List[OverhaulJob]] = Field([]) diff --git a/src/overhaul_activity/service.py b/src/overhaul_activity/service.py index 088d7bc..cf1c214 100644 --- a/src/overhaul_activity/service.py +++ b/src/overhaul_activity/service.py @@ -27,7 +27,7 @@ from .schema import (OverhaulActivityCreate, OverhaulActivityRead, import json from src.database.core import CollectorDbSession -from src.maximo.service import get_cm_cost_summary +from src.maximo.service import get_cm_cost_summary, get_oh_cost_summary async def get( *, db_session: DbSession, assetnum: str, overhaul_session_id: Optional[UUID] = None @@ -108,6 +108,7 @@ async def get_all( material_cost = await get_cm_cost_summary(collector_db=collector_db, last_oh_date=prev_oh_scope.end_date, upcoming_oh_date=overhaul.start_date) service_cost = get_service_cost(scope=overhaul.maintenance_type.name, total_equipment=num_equipments) + overhaul_cost = await get_oh_cost_summary(collector_db=collector_db, last_oh_date=prev_oh_scope.end_date, upcoming_oh_date=overhaul.start_date) equipments = await search_filter_sort_paginate(model=query, **common) data = equipments['items'] @@ -120,11 +121,13 @@ async def get_all( continue cost = material_cost.get(equipment.location_tag, 0) + oh_cost = overhaul_cost.get(equipment.location_tag, 0) res = OverhaulActivityRead( id=equipment.id, material_cost=float(cost), service_cost=float(service_cost), + overhaul_cost=float(oh_cost), location_tag=equipment.location_tag, equipment_name=equipment.master_equipment.name if equipment.master_equipment else None, oh_scope=overhaul.maintenance_type.name, @@ -161,14 +164,16 @@ async def get_standard_scope_by_session_id(*, db_session: DbSession, overhaul_se results = [] material_cost = await get_cm_cost_summary(collector_db=collector_db, last_oh_date=prev_oh_scope.end_date, upcoming_oh_date=overhaul.start_date) service_cost = get_service_cost(scope=overhaul.maintenance_type.name, total_equipment=len(eqs)) - + overhaul_cost = await get_oh_cost_summary(collector_db=collector_db, last_oh_date=prev_oh_scope.end_date, upcoming_oh_date=overhaul.start_date) for equipment in eqs: cost = material_cost.get(equipment.location_tag,0) + oh_cost = overhaul_cost.get(equipment.location_tag,0) res = OverhaulActivityRead( id=equipment.id, material_cost=float(cost), service_cost=float(service_cost), + overhaul_cost=float(oh_cost), location_tag=equipment.location_tag, equipment_name=equipment.master_equipment.name if equipment.master_equipment else None, oh_scope=overhaul.maintenance_type.name,