From 44a22eedcfe4ff8ac03f0b0599da9f69f7d01232 Mon Sep 17 00:00:00 2001 From: ACX <8075870+acx10@users.noreply.github.com> Date: Sun, 22 Feb 2026 11:12:03 -0700 Subject: [PATCH] Fix reading stats queries not accounting for timezone (#2842) (#2844) --- .../repository/ReadingSessionRepository.java | 103 ++++++++++-------- .../service/ReadingSessionService.java | 16 ++- 2 files changed, 68 insertions(+), 51 deletions(-) diff --git a/booklore-api/src/main/java/org/booklore/repository/ReadingSessionRepository.java b/booklore-api/src/main/java/org/booklore/repository/ReadingSessionRepository.java index f08b78b27..bef5c6597 100644 --- a/booklore-api/src/main/java/org/booklore/repository/ReadingSessionRepository.java +++ b/booklore-api/src/main/java/org/booklore/repository/ReadingSessionRepository.java @@ -18,29 +18,35 @@ import java.util.List; @Repository public interface ReadingSessionRepository extends JpaRepository { - @Query(""" - SELECT CAST(rs.startTime AS LocalDate) as date, COUNT(rs) as count - FROM ReadingSessionEntity rs - WHERE rs.user.id = :userId - AND YEAR(rs.startTime) = :year - GROUP BY CAST(rs.startTime AS LocalDate) + @Query(value = """ + SELECT DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as date, + COUNT(*) as count + FROM reading_sessions + WHERE user_id = :userId + AND YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year + GROUP BY DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset)) ORDER BY date - """) - List findSessionCountsByUserAndYear(@Param("userId") Long userId, @Param("year") int year); + """, nativeQuery = true) + List findSessionCountsByUserAndYear( + @Param("userId") Long userId, + @Param("year") int year, + @Param("tzOffset") String tzOffset); - @Query(""" - SELECT CAST(rs.startTime AS LocalDate) as date, COUNT(rs) as count - FROM ReadingSessionEntity rs - WHERE rs.user.id = :userId - AND YEAR(rs.startTime) = :year - AND MONTH(rs.startTime) = :month - GROUP BY CAST(rs.startTime AS LocalDate) + @Query(value = """ + SELECT DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as date, + COUNT(*) as count + FROM reading_sessions + WHERE user_id = :userId + AND YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year + AND MONTH(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :month + GROUP BY DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset)) ORDER BY date - """) + """, nativeQuery = true) List findSessionCountsByUserAndYearAndMonth( @Param("userId") Long userId, @Param("year") int year, - @Param("month") int month); + @Param("month") int month, + @Param("tzOffset") String tzOffset); @Query(""" SELECT @@ -79,39 +85,41 @@ public interface ReadingSessionRepository extends JpaRepository findReadingSpeedByUserAndYear(@Param("userId") Long userId, @Param("year") int year); - @Query(""" + @Query(value = """ SELECT - HOUR(rs.startTime) as hourOfDay, - COUNT(rs) as sessionCount, - SUM(rs.durationSeconds) as totalDurationSeconds - FROM ReadingSessionEntity rs - WHERE rs.user.id = :userId - AND (:year IS NULL OR YEAR(rs.startTime) = :year) - AND (:month IS NULL OR MONTH(rs.startTime) = :month) - GROUP BY HOUR(rs.startTime) + HOUR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as hourOfDay, + COUNT(*) as sessionCount, + SUM(duration_seconds) as totalDurationSeconds + FROM reading_sessions + WHERE user_id = :userId + AND (:year IS NULL OR YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year) + AND (:month IS NULL OR MONTH(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :month) + GROUP BY HOUR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) ORDER BY hourOfDay - """) + """, nativeQuery = true) List findPeakReadingHoursByUser( @Param("userId") Long userId, @Param("year") Integer year, - @Param("month") Integer month); + @Param("month") Integer month, + @Param("tzOffset") String tzOffset); - @Query(""" + @Query(value = """ SELECT - DAYOFWEEK(rs.startTime) as dayOfWeek, - COUNT(rs) as sessionCount, - COALESCE(SUM(rs.durationSeconds), 0) as totalDurationSeconds - FROM ReadingSessionEntity rs - WHERE rs.user.id = :userId - AND (:year IS NULL OR YEAR(rs.startTime) = :year) - AND (:month IS NULL OR MONTH(rs.startTime) = :month) - GROUP BY DAYOFWEEK(rs.startTime) + DAYOFWEEK(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as dayOfWeek, + COUNT(*) as sessionCount, + COALESCE(SUM(duration_seconds), 0) as totalDurationSeconds + FROM reading_sessions + WHERE user_id = :userId + AND (:year IS NULL OR YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year) + AND (:month IS NULL OR MONTH(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :month) + GROUP BY DAYOFWEEK(CONVERT_TZ(start_time, '+00:00', :tzOffset)) ORDER BY dayOfWeek - """) + """, nativeQuery = true) List findFavoriteReadingDaysByUser( @Param("userId") Long userId, @Param("year") Integer year, - @Param("month") Integer month); + @Param("month") Integer month, + @Param("tzOffset") String tzOffset); @Query(""" SELECT @@ -179,12 +187,15 @@ public interface ReadingSessionRepository extends JpaRepository findAllSessionCountsByUser(@Param("userId") Long userId); + """, nativeQuery = true) + List findAllSessionCountsByUser( + @Param("userId") Long userId, + @Param("tzOffset") String tzOffset); } diff --git a/booklore-api/src/main/java/org/booklore/service/ReadingSessionService.java b/booklore-api/src/main/java/org/booklore/service/ReadingSessionService.java index b2e8a63eb..f3b237499 100644 --- a/booklore-api/src/main/java/org/booklore/service/ReadingSessionService.java +++ b/booklore-api/src/main/java/org/booklore/service/ReadingSessionService.java @@ -41,6 +41,7 @@ import java.time.Instant; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.ZoneId; +import java.time.ZoneOffset; import java.time.temporal.ChronoUnit; import java.time.temporal.TemporalAdjusters; import java.time.temporal.WeekFields; @@ -58,6 +59,11 @@ public class ReadingSessionService { private final UserRepository userRepository; private final UserBookProgressRepository userBookProgressRepository; + private String getTimezoneOffset() { + ZoneOffset offset = ZoneId.systemDefault().getRules().getOffset(Instant.now()); + return offset.getId().equals("Z") ? "+00:00" : offset.getId(); + } + @Transactional public void recordSession(ReadingSessionRequest request) { BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); @@ -91,7 +97,7 @@ public class ReadingSessionService { BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); Long userId = authenticatedUser.getId(); - return readingSessionRepository.findSessionCountsByUserAndYear(userId, year) + return readingSessionRepository.findSessionCountsByUserAndYear(userId, year, getTimezoneOffset()) .stream() .map(dto -> ReadingSessionHeatmapResponse.builder() .date(dto.getDate()) @@ -105,7 +111,7 @@ public class ReadingSessionService { BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); Long userId = authenticatedUser.getId(); - return readingSessionRepository.findSessionCountsByUserAndYearAndMonth(userId, year, month) + return readingSessionRepository.findSessionCountsByUserAndYearAndMonth(userId, year, month, getTimezoneOffset()) .stream() .map(dto -> ReadingSessionHeatmapResponse.builder() .date(dto.getDate()) @@ -158,7 +164,7 @@ public class ReadingSessionService { BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); Long userId = authenticatedUser.getId(); - return readingSessionRepository.findPeakReadingHoursByUser(userId, year, month) + return readingSessionRepository.findPeakReadingHoursByUser(userId, year, month, getTimezoneOffset()) .stream() .map(dto -> PeakReadingHoursResponse.builder() .hourOfDay(dto.getHourOfDay()) @@ -175,7 +181,7 @@ public class ReadingSessionService { String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; - return readingSessionRepository.findFavoriteReadingDaysByUser(userId, year, month) + return readingSessionRepository.findFavoriteReadingDaysByUser(userId, year, month, getTimezoneOffset()) .stream() .map(dto -> FavoriteReadingDaysResponse.builder() .dayOfWeek(dto.getDayOfWeek()) @@ -414,7 +420,7 @@ public class ReadingSessionService { BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); Long userId = authenticatedUser.getId(); - return readingSessionRepository.findAllSessionCountsByUser(userId) + return readingSessionRepository.findAllSessionCountsByUser(userId, getTimezoneOffset()) .stream() .map(dto -> ReadingSessionHeatmapResponse.builder() .date(dto.getDate())