From 350e9a29c8a35a6791cc409ad5c71a8c76f2041f Mon Sep 17 00:00:00 2001 From: Victor Fraile Garcia Date: Wed, 27 May 2026 20:31:56 +0200 Subject: [PATCH] fix(ucepsa): scope Loss Intelligence telemetry alerts to active orders --- ...ss_intelligence_scope_to_active_orders.sql | 78 +++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 ucepsa/edge-oee-demo/sql/072_loss_intelligence_scope_to_active_orders.sql diff --git a/ucepsa/edge-oee-demo/sql/072_loss_intelligence_scope_to_active_orders.sql b/ucepsa/edge-oee-demo/sql/072_loss_intelligence_scope_to_active_orders.sql new file mode 100644 index 0000000..b6aa29e --- /dev/null +++ b/ucepsa/edge-oee-demo/sql/072_loss_intelligence_scope_to_active_orders.sql @@ -0,0 +1,78 @@ +DROP VIEW IF EXISTS mv_reports_ucepsa_demo.loss_priority_board; + +CREATE VIEW mv_reports_ucepsa_demo.loss_priority_board AS +WITH active_orders AS ( + SELECT DISTINCT + machine_id + FROM mv_reports_ucepsa_demo.odoo_current_order_demo +), +filtered AS ( + SELECT e.* + FROM mv_reports_ucepsa_demo.loss_events_current e + WHERE NOT ( + e.loss_type IN ('STALE_OR_BAD_TELEMETRY', 'LOW_CYCLE_RATE') + AND NOT EXISTS ( + SELECT 1 + FROM active_orders a + WHERE a.machine_id = e.machine_id + ) + ) + AND NOT ( + e.loss_type = 'STALE_OR_BAD_TELEMETRY' + AND EXISTS ( + SELECT 1 + FROM mv_reports_ucepsa_demo.loss_events_current x + WHERE x.machine_id = e.machine_id + AND x.loss_type = 'SIN_TELEMETRIA' + ) + ) +) +SELECT + row_number() OVER ( + ORDER BY + ( + severity_score + + LEAST((COALESCE(cost_eur, 0) * 10), 50)::int + + LEAST((COALESCE(duration_min, 0) * 2), 40)::int + ) DESC, + detected_at DESC + ) AS priority_rank, + ( + severity_score + + LEAST((COALESCE(cost_eur, 0) * 10), 50)::int + + LEAST((COALESCE(duration_min, 0) * 2), 40)::int + ) AS priority_score, + CASE + WHEN ( + severity_score + + LEAST((COALESCE(cost_eur, 0) * 10), 50)::int + + LEAST((COALESCE(duration_min, 0) * 2), 40)::int + ) >= 90 THEN 'CRÍTICA' + WHEN ( + severity_score + + LEAST((COALESCE(cost_eur, 0) * 10), 50)::int + + LEAST((COALESCE(duration_min, 0) * 2), 40)::int + ) >= 60 THEN 'ALTA' + WHEN ( + severity_score + + LEAST((COALESCE(cost_eur, 0) * 10), 50)::int + + LEAST((COALESCE(duration_min, 0) * 2), 40)::int + ) >= 35 THEN 'MEDIA' + ELSE 'BAJA' + END AS priority_level, + detected_at, + machine_id, + asset, + order_ref, + loss_type, + severity, + duration_min, + cost_eur, + status, + description, + recommended_action, + evidence_json +FROM filtered +ORDER BY priority_score DESC, detected_at DESC; + +GRANT SELECT ON mv_reports_ucepsa_demo.loss_priority_board TO grafana_ucepsa_ro;