|
|
|
|
@ -2,13 +2,13 @@ from collections import defaultdict
|
|
|
|
|
from datetime import datetime
|
|
|
|
|
from typing import List, Optional
|
|
|
|
|
from uuid import UUID
|
|
|
|
|
|
|
|
|
|
from sqlalchemy.orm import selectinload
|
|
|
|
|
from fastapi import APIRouter, BackgroundTasks, HTTPException, background, status, Query
|
|
|
|
|
from sqlalchemy import select
|
|
|
|
|
from sqlalchemy import select, text
|
|
|
|
|
from temporalio.client import Client
|
|
|
|
|
from src.aeros_contribution.service import update_contribution_bulk_mappings
|
|
|
|
|
from src.aeros_equipment.model import AerosEquipment
|
|
|
|
|
from src.aeros_simulation.model import EafContribution
|
|
|
|
|
from src.aeros_simulation.model import AerosSimulationCalcResult, EafContribution, AerosNode
|
|
|
|
|
from src.auth.service import CurrentUser
|
|
|
|
|
from src.config import TEMPORAL_URL
|
|
|
|
|
from src.database.core import CollectorDbSession, DbSession
|
|
|
|
|
@ -386,3 +386,77 @@ async def calculate_contribution(
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/result/critical/{simulation_id}]", response_model=StandardResponse[List[SimulationCalc]])
|
|
|
|
|
async def get_critical_equipment(db_session:DbSession, simulation_id:UUID):
|
|
|
|
|
# Step 1: Get all failure events for this simulation
|
|
|
|
|
failure_query = text("""
|
|
|
|
|
SELECT DISTINCT
|
|
|
|
|
(elem ->> 'currentEvent') AS jenis,
|
|
|
|
|
(elem ->> 'cumulativeTime')::numeric AS cumulative_time
|
|
|
|
|
FROM rbd_tr_aeros_simulation_plot_result AS a
|
|
|
|
|
JOIN public.rbd_ms_aeros_node AS b
|
|
|
|
|
ON a.aeros_node_id = b.id
|
|
|
|
|
JOIN LATERAL jsonb_array_elements(a.timestamp_outs) AS elem ON TRUE
|
|
|
|
|
WHERE a.aeros_simulation_id = :simulation_id
|
|
|
|
|
AND b.node_name = '- TJB - Unit 3 -'
|
|
|
|
|
AND (elem ->> 'currentEQStatus') = 'OoS'
|
|
|
|
|
AND (elem ->> 'currentEvent') != 'ON_OH'
|
|
|
|
|
ORDER BY cumulative_time;
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
query = await db_session.execute(failure_query, {
|
|
|
|
|
"simulation_id": simulation_id,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
failures = query.fetchall()
|
|
|
|
|
|
|
|
|
|
results = []
|
|
|
|
|
|
|
|
|
|
# Step 2: For each failure, find which equipment caused it
|
|
|
|
|
for fail in failures:
|
|
|
|
|
cumulative_time = fail.cumulative_time
|
|
|
|
|
jenis = fail.jenis
|
|
|
|
|
|
|
|
|
|
equipment_query = text("""
|
|
|
|
|
SELECT b.id
|
|
|
|
|
FROM rbd_tr_aeros_simulation_plot_result AS a
|
|
|
|
|
JOIN public.rbd_ms_aeros_node AS b ON a.aeros_node_id = b.id
|
|
|
|
|
WHERE EXISTS (
|
|
|
|
|
SELECT 1
|
|
|
|
|
FROM jsonb_array_elements(a.timestamp_outs) AS elem
|
|
|
|
|
WHERE
|
|
|
|
|
(elem ->> 'currentEQStatus') = 'OoS' AND
|
|
|
|
|
(elem ->> 'cumulativeTime')::numeric = :cumulative_time
|
|
|
|
|
)
|
|
|
|
|
AND a.aeros_simulation_id = :simulation_id
|
|
|
|
|
AND b.node_type = 'RegularNode';
|
|
|
|
|
""")
|
|
|
|
|
|
|
|
|
|
equipment = (await db_session.execute(equipment_query, {
|
|
|
|
|
"simulation_id": simulation_id,
|
|
|
|
|
"cumulative_time": cumulative_time
|
|
|
|
|
})).fetchall()
|
|
|
|
|
|
|
|
|
|
equipment_list = [eq.id for eq in equipment]
|
|
|
|
|
|
|
|
|
|
results.extend(equipment_list)
|
|
|
|
|
|
|
|
|
|
query = (select(AerosSimulationCalcResult).filter(
|
|
|
|
|
AerosSimulationCalcResult.aeros_simulation_id == simulation_id)).filter(AerosSimulationCalcResult.aeros_node_id.in_(results))
|
|
|
|
|
|
|
|
|
|
query = query.options(
|
|
|
|
|
selectinload(AerosSimulationCalcResult.aeros_node).options(
|
|
|
|
|
selectinload(AerosNode.equipment)
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
data = await db_session.execute(query)
|
|
|
|
|
|
|
|
|
|
equipments = data.scalars.all()
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
"data": equipments,
|
|
|
|
|
"status": "success",
|
|
|
|
|
"message": "Success",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|