Sfoglia il codice sorgente

Fix: Use actual audiobook duration instead of wall-clock time for hours

Fixed incorrect hours calculation in reading log statistics that was using
the time difference between started_at and finished_at (wall-clock time)
instead of the actual audiobook duration.

Problem:
- If a book was started Jan 1 and finished Jan 10, it showed 216 hours
  (9 days * 24 hours) even if the audiobook was only 10 hours long
- Hours were calculated from timestamps instead of actual listening time

Solution:
- Changed total_hours calculation to use book.duration (in seconds)
- Updated individual book listening_duration to use book.duration
- Optimized to fetch books once and reuse books_dict

Changes in app/services/stats.py:
- Lines 63-78: Use book.duration for total hours calculation
- Lines 250-253: Use book.duration for individual book duration display

Now hours accurately reflect actual audiobook listening time.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Brad Lance 3 mesi fa
parent
commit
a370e3c44d
1 ha cambiato i file con 12 aggiunte e 11 eliminazioni
  1. 12 11
      app/services/stats.py

+ 12 - 11
app/services/stats.py

@@ -60,14 +60,7 @@ class ReadingStatsService:
         # Calculate finished books
         finished_sessions = [s for s in sessions if s.is_finished and s.finished_at]
 
-        # Calculate total listening time
-        total_hours = 0.0
-        for session in finished_sessions:
-            if session.started_at and session.finished_at:
-                duration = (session.finished_at - session.started_at).total_seconds() / 3600
-                total_hours += duration
-
-        # Get book details for finished books
+        # Get book details for finished books first (needed for calculations)
         finished_book_ids = [s.book_id for s in finished_sessions]
         books_dict = {}
         if finished_book_ids:
@@ -76,6 +69,14 @@ class ReadingStatsService:
             )
             books_dict = {book.id: book for book in books_result.scalars().all()}
 
+        # Calculate total listening time using actual book durations
+        total_hours = 0.0
+        for session in finished_sessions:
+            book = books_dict.get(session.book_id)
+            if book and book.duration:
+                # duration is in seconds, convert to hours
+                total_hours += book.duration / 3600
+
         # Calculate average rating
         rated_sessions = [s for s in finished_sessions if s.rating]
         avg_rating = (
@@ -243,9 +244,9 @@ class ReadingStatsService:
                 continue
 
             listening_duration = None
-            if session.started_at and session.finished_at:
-                duration_hours = (session.finished_at - session.started_at).total_seconds() / 3600
-                listening_duration = round(duration_hours, 1)
+            if book.duration:
+                # Use actual book duration instead of wall-clock time
+                listening_duration = round(book.duration / 3600, 1)
 
             recent.append({
                 "book_id": book.id,