From 569dbc3d21d988f19e892fda76ebee688b49a07f Mon Sep 17 00:00:00 2001 From: maziggy Date: Thu, 1 Jan 2026 07:58:23 +0100 Subject: [PATCH] - Printer Hour Counter: - Fix runtime_seconds never incrementing during prints - Bug: first timestamp was set but never committed (updated_count was 0) - Add needs_commit flag to ensure timestamp is saved on first iteration --- backend/app/main.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/backend/app/main.py b/backend/app/main.py index e7c68d428..64e5d87c4 100644 --- a/backend/app/main.py +++ b/backend/app/main.py @@ -1466,6 +1466,8 @@ async def track_printer_runtime(): now = datetime.now() updated_count = 0 + needs_commit = False + for printer in printers: # Get current state from printer manager state = printer_manager.get_status(printer.id) @@ -1481,15 +1483,22 @@ async def track_printer_runtime(): if elapsed > 0 and elapsed < RUNTIME_TRACKING_INTERVAL * 2: printer.runtime_seconds += int(elapsed) updated_count += 1 + needs_commit = True + else: + # First time seeing printer active - need to commit to save timestamp + needs_commit = True printer.last_runtime_update = now else: # Printer is idle/offline - clear last_runtime_update - printer.last_runtime_update = None + if printer.last_runtime_update is not None: + printer.last_runtime_update = None + needs_commit = True - if updated_count > 0: + if needs_commit: await db.commit() - logger.debug(f"Updated runtime for {updated_count} printer(s)") + if updated_count > 0: + logger.debug(f"Updated runtime for {updated_count} printer(s)") except asyncio.CancelledError: logger.info("Runtime tracking cancelled")