feat(production): dai una vita propria alla produzione, lato server

Punto 1 del documento modifiche del 28/07, il prerequisito su cui poggiano i
punti 3, 4 e 6.

Lo stato di una produzione viveva dentro una pagina del browser: timer, conteggio
cicli e flag "produzione avviata" erano variabili Alpine di task_execute.html, e
la navigazione fra task e' un ricaricamento completo, quindi cambiando task si
perdeva tutto. Da qui il loop di misura che non reggeva, il fermo linea che non
aveva nulla da fermare e l'assenza di storico.

Nuove tabelle production_runs e production_events (migrazione 005), endpoint REST
senza stato in memoria di processo - con un'app di stazione installata su ogni PC
il database e' l'unico posto condiviso - e il frontend che legge lo stato
all'apertura invece di tenerlo in memoria.

Tre scelte di modello:

- la scadenza e' un timestamp assoluto (next_measurement_at), non un contatore:
  il countdown si ricalcola da li' a ogni caricamento, e lasciarne andare la
  differenza sotto zero dara' gratis il contatore del ritardo del punto 3. Al
  client vanno i secondi gia' calcolati, non il timestamp: un datetime naive
  verrebbe letto nel fuso del browser e il conto sarebbe sfasato dell'offset UTC;
- l'intervallo di misura e' copiato sulla produzione, non referenziato: modificare
  la ricetta a produzione avviata non deve spostare una scadenza in corso;
- active_station_id rispecchia la stazione finche' la produzione e' aperta e va a
  NULL alla chiusura. Con un vincolo unico sopra, "una stazione = una produzione
  aperta" e' una garanzia del database e non un controllo soggetto a race; i NULL
  non collidono, quindi le produzioni chiuse si accumulano senza disturbo.

Il fermo linea congela il conto alla rovescia e alla ripresa la scadenza viene
traslata della durata del fermo, non ricalcolata: un fermo non regala ne' toglie
tempo all'operatore. L'autorizzazione del capoturno passa da authenticate_user e
non da un login, che rigenererebbe la sua API key buttando giu' la sessione che ha
aperta altrove.

La migrazione e' stata eseguita davvero, non solo scritta, su uno SQLite usa e
getta: upgrade e downgrade girano e le colonne coincidono con i modelli. La prova
ha trovato un difetto - create_unique_constraint dopo create_table e' un ALTER,
che SQLite rifiuta - ora il vincolo e' dichiarato dentro create_table.

Fuori da questo commit, per stare nei confini del punto 1: l'API espone gia'
pause, resume e close, ma i pulsanti fermo linea e fine produzione restano da
collegare (punto 6), e il rientro forzato sulla misura allo scadere e' il punto 3.

Corretti due difetti trovati strada facendo: env.py non importava ne' Station ne'
ProductionRun, quindi l'autogenerate di Alembic era gia' cieco sulle stazioni; e
task_execute.html, lo schermo con piu' JavaScript dell'applicazione, non era
coperto dal test di sintassi. Aggiungerlo ha richiesto di correggere l'helper, che
validava le espressioni Alpine solo come espressione singola e bocciava
@click="a = false; b = true", forma che Alpine accetta: ora prova entrambe le
letture e fallisce solo se cadono tutte e due.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Adriano Dal Pastro
2026-07-28 16:20:43 +00:00
parent e7889f80a3
commit 55e5e0153f
16 changed files with 1851 additions and 147 deletions
+137
View File
@@ -0,0 +1,137 @@
"""Production runs router - open, read, and drive the life of a production.
Deliberately stateless: nothing lives in process memory. With one station app
installed per PC the database is the only shared place, and any station app must be
able to ask "what is going on here?" and get the same answer.
"""
from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.ext.asyncio import AsyncSession
from src.backend.api.middleware.api_key import get_current_user
from src.backend.database import get_db
from src.backend.models.api.production import (
CycleCompletePayload,
ProductionEventResponse,
ProductionRunCreate,
ProductionRunResponse,
ProductionRunWithEventsResponse,
SupervisorAction,
)
from src.backend.models.orm.production import ProductionRun
from src.backend.models.orm.user import User
from src.backend.services import production_service
router = APIRouter(prefix="/api/production-runs", tags=["production"])
def _as_response(run: ProductionRun) -> ProductionRunResponse:
return ProductionRunResponse(
**ProductionRunResponse.model_validate(run).model_dump(
exclude={"seconds_to_next_measurement", "overdue", "server_time"}
),
**production_service.describe(run),
)
@router.post("", response_model=ProductionRunResponse, status_code=status.HTTP_201_CREATED)
async def open_production_run(
data: ProductionRunCreate,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Start a production at a station."""
run = await production_service.open_run(db, data, user)
return _as_response(run)
@router.get("/current", response_model=ProductionRunResponse | None)
async def get_current_production_run(
station_code: str = Query(..., min_length=1),
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""The run open at this station, or null.
This is what every page asks on load instead of keeping the timer in memory.
"""
station = await production_service.get_station_by_code(db, station_code)
run = await production_service.get_open_run_for_station(db, station.id)
return _as_response(run) if run is not None else None
@router.get("/{run_id}", response_model=ProductionRunWithEventsResponse)
async def get_production_run(
run_id: int,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""A run with its full trace - the history a production did not have before."""
run = await production_service.get_run(db, run_id)
events = await production_service.list_run_events(db, run_id)
return ProductionRunWithEventsResponse(
**_as_response(run).model_dump(),
events=[ProductionEventResponse.model_validate(e) for e in events],
)
@router.post("/{run_id}/cycle", response_model=ProductionRunResponse)
async def complete_measurement_cycle(
run_id: int,
payload: CycleCompletePayload | None = None,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Record a finished measurement cycle and restart the interval."""
run = await production_service.get_run(db, run_id)
run = await production_service.complete_cycle(
db, run, user, note=payload.note if payload else None,
)
return _as_response(run)
@router.post("/{run_id}/pause", response_model=ProductionRunResponse)
async def pause_production_run(
run_id: int,
action: SupervisorAction,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Fermo linea - requires a supervisor."""
run = await production_service.get_run(db, run_id)
supervisor = await production_service.authorise_supervisor(
db, action.supervisor_username, action.supervisor_password,
)
run = await production_service.pause_run(db, run, user, supervisor, note=action.note)
return _as_response(run)
@router.post("/{run_id}/resume", response_model=ProductionRunResponse)
async def resume_production_run(
run_id: int,
action: SupervisorAction,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Restart a stopped line - requires a supervisor."""
run = await production_service.get_run(db, run_id)
supervisor = await production_service.authorise_supervisor(
db, action.supervisor_username, action.supervisor_password,
)
run = await production_service.resume_run(db, run, user, supervisor, note=action.note)
return _as_response(run)
@router.post("/{run_id}/close", response_model=ProductionRunResponse)
async def close_production_run(
run_id: int,
action: SupervisorAction,
user: User = Depends(get_current_user),
db: AsyncSession = Depends(get_db),
):
"""Fine produzione - requires a supervisor. Stops the timer for good."""
run = await production_service.get_run(db, run_id)
supervisor = await production_service.authorise_supervisor(
db, action.supervisor_username, action.supervisor_password,
)
run = await production_service.close_run(db, run, user, supervisor, note=action.note)
return _as_response(run)
+2
View File
@@ -21,6 +21,7 @@ from src.backend.api.routers.reports import router as reports_router
from src.backend.api.routers.statistics import router as statistics_router
from src.backend.api.routers.setup import router as setup_router
from src.backend.api.routers.stations import router as stations_router
from src.backend.api.routers.production import router as production_router
@asynccontextmanager
@@ -73,6 +74,7 @@ app.include_router(statistics_router)
app.include_router(reports_router)
app.include_router(setup_router)
app.include_router(stations_router)
app.include_router(production_router)
@app.get("/api/health")
+2
View File
@@ -36,6 +36,8 @@ from src.backend.models.orm.task import RecipeTask, RecipeSubtask # noqa: F401
from src.backend.models.orm.measurement import Measurement # noqa: F401
from src.backend.models.orm.access_log import AccessLog # noqa: F401
from src.backend.models.orm.setting import SystemSetting, RecipeVersionAudit # noqa: F401
from src.backend.models.orm.station import Station, StationRecipeAssignment # noqa: F401
from src.backend.models.orm.production import ProductionRun, ProductionEvent # noqa: F401
target_metadata = Base.metadata
@@ -0,0 +1,100 @@
"""add production_runs and production_events
Gives a production a life of its own: before this its state lived in the Alpine
component of task_execute.html, so changing task - a full page load - lost the timer,
the cycle count and the "production started" flag.
Revision ID: 005_production_runs
Revises: 004_input_duration
Create Date: 2026-07-28
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
revision: str = '005_production_runs'
down_revision: Union[str, None] = '004_input_duration'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_table(
'production_runs',
sa.Column('id', sa.Integer, primary_key=True, autoincrement=True),
sa.Column('station_id', sa.Integer, sa.ForeignKey('stations.id'), nullable=False),
sa.Column('recipe_id', sa.Integer, sa.ForeignKey('recipes.id'), nullable=False),
sa.Column('version_id', sa.Integer, sa.ForeignKey('recipe_versions.id'), nullable=False),
sa.Column('operator_id', sa.Integer, sa.ForeignKey('users.id'), nullable=False),
sa.Column('lot_number', sa.String(100), nullable=True),
sa.Column('serial_number', sa.String(100), nullable=True),
sa.Column(
'status',
sa.Enum('running', 'paused', 'closed', name='production_run_status_enum'),
nullable=False,
server_default='running',
),
sa.Column('measurement_interval_minutes', sa.SmallInteger, nullable=True),
sa.Column('next_measurement_at', sa.DateTime, nullable=True),
sa.Column('cycle_count', sa.Integer, nullable=False, server_default='0'),
sa.Column('started_at', sa.DateTime, nullable=False, server_default=sa.func.now()),
sa.Column('paused_at', sa.DateTime, nullable=True),
sa.Column('closed_at', sa.DateTime, nullable=True),
sa.Column('closed_by', sa.Integer, sa.ForeignKey('users.id'), nullable=True),
# Mirrors station_id while open, NULL once closed. The unique constraint makes
# "one open run per station" a database guarantee rather than a race; repeated
# NULLs do not collide, so closed runs are free to pile up.
sa.Column('active_station_id', sa.Integer, nullable=True),
# Declared inline rather than added afterwards: adding a constraint is an ALTER,
# which SQLite cannot do, and the test databases are SQLite.
sa.UniqueConstraint('active_station_id', name='uq_production_runs_active_station'),
mysql_engine='InnoDB',
mysql_charset='utf8mb4',
)
op.create_index('ix_production_runs_station_id', 'production_runs', ['station_id'])
op.create_index('ix_production_runs_recipe_id', 'production_runs', ['recipe_id'])
op.create_index('ix_production_runs_version_id', 'production_runs', ['version_id'])
op.create_index('ix_production_runs_operator_id', 'production_runs', ['operator_id'])
op.create_index('ix_production_runs_lot_number', 'production_runs', ['lot_number'])
op.create_index('ix_production_runs_serial_number', 'production_runs', ['serial_number'])
op.create_index('ix_production_runs_status', 'production_runs', ['status'])
op.create_index(
'ix_production_runs_next_measurement_at', 'production_runs', ['next_measurement_at'],
)
op.create_index(
'ix_production_runs_station_status', 'production_runs', ['station_id', 'status'],
)
op.create_table(
'production_events',
sa.Column('id', sa.Integer, primary_key=True, autoincrement=True),
sa.Column(
'run_id', sa.Integer,
sa.ForeignKey('production_runs.id', ondelete='CASCADE'), nullable=False,
),
sa.Column(
'event_type',
sa.Enum(
'start', 'cycle_completed', 'line_stop', 'resume', 'close',
name='production_event_type_enum',
),
nullable=False,
),
sa.Column('user_id', sa.Integer, sa.ForeignKey('users.id'), nullable=False),
sa.Column('supervisor_id', sa.Integer, sa.ForeignKey('users.id'), nullable=True),
sa.Column('note', sa.Text, nullable=True),
sa.Column('created_at', sa.DateTime, nullable=False, server_default=sa.func.now()),
mysql_engine='InnoDB',
mysql_charset='utf8mb4',
)
op.create_index('ix_production_events_run_id', 'production_events', ['run_id'])
op.create_index('ix_production_events_event_type', 'production_events', ['event_type'])
op.create_index('ix_production_events_user_id', 'production_events', ['user_id'])
op.create_index('ix_production_events_created_at', 'production_events', ['created_at'])
def downgrade() -> None:
op.drop_table('production_events')
op.drop_table('production_runs')
+71
View File
@@ -0,0 +1,71 @@
"""Pydantic schemas for production runs and their events."""
from datetime import datetime
from typing import Literal, Optional
from pydantic import BaseModel, ConfigDict, Field
class ProductionRunCreate(BaseModel):
station_code: str = Field(..., min_length=1, max_length=100)
recipe_id: int = Field(..., gt=0)
version_id: Optional[int] = Field(default=None, gt=0)
lot_number: Optional[str] = Field(default=None, max_length=100)
serial_number: Optional[str] = Field(default=None, max_length=100)
class ProductionEventResponse(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
event_type: str
user_id: int
supervisor_id: Optional[int]
note: Optional[str]
created_at: datetime
class ProductionRunResponse(BaseModel):
model_config = ConfigDict(from_attributes=True)
id: int
station_id: int
recipe_id: int
version_id: int
operator_id: int
lot_number: Optional[str]
serial_number: Optional[str]
status: str
measurement_interval_minutes: Optional[int]
next_measurement_at: Optional[datetime]
cycle_count: int
started_at: datetime
paused_at: Optional[datetime]
closed_at: Optional[datetime]
closed_by: Optional[int]
# Derived server-side so every client agrees on the countdown regardless of
# clock skew. Negative once the interval has elapsed: how long the run has been
# overdue is a fact the operator must see, not deduce.
seconds_to_next_measurement: Optional[int] = None
overdue: bool = False
server_time: Optional[datetime] = None
class ProductionRunWithEventsResponse(ProductionRunResponse):
events: list[ProductionEventResponse] = Field(default_factory=list)
class SupervisorAction(BaseModel):
"""Credentials of the supervisor authorising a line stop or a close."""
supervisor_username: str = Field(..., min_length=1)
supervisor_password: str = Field(..., min_length=1)
note: Optional[str] = None
class CycleCompletePayload(BaseModel):
note: Optional[str] = None
ProductionEventType = Literal[
"start", "cycle_completed", "line_stop", "resume", "close",
]
+3
View File
@@ -6,6 +6,7 @@ from src.backend.models.orm.measurement import Measurement
from src.backend.models.orm.access_log import AccessLog
from src.backend.models.orm.setting import SystemSetting, RecipeVersionAudit
from src.backend.models.orm.station import Station, StationRecipeAssignment
from src.backend.models.orm.production import ProductionRun, ProductionEvent
__all__ = [
"User",
@@ -19,4 +20,6 @@ __all__ = [
"RecipeVersionAudit",
"Station",
"StationRecipeAssignment",
"ProductionRun",
"ProductionEvent",
]
+152
View File
@@ -0,0 +1,152 @@
"""ProductionRun and ProductionEvent models.
A production run is what an operator opens when production starts and a supervisor
closes when it ends. It is the thing that outlives a page: before this, the timer,
the cycle count and the "production started" flag were Alpine variables in
task_execute.html, and navigating between tasks - a full page load - destroyed them.
The measurement deadline is stored as an absolute timestamp (next_measurement_at)
rather than a remaining count. Any client can then derive the countdown from it, get
the same answer, survive a reload, and tell how far *past* the interval it is by
letting the difference go negative.
With one station app installed per PC (see the install architecture of 28/07) the
database is the only place this state can live: the API keeps nothing in process
memory.
"""
from datetime import datetime
from typing import TYPE_CHECKING, Optional
from sqlalchemy import (
DateTime, Enum, ForeignKey, Index, Integer, SmallInteger, String, Text,
UniqueConstraint, func,
)
from sqlalchemy.orm import Mapped, mapped_column, relationship
from src.backend.database import Base
if TYPE_CHECKING:
from src.backend.models.orm.recipe import Recipe, RecipeVersion
from src.backend.models.orm.station import Station
RUN_STATUSES = ("running", "paused", "closed")
EVENT_TYPES = ("start", "cycle_completed", "line_stop", "resume", "close")
class ProductionRun(Base):
__tablename__ = "production_runs"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
station_id: Mapped[int] = mapped_column(
Integer, ForeignKey("stations.id"), nullable=False, index=True
)
recipe_id: Mapped[int] = mapped_column(
Integer, ForeignKey("recipes.id"), nullable=False, index=True
)
version_id: Mapped[int] = mapped_column(
Integer, ForeignKey("recipe_versions.id"), nullable=False, index=True
)
operator_id: Mapped[int] = mapped_column(
Integer, ForeignKey("users.id"), nullable=False, index=True
)
# Traceability travels with the run instead of the Flask session, so it stays
# attached to every measurement taken during it.
lot_number: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, index=True)
serial_number: Mapped[Optional[str]] = mapped_column(String(100), nullable=True, index=True)
status: Mapped[str] = mapped_column(
Enum(*RUN_STATUSES, name="production_run_status_enum"),
nullable=False,
default="running",
index=True,
)
# Snapshot of the recipe interval at start: editing the recipe mid-run must not
# move the deadline of a production already under way.
measurement_interval_minutes: Mapped[Optional[int]] = mapped_column(
SmallInteger, nullable=True
)
# When the next measurement falls due. Null when the recipe has no interval.
# Past this instant the run is overdue, and by how much is simply now - this.
next_measurement_at: Mapped[Optional[datetime]] = mapped_column(
DateTime, nullable=True, index=True
)
cycle_count: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
started_at: Mapped[datetime] = mapped_column(
DateTime, nullable=False, server_default=func.now()
)
paused_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
closed_at: Mapped[Optional[datetime]] = mapped_column(DateTime, nullable=True)
closed_by: Mapped[Optional[int]] = mapped_column(
Integer, ForeignKey("users.id"), nullable=True
)
# Mirrors station_id while the run is open and goes NULL when it closes. A unique
# index on it lets the database - not a check-then-insert race - guarantee that a
# station never has two open runs. Repeated NULLs do not collide in a unique index.
active_station_id: Mapped[Optional[int]] = mapped_column(Integer, nullable=True)
station: Mapped["Station"] = relationship(lazy="selectin")
recipe: Mapped["Recipe"] = relationship(lazy="selectin")
version: Mapped["RecipeVersion"] = relationship(lazy="selectin")
events: Mapped[list["ProductionEvent"]] = relationship(
back_populates="run",
cascade="all, delete-orphan",
order_by="ProductionEvent.created_at",
)
__table_args__ = (
UniqueConstraint("active_station_id", name="uq_production_runs_active_station"),
Index("ix_production_runs_station_status", "station_id", "status"),
{"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"},
)
@property
def is_open(self) -> bool:
return self.status in ("running", "paused")
def __repr__(self) -> str:
return f"<ProductionRun {self.id} station={self.station_id} {self.status}>"
class ProductionEvent(Base):
"""Append-only trace of what happened during a run.
This is the record that did not exist before: without it there is no history of
a production, and nothing for the statistics file or the ERP hand-off to stand on.
"""
__tablename__ = "production_events"
id: Mapped[int] = mapped_column(Integer, primary_key=True, autoincrement=True)
run_id: Mapped[int] = mapped_column(
Integer, ForeignKey("production_runs.id", ondelete="CASCADE"),
nullable=False, index=True,
)
event_type: Mapped[str] = mapped_column(
Enum(*EVENT_TYPES, name="production_event_type_enum"), nullable=False, index=True
)
user_id: Mapped[int] = mapped_column(
Integer, ForeignKey("users.id"), nullable=False, index=True
)
# Who authorised it, when the action needed a supervisor (fermo linea, chiusura).
supervisor_id: Mapped[Optional[int]] = mapped_column(
Integer, ForeignKey("users.id"), nullable=True
)
note: Mapped[Optional[str]] = mapped_column(Text, nullable=True)
created_at: Mapped[datetime] = mapped_column(
DateTime, nullable=False, server_default=func.now(), index=True
)
run: Mapped["ProductionRun"] = relationship(back_populates="events")
__table_args__ = (
{"mysql_engine": "InnoDB", "mysql_charset": "utf8mb4"},
)
def __repr__(self) -> str:
return f"<ProductionEvent run={self.run_id} {self.event_type}>"
+349
View File
@@ -0,0 +1,349 @@
"""Business logic for production runs.
Routers must call into these functions rather than manipulating models directly.
All functions are async and accept an AsyncSession; they flush but do NOT commit
(commit is handled by the FastAPI get_db dependency).
Clock convention
----------------
Every timestamp this module writes comes from ``_now()``, so the deadline and the
comparison against it are always on the same clock. Clients are handed
``seconds_to_next_measurement`` already computed rather than the raw deadline: a
naive datetime crossing the wire would be parsed in the browser's own timezone, and
the countdown would be off by the UTC offset. The number is the contract; the
timestamp is only there for the audit trail.
"""
from datetime import datetime, timedelta
from typing import Optional
from fastapi import HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
from src.backend.models.api.production import ProductionRunCreate
from src.backend.models.orm.production import ProductionEvent, ProductionRun
from src.backend.models.orm.recipe import Recipe, RecipeVersion
from src.backend.models.orm.station import Station
from src.backend.models.orm.user import User
from src.backend.services import auth_service
def _now() -> datetime:
"""Single source of time for runs.
Naive local time, matching what the database writes for ``func.now()`` defaults,
so a run's started_at and its deadline are comparable.
"""
return datetime.now()
# ---------------------------------------------------------------------------
# Derived view
# ---------------------------------------------------------------------------
def seconds_to_next_measurement(run: ProductionRun, now: Optional[datetime] = None) -> Optional[int]:
"""Seconds until the next measurement is due; negative once it is overdue.
Letting the value go negative is deliberate: the operator has to see how long
the line has been past the interval, not merely that it elapsed.
While the run is paused the countdown is frozen at what was left when the line
stopped - a fermo linea must not eat into the measurement interval.
"""
if run.next_measurement_at is None:
return None
reference = run.paused_at if (run.status == "paused" and run.paused_at) else (now or _now())
return int(round((run.next_measurement_at - reference).total_seconds()))
def describe(run: ProductionRun) -> dict:
"""Fields the API adds on top of the stored columns."""
now = _now()
remaining = seconds_to_next_measurement(run, now)
return {
"seconds_to_next_measurement": remaining,
"overdue": remaining is not None and remaining < 0,
"server_time": now,
}
# ---------------------------------------------------------------------------
# Lookups
# ---------------------------------------------------------------------------
async def get_run(db: AsyncSession, run_id: int) -> ProductionRun:
result = await db.execute(select(ProductionRun).where(ProductionRun.id == run_id))
run = result.scalar_one_or_none()
if run is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Production run not found",
)
return run
async def get_open_run_for_station(
db: AsyncSession, station_id: int,
) -> Optional[ProductionRun]:
"""The run currently open at a station, running or paused."""
result = await db.execute(
select(ProductionRun).where(ProductionRun.active_station_id == station_id)
)
return result.scalar_one_or_none()
async def get_station_by_code(db: AsyncSession, code: str) -> Station:
result = await db.execute(select(Station).where(Station.code == code))
station = result.scalar_one_or_none()
if station is None or not station.active:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail=f"Station '{code}' not found or inactive",
)
return station
async def list_run_events(db: AsyncSession, run_id: int) -> list[ProductionEvent]:
result = await db.execute(
select(ProductionEvent)
.where(ProductionEvent.run_id == run_id)
.order_by(ProductionEvent.created_at, ProductionEvent.id)
)
return list(result.scalars().all())
# ---------------------------------------------------------------------------
# Supervisor authorisation
# ---------------------------------------------------------------------------
async def authorise_supervisor(db: AsyncSession, username: str, password: str) -> User:
"""Check the credentials of the supervisor authorising a stop or a close.
Uses authenticate_user rather than a full login: logging in would rotate the
supervisor's API key and knock out whatever session they have open elsewhere.
"""
supervisor = await auth_service.authenticate_user(db, username, password)
if supervisor is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid supervisor credentials",
)
if not supervisor.has_role("Supervisor") and not supervisor.is_admin:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="User is not authorised as supervisor",
)
return supervisor
# ---------------------------------------------------------------------------
# Lifecycle
# ---------------------------------------------------------------------------
def _add_event(
db: AsyncSession,
run: ProductionRun,
event_type: str,
user: User,
supervisor: Optional[User] = None,
note: Optional[str] = None,
) -> ProductionEvent:
"""Append to the run's trace.
Added through the session rather than run.events: touching the collection would
trigger a lazy load, which raises under async SQLAlchemy.
"""
event = ProductionEvent(
run_id=run.id,
event_type=event_type,
user_id=user.id,
supervisor_id=supervisor.id if supervisor else None,
note=note,
created_at=_now(),
)
db.add(event)
return event
async def open_run(
db: AsyncSession, data: ProductionRunCreate, operator: User,
) -> ProductionRun:
"""Start a production at a station.
Refuses if the station already has one open: two runs on the same station would
mean two timers and two histories for one physical line.
"""
station = await get_station_by_code(db, data.station_code)
existing = await get_open_run_for_station(db, station.id)
if existing is not None:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail=f"Station '{station.code}' already has an open production run",
)
recipe_row = await db.execute(select(Recipe).where(Recipe.id == data.recipe_id))
recipe = recipe_row.scalar_one_or_none()
if recipe is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND, detail="Recipe not found",
)
if data.version_id is not None:
version_row = await db.execute(
select(RecipeVersion).where(
RecipeVersion.id == data.version_id,
RecipeVersion.recipe_id == recipe.id,
)
)
version = version_row.scalar_one_or_none()
if version is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Recipe version not found for this recipe",
)
else:
version_row = await db.execute(
select(RecipeVersion).where(
RecipeVersion.recipe_id == recipe.id,
RecipeVersion.is_current == True, # noqa: E712
)
)
version = version_row.scalar_one_or_none()
if version is None:
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Recipe has no current version",
)
now = _now()
# The interval is copied, not referenced: editing the recipe mid-production must
# not move the deadline of a run already under way.
interval = recipe.measurement_interval_minutes
run = ProductionRun(
station_id=station.id,
recipe_id=recipe.id,
version_id=version.id,
operator_id=operator.id,
lot_number=data.lot_number,
serial_number=data.serial_number,
status="running",
measurement_interval_minutes=interval,
next_measurement_at=(
now + timedelta(minutes=interval) if interval and interval > 0 else None
),
cycle_count=0,
started_at=now,
active_station_id=station.id,
)
db.add(run)
await db.flush()
_add_event(db, run, "start", operator)
await db.flush()
await db.refresh(run)
return run
def _require_open(run: ProductionRun) -> None:
if run.status == "closed":
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Production run is already closed",
)
async def complete_cycle(
db: AsyncSession, run: ProductionRun, user: User, note: Optional[str] = None,
) -> ProductionRun:
"""Record a finished measurement cycle and restart the interval."""
_require_open(run)
if run.status == "paused":
raise HTTPException(
status_code=status.HTTP_409_CONFLICT,
detail="Production run is paused: resume it before measuring",
)
now = _now()
run.cycle_count += 1
if run.measurement_interval_minutes and run.measurement_interval_minutes > 0:
# Measured from now, not from the previous deadline: a late measurement must
# not compress the following interval.
run.next_measurement_at = now + timedelta(
minutes=run.measurement_interval_minutes
)
_add_event(db, run, "cycle_completed", user, note=note)
await db.flush()
await db.refresh(run)
return run
async def pause_run(
db: AsyncSession,
run: ProductionRun,
user: User,
supervisor: User,
note: Optional[str] = None,
) -> ProductionRun:
"""Fermo linea: suspend the run and freeze the countdown."""
_require_open(run)
if run.status == "paused":
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Production run is already paused",
)
run.status = "paused"
run.paused_at = _now()
_add_event(db, run, "line_stop", user, supervisor=supervisor, note=note)
await db.flush()
await db.refresh(run)
return run
async def resume_run(
db: AsyncSession,
run: ProductionRun,
user: User,
supervisor: User,
note: Optional[str] = None,
) -> ProductionRun:
"""Restart a paused run, pushing the deadline out by the length of the stop."""
_require_open(run)
if run.status != "paused":
raise HTTPException(
status_code=status.HTTP_409_CONFLICT, detail="Production run is not paused",
)
now = _now()
if run.next_measurement_at is not None and run.paused_at is not None:
# Shift rather than recompute: whatever was left when the line stopped is
# what the operator gets back, so a stop neither grants nor costs time.
run.next_measurement_at = run.next_measurement_at + (now - run.paused_at)
run.status = "running"
run.paused_at = None
_add_event(db, run, "resume", user, supervisor=supervisor, note=note)
await db.flush()
await db.refresh(run)
return run
async def close_run(
db: AsyncSession,
run: ProductionRun,
user: User,
supervisor: User,
note: Optional[str] = None,
) -> ProductionRun:
"""Fine produzione: close the run for good and stop the timer."""
_require_open(run)
run.status = "closed"
run.closed_at = _now()
run.closed_by = supervisor.id
run.next_measurement_at = None
run.paused_at = None
# Releasing the slot lets the station open a new run; the unique index on this
# column is what keeps exactly one open at a time.
run.active_station_id = None
_add_event(db, run, "close", user, supervisor=supervisor, note=note)
await db.flush()
await db.refresh(run)
return run
+443
View File
@@ -0,0 +1,443 @@
"""Integration tests for /api/production-runs.
The point of these tables is that a production outlives the page it was started
from, so the tests lean on what must survive: the deadline, the cycle count, the
trace of what happened, and the rule that a station has one production at a time.
"""
from datetime import datetime, timedelta
import pytest
from httpx import AsyncClient
from sqlalchemy import select
from src.backend.models.orm.production import ProductionRun
from src.backend.models.orm.recipe import Recipe
from src.backend.models.orm.station import Station
from src.backend.services import auth_service, production_service
from src.backend.tests.conftest import auth_headers, create_test_recipe
async def _station(db_session, user_id: int, code: str = "ST-PROD") -> Station:
station = Station(code=code, name=f"Stazione {code}", active=True, created_by=user_id)
db_session.add(station)
await db_session.commit()
await db_session.refresh(station)
return station
async def _recipe_with_interval(db_session, user_id: int, minutes, code="REC-PROD") -> Recipe:
recipe = await create_test_recipe(db_session, user_id=user_id, code=code)
recipe.measurement_interval_minutes = minutes
await db_session.commit()
await db_session.refresh(recipe)
return recipe
async def _supervisor(db_session, username="capoturno", password="TurnoPwd1"):
user = await auth_service.create_user(
db_session,
username=username,
password=password,
display_name="Capo Turno",
roles=["Supervisor"],
)
await db_session.commit()
return user, password
async def _advance_clock(db_session, run_id: int, delta: timedelta) -> None:
"""Simulate wall-clock time passing, without sleeping in a test.
Winding every instant stored on the run back by D is indistinguishable from D
having elapsed. Moving only one of them would not be time passing - it would be
rewriting history, and would measure the wrong thing.
"""
run = (await db_session.execute(
select(ProductionRun).where(ProductionRun.id == run_id)
)).scalar_one()
run.started_at = run.started_at - delta
if run.next_measurement_at is not None:
run.next_measurement_at = run.next_measurement_at - delta
if run.paused_at is not None:
run.paused_at = run.paused_at - delta
await db_session.commit()
async def _open(client, user, station, recipe, **extra):
return await client.post(
"/api/production-runs",
headers=auth_headers(user),
json={"station_code": station.code, "recipe_id": recipe.id, **extra},
)
# ---------------------------------------------------------------------------
# Opening
# ---------------------------------------------------------------------------
async def test_open_run_requires_auth(client: AsyncClient):
resp = await client.post("/api/production-runs", json={"station_code": "X", "recipe_id": 1})
assert resp.status_code == 401
async def test_open_run_starts_the_countdown(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id)
recipe = await _recipe_with_interval(db_session, admin_user.id, minutes=30)
resp = await _open(client, measurement_tec_user, station, recipe)
assert resp.status_code == 201, resp.text
body = resp.json()
assert body["status"] == "running"
assert body["cycle_count"] == 0
assert body["measurement_interval_minutes"] == 30
# About half an hour out, allowing for the round trip.
assert 29 * 60 <= body["seconds_to_next_measurement"] <= 30 * 60
assert body["overdue"] is False
async def test_open_run_without_interval_has_no_deadline(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-NOINT")
recipe = await _recipe_with_interval(db_session, admin_user.id, None, code="REC-NOINT")
body = (await _open(client, measurement_tec_user, station, recipe)).json()
assert body["next_measurement_at"] is None
assert body["seconds_to_next_measurement"] is None
assert body["overdue"] is False
async def test_open_run_snapshots_the_interval(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
"""Editing the recipe mid-production must not move a running deadline."""
station = await _station(db_session, admin_user.id, code="ST-SNAP")
recipe = await _recipe_with_interval(db_session, admin_user.id, 20, code="REC-SNAP")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
recipe.measurement_interval_minutes = 90
await db_session.commit()
resp = await client.get(
f"/api/production-runs/{run_id}", headers=auth_headers(measurement_tec_user),
)
assert resp.json()["measurement_interval_minutes"] == 20
async def test_station_cannot_have_two_open_runs(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-ONE")
recipe = await _recipe_with_interval(db_session, admin_user.id, 15, code="REC-ONE")
first = await _open(client, measurement_tec_user, station, recipe)
assert first.status_code == 201
second = await _open(client, measurement_tec_user, station, recipe)
assert second.status_code == 409
async def test_open_run_rejects_unknown_station(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
recipe = await _recipe_with_interval(db_session, admin_user.id, 15, code="REC-NOSTAT")
resp = await client.post(
"/api/production-runs",
headers=auth_headers(measurement_tec_user),
json={"station_code": "ST-GHOST", "recipe_id": recipe.id},
)
assert resp.status_code == 404
# ---------------------------------------------------------------------------
# Reading the current run - what replaces the in-page state
# ---------------------------------------------------------------------------
async def test_current_run_is_null_when_nothing_is_running(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-IDLE")
resp = await client.get(
"/api/production-runs/current",
params={"station_code": station.code},
headers=auth_headers(measurement_tec_user),
)
assert resp.status_code == 200
assert resp.json() is None
async def test_current_run_survives_and_keeps_counting_down(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
"""The deadline is stored, so a fresh page load rejoins the same countdown."""
station = await _station(db_session, admin_user.id, code="ST-KEEP")
recipe = await _recipe_with_interval(db_session, admin_user.id, 45, code="REC-KEEP")
opened = (await _open(client, measurement_tec_user, station, recipe)).json()
later = await client.get(
"/api/production-runs/current",
params={"station_code": station.code},
headers=auth_headers(measurement_tec_user),
)
body = later.json()
assert body["id"] == opened["id"]
assert body["next_measurement_at"] == opened["next_measurement_at"]
assert body["seconds_to_next_measurement"] <= opened["seconds_to_next_measurement"]
# ---------------------------------------------------------------------------
# Cycles
# ---------------------------------------------------------------------------
async def test_cycle_restarts_the_interval_and_counts(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-CYC")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-CYC")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
resp = await client.post(
f"/api/production-runs/{run_id}/cycle", headers=auth_headers(measurement_tec_user),
)
assert resp.status_code == 200, resp.text
body = resp.json()
assert body["cycle_count"] == 1
assert 9 * 60 <= body["seconds_to_next_measurement"] <= 10 * 60
async def test_overdue_run_reports_negative_seconds(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
"""Past the interval the countdown keeps going the other way, so the delay shows."""
station = await _station(db_session, admin_user.id, code="ST-LATE")
recipe = await _recipe_with_interval(db_session, admin_user.id, 5, code="REC-LATE")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
await _advance_clock(db_session, run_id, timedelta(minutes=8))
body = (await client.get(
"/api/production-runs/current",
params={"station_code": station.code},
headers=auth_headers(measurement_tec_user),
)).json()
assert body["overdue"] is True
assert -190 <= body["seconds_to_next_measurement"] <= -170
# ---------------------------------------------------------------------------
# Fermo linea / ripresa / chiusura
# ---------------------------------------------------------------------------
async def test_pause_requires_supervisor_credentials(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-PAUSE1")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-PAUSE1")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
resp = await client.post(
f"/api/production-runs/{run_id}/pause",
headers=auth_headers(measurement_tec_user),
json={"supervisor_username": "nobody", "supervisor_password": "wrong"},
)
assert resp.status_code == 401
async def test_pause_rejects_non_supervisor(
client: AsyncClient, measurement_tec_user, maker_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-PAUSE2")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-PAUSE2")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
resp = await client.post(
f"/api/production-runs/{run_id}/pause",
headers=auth_headers(measurement_tec_user),
json={
"supervisor_username": maker_user.username,
"supervisor_password": "testpassword123",
},
)
assert resp.status_code in (401, 403)
async def test_pause_freezes_the_countdown(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
"""A fermo linea must not eat the measurement interval."""
station = await _station(db_session, admin_user.id, code="ST-FREEZE")
recipe = await _recipe_with_interval(db_session, admin_user.id, 30, code="REC-FREEZE")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
supervisor, password = await _supervisor(db_session, username="capo-freeze")
paused = (await client.post(
f"/api/production-runs/{run_id}/pause",
headers=auth_headers(measurement_tec_user),
json={"supervisor_username": supervisor.username, "supervisor_password": password},
)).json()
assert paused["status"] == "paused"
frozen = paused["seconds_to_next_measurement"]
# An hour goes by with the line stopped: were the countdown still ticking, the
# run would now be deeply overdue.
await _advance_clock(db_session, run_id, timedelta(hours=1))
still = (await client.get(
"/api/production-runs/current",
params={"station_code": station.code},
headers=auth_headers(measurement_tec_user),
)).json()
assert still["status"] == "paused"
assert still["seconds_to_next_measurement"] == frozen
assert still["overdue"] is False
async def test_resume_gives_back_the_time_the_stop_took(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-RESUME")
recipe = await _recipe_with_interval(db_session, admin_user.id, 30, code="REC-RESUME")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
supervisor, password = await _supervisor(db_session, username="capo-resume")
creds = {"supervisor_username": supervisor.username, "supervisor_password": password}
frozen = (await client.post(
f"/api/production-runs/{run_id}/pause",
headers=auth_headers(measurement_tec_user), json=creds,
)).json()["seconds_to_next_measurement"]
# The line stays down for ten minutes.
await _advance_clock(db_session, run_id, timedelta(minutes=10))
resumed = (await client.post(
f"/api/production-runs/{run_id}/resume",
headers=auth_headers(measurement_tec_user), json=creds,
)).json()
assert resumed["status"] == "running"
# The ten minutes of stop were handed back, not charged to the operator.
assert abs(resumed["seconds_to_next_measurement"] - frozen) <= 2
async def test_cycle_refused_while_paused(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-PCYC")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-PCYC")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
supervisor, password = await _supervisor(db_session, username="capo-pcyc")
await client.post(
f"/api/production-runs/{run_id}/pause",
headers=auth_headers(measurement_tec_user),
json={"supervisor_username": supervisor.username, "supervisor_password": password},
)
resp = await client.post(
f"/api/production-runs/{run_id}/cycle", headers=auth_headers(measurement_tec_user),
)
assert resp.status_code == 409
async def test_close_stops_the_timer_and_frees_the_station(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-CLOSE")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-CLOSE")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
supervisor, password = await _supervisor(db_session, username="capo-close")
closed = (await client.post(
f"/api/production-runs/{run_id}/close",
headers=auth_headers(measurement_tec_user),
json={"supervisor_username": supervisor.username, "supervisor_password": password},
)).json()
assert closed["status"] == "closed"
assert closed["closed_by"] == supervisor.id
assert closed["seconds_to_next_measurement"] is None
# Nothing is open at the station any more...
current = await client.get(
"/api/production-runs/current",
params={"station_code": station.code},
headers=auth_headers(measurement_tec_user),
)
assert current.json() is None
# ...and a new production can start there.
again = await _open(client, measurement_tec_user, station, recipe)
assert again.status_code == 201
async def test_closed_run_refuses_further_actions(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-DEAD")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-DEAD")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
supervisor, password = await _supervisor(db_session, username="capo-dead")
creds = {"supervisor_username": supervisor.username, "supervisor_password": password}
await client.post(
f"/api/production-runs/{run_id}/close",
headers=auth_headers(measurement_tec_user), json=creds,
)
cycle = await client.post(
f"/api/production-runs/{run_id}/cycle", headers=auth_headers(measurement_tec_user),
)
assert cycle.status_code == 409
pause = await client.post(
f"/api/production-runs/{run_id}/pause",
headers=auth_headers(measurement_tec_user), json=creds,
)
assert pause.status_code == 409
# ---------------------------------------------------------------------------
# The trace
# ---------------------------------------------------------------------------
async def test_run_records_what_happened(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
"""The history a production never had: start, cycles, stop, resume, close."""
station = await _station(db_session, admin_user.id, code="ST-TRACE")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-TRACE")
run_id = (await _open(client, measurement_tec_user, station, recipe)).json()["id"]
supervisor, password = await _supervisor(db_session, username="capo-trace")
creds = {"supervisor_username": supervisor.username, "supervisor_password": password}
headers = auth_headers(measurement_tec_user)
await client.post(f"/api/production-runs/{run_id}/cycle", headers=headers)
await client.post(f"/api/production-runs/{run_id}/pause", headers=headers, json=creds)
await client.post(f"/api/production-runs/{run_id}/resume", headers=headers, json=creds)
await client.post(f"/api/production-runs/{run_id}/close", headers=headers, json=creds)
events = (await client.get(
f"/api/production-runs/{run_id}", headers=headers,
)).json()["events"]
assert [e["event_type"] for e in events] == [
"start", "cycle_completed", "line_stop", "resume", "close",
]
# Who authorised what is on the record.
by_type = {e["event_type"]: e for e in events}
assert by_type["line_stop"]["supervisor_id"] == supervisor.id
assert by_type["close"]["supervisor_id"] == supervisor.id
assert by_type["cycle_completed"]["supervisor_id"] is None
async def test_traceability_travels_with_the_run(
client: AsyncClient, measurement_tec_user, admin_user, db_session,
):
station = await _station(db_session, admin_user.id, code="ST-LOT")
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-LOT")
body = (await _open(
client, measurement_tec_user, station, recipe,
lot_number="LOT-42", serial_number="SN-7",
)).json()
assert body["lot_number"] == "LOT-42"
assert body["serial_number"] == "SN-7"