Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 3856ee335a |
@@ -20,8 +20,7 @@ from src.backend.models.api.production import (
|
|||||||
)
|
)
|
||||||
from src.backend.models.orm.production import ProductionRun
|
from src.backend.models.orm.production import ProductionRun
|
||||||
from src.backend.models.orm.user import User
|
from src.backend.models.orm.user import User
|
||||||
from src.backend.models.api.monitor import MonitorRun
|
from src.backend.services import auth_service, production_service
|
||||||
from src.backend.services import auth_service, monitor_service, production_service
|
|
||||||
|
|
||||||
router = APIRouter(prefix="/api/production-runs", tags=["production"])
|
router = APIRouter(prefix="/api/production-runs", tags=["production"])
|
||||||
|
|
||||||
@@ -82,27 +81,6 @@ async def get_current_production_run(
|
|||||||
return await _as_response(db, run) if run is not None else None
|
return await _as_response(db, run) if run is not None else None
|
||||||
|
|
||||||
|
|
||||||
async def _require_high_authority(user: User = Depends(get_current_user)) -> User:
|
|
||||||
"""Monitor of all stations (#57): admin or Supervisor (capoturno) only."""
|
|
||||||
if user.is_admin or user.has_role("Supervisor"):
|
|
||||||
return user
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=status.HTTP_403_FORBIDDEN, detail="Supervisor role required",
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/open", response_model=list[MonitorRun])
|
|
||||||
async def list_open_production_runs(
|
|
||||||
user: User = Depends(_require_high_authority),
|
|
||||||
db: AsyncSession = Depends(get_db),
|
|
||||||
):
|
|
||||||
"""Every open run with timer, last event and recent measurements (#57).
|
|
||||||
|
|
||||||
Declared before /{run_id} so "open" is not parsed as a run id.
|
|
||||||
"""
|
|
||||||
return await monitor_service.list_open_runs(db)
|
|
||||||
|
|
||||||
|
|
||||||
@router.get("/{run_id}", response_model=ProductionRunWithEventsResponse)
|
@router.get("/{run_id}", response_model=ProductionRunWithEventsResponse)
|
||||||
async def get_production_run(
|
async def get_production_run(
|
||||||
run_id: int,
|
run_id: int,
|
||||||
|
|||||||
@@ -1,49 +0,0 @@
|
|||||||
"""API models for the live station monitor (#57)."""
|
|
||||||
from datetime import datetime
|
|
||||||
from typing import Optional
|
|
||||||
|
|
||||||
from pydantic import BaseModel
|
|
||||||
|
|
||||||
|
|
||||||
class MonitorSubtask(BaseModel):
|
|
||||||
"""Mean of the recent measurements of one quote, against its tolerance."""
|
|
||||||
|
|
||||||
subtask_id: int
|
|
||||||
description: str
|
|
||||||
unit: str
|
|
||||||
count: int
|
|
||||||
mean: float
|
|
||||||
ltl: Optional[float] = None
|
|
||||||
utl: Optional[float] = None
|
|
||||||
# True when ltl <= mean <= utl (a missing limit does not constrain).
|
|
||||||
in_tolerance: bool
|
|
||||||
|
|
||||||
|
|
||||||
class MonitorEvent(BaseModel):
|
|
||||||
event_type: str
|
|
||||||
created_at: datetime
|
|
||||||
note: Optional[str] = None
|
|
||||||
|
|
||||||
|
|
||||||
class MonitorRun(BaseModel):
|
|
||||||
id: int
|
|
||||||
station_code: str
|
|
||||||
station_name: str
|
|
||||||
recipe_code: str
|
|
||||||
recipe_name: str
|
|
||||||
version_number: int
|
|
||||||
operator: str
|
|
||||||
status: str
|
|
||||||
started_at: datetime
|
|
||||||
duration_seconds: int
|
|
||||||
cycle_count: int
|
|
||||||
seconds_to_next_measurement: Optional[int] = None
|
|
||||||
overdue: bool
|
|
||||||
last_event: Optional[MonitorEvent] = None
|
|
||||||
# Counts over the last MONITOR_WINDOW measurements of the run.
|
|
||||||
measured: int
|
|
||||||
pass_count: int
|
|
||||||
warning_count: int
|
|
||||||
fail_count: int
|
|
||||||
subtasks: list[MonitorSubtask]
|
|
||||||
server_time: datetime
|
|
||||||
@@ -1,133 +0,0 @@
|
|||||||
"""Live monitor of the open production runs (#57).
|
|
||||||
|
|
||||||
One query per table, whatever the number of runs: measurements and last events are
|
|
||||||
ranked per run with a window function and cut in the database, not in a loop.
|
|
||||||
"""
|
|
||||||
from collections import defaultdict
|
|
||||||
|
|
||||||
from sqlalchemy import func, select
|
|
||||||
from sqlalchemy.ext.asyncio import AsyncSession
|
|
||||||
|
|
||||||
from src.backend.models.api.monitor import MonitorEvent, MonitorRun, MonitorSubtask
|
|
||||||
from src.backend.models.orm.measurement import Measurement
|
|
||||||
from src.backend.models.orm.production import ProductionEvent, ProductionRun
|
|
||||||
from src.backend.models.orm.task import RecipeSubtask
|
|
||||||
from src.backend.models.orm.user import User
|
|
||||||
from src.backend.services import production_service
|
|
||||||
|
|
||||||
# Recent measurements per run that feed counts and means (#57: last N).
|
|
||||||
MONITOR_WINDOW = 20
|
|
||||||
|
|
||||||
|
|
||||||
def _in_tolerance(mean: float, ltl, utl) -> bool:
|
|
||||||
# A missing limit does not constrain.
|
|
||||||
if ltl is not None and mean < float(ltl):
|
|
||||||
return False
|
|
||||||
if utl is not None and mean > float(utl):
|
|
||||||
return False
|
|
||||||
return True
|
|
||||||
|
|
||||||
|
|
||||||
async def list_open_runs(db: AsyncSession) -> list[MonitorRun]:
|
|
||||||
runs = (await db.execute(
|
|
||||||
select(ProductionRun)
|
|
||||||
.where(ProductionRun.status.in_(("running", "paused")))
|
|
||||||
.order_by(ProductionRun.started_at)
|
|
||||||
)).scalars().all()
|
|
||||||
if not runs:
|
|
||||||
return []
|
|
||||||
run_ids = [r.id for r in runs]
|
|
||||||
|
|
||||||
# Operators: one query.
|
|
||||||
users = {
|
|
||||||
u.id: u for u in (await db.execute(
|
|
||||||
select(User).where(User.id.in_({r.operator_id for r in runs}))
|
|
||||||
)).scalars().all()
|
|
||||||
}
|
|
||||||
|
|
||||||
# Last event per run: rank desc inside the run, keep rank 1.
|
|
||||||
ev_rank = func.row_number().over(
|
|
||||||
partition_by=ProductionEvent.run_id,
|
|
||||||
order_by=(ProductionEvent.created_at.desc(), ProductionEvent.id.desc()),
|
|
||||||
).label("rn")
|
|
||||||
ev_sub = select(
|
|
||||||
ProductionEvent.run_id.label("run_id"),
|
|
||||||
ProductionEvent.event_type.label("event_type"),
|
|
||||||
ProductionEvent.created_at.label("created_at"),
|
|
||||||
ProductionEvent.note.label("note"),
|
|
||||||
ev_rank,
|
|
||||||
).where(ProductionEvent.run_id.in_(run_ids)).subquery()
|
|
||||||
ev_rows = (await db.execute(
|
|
||||||
select(ev_sub.c.run_id, ev_sub.c.event_type, ev_sub.c.created_at, ev_sub.c.note)
|
|
||||||
.where(ev_sub.c.rn == 1)
|
|
||||||
)).all()
|
|
||||||
last_event = {r.run_id: r for r in ev_rows}
|
|
||||||
|
|
||||||
# Last N measurements per run, joined to their subtask for the limits.
|
|
||||||
m_rank = func.row_number().over(
|
|
||||||
partition_by=Measurement.production_run_id,
|
|
||||||
order_by=(Measurement.measured_at.desc(), Measurement.id.desc()),
|
|
||||||
).label("rn")
|
|
||||||
m_sub = select(
|
|
||||||
Measurement.production_run_id.label("run_id"),
|
|
||||||
Measurement.subtask_id.label("subtask_id"),
|
|
||||||
Measurement.value.label("value"),
|
|
||||||
Measurement.pass_fail.label("pass_fail"),
|
|
||||||
m_rank,
|
|
||||||
).where(Measurement.production_run_id.in_(run_ids)).subquery()
|
|
||||||
m_rows = (await db.execute(
|
|
||||||
select(
|
|
||||||
m_sub.c.run_id, m_sub.c.subtask_id, m_sub.c.value, m_sub.c.pass_fail,
|
|
||||||
RecipeSubtask.description, RecipeSubtask.unit,
|
|
||||||
RecipeSubtask.ltl, RecipeSubtask.utl,
|
|
||||||
)
|
|
||||||
.join(RecipeSubtask, RecipeSubtask.id == m_sub.c.subtask_id)
|
|
||||||
.where(m_sub.c.rn <= MONITOR_WINDOW)
|
|
||||||
)).all()
|
|
||||||
by_run = defaultdict(list)
|
|
||||||
for row in m_rows:
|
|
||||||
by_run[row.run_id].append(row)
|
|
||||||
|
|
||||||
result = []
|
|
||||||
for run in runs:
|
|
||||||
rows = by_run.get(run.id, [])
|
|
||||||
per_sub: dict[int, list] = defaultdict(list)
|
|
||||||
for row in rows:
|
|
||||||
per_sub[row.subtask_id].append(row)
|
|
||||||
subtasks = []
|
|
||||||
for sid, group in sorted(per_sub.items()):
|
|
||||||
mean = sum(float(g.value) for g in group) / len(group)
|
|
||||||
first = group[0]
|
|
||||||
subtasks.append(MonitorSubtask(
|
|
||||||
subtask_id=sid, description=first.description, unit=first.unit,
|
|
||||||
count=len(group), mean=round(mean, 6),
|
|
||||||
ltl=float(first.ltl) if first.ltl is not None else None,
|
|
||||||
utl=float(first.utl) if first.utl is not None else None,
|
|
||||||
in_tolerance=_in_tolerance(mean, first.ltl, first.utl),
|
|
||||||
))
|
|
||||||
timer = production_service.describe(run)
|
|
||||||
ev = last_event.get(run.id)
|
|
||||||
operator = users.get(run.operator_id)
|
|
||||||
result.append(MonitorRun(
|
|
||||||
id=run.id,
|
|
||||||
station_code=run.station.code, station_name=run.station.name,
|
|
||||||
recipe_code=run.recipe.code, recipe_name=run.recipe.name,
|
|
||||||
version_number=run.version.version_number,
|
|
||||||
operator=(operator.display_name or operator.username) if operator else "",
|
|
||||||
status=run.status,
|
|
||||||
started_at=run.started_at,
|
|
||||||
duration_seconds=max(0, int((timer["server_time"] - run.started_at).total_seconds())),
|
|
||||||
cycle_count=run.cycle_count,
|
|
||||||
seconds_to_next_measurement=timer["seconds_to_next_measurement"],
|
|
||||||
overdue=timer["overdue"],
|
|
||||||
last_event=MonitorEvent(
|
|
||||||
event_type=ev.event_type, created_at=ev.created_at, note=ev.note,
|
|
||||||
) if ev else None,
|
|
||||||
measured=len(rows),
|
|
||||||
pass_count=sum(1 for g in rows if g.pass_fail == "pass"),
|
|
||||||
warning_count=sum(1 for g in rows if g.pass_fail == "warning"),
|
|
||||||
fail_count=sum(1 for g in rows if g.pass_fail == "fail"),
|
|
||||||
subtasks=subtasks,
|
|
||||||
server_time=timer["server_time"],
|
|
||||||
))
|
|
||||||
return result
|
|
||||||
@@ -1,83 +0,0 @@
|
|||||||
"""GET /api/production-runs/open - live monitor of the open runs (#57)."""
|
|
||||||
from datetime import datetime, timedelta
|
|
||||||
|
|
||||||
from sqlalchemy import select
|
|
||||||
|
|
||||||
from src.backend.models.orm.measurement import Measurement
|
|
||||||
from src.backend.models.orm.production import ProductionEvent, ProductionRun
|
|
||||||
from src.backend.models.orm.station import Station
|
|
||||||
from src.backend.models.orm.task import RecipeSubtask, RecipeTask
|
|
||||||
from src.backend.tests.conftest import _create_user, auth_headers, create_test_recipe
|
|
||||||
|
|
||||||
|
|
||||||
async def _run(db, user, code, status="running", overdue=False, open_=True):
|
|
||||||
"""A station plus a run on a fresh recipe (limits ltl 9.5 / utl 10.5)."""
|
|
||||||
station = Station(code=f"ST-{code}", name=f"Linea {code}", active=True, created_by=user.id)
|
|
||||||
db.add(station)
|
|
||||||
recipe = await create_test_recipe(db, user_id=user.id, code=f"REC-{code}")
|
|
||||||
await db.flush()
|
|
||||||
version = recipe.versions[0]
|
|
||||||
now = datetime.utcnow()
|
|
||||||
run = ProductionRun(
|
|
||||||
station_id=station.id, recipe_id=recipe.id, version_id=version.id,
|
|
||||||
operator_id=user.id, status=status if open_ else "closed",
|
|
||||||
started_at=now - timedelta(hours=1),
|
|
||||||
measurement_interval_minutes=30,
|
|
||||||
next_measurement_at=now + (timedelta(minutes=-5) if overdue else timedelta(minutes=20)),
|
|
||||||
cycle_count=3,
|
|
||||||
active_station_id=station.id if open_ else None,
|
|
||||||
)
|
|
||||||
db.add(run)
|
|
||||||
await db.flush()
|
|
||||||
db.add(ProductionEvent(run_id=run.id, event_type="start", user_id=user.id))
|
|
||||||
sub = (await db.execute(
|
|
||||||
select(RecipeSubtask).join(RecipeTask, RecipeTask.id == RecipeSubtask.task_id)
|
|
||||||
.where(RecipeTask.version_id == version.id)
|
|
||||||
)).scalars().first()
|
|
||||||
return run, sub, version
|
|
||||||
|
|
||||||
|
|
||||||
def _m(run, sub, version, user, value, pf, minutes=0):
|
|
||||||
return Measurement(
|
|
||||||
subtask_id=sub.id, version_id=version.id, measured_by=user.id, value=value,
|
|
||||||
pass_fail=pf, production_run_id=run.id,
|
|
||||||
measured_at=datetime.utcnow() - timedelta(minutes=minutes),
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
async def test_open_requires_high_authority(client, db_session, measurement_tec_user):
|
|
||||||
resp = await client.get("/api/production-runs/open", headers=auth_headers(measurement_tec_user))
|
|
||||||
assert resp.status_code == 403
|
|
||||||
|
|
||||||
|
|
||||||
async def test_open_lists_only_open_runs_with_means(client, db_session, admin_user):
|
|
||||||
sup = await _create_user(db_session, "capo", roles=["Supervisor"])
|
|
||||||
r1, s1, v1 = await _run(db_session, admin_user, "A", overdue=True)
|
|
||||||
r2, s2, v2 = await _run(db_session, admin_user, "B", status="paused")
|
|
||||||
await _run(db_session, admin_user, "C", open_=False)
|
|
||||||
# Run A: mean 10.0 (inside), run B: mean 10.8 (outside, one fail).
|
|
||||||
for i, (val, pf) in enumerate([(9.9, "pass"), (10.1, "pass")]):
|
|
||||||
db_session.add(_m(r1, s1, v1, admin_user, val, pf, i))
|
|
||||||
for i, (val, pf) in enumerate([(10.6, "fail"), (11.0, "fail"), (10.8, "warning")]):
|
|
||||||
db_session.add(_m(r2, s2, v2, admin_user, val, pf, i))
|
|
||||||
await db_session.commit()
|
|
||||||
|
|
||||||
resp = await client.get("/api/production-runs/open", headers=auth_headers(sup))
|
|
||||||
assert resp.status_code == 200
|
|
||||||
body = {r["station_code"]: r for r in resp.json()}
|
|
||||||
assert set(body) == {"ST-A", "ST-B"} # closed run C is absent
|
|
||||||
a, b = body["ST-A"], body["ST-B"]
|
|
||||||
assert a["overdue"] is True and a["seconds_to_next_measurement"] < 0
|
|
||||||
assert b["overdue"] is False and b["status"] == "paused"
|
|
||||||
assert a["duration_seconds"] >= 3600 and a["cycle_count"] == 3
|
|
||||||
assert a["last_event"]["event_type"] == "start"
|
|
||||||
assert a["pass_count"] == 2 and a["fail_count"] == 0
|
|
||||||
assert a["subtasks"][0]["mean"] == 10.0 and a["subtasks"][0]["in_tolerance"] is True
|
|
||||||
assert b["fail_count"] == 2 and b["warning_count"] == 1
|
|
||||||
assert round(b["subtasks"][0]["mean"], 3) == 10.8
|
|
||||||
assert b["subtasks"][0]["in_tolerance"] is False
|
|
||||||
|
|
||||||
|
|
||||||
async def test_open_empty_for_admin(client, admin_user):
|
|
||||||
resp = await client.get("/api/production-runs/open", headers=auth_headers(admin_user))
|
|
||||||
assert resp.status_code == 200 and resp.json() == []
|
|
||||||
@@ -74,14 +74,12 @@ def create_app() -> Flask:
|
|||||||
from blueprints.maker import maker_bp
|
from blueprints.maker import maker_bp
|
||||||
from blueprints.statistics import statistics_bp
|
from blueprints.statistics import statistics_bp
|
||||||
from blueprints.admin import admin_bp
|
from blueprints.admin import admin_bp
|
||||||
from blueprints.monitor import monitor_bp # #57 live station monitor
|
|
||||||
|
|
||||||
app.register_blueprint(auth_bp)
|
app.register_blueprint(auth_bp)
|
||||||
app.register_blueprint(measure_bp, url_prefix="/measure")
|
app.register_blueprint(measure_bp, url_prefix="/measure")
|
||||||
app.register_blueprint(maker_bp, url_prefix="/maker")
|
app.register_blueprint(maker_bp, url_prefix="/maker")
|
||||||
app.register_blueprint(statistics_bp, url_prefix="/statistics")
|
app.register_blueprint(statistics_bp, url_prefix="/statistics")
|
||||||
app.register_blueprint(admin_bp, url_prefix="/admin")
|
app.register_blueprint(admin_bp, url_prefix="/admin")
|
||||||
app.register_blueprint(monitor_bp, url_prefix="/monitor") # #57
|
|
||||||
|
|
||||||
@app.before_request
|
@app.before_request
|
||||||
def _check_secret_key():
|
def _check_secret_key():
|
||||||
|
|||||||
@@ -1,8 +1,10 @@
|
|||||||
"""MeasurementTec blueprint - recipe selection and measurement execution."""
|
"""MeasurementTec blueprint - recipe selection and measurement execution."""
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
|
|
||||||
|
import requests as http_requests
|
||||||
|
|
||||||
from flask import (
|
from flask import (
|
||||||
Blueprint, abort, flash, jsonify, redirect, render_template,
|
Blueprint, Response, abort, flash, jsonify, redirect, render_template,
|
||||||
request, session, url_for,
|
request, session, url_for,
|
||||||
)
|
)
|
||||||
from flask_babel import gettext as _
|
from flask_babel import gettext as _
|
||||||
@@ -820,3 +822,67 @@ def api_close_production(run_id: int):
|
|||||||
def api_get_file(file_path: str):
|
def api_get_file(file_path: str):
|
||||||
"""Proxy: Serve file from API server (browser can't send X-API-Key)."""
|
"""Proxy: Serve file from API server (browser can't send X-API-Key)."""
|
||||||
return proxy_file(file_path)
|
return proxy_file(file_path)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Route: vision execute (#46) - the browser sends an image, we relay it to
|
||||||
|
# POST /api/vision/execute (multipart) because it cannot send X-API-Key itself.
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
@measure_bp.route("/api/vision/execute", methods=["POST"])
|
||||||
|
@login_required
|
||||||
|
@role_required("MeasurementTec")
|
||||||
|
def api_vision_execute():
|
||||||
|
"""Proxy: run the task's VisionSuite recipe on the uploaded image."""
|
||||||
|
image = request.files.get("image")
|
||||||
|
task_id = request.form.get("task_id")
|
||||||
|
if image is None or not task_id:
|
||||||
|
return jsonify({
|
||||||
|
"error": True,
|
||||||
|
"detail": _("Dati mancanti: task_id e image sono obbligatori"),
|
||||||
|
}), 400
|
||||||
|
|
||||||
|
# Same context fields as the manual flow; empty ones are left out so the
|
||||||
|
# backend sees them as absent rather than as "".
|
||||||
|
form = {"task_id": task_id}
|
||||||
|
for key in ("task_type", "lot_number", "serial_number", "production_run_id"):
|
||||||
|
val = request.form.get(key)
|
||||||
|
if val:
|
||||||
|
form[key] = val
|
||||||
|
form.setdefault("lot_number", session.get("lot_number", ""))
|
||||||
|
form.setdefault("serial_number", session.get("serial_number", ""))
|
||||||
|
|
||||||
|
resp = api_client.post(
|
||||||
|
"/api/vision/execute",
|
||||||
|
data=form,
|
||||||
|
files={"image": (
|
||||||
|
image.filename or "capture.jpg",
|
||||||
|
image.read(),
|
||||||
|
image.mimetype or "application/octet-stream",
|
||||||
|
)},
|
||||||
|
)
|
||||||
|
if resp.get("error"):
|
||||||
|
status_code = resp.get("status_code", 500)
|
||||||
|
body = {"error": True, "detail": resp.get("detail", _("Errore nell'esecuzione"))}
|
||||||
|
if resp.get("session_expired"):
|
||||||
|
body["session_expired"] = True
|
||||||
|
return jsonify(body), status_code if status_code >= 400 else 500
|
||||||
|
return jsonify(resp), 200
|
||||||
|
|
||||||
|
|
||||||
|
@measure_bp.route("/api/vision/results/<int:result_id>/image", methods=["GET"])
|
||||||
|
@login_required
|
||||||
|
@role_required("MeasurementTec")
|
||||||
|
def api_vision_result_image(result_id: int):
|
||||||
|
"""Proxy: the image saved with a vision result (needs X-API-Key)."""
|
||||||
|
base_url = Config.API_SERVER_URL.rstrip("/")
|
||||||
|
resp = http_requests.get(
|
||||||
|
f"{base_url}/api/vision/results/{result_id}/image",
|
||||||
|
headers={"X-API-Key": session.get("api_key", "")},
|
||||||
|
timeout=30,
|
||||||
|
)
|
||||||
|
if resp.status_code != 200:
|
||||||
|
return Response(resp.text, status=resp.status_code)
|
||||||
|
return Response(
|
||||||
|
resp.content,
|
||||||
|
content_type=resp.headers.get("content-type", "application/octet-stream"),
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
"""Live monitor of the open production runs (#57).
|
|
||||||
|
|
||||||
Page at /monitor for admin and Supervisor (capoturno); the page polls a JSON proxy
|
|
||||||
of GET /api/production-runs/open every 10 seconds.
|
|
||||||
"""
|
|
||||||
from functools import wraps
|
|
||||||
|
|
||||||
from flask import Blueprint, abort, jsonify, render_template, session
|
|
||||||
|
|
||||||
from blueprints.auth import login_required
|
|
||||||
from services.api_client import api_client
|
|
||||||
|
|
||||||
monitor_bp = Blueprint("monitor", __name__)
|
|
||||||
|
|
||||||
|
|
||||||
def monitor_required(f):
|
|
||||||
"""Admin or Supervisor only: the same people the backend lets read /open."""
|
|
||||||
@wraps(f)
|
|
||||||
def decorated(*args, **kwargs):
|
|
||||||
user = session.get("user", {})
|
|
||||||
if not (user.get("is_admin") or "Supervisor" in user.get("roles", [])):
|
|
||||||
abort(403)
|
|
||||||
return f(*args, **kwargs)
|
|
||||||
return decorated
|
|
||||||
|
|
||||||
|
|
||||||
@monitor_bp.route("", strict_slashes=False)
|
|
||||||
@login_required
|
|
||||||
@monitor_required
|
|
||||||
def index():
|
|
||||||
return render_template("monitor/index.html")
|
|
||||||
|
|
||||||
|
|
||||||
@monitor_bp.route("/api/runs")
|
|
||||||
@login_required
|
|
||||||
@monitor_required
|
|
||||||
def api_runs():
|
|
||||||
"""JSON proxy polled by the page."""
|
|
||||||
resp = api_client.get("/api/production-runs/open")
|
|
||||||
if isinstance(resp, dict) and resp.get("error"):
|
|
||||||
body = {"error": True, "detail": resp.get("detail", "")}
|
|
||||||
if resp.get("session_expired"):
|
|
||||||
body["session_expired"] = True
|
|
||||||
return jsonify(body), resp.get("status_code", 500)
|
|
||||||
return jsonify(resp)
|
|
||||||
@@ -0,0 +1,125 @@
|
|||||||
|
/**
|
||||||
|
* Vision execute - Alpine component for camera_measure / dxf_compare tasks (#46).
|
||||||
|
*
|
||||||
|
* The operator takes (or picks) an image, `acquire(blob)` sends it to the Flask
|
||||||
|
* proxy, and the result - quote table, outcome, failures, overlay - is shown on
|
||||||
|
* the image. `acquire(blob)` is the single entry point on purpose: the agent
|
||||||
|
* camera (#3) will call it with its own blob and reuse everything below.
|
||||||
|
*
|
||||||
|
* Config comes from window.__visionCfg (set by the template):
|
||||||
|
* executeUrl, imageUrlTpl (with a 0 placeholder for the result id), taskId,
|
||||||
|
* taskType, subtasks, labels.
|
||||||
|
* Run context (lot, serial, production run) is read lazily through `ctx()`,
|
||||||
|
* which the template binds to the parent task component.
|
||||||
|
*/
|
||||||
|
function visionExecute() {
|
||||||
|
// Overlay colour per role (VisionSuite roles: reference, roi, found).
|
||||||
|
const ROLE_COLORS = { reference: '#2563eb', found: '#16a34a', roi: '#f59e0b' };
|
||||||
|
|
||||||
|
return {
|
||||||
|
busy: false,
|
||||||
|
error: '',
|
||||||
|
result: null, // last execute response
|
||||||
|
imageSrc: '', // object URL of the acquired image (shown under the overlay)
|
||||||
|
ctx: () => ({}), // replaced by the template: {lot, serial, runId}
|
||||||
|
|
||||||
|
// Entry point shared with the future agent camera.
|
||||||
|
async acquire(blob) {
|
||||||
|
if (!blob || this.busy) return;
|
||||||
|
const cfg = window.__visionCfg || {};
|
||||||
|
this.busy = true;
|
||||||
|
this.error = '';
|
||||||
|
this.result = null;
|
||||||
|
// Show the picked image at once: the overlay is drawn on top when the result arrives.
|
||||||
|
if (this.imageSrc) URL.revokeObjectURL(this.imageSrc);
|
||||||
|
this.imageSrc = URL.createObjectURL(blob);
|
||||||
|
try {
|
||||||
|
const c = this.ctx() || {};
|
||||||
|
const fd = new FormData();
|
||||||
|
fd.append('task_id', cfg.taskId);
|
||||||
|
fd.append('task_type', cfg.taskType || '');
|
||||||
|
fd.append('image', blob, blob.name || 'capture.jpg');
|
||||||
|
fd.append('lot_number', c.lot || '');
|
||||||
|
fd.append('serial_number', c.serial || '');
|
||||||
|
if (c.runId) fd.append('production_run_id', c.runId);
|
||||||
|
const csrf = document.querySelector('meta[name=csrf-token]')?.content || '';
|
||||||
|
const resp = await fetch(cfg.executeUrl, {
|
||||||
|
method: 'POST', headers: { 'X-CSRFToken': csrf }, body: fd,
|
||||||
|
});
|
||||||
|
const body = await resp.json().catch(() => ({}));
|
||||||
|
if (!resp.ok || body.error) {
|
||||||
|
this.error = body.detail || (cfg.labels && cfg.labels.requestFailed) || 'Error';
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.result = body;
|
||||||
|
// Draw once Alpine has rendered the canvas.
|
||||||
|
this.$nextTick(() => this.drawOverlay());
|
||||||
|
} catch (e) {
|
||||||
|
this.error = (cfg.labels && cfg.labels.requestFailed) || 'Error';
|
||||||
|
} finally {
|
||||||
|
this.busy = false;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Input change: take the file and allow picking the same one again.
|
||||||
|
onPick(ev) {
|
||||||
|
const f = ev.target.files && ev.target.files[0];
|
||||||
|
ev.target.value = '';
|
||||||
|
if (f) this.acquire(f);
|
||||||
|
},
|
||||||
|
|
||||||
|
// Image shown: the saved one from the server when available, else the local one.
|
||||||
|
get shownImage() {
|
||||||
|
const cfg = window.__visionCfg || {};
|
||||||
|
if (this.result && this.result.vision_result_id && cfg.imageUrlTpl) {
|
||||||
|
return cfg.imageUrlTpl.replace('/0/', '/' + this.result.vision_result_id + '/');
|
||||||
|
}
|
||||||
|
return this.imageSrc;
|
||||||
|
},
|
||||||
|
|
||||||
|
// Quote rows: server measurements joined with the subtask names.
|
||||||
|
get rows() {
|
||||||
|
const cfg = window.__visionCfg || {};
|
||||||
|
const subs = cfg.subtasks || [];
|
||||||
|
return ((this.result && this.result.measurements) || []).map((m) => {
|
||||||
|
const st = subs.find((s) => s.id === m.subtask_id) || {};
|
||||||
|
return {
|
||||||
|
label: (st.marker_number ? st.marker_number + '. ' : '') + (st.measurement_type || st.name || '#' + m.subtask_id),
|
||||||
|
value: m.value,
|
||||||
|
unit: st.unit || 'mm',
|
||||||
|
status: m.pass_fail,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
statusClass(s) {
|
||||||
|
// Same palette as the measurement markers of the manual flow.
|
||||||
|
if (s === 'pass') return 'bg-measure-pass/20 text-measure-pass border border-measure-pass/40';
|
||||||
|
if (s === 'fail') return 'bg-red-100 dark:bg-red-900/30 text-measure-fail border border-measure-fail/40';
|
||||||
|
if (s === 'warning') return 'bg-amber-100 dark:bg-amber-900/30 text-measure-warning border border-measure-warning/40';
|
||||||
|
return 'bg-[var(--bg-secondary)] text-[var(--text-muted)] border border-[var(--border-color)]';
|
||||||
|
},
|
||||||
|
|
||||||
|
// Canvas is sized to the natural image, so overlay coordinates (image px)
|
||||||
|
// are used as they are and CSS scales the whole thing to the displayed size.
|
||||||
|
drawOverlay() {
|
||||||
|
const img = this.$refs.img, cv = this.$refs.overlay;
|
||||||
|
if (!img || !cv || !img.naturalWidth) return;
|
||||||
|
cv.width = img.naturalWidth;
|
||||||
|
cv.height = img.naturalHeight;
|
||||||
|
const g = cv.getContext('2d');
|
||||||
|
g.clearRect(0, 0, cv.width, cv.height);
|
||||||
|
g.lineWidth = Math.max(2, cv.width / 400);
|
||||||
|
g.lineJoin = 'round';
|
||||||
|
for (const item of ((this.result && this.result.overlay) || [])) {
|
||||||
|
const rows = item.rows || [], cols = item.cols || [];
|
||||||
|
if (!rows.length || rows.length !== cols.length) continue;
|
||||||
|
g.strokeStyle = ROLE_COLORS[item.role] || '#ef4444';
|
||||||
|
g.beginPath();
|
||||||
|
g.moveTo(cols[0], rows[0]);
|
||||||
|
for (let i = 1; i < rows.length; i++) g.lineTo(cols[i], rows[i]);
|
||||||
|
g.stroke();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -191,7 +191,8 @@
|
|||||||
{# ================================================================
|
{# ================================================================
|
||||||
MAIN — 3-column layout
|
MAIN — 3-column layout
|
||||||
================================================================ #}
|
================================================================ #}
|
||||||
<div class="flex-1 flex overflow-hidden">
|
{# #46: camera_measure / dxf_compare get the acquisition panel below instead of the manual layout #}
|
||||||
|
<div class="flex-1 flex overflow-hidden" x-show="!isVisionTask">
|
||||||
|
|
||||||
{# ──────────────────────────────────────────────
|
{# ──────────────────────────────────────────────
|
||||||
LEFT SIDEBAR — Marker list (vertical, hidden when no subtasks)
|
LEFT SIDEBAR — Marker list (vertical, hidden when no subtasks)
|
||||||
@@ -451,6 +452,63 @@
|
|||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{# ================================================================
|
||||||
|
VISION PANEL (#46) - acquire an image, run the recipe server-side, show the
|
||||||
|
quotes and the overlay. acquire(blob) is the entry point the agent camera
|
||||||
|
(#3) will reuse.
|
||||||
|
================================================================ #}
|
||||||
|
<div x-show="isVisionTask" x-cloak
|
||||||
|
x-data="visionExecute()"
|
||||||
|
x-init="ctx = () => ({ lot: lotNumber, serial: serialNumber, runId: productionRun ? productionRun.id : null })"
|
||||||
|
class="flex-1 overflow-auto p-3 flex flex-col lg:flex-row gap-3">
|
||||||
|
|
||||||
|
<div class="flex-1 min-w-0 flex flex-col gap-2">
|
||||||
|
<div class="flex items-center gap-2 flex-wrap">
|
||||||
|
{# On a tablet this opens the camera; on a PC it picks a file. «Riprova» reuses the same input. #}
|
||||||
|
<label class="btn btn-primary text-sm gap-1.5 cursor-pointer" :class="busy ? 'opacity-50 pointer-events-none' : ''">
|
||||||
|
<span x-text="result ? '{{ _('Riprova') }}' : '{{ _('Acquisisci immagine') }}'"></span>
|
||||||
|
<input type="file" accept="image/*" capture="environment" class="hidden"
|
||||||
|
@change="onPick($event)" :disabled="busy">
|
||||||
|
</label>
|
||||||
|
<span x-show="busy" class="text-sm text-[var(--text-muted)]">{{ _('Elaborazione in corso...') }}</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div x-show="error" class="p-2 rounded-lg bg-red-50 dark:bg-red-900/20 border border-red-200 dark:border-red-800 text-sm text-measure-fail" x-text="error"></div>
|
||||||
|
|
||||||
|
<div x-show="shownImage" class="relative border border-[var(--border-color)] rounded-lg overflow-hidden bg-black/5">
|
||||||
|
<img x-ref="img" :src="shownImage" @load="drawOverlay()" alt="" class="block w-full h-auto">
|
||||||
|
<canvas x-ref="overlay" class="absolute inset-0 w-full h-full pointer-events-none"></canvas>
|
||||||
|
</div>
|
||||||
|
<div x-show="!shownImage" class="text-sm text-[var(--text-muted)]">
|
||||||
|
{{ _('Nessuna immagine acquisita: premi «Acquisisci immagine».') }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="lg:w-80 shrink-0 flex flex-col gap-2" x-show="result">
|
||||||
|
<div class="text-sm font-bold text-[var(--text-primary)]">
|
||||||
|
{{ _('Esito') }}: <span x-text="(result && result.outcome) || '-'"></span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<table class="w-full text-sm" x-show="rows.length">
|
||||||
|
<template x-for="r in rows" :key="r.label">
|
||||||
|
<tr>
|
||||||
|
<td class="py-1 pr-2 text-[var(--text-secondary)]" x-text="r.label"></td>
|
||||||
|
<td class="py-1 pr-2 font-mono text-right" x-text="Number(r.value).toFixed(3) + ' ' + r.unit"></td>
|
||||||
|
<td class="py-1"><span class="inline-block px-2 py-0.5 rounded text-xs font-bold" :class="statusClass(r.status)" x-text="r.status"></span></td>
|
||||||
|
</tr>
|
||||||
|
</template>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<div x-show="result && result.failures && result.failures.length"
|
||||||
|
class="p-2 rounded-lg bg-amber-50 dark:bg-amber-900/20 border border-amber-300 dark:border-amber-700 text-xs">
|
||||||
|
<div class="font-bold mb-1">{{ _('Strumenti falliti') }}</div>
|
||||||
|
<template x-for="f in ((result && result.failures) || [])" :key="f.tool_id">
|
||||||
|
<div><span class="font-mono" x-text="f.tool_name || f.tool_id"></span>: <span x-text="f.error"></span></div>
|
||||||
|
</template>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{# ================================================================
|
{# ================================================================
|
||||||
MEASUREMENT TIMER BANNER
|
MEASUREMENT TIMER BANNER
|
||||||
================================================================ #}
|
================================================================ #}
|
||||||
@@ -912,6 +970,18 @@
|
|||||||
<script src="{{ url_for('static', filename='js/numpad.js') }}"></script>
|
<script src="{{ url_for('static', filename='js/numpad.js') }}"></script>
|
||||||
<script src="{{ url_for('static', filename='js/annotation-viewer.js') }}?v=6"></script>
|
<script src="{{ url_for('static', filename='js/annotation-viewer.js') }}?v=6"></script>
|
||||||
<script src="{{ url_for('static', filename='js/caliper.js') }}"></script>
|
<script src="{{ url_for('static', filename='js/caliper.js') }}"></script>
|
||||||
|
{# #46: acquisition + overlay for camera_measure / dxf_compare #}
|
||||||
|
<script>
|
||||||
|
window.__visionCfg = {
|
||||||
|
executeUrl: '{{ url_for("measure.api_vision_execute") }}',
|
||||||
|
imageUrlTpl: '{{ url_for("measure.api_vision_result_image", result_id=0) }}',
|
||||||
|
taskId: {{ task.id|tojson }},
|
||||||
|
taskType: {{ (task.task_type or '')|tojson }},
|
||||||
|
subtasks: window.__taskSubtasks,
|
||||||
|
labels: { requestFailed: {{ _('Richiesta non riuscita')|tojson }} },
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
<script src="{{ url_for('static', filename='js/vision_execute.js') }}"></script>
|
||||||
{# The clock's rules - how a delay reads, when the buzzer goes, where the
|
{# The clock's rules - how a delay reads, when the buzzer goes, where the
|
||||||
measurement lives - are shared with the other screens rather than restated. #}
|
measurement lives - are shared with the other screens rather than restated. #}
|
||||||
<script src="{{ url_for('static', filename='js/production-clock.js') }}"></script>
|
<script src="{{ url_for('static', filename='js/production-clock.js') }}"></script>
|
||||||
@@ -1776,6 +1846,12 @@ function taskExecute() {
|
|||||||
return declared === 'measure' || declared === 'camera_measure';
|
return declared === 'measure' || declared === 'camera_measure';
|
||||||
},
|
},
|
||||||
|
|
||||||
|
/* #46: image-based tasks run a VisionSuite recipe on the server. */
|
||||||
|
get isVisionTask() {
|
||||||
|
const t = this.task.task_type;
|
||||||
|
return t === 'camera_measure' || t === 'dxf_compare';
|
||||||
|
},
|
||||||
|
|
||||||
/* A measurement task with no quotes yet: the recipe is unfinished. Said out
|
/* A measurement task with no quotes yet: the recipe is unfinished. Said out
|
||||||
loud rather than silently rendering an empty screen. */
|
loud rather than silently rendering an empty screen. */
|
||||||
get isMeasureTaskWithoutQuotes() {
|
get isMeasureTaskWithoutQuotes() {
|
||||||
|
|||||||
@@ -1,105 +0,0 @@
|
|||||||
{% extends "base.html" %}
|
|
||||||
|
|
||||||
{% block title %}{{ _("Monitor stazioni") }} — TieMeasureFlow{% endblock %}
|
|
||||||
|
|
||||||
{% block content %}
|
|
||||||
<div class="tmf-page" x-data="stationMonitor()" x-init="init()">
|
|
||||||
<div class="mb-6 flex items-center justify-between">
|
|
||||||
<div>
|
|
||||||
<h1 class="text-2xl font-bold text-[var(--text-primary)]">{{ _("Monitor stazioni") }}</h1>
|
|
||||||
<p class="text-sm text-steel mt-1">{{ _("Produzioni aperte, aggiornate ogni 10 secondi") }}</p>
|
|
||||||
</div>
|
|
||||||
<div class="text-xs text-steel" x-text="updatedLabel"></div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div x-show="error" class="mb-4 p-3 rounded-lg bg-red-50 text-red-700 text-sm" x-text="error"></div>
|
|
||||||
|
|
||||||
<div x-show="loaded && runs.length === 0 && !error"
|
|
||||||
class="p-8 text-center text-steel bg-[var(--bg-secondary)] rounded-xl border border-[var(--border-color)]">
|
|
||||||
{{ _("Nessuna stazione in produzione") }}
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-4">
|
|
||||||
<template x-for="r in runs" :key="r.id">
|
|
||||||
<div class="bg-[var(--bg-secondary)] rounded-xl border border-[var(--border-color)] border-l-8 shadow-sm p-4"
|
|
||||||
:class="borderClass(r)">
|
|
||||||
<div class="flex items-start justify-between gap-2">
|
|
||||||
<div>
|
|
||||||
<div class="text-lg font-bold text-[var(--text-primary)]" x-text="r.station_code + ' — ' + r.station_name"></div>
|
|
||||||
<div class="text-sm text-steel" x-text="r.recipe_code + ' ' + r.recipe_name + ' (v' + r.version_number + ')'"></div>
|
|
||||||
</div>
|
|
||||||
<span class="text-xs font-semibold px-2 py-1 rounded"
|
|
||||||
:class="r.overdue ? 'bg-red-600 text-white animate-pulse' : 'bg-gray-200 text-gray-700'"
|
|
||||||
x-text="statusLabel(r)"></span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<dl class="mt-3 grid grid-cols-2 gap-x-3 gap-y-1 text-sm">
|
|
||||||
<dt class="text-steel">{{ _("Operatore") }}</dt><dd x-text="r.operator"></dd>
|
|
||||||
<dt class="text-steel">{{ _("Avviata alle") }}</dt><dd x-text="startedAt(r)"></dd>
|
|
||||||
<dt class="text-steel">{{ _("Durata") }}</dt><dd x-text="fmt(r.duration_seconds)"></dd>
|
|
||||||
<dt class="text-steel">{{ _("Cicli") }}</dt><dd x-text="r.cycle_count"></dd>
|
|
||||||
<dt class="text-steel">{{ _("Prossima misura") }}</dt>
|
|
||||||
<dd :class="r.overdue ? 'text-red-600 font-bold' : ''" x-text="timerLabel(r)"></dd>
|
|
||||||
<dt class="text-steel">{{ _("Ultimo evento") }}</dt>
|
|
||||||
<dd x-text="r.last_event ? r.last_event.event_type : '-'"></dd>
|
|
||||||
</dl>
|
|
||||||
|
|
||||||
<div class="mt-3 flex gap-2 text-xs font-semibold">
|
|
||||||
<span class="px-2 py-1 rounded bg-green-100 text-green-800" x-text="'OK ' + r.pass_count"></span>
|
|
||||||
<span class="px-2 py-1 rounded bg-yellow-100 text-yellow-800" x-text="'{{ _("Avviso") }} ' + r.warning_count"></span>
|
|
||||||
<span class="px-2 py-1 rounded bg-red-100 text-red-800" x-text="'NOK ' + r.fail_count"></span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<ul class="mt-3 space-y-1 text-sm">
|
|
||||||
<template x-for="s in r.subtasks" :key="s.subtask_id">
|
|
||||||
<li class="flex justify-between gap-2">
|
|
||||||
<span class="truncate" x-text="s.description"></span>
|
|
||||||
<span class="font-mono whitespace-nowrap"
|
|
||||||
:class="s.in_tolerance ? 'text-green-700' : 'text-red-700 font-bold'"
|
|
||||||
x-text="s.mean.toFixed(3) + ' ' + s.unit"></span>
|
|
||||||
</li>
|
|
||||||
</template>
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
function stationMonitor() {
|
|
||||||
return {
|
|
||||||
runs: [], loaded: false, error: '', updatedLabel: '',
|
|
||||||
init() { this.load(); setInterval(() => this.load(), 10000); },
|
|
||||||
async load() {
|
|
||||||
try {
|
|
||||||
const resp = await fetch('/monitor/api/runs');
|
|
||||||
const data = await resp.json();
|
|
||||||
if (!resp.ok || data.error) { this.error = data.detail || {{ _("Errore di caricamento")|tojson }}; return; }
|
|
||||||
this.runs = data; this.error = ''; this.loaded = true;
|
|
||||||
this.updatedLabel = new Date().toLocaleTimeString();
|
|
||||||
} catch (e) { this.error = {{ _("Errore di rete")|tojson }}; }
|
|
||||||
},
|
|
||||||
// Green in tolerance, yellow with warnings, red with fails or a mean out of tolerance.
|
|
||||||
borderClass(r) {
|
|
||||||
if (r.fail_count > 0 || r.subtasks.some(s => !s.in_tolerance)) return 'border-l-red-600';
|
|
||||||
if (r.warning_count > 0) return 'border-l-yellow-500';
|
|
||||||
return 'border-l-green-600';
|
|
||||||
},
|
|
||||||
statusLabel(r) {
|
|
||||||
if (r.overdue) return {{ _("IN RITARDO")|tojson }};
|
|
||||||
return r.status === 'paused' ? {{ _("In pausa")|tojson }} : {{ _("In corso")|tojson }};
|
|
||||||
},
|
|
||||||
startedAt(r) { return new Date(r.started_at + 'Z').toLocaleTimeString(); },
|
|
||||||
timerLabel(r) {
|
|
||||||
if (r.seconds_to_next_measurement === null) return '-';
|
|
||||||
const s = r.seconds_to_next_measurement;
|
|
||||||
return (s < 0 ? '-' : '') + this.fmt(Math.abs(s));
|
|
||||||
},
|
|
||||||
fmt(sec) {
|
|
||||||
const h = Math.floor(sec / 3600), m = Math.floor((sec % 3600) / 60), s = sec % 60;
|
|
||||||
return (h ? h + 'h ' : '') + m + 'm ' + String(s).padStart(2, '0') + 's';
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
{% endblock %}
|
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
"""gitea:Adriano/TieMeasureFlow#46 - operator side of the vision tasks.
|
||||||
|
|
||||||
|
The backend of contract B is not merged yet: api_client is mocked.
|
||||||
|
"""
|
||||||
|
import io
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
from tests.conftest import api_get_router
|
||||||
|
|
||||||
|
TASK = {
|
||||||
|
"id": 12, "recipe_id": 3, "version_id": 5, "task_type": "camera_measure",
|
||||||
|
"title": "Foto", "directive": None, "description": None, "file_path": None,
|
||||||
|
"file_type": None, "annotations_json": None, "subtasks": [],
|
||||||
|
}
|
||||||
|
RECIPE = {
|
||||||
|
"id": 3, "code": "REC-1", "name": "Ricetta",
|
||||||
|
"measurement_interval_minutes": 30, "allow_manual_input": True,
|
||||||
|
"current_version": {"id": 5, "version_number": 1},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_execute_page_has_acquire_input(logged_in_client, mock_api_client, monkeypatch):
|
||||||
|
"""camera_measure screen offers the file/camera input and the acquire entry point."""
|
||||||
|
monkeypatch.setattr("blueprints.measure.Config.STATION_CODE", "ST-TEST")
|
||||||
|
mock_api_client.get_station_recipes.return_value = {"items": [{"id": 3}]}
|
||||||
|
mock_api_client.get.side_effect = api_get_router({
|
||||||
|
"/api/tasks/12": TASK,
|
||||||
|
"/api/recipes/3/tasks": [{"id": 12, "order_index": 0}],
|
||||||
|
"/api/recipes/3": RECIPE,
|
||||||
|
})
|
||||||
|
html = logged_in_client.get("/measure/execute/12").get_data(as_text=True)
|
||||||
|
assert 'capture="environment"' in html
|
||||||
|
assert 'accept="image/*"' in html
|
||||||
|
assert "visionExecute()" in html
|
||||||
|
assert "/measure/api/vision/execute" in html
|
||||||
|
|
||||||
|
|
||||||
|
def test_vision_execute_proxy_forwards_multipart(logged_in_client, mock_api_client):
|
||||||
|
mock_api_client.post.return_value = {
|
||||||
|
"vision_result_id": 7, "outcome": "ok", "overlay": [], "failures": [],
|
||||||
|
"measurements": [{"subtask_id": 1, "value": 9.9, "pass_fail": "pass"}],
|
||||||
|
}
|
||||||
|
resp = logged_in_client.post(
|
||||||
|
"/measure/api/vision/execute",
|
||||||
|
data={
|
||||||
|
"task_id": "12", "lot_number": "L1", "serial_number": "S1",
|
||||||
|
"production_run_id": "4",
|
||||||
|
"image": (io.BytesIO(b"\xff\xd8\xff fake"), "shot.jpg", "image/jpeg"),
|
||||||
|
},
|
||||||
|
content_type="multipart/form-data",
|
||||||
|
)
|
||||||
|
assert resp.status_code == 200
|
||||||
|
assert resp.get_json()["vision_result_id"] == 7
|
||||||
|
args, kwargs = mock_api_client.post.call_args
|
||||||
|
assert args[0] == "/api/vision/execute"
|
||||||
|
assert kwargs["data"]["task_id"] == "12"
|
||||||
|
assert kwargs["data"]["lot_number"] == "L1"
|
||||||
|
assert kwargs["data"]["production_run_id"] == "4"
|
||||||
|
name, content, mime = kwargs["files"]["image"]
|
||||||
|
assert name == "shot.jpg" and mime == "image/jpeg" and content.startswith(b"\xff\xd8")
|
||||||
|
|
||||||
|
|
||||||
|
def test_vision_execute_proxy_requires_image(logged_in_client, mock_api_client):
|
||||||
|
resp = logged_in_client.post("/measure/api/vision/execute", data={"task_id": "12"})
|
||||||
|
assert resp.status_code == 400
|
||||||
|
mock_api_client.post.assert_not_called()
|
||||||
|
|
||||||
|
|
||||||
|
def test_vision_execute_proxy_relays_backend_error(logged_in_client, mock_api_client):
|
||||||
|
mock_api_client.post.return_value = {"error": True, "status_code": 422, "detail": "bad"}
|
||||||
|
resp = logged_in_client.post(
|
||||||
|
"/measure/api/vision/execute",
|
||||||
|
data={"task_id": "12", "image": (io.BytesIO(b"x"), "a.jpg", "image/jpeg")},
|
||||||
|
content_type="multipart/form-data",
|
||||||
|
)
|
||||||
|
assert resp.status_code == 422
|
||||||
|
assert resp.get_json()["detail"] == "bad"
|
||||||
|
|
||||||
|
|
||||||
|
def test_vision_result_image_proxy(logged_in_client):
|
||||||
|
fake = MagicMock(status_code=200, content=b"IMG", headers={"content-type": "image/jpeg"})
|
||||||
|
with patch("blueprints.measure.http_requests.get", return_value=fake) as g:
|
||||||
|
resp = logged_in_client.get("/measure/api/vision/results/7/image")
|
||||||
|
assert resp.status_code == 200 and resp.data == b"IMG"
|
||||||
|
assert g.call_args[0][0].endswith("/api/vision/results/7/image")
|
||||||
|
assert g.call_args[1]["headers"]["X-API-Key"] == "test-api-key-12345"
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
"""Live station monitor page and JSON proxy (#57)."""
|
|
||||||
from unittest.mock import patch
|
|
||||||
|
|
||||||
import pytest
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture
|
|
||||||
def mock_api():
|
|
||||||
with patch("blueprints.monitor.api_client") as m:
|
|
||||||
yield m
|
|
||||||
|
|
||||||
|
|
||||||
def _login(client, roles, is_admin=False):
|
|
||||||
with client.session_transaction() as sess:
|
|
||||||
sess["api_key"] = "k"
|
|
||||||
sess["user"] = {"id": 2, "username": "u", "display_name": "U", "roles": roles,
|
|
||||||
"is_admin": is_admin, "language_pref": "en", "theme_pref": "light",
|
|
||||||
"active": True}
|
|
||||||
sess["language"] = "en"
|
|
||||||
|
|
||||||
|
|
||||||
def test_page_ok_for_supervisor(client, mock_api):
|
|
||||||
_login(client, ["Supervisor"])
|
|
||||||
resp = client.get("/monitor")
|
|
||||||
assert resp.status_code == 200 and b"stationMonitor" in resp.data
|
|
||||||
|
|
||||||
|
|
||||||
def test_page_forbidden_for_operator(client, mock_api):
|
|
||||||
_login(client, ["MeasurementTec"])
|
|
||||||
assert client.get("/monitor").status_code == 403
|
|
||||||
assert client.get("/monitor/api/runs").status_code == 403
|
|
||||||
|
|
||||||
|
|
||||||
def test_proxy_returns_backend_json(client, mock_api):
|
|
||||||
_login(client, ["Supervisor"])
|
|
||||||
mock_api.get.return_value = [{"id": 1, "station_code": "ST-1"}]
|
|
||||||
resp = client.get("/monitor/api/runs")
|
|
||||||
assert resp.status_code == 200 and resp.get_json()[0]["station_code"] == "ST-1"
|
|
||||||
mock_api.get.assert_called_once_with("/api/production-runs/open")
|
|
||||||
|
|
||||||
|
|
||||||
def test_proxy_passes_backend_error(client, mock_api):
|
|
||||||
_login(client, [], is_admin=True)
|
|
||||||
mock_api.get.return_value = {"error": True, "status_code": 403, "detail": "no"}
|
|
||||||
resp = client.get("/monitor/api/runs")
|
|
||||||
assert resp.status_code == 403 and resp.get_json()["error"] is True
|
|
||||||
Reference in New Issue
Block a user