feat(production): fermo linea e fine produzione fanno finalmente qualcosa
Punto 6 del documento modifiche del 28/07. I due pulsanti esistevano, chiedevano correttamente l'autorizzazione del capoturno, e poi ricadevano su un commento: "handled by GAIA integration (future)". E' la riga di apertura del documento. Ora agiscono sulla produzione aperta: fermo linea sospende e congela il conto alla rovescia, la ripresa lo fa ripartire restituendo il tempo del fermo, fine produzione chiude ed emette il file di statistica. Le credenziali del capoturno vanno direttamente all'endpoint, che le verifica e registra sulla traccia della produzione chi ha autorizzato cosa: validarle a parte non lascerebbe traccia, e sarebbe un giro in piu'. In interfaccia il pulsante di fermo diventa "Riprendi" quando la linea e' ferma, e una banda dichiara lo stato con il valore congelato a video: una linea ferma che sembra in marcia e' il modo in cui si salta un intervallo di misura senza accorgersene. Il file di statistica ha richiesto di legare le misure alla produzione (migrazione 006): Measurement conosceva solo versione, lotto e seriale, nessuno dei quali separa una produzione dalla successiva sulla stessa ricetta e sullo stesso lotto, quindi "le misure dell'intera produzione" non era una query. Il CSV rispetta i separatori configurati in system_settings e porta con se' i limiti di tolleranza: senza quelli un esito pass/fail non e' piu' ricalcolabile dal file a distanza di anni, che e' il senso di un documento per audit. Le misure esportate vengono marcate synced_to_csv. Se la produzione non ha misure non viene scritto nulla: un file vuoto sarebbe rumore nella cartella, non evidenza. L'invio a GAIA resta assente e non abbozzato, con il punto d'innesto dichiarato in close_run: una chiamata vuota che sembra collegata e' peggio di niente. Tutto il resto del punto 6 non dipende dal gestionale e funziona adesso. Anche la 006 e' stata eseguita su SQLite usa e getta prima di essere considerata buona, e anche qui la prova ha trovato un difetto: aggiungere una colonna con foreign key fa emettere ad alembic un ALTER di vincolo, che SQLite rifiuta. Ora usa batch_alter_table con il vincolo nominato, che su MySQL resta un ALTER normale e su SQLite ricostruisce la tabella - cosi' la chiave esterna non va persa per far contento il dialetto dei test. Corretto anche il proxy di salvataggio misura, che chiamava /api/measurements senza slash finale e pagava un redirect 307 a ogni singola misura. Traduzioni: pybabel aveva di nuovo indovinato sette voci marcandole fuzzy, e in italiano "Linea ferma" era diventato "Lingua Preferita" su una banda di sicurezza. Tradotte per esteso in IT ed EN e tolti i flag. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -441,3 +441,151 @@ async def test_traceability_travels_with_the_run(
|
||||
)).json()
|
||||
assert body["lot_number"] == "LOT-42"
|
||||
assert body["serial_number"] == "SN-7"
|
||||
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Fine produzione: the statistics file (point 6)
|
||||
# ---------------------------------------------------------------------------
|
||||
|
||||
|
||||
async def _measure(client, user, run_id, subtask_id, version_id, value):
|
||||
return await client.post(
|
||||
"/api/measurements/",
|
||||
headers=auth_headers(user),
|
||||
json={
|
||||
"subtask_id": subtask_id,
|
||||
"version_id": version_id,
|
||||
"value": value,
|
||||
"lot_number": "LOT-STAT",
|
||||
"production_run_id": run_id,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def _first_subtask(db_session, recipe_id: int):
|
||||
from src.backend.models.orm.recipe import RecipeVersion
|
||||
from src.backend.models.orm.task import RecipeSubtask, RecipeTask
|
||||
|
||||
row = await db_session.execute(
|
||||
select(RecipeSubtask)
|
||||
.join(RecipeTask, RecipeTask.id == RecipeSubtask.task_id)
|
||||
.join(RecipeVersion, RecipeVersion.id == RecipeTask.version_id)
|
||||
.where(RecipeVersion.recipe_id == recipe_id)
|
||||
)
|
||||
return row.scalars().first()
|
||||
|
||||
|
||||
async def test_measurement_is_tied_to_the_run(
|
||||
client: AsyncClient, measurement_tec_user, admin_user, db_session,
|
||||
):
|
||||
"""Without this link 'the measurements of this production' is not a query."""
|
||||
station = await _station(db_session, admin_user.id, code="ST-LINK")
|
||||
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-LINK")
|
||||
opened = (await _open(client, measurement_tec_user, station, recipe)).json()
|
||||
subtask = await _first_subtask(db_session, recipe.id)
|
||||
|
||||
resp = await _measure(
|
||||
client, measurement_tec_user, opened["id"], subtask.id, opened["version_id"], 10.0,
|
||||
)
|
||||
assert resp.status_code == 200, resp.text
|
||||
assert resp.json()["production_run_id"] == opened["id"]
|
||||
|
||||
|
||||
async def test_close_emits_the_statistics_file(
|
||||
client: AsyncClient, measurement_tec_user, admin_user, db_session, tmp_path,
|
||||
monkeypatch,
|
||||
):
|
||||
from src.backend.config import settings
|
||||
from src.backend.services import production_export_service
|
||||
|
||||
monkeypatch.setattr(
|
||||
type(settings), "upload_path", property(lambda self: tmp_path), raising=False,
|
||||
)
|
||||
|
||||
station = await _station(db_session, admin_user.id, code="ST-STAT")
|
||||
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-STAT")
|
||||
opened = (await _open(client, measurement_tec_user, station, recipe)).json()
|
||||
subtask = await _first_subtask(db_session, recipe.id)
|
||||
await _measure(
|
||||
client, measurement_tec_user, opened["id"], subtask.id, opened["version_id"], 10.0,
|
||||
)
|
||||
supervisor, password = await _supervisor(db_session, username="capo-stat")
|
||||
|
||||
closed = (await client.post(
|
||||
f"/api/production-runs/{opened['id']}/close",
|
||||
headers=auth_headers(measurement_tec_user),
|
||||
json={"supervisor_username": supervisor.username, "supervisor_password": password},
|
||||
)).json()
|
||||
|
||||
assert closed["statistics_path"], "la chiusura deve produrre il file di statistica"
|
||||
written = tmp_path / closed["statistics_path"]
|
||||
assert written.exists()
|
||||
|
||||
text = written.read_text(encoding="utf-8-sig")
|
||||
lines = [line for line in text.splitlines() if line.strip()]
|
||||
assert len(lines) == 2, "intestazione piu' una misura"
|
||||
assert "production_run_id" in lines[0]
|
||||
# The tolerance limits travel with the row: a pass/fail must stay re-derivable.
|
||||
for column in ("ltl", "lwl", "uwl", "utl", "nominal"):
|
||||
assert column in lines[0]
|
||||
assert "LOT-STAT" in lines[1]
|
||||
assert str(opened["id"]) in lines[1]
|
||||
|
||||
|
||||
async def test_close_marks_measurements_as_exported(
|
||||
client: AsyncClient, measurement_tec_user, admin_user, db_session, tmp_path,
|
||||
monkeypatch,
|
||||
):
|
||||
from src.backend.config import settings
|
||||
from src.backend.models.orm.measurement import Measurement
|
||||
|
||||
monkeypatch.setattr(
|
||||
type(settings), "upload_path", property(lambda self: tmp_path), raising=False,
|
||||
)
|
||||
|
||||
station = await _station(db_session, admin_user.id, code="ST-SYNC")
|
||||
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-SYNC")
|
||||
opened = (await _open(client, measurement_tec_user, station, recipe)).json()
|
||||
subtask = await _first_subtask(db_session, recipe.id)
|
||||
created = (await _measure(
|
||||
client, measurement_tec_user, opened["id"], subtask.id, opened["version_id"], 10.0,
|
||||
)).json()
|
||||
assert created["synced_to_csv"] is False
|
||||
|
||||
supervisor, password = await _supervisor(db_session, username="capo-sync")
|
||||
await client.post(
|
||||
f"/api/production-runs/{opened['id']}/close",
|
||||
headers=auth_headers(measurement_tec_user),
|
||||
json={"supervisor_username": supervisor.username, "supervisor_password": password},
|
||||
)
|
||||
|
||||
row = (await db_session.execute(
|
||||
select(Measurement).where(Measurement.id == created["id"])
|
||||
)).scalar_one()
|
||||
await db_session.refresh(row)
|
||||
assert row.synced_to_csv is True
|
||||
|
||||
|
||||
async def test_close_without_measurements_writes_no_file(
|
||||
client: AsyncClient, measurement_tec_user, admin_user, db_session, tmp_path,
|
||||
monkeypatch,
|
||||
):
|
||||
"""An empty file would be noise in the folder, not evidence."""
|
||||
from src.backend.config import settings
|
||||
|
||||
monkeypatch.setattr(
|
||||
type(settings), "upload_path", property(lambda self: tmp_path), raising=False,
|
||||
)
|
||||
|
||||
station = await _station(db_session, admin_user.id, code="ST-EMPTY")
|
||||
recipe = await _recipe_with_interval(db_session, admin_user.id, 10, code="REC-EMPTY")
|
||||
opened = (await _open(client, measurement_tec_user, station, recipe)).json()
|
||||
supervisor, password = await _supervisor(db_session, username="capo-empty")
|
||||
|
||||
closed = (await client.post(
|
||||
f"/api/production-runs/{opened['id']}/close",
|
||||
headers=auth_headers(measurement_tec_user),
|
||||
json={"supervisor_username": supervisor.username, "supervisor_password": password},
|
||||
)).json()
|
||||
assert closed["statistics_path"] is None
|
||||
assert not (tmp_path / "statistics").exists()
|
||||
|
||||
Reference in New Issue
Block a user