From e44ac3129f3278d73d5aec81c2a20af9533fd955 Mon Sep 17 00:00:00 2001 From: "Adriano (daitarn)" Date: Wed, 7 Oct 2026 12:31:53 +0200 Subject: [PATCH] Statistiche sessioni: conteggio misure solo sui run della ricetta (#58) Co-Authored-By: Claude Sonnet 5.5 --- src/backend/api/routers/statistics.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/backend/api/routers/statistics.py b/src/backend/api/routers/statistics.py index 5fc7d67..1f8321d 100644 --- a/src/backend/api/routers/statistics.py +++ b/src/backend/api/routers/statistics.py @@ -257,17 +257,20 @@ async def get_recipe_runs( db: AsyncSession = Depends(get_db), ) -> list[dict]: """#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( select(ProductionRun) .where(ProductionRun.recipe_id == recipe_id) .order_by(ProductionRun.started_at.desc(), ProductionRun.id.desc()) )).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} names = {} if operator_ids: