Statistiche sessioni: conteggio misure solo sui run della ricetta (#58)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-10-07 12:31:53 +02:00
parent 4e52095423
commit e44ac3129f
+9 -6
View File
@@ -257,17 +257,20 @@ async def get_recipe_runs(
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
) -> list[dict]: ) -> list[dict]:
"""#58: production runs of a recipe (for the "Sessione" filter), newest first.""" """#58: production runs of a recipe (for the "Sessione" filter), newest first."""
# Misure per sessione in una sola query, poi unite alle sessioni.
counts = dict((await db.execute(
select(Measurement.production_run_id, func.count(Measurement.id))
.where(Measurement.production_run_id.is_not(None))
.group_by(Measurement.production_run_id)
)).all())
runs = (await db.execute( runs = (await db.execute(
select(ProductionRun) select(ProductionRun)
.where(ProductionRun.recipe_id == recipe_id) .where(ProductionRun.recipe_id == recipe_id)
.order_by(ProductionRun.started_at.desc(), ProductionRun.id.desc()) .order_by(ProductionRun.started_at.desc(), ProductionRun.id.desc())
)).scalars().all() )).scalars().all()
# #58: conta solo le misure dei run della ricetta, non tutta la tabella.
run_ids = [r.id for r in runs]
counts = {}
if run_ids:
counts = dict((await db.execute(
select(Measurement.production_run_id, func.count(Measurement.id))
.where(Measurement.production_run_id.in_(run_ids))
.group_by(Measurement.production_run_id)
)).all())
operator_ids = {r.operator_id for r in runs} operator_ids = {r.operator_id for r in runs}
names = {} names = {}
if operator_ids: if operator_ids: