fix(stats): cancelled prints get their own bucket; gauge denominator excludes them (#1390 follow-up)

Reporter (@IndividualGhost1905) saw Total: 20 / Success: 18 / Failed: 1
  and asked where the 20th print went. The Quick Stats endpoint counted
  status == "completed" → Successful and status == "failed" → Failed, but
  used a raw count(*) for Total Prints, so the four other PrintLogEntry
  statuses (aborted, stopped, cancelled, skipped) silently inflated the
  total without showing up in any breakdown row. The earlier #1390 round
  had committed a test locking in this exact behaviour
  ("uses total_prints as denominator so cancelled/stopped events count"),
  which was wrong: it conflated user intent with print quality.

  Three-bucket classification, applied across the whole stats surface
  and matching how the rest of the codebase already groups statuses
  (main.py:430, 1729; failure_analysis status filter):

    successful = completed
    failed     = failed + aborted    (printer-detected quality failures)
    cancelled  = stopped + cancelled + skipped  (user/queue stopped)

  Quick Stats endpoint returns the new cancelled_prints field;
  ArchiveStats.cancelled_prints defaults to 0 so older fixtures still
  parse. SuccessRateWidget gauge now divides by successful + failed
  only — a cancelled roll no longer drags the gauge down — and a
  Cancelled row appears in the breakdown so the missing prints don't
  silently vanish from Total Prints.

  Failure Analysis service applies the same denominator fix to both
  the headline failure_rate and the per-week trend, so a week with
  several cancellations and zero failures reads as 0% rather than
  a misleading "failed / total".

  i18n: new stats.cancelled key in all 9 locales with real
  translations (no English fallback), parity script clean.

  Tests: the existing 'uses total_prints as denominator' assertion
  is inverted to assert the new behaviour (40 / 20 / 35 → 67% gauge,
  Cancelled: 35 visible). The unchanged-display path (140 / 10 / 0
  → 93%) still holds since 140 / (140 + 10) = 93.33% rounds the
  same. 33 StatsPage tests + 6 backend stats/failure tests green.
This commit is contained in:
maziggy
2026-05-24 15:21:10 +02:00
parent b9b06a7351
commit 1e734fb7c6
18 changed files with 492 additions and 423 deletions
+15 -1
View File
@@ -865,10 +865,23 @@ async def get_archive_stats(
successful_prints = successful_result.scalar() or 0
failed_result = await db.execute(
select(func.count(PrintLogEntry.id)).where(PrintLogEntry.status == "failed", *base_conditions)
select(func.count(PrintLogEntry.id)).where(PrintLogEntry.status.in_(("failed", "aborted")), *base_conditions)
)
failed_prints = failed_result.scalar() or 0
# User/system-stopped prints — stopped/cancelled/skipped are distinct from
# quality failures: the user (or the queue) interrupted them, the printer
# didn't detect a fault. Bucketed separately so the Success Rate gauge
# divides by completed + failed only (a cancelled print shouldn't drag
# the gauge down), while still being visible in the breakdown so they
# don't silently vanish from Total Prints (#1390).
cancelled_result = await db.execute(
select(func.count(PrintLogEntry.id)).where(
PrintLogEntry.status.in_(("stopped", "cancelled", "skipped")), *base_conditions
)
)
cancelled_prints = cancelled_result.scalar() or 0
# Total elapsed time — PrintLogEntry stores duration_seconds directly so we
# can sum it server-side. Rows missing duration fall back to the slicer
# estimate from the archive (joined for that case only).
@@ -990,6 +1003,7 @@ async def get_archive_stats(
total_prints=total_prints,
successful_prints=successful_prints,
failed_prints=failed_prints,
cancelled_prints=cancelled_prints,
total_print_time_hours=round(total_time, 1),
total_filament_grams=round(total_filament, 1),
total_cost=round(total_cost, 2),