580 lines
13 KiB
Python
580 lines
13 KiB
Python
import os
|
|
from datetime import datetime
|
|
|
|
import psycopg2
|
|
import psycopg2.extras
|
|
from flask import Flask, redirect, render_template_string, request, url_for
|
|
|
|
|
|
def getenv(name, default=None, required=False):
|
|
value = os.getenv(name, default)
|
|
if required and not value:
|
|
raise RuntimeError(f"Missing required environment variable: {name}")
|
|
return value
|
|
|
|
|
|
PG_CONFIG = {
|
|
"host": getenv("PGHOST", required=True),
|
|
"port": int(getenv("PGPORT", "5432")),
|
|
"dbname": getenv("PGDATABASE", required=True),
|
|
"user": getenv("PGUSER", required=True),
|
|
"password": getenv("PGPASSWORD", required=True),
|
|
"connect_timeout": 5,
|
|
}
|
|
|
|
APP_TITLE = getenv("APP_TITLE", "MESAVAULT Edge-OEE - Consola de paros UCEPSA")
|
|
|
|
DEFAULT_MACHINE_ID = getenv("DEFAULT_MACHINE_ID", "CORT-01")
|
|
DEFAULT_ASSET = getenv("DEFAULT_ASSET", "revpi_oee_node_01")
|
|
|
|
ALLOWED_MACHINES = [
|
|
item.strip()
|
|
for item in getenv("ALLOWED_MACHINES", DEFAULT_MACHINE_ID).split(",")
|
|
if item.strip()
|
|
]
|
|
|
|
|
|
def resolve_machine_id(raw_value):
|
|
machine_id = (raw_value or DEFAULT_MACHINE_ID).strip()
|
|
|
|
if machine_id not in ALLOWED_MACHINES:
|
|
return DEFAULT_MACHINE_ID
|
|
|
|
return machine_id
|
|
|
|
|
|
app = Flask(__name__)
|
|
|
|
|
|
def get_conn():
|
|
return psycopg2.connect(**PG_CONFIG)
|
|
|
|
|
|
def fetch_pending_stops(machine_id):
|
|
sql = """
|
|
SELECT
|
|
id,
|
|
tenant,
|
|
site,
|
|
vertical,
|
|
asset,
|
|
machine_id,
|
|
order_id,
|
|
started_at,
|
|
ended_at,
|
|
status,
|
|
classification_status,
|
|
cause_code,
|
|
cause_name,
|
|
operator_note,
|
|
duration_min,
|
|
cost_eur
|
|
FROM mv_hot.edge_oee_stops_recent
|
|
WHERE classification_status = 'PENDING'
|
|
AND machine_id = %(machine_id)s
|
|
ORDER BY started_at DESC
|
|
LIMIT 50;
|
|
"""
|
|
|
|
with get_conn() as conn:
|
|
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
|
|
cur.execute(sql, {"machine_id": machine_id})
|
|
return cur.fetchall()
|
|
|
|
|
|
def fetch_causes():
|
|
sql = """
|
|
SELECT
|
|
cause_code,
|
|
cause_name,
|
|
description,
|
|
is_planned
|
|
FROM mv_hot.edge_oee_stop_cause_options
|
|
ORDER BY sort_order, cause_name;
|
|
"""
|
|
|
|
with get_conn() as conn:
|
|
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as cur:
|
|
cur.execute(sql)
|
|
return cur.fetchall()
|
|
|
|
|
|
def classify_stop(stop_id, machine_id, cause_code, operator_note):
|
|
sql = """
|
|
UPDATE mv_hot.edge_oee_machine_stops
|
|
SET
|
|
cause_code = %(cause_code)s,
|
|
operator_note = %(operator_note)s,
|
|
classification_status = 'CLASSIFIED',
|
|
updated_at = now()
|
|
WHERE id = %(stop_id)s
|
|
AND machine_id = %(machine_id)s
|
|
AND classification_status = 'PENDING';
|
|
"""
|
|
|
|
with get_conn() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute(
|
|
sql,
|
|
{
|
|
"stop_id": stop_id,
|
|
"machine_id": machine_id,
|
|
"cause_code": cause_code,
|
|
"operator_note": operator_note,
|
|
},
|
|
)
|
|
return cur.rowcount
|
|
|
|
|
|
def fmt_dt(value):
|
|
if not value:
|
|
return ""
|
|
if isinstance(value, datetime):
|
|
return value.strftime("%Y-%m-%d %H:%M:%S")
|
|
return str(value)
|
|
|
|
|
|
def fmt_num(value, decimals=2):
|
|
if value is None:
|
|
return "0"
|
|
try:
|
|
return f"{float(value):.{decimals}f}"
|
|
except Exception:
|
|
return str(value)
|
|
|
|
|
|
HTML = """
|
|
<!doctype html>
|
|
<html lang="es">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>{{ app_title }}</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
|
|
<style>
|
|
:root {
|
|
--bg: #0f172a;
|
|
--panel: #111827;
|
|
--panel2: #1f2937;
|
|
--text: #e5e7eb;
|
|
--muted: #94a3b8;
|
|
--line: #334155;
|
|
--blue: #2563eb;
|
|
--blue2: #60a5fa;
|
|
--green: #22c55e;
|
|
--red: #ef4444;
|
|
--orange: #f97316;
|
|
--input: #020617;
|
|
}
|
|
|
|
* {
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
margin: 0;
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
background: var(--bg);
|
|
color: var(--text);
|
|
}
|
|
|
|
header {
|
|
padding: 18px 22px;
|
|
border-bottom: 1px solid var(--line);
|
|
}
|
|
|
|
h1 {
|
|
margin: 0 0 6px 0;
|
|
font-size: 28px;
|
|
line-height: 1.1;
|
|
}
|
|
|
|
.subtitle {
|
|
color: var(--muted);
|
|
font-size: 15px;
|
|
}
|
|
|
|
main {
|
|
padding: 22px;
|
|
max-width: 1800px;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
.machine-tabs {
|
|
display: flex;
|
|
gap: 8px;
|
|
margin: 0 0 22px 0;
|
|
flex-wrap: wrap;
|
|
}
|
|
|
|
.machine-tab {
|
|
padding: 9px 13px;
|
|
border-radius: 10px;
|
|
background: var(--panel2);
|
|
color: #dbeafe;
|
|
text-decoration: none;
|
|
border: 1px solid var(--line);
|
|
font-weight: 600;
|
|
font-size: 14px;
|
|
}
|
|
|
|
.machine-tab.active {
|
|
background: var(--blue);
|
|
color: white;
|
|
border-color: var(--blue2);
|
|
}
|
|
|
|
.cards {
|
|
display: grid;
|
|
grid-template-columns: repeat(3, minmax(220px, 1fr));
|
|
gap: 14px;
|
|
margin-bottom: 22px;
|
|
}
|
|
|
|
.card {
|
|
background: var(--panel);
|
|
border: 1px solid var(--line);
|
|
border-radius: 12px;
|
|
padding: 18px;
|
|
min-height: 96px;
|
|
}
|
|
|
|
.card-title {
|
|
color: var(--muted);
|
|
font-size: 15px;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.card-value {
|
|
font-size: 34px;
|
|
line-height: 1;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.card-subtitle {
|
|
margin-top: 8px;
|
|
color: var(--muted);
|
|
font-size: 14px;
|
|
}
|
|
|
|
.table-wrap {
|
|
overflow-x: auto;
|
|
background: var(--panel);
|
|
border: 1px solid var(--line);
|
|
border-radius: 12px;
|
|
}
|
|
|
|
table {
|
|
width: 100%;
|
|
border-collapse: collapse;
|
|
min-width: 1300px;
|
|
}
|
|
|
|
th {
|
|
background: var(--panel2);
|
|
color: white;
|
|
text-align: left;
|
|
font-size: 14px;
|
|
padding: 12px 14px;
|
|
border-bottom: 1px solid var(--line);
|
|
white-space: nowrap;
|
|
}
|
|
|
|
td {
|
|
padding: 12px 14px;
|
|
border-bottom: 1px solid #1e293b;
|
|
vertical-align: top;
|
|
font-size: 14px;
|
|
}
|
|
|
|
tr:last-child td {
|
|
border-bottom: none;
|
|
}
|
|
|
|
.machine-main {
|
|
font-weight: 700;
|
|
font-size: 16px;
|
|
}
|
|
|
|
.machine-sub {
|
|
color: var(--muted);
|
|
margin-top: 2px;
|
|
font-size: 13px;
|
|
}
|
|
|
|
.badge {
|
|
display: inline-block;
|
|
padding: 4px 9px;
|
|
border-radius: 999px;
|
|
font-size: 12px;
|
|
font-weight: 700;
|
|
border: 1px solid transparent;
|
|
margin-right: 4px;
|
|
}
|
|
|
|
.badge-open {
|
|
background: rgba(239, 68, 68, 0.14);
|
|
color: #fecaca;
|
|
border-color: rgba(239, 68, 68, 0.45);
|
|
}
|
|
|
|
.badge-closed {
|
|
background: rgba(34, 197, 94, 0.14);
|
|
color: #bbf7d0;
|
|
border-color: rgba(34, 197, 94, 0.45);
|
|
}
|
|
|
|
.badge-pending {
|
|
background: rgba(249, 115, 22, 0.14);
|
|
color: #fed7aa;
|
|
border-color: rgba(249, 115, 22, 0.45);
|
|
margin-top: 5px;
|
|
}
|
|
|
|
select,
|
|
input[type="text"] {
|
|
width: 100%;
|
|
min-width: 210px;
|
|
padding: 10px 12px;
|
|
border-radius: 8px;
|
|
border: 1px solid var(--line);
|
|
background: var(--input);
|
|
color: var(--text);
|
|
font-size: 14px;
|
|
}
|
|
|
|
button {
|
|
padding: 10px 18px;
|
|
border-radius: 8px;
|
|
border: 0;
|
|
background: var(--blue);
|
|
color: white;
|
|
font-weight: 700;
|
|
cursor: pointer;
|
|
font-size: 14px;
|
|
}
|
|
|
|
button:hover {
|
|
background: #1d4ed8;
|
|
}
|
|
|
|
.empty {
|
|
padding: 40px;
|
|
text-align: center;
|
|
color: var(--muted);
|
|
font-size: 18px;
|
|
}
|
|
|
|
.footer-note {
|
|
margin-top: 18px;
|
|
color: var(--muted);
|
|
font-size: 13px;
|
|
}
|
|
|
|
@media (max-width: 900px) {
|
|
main {
|
|
padding: 14px;
|
|
}
|
|
|
|
.cards {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
|
|
h1 {
|
|
font-size: 22px;
|
|
}
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<header>
|
|
<h1>{{ app_title }}</h1>
|
|
<div class="subtitle">
|
|
Clasificación manual de paros detectados por Edge-OEE ·
|
|
Máquina seleccionada: <strong>{{ selected_machine }}</strong> ·
|
|
Última carga: {{ loaded_at }}
|
|
</div>
|
|
</header>
|
|
|
|
<main>
|
|
<div class="machine-tabs">
|
|
{% for machine in allowed_machines %}
|
|
<a
|
|
class="machine-tab {% if machine == selected_machine %}active{% endif %}"
|
|
href="{{ url_for('index', machine_id=machine) }}"
|
|
>
|
|
{{ machine }}
|
|
</a>
|
|
{% endfor %}
|
|
</div>
|
|
|
|
<div class="cards">
|
|
<div class="card">
|
|
<div class="card-title">Paros pendientes</div>
|
|
<div class="card-value">{{ pending_count }}</div>
|
|
</div>
|
|
|
|
<div class="card">
|
|
<div class="card-title">Máquina monitorizada</div>
|
|
<div class="card-value">{{ selected_machine }}</div>
|
|
<div class="card-subtitle">{{ default_asset }}</div>
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div class="table-wrap">
|
|
{% if stops %}
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>ID</th>
|
|
<th>Máquina</th>
|
|
<th>Inicio</th>
|
|
<th>Fin</th>
|
|
<th>Estado</th>
|
|
<th>Duración</th>
|
|
<th>Coste</th>
|
|
<th>Causa</th>
|
|
<th>Nota</th>
|
|
<th>Guardar</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{% for stop in stops %}
|
|
<tr>
|
|
<td>#{{ stop.id }}</td>
|
|
|
|
<td>
|
|
<div class="machine-main">{{ stop.machine_id }}</div>
|
|
<div class="machine-sub">{{ stop.asset }}</div>
|
|
</td>
|
|
|
|
<td>{{ fmt_dt(stop.started_at) }}</td>
|
|
|
|
<td>
|
|
{% if stop.ended_at %}
|
|
{{ fmt_dt(stop.ended_at) }}
|
|
{% else %}
|
|
Abierto
|
|
{% endif %}
|
|
</td>
|
|
|
|
<td>
|
|
{% if stop.status == "OPEN" %}
|
|
<span class="badge badge-open">OPEN</span>
|
|
{% else %}
|
|
<span class="badge badge-closed">{{ stop.status }}</span>
|
|
{% endif %}
|
|
<br>
|
|
<span class="badge badge-pending">{{ stop.classification_status }}</span>
|
|
</td>
|
|
|
|
<td>{{ fmt_num(stop.duration_min, 2) }} min</td>
|
|
<td>{{ fmt_num(stop.cost_eur, 2) }} €</td>
|
|
|
|
<td>
|
|
<form method="post" action="{{ url_for('classify', stop_id=stop.id) }}">
|
|
<input type="hidden" name="machine_id" value="{{ selected_machine }}">
|
|
|
|
<select name="cause_code" required>
|
|
<option value="">Seleccionar causa...</option>
|
|
{% for cause in causes %}
|
|
<option value="{{ cause.cause_code }}">
|
|
{{ cause.cause_name }}
|
|
</option>
|
|
{% endfor %}
|
|
</select>
|
|
</td>
|
|
|
|
<td>
|
|
<input
|
|
type="text"
|
|
name="operator_note"
|
|
placeholder="Nota opcional"
|
|
value="{{ stop.operator_note or '' }}"
|
|
>
|
|
</td>
|
|
|
|
<td>
|
|
<button type="submit">Guardar</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
{% else %}
|
|
<div class="empty">
|
|
No hay paros pendientes para {{ selected_machine }}.
|
|
</div>
|
|
{% endif %}
|
|
</div>
|
|
|
|
</main>
|
|
</body>
|
|
</html>
|
|
"""
|
|
|
|
|
|
@app.route("/", methods=["GET"])
|
|
def index():
|
|
selected_machine = resolve_machine_id(request.args.get("machine_id"))
|
|
|
|
stops = fetch_pending_stops(selected_machine)
|
|
causes = fetch_causes()
|
|
|
|
return render_template_string(
|
|
HTML,
|
|
app_title=APP_TITLE,
|
|
stops=stops,
|
|
causes=causes,
|
|
pending_count=len(stops),
|
|
selected_machine=selected_machine,
|
|
allowed_machines=ALLOWED_MACHINES,
|
|
default_machine=selected_machine,
|
|
default_asset=DEFAULT_ASSET,
|
|
loaded_at=datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
|
|
fmt_dt=fmt_dt,
|
|
fmt_num=fmt_num,
|
|
)
|
|
|
|
|
|
@app.route("/classify/<int:stop_id>", methods=["POST"])
|
|
def classify(stop_id):
|
|
selected_machine = resolve_machine_id(
|
|
request.form.get("machine_id") or request.args.get("machine_id")
|
|
)
|
|
|
|
cause_code = (request.form.get("cause_code") or "").strip()
|
|
operator_note = (request.form.get("operator_note") or "").strip()
|
|
|
|
if cause_code:
|
|
classify_stop(
|
|
stop_id=stop_id,
|
|
machine_id=selected_machine,
|
|
cause_code=cause_code,
|
|
operator_note=operator_note,
|
|
)
|
|
|
|
return redirect(url_for("index", machine_id=selected_machine))
|
|
|
|
|
|
@app.route("/health", methods=["GET"])
|
|
def health():
|
|
try:
|
|
with get_conn() as conn:
|
|
with conn.cursor() as cur:
|
|
cur.execute("SELECT 1;")
|
|
cur.fetchone()
|
|
|
|
return {"status": "ok"}, 200
|
|
|
|
except Exception as exc:
|
|
return {"status": "error", "error": str(exc)}, 500
|
|
|
|
|
|
if __name__ == "__main__":
|
|
port = int(getenv("APP_PORT", "8080"))
|
|
app.run(host="0.0.0.0", port=port)
|