Fix reading stats queries not accounting for timezone (#2842) (#2844)

This commit is contained in:
ACX
2026-02-22 11:12:03 -07:00
committed by GitHub
parent 094e123d0e
commit 44a22eedcf
2 changed files with 68 additions and 51 deletions
@@ -18,29 +18,35 @@ import java.util.List;
@Repository @Repository
public interface ReadingSessionRepository extends JpaRepository<ReadingSessionEntity, Long> { public interface ReadingSessionRepository extends JpaRepository<ReadingSessionEntity, Long> {
@Query(""" @Query(value = """
SELECT CAST(rs.startTime AS LocalDate) as date, COUNT(rs) as count SELECT DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as date,
FROM ReadingSessionEntity rs COUNT(*) as count
WHERE rs.user.id = :userId FROM reading_sessions
AND YEAR(rs.startTime) = :year WHERE user_id = :userId
GROUP BY CAST(rs.startTime AS LocalDate) AND YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year
GROUP BY DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset))
ORDER BY date ORDER BY date
""") """, nativeQuery = true)
List<ReadingSessionCountDto> findSessionCountsByUserAndYear(@Param("userId") Long userId, @Param("year") int year); List<ReadingSessionCountDto> findSessionCountsByUserAndYear(
@Param("userId") Long userId,
@Param("year") int year,
@Param("tzOffset") String tzOffset);
@Query(""" @Query(value = """
SELECT CAST(rs.startTime AS LocalDate) as date, COUNT(rs) as count SELECT DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as date,
FROM ReadingSessionEntity rs COUNT(*) as count
WHERE rs.user.id = :userId FROM reading_sessions
AND YEAR(rs.startTime) = :year WHERE user_id = :userId
AND MONTH(rs.startTime) = :month AND YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year
GROUP BY CAST(rs.startTime AS LocalDate) AND MONTH(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :month
GROUP BY DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset))
ORDER BY date ORDER BY date
""") """, nativeQuery = true)
List<ReadingSessionCountDto> findSessionCountsByUserAndYearAndMonth( List<ReadingSessionCountDto> findSessionCountsByUserAndYearAndMonth(
@Param("userId") Long userId, @Param("userId") Long userId,
@Param("year") int year, @Param("year") int year,
@Param("month") int month); @Param("month") int month,
@Param("tzOffset") String tzOffset);
@Query(""" @Query("""
SELECT SELECT
@@ -79,39 +85,41 @@ public interface ReadingSessionRepository extends JpaRepository<ReadingSessionEn
""") """)
List<ReadingSpeedDto> findReadingSpeedByUserAndYear(@Param("userId") Long userId, @Param("year") int year); List<ReadingSpeedDto> findReadingSpeedByUserAndYear(@Param("userId") Long userId, @Param("year") int year);
@Query(""" @Query(value = """
SELECT SELECT
HOUR(rs.startTime) as hourOfDay, HOUR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as hourOfDay,
COUNT(rs) as sessionCount, COUNT(*) as sessionCount,
SUM(rs.durationSeconds) as totalDurationSeconds SUM(duration_seconds) as totalDurationSeconds
FROM ReadingSessionEntity rs FROM reading_sessions
WHERE rs.user.id = :userId WHERE user_id = :userId
AND (:year IS NULL OR YEAR(rs.startTime) = :year) AND (:year IS NULL OR YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year)
AND (:month IS NULL OR MONTH(rs.startTime) = :month) AND (:month IS NULL OR MONTH(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :month)
GROUP BY HOUR(rs.startTime) GROUP BY HOUR(CONVERT_TZ(start_time, '+00:00', :tzOffset))
ORDER BY hourOfDay ORDER BY hourOfDay
""") """, nativeQuery = true)
List<PeakReadingHourDto> findPeakReadingHoursByUser( List<PeakReadingHourDto> findPeakReadingHoursByUser(
@Param("userId") Long userId, @Param("userId") Long userId,
@Param("year") Integer year, @Param("year") Integer year,
@Param("month") Integer month); @Param("month") Integer month,
@Param("tzOffset") String tzOffset);
@Query(""" @Query(value = """
SELECT SELECT
DAYOFWEEK(rs.startTime) as dayOfWeek, DAYOFWEEK(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as dayOfWeek,
COUNT(rs) as sessionCount, COUNT(*) as sessionCount,
COALESCE(SUM(rs.durationSeconds), 0) as totalDurationSeconds COALESCE(SUM(duration_seconds), 0) as totalDurationSeconds
FROM ReadingSessionEntity rs FROM reading_sessions
WHERE rs.user.id = :userId WHERE user_id = :userId
AND (:year IS NULL OR YEAR(rs.startTime) = :year) AND (:year IS NULL OR YEAR(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :year)
AND (:month IS NULL OR MONTH(rs.startTime) = :month) AND (:month IS NULL OR MONTH(CONVERT_TZ(start_time, '+00:00', :tzOffset)) = :month)
GROUP BY DAYOFWEEK(rs.startTime) GROUP BY DAYOFWEEK(CONVERT_TZ(start_time, '+00:00', :tzOffset))
ORDER BY dayOfWeek ORDER BY dayOfWeek
""") """, nativeQuery = true)
List<FavoriteReadingDayDto> findFavoriteReadingDaysByUser( List<FavoriteReadingDayDto> findFavoriteReadingDaysByUser(
@Param("userId") Long userId, @Param("userId") Long userId,
@Param("year") Integer year, @Param("year") Integer year,
@Param("month") Integer month); @Param("month") Integer month,
@Param("tzOffset") String tzOffset);
@Query(""" @Query("""
SELECT SELECT
@@ -179,12 +187,15 @@ public interface ReadingSessionRepository extends JpaRepository<ReadingSessionEn
@Param("userId") Long userId, @Param("userId") Long userId,
@Param("year") int year); @Param("year") int year);
@Query(""" @Query(value = """
SELECT CAST(rs.startTime AS LocalDate) as date, COUNT(rs) as count SELECT DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset)) as date,
FROM ReadingSessionEntity rs COUNT(*) as count
WHERE rs.user.id = :userId FROM reading_sessions
GROUP BY CAST(rs.startTime AS LocalDate) WHERE user_id = :userId
GROUP BY DATE(CONVERT_TZ(start_time, '+00:00', :tzOffset))
ORDER BY date ORDER BY date
""") """, nativeQuery = true)
List<ReadingSessionCountDto> findAllSessionCountsByUser(@Param("userId") Long userId); List<ReadingSessionCountDto> findAllSessionCountsByUser(
@Param("userId") Long userId,
@Param("tzOffset") String tzOffset);
} }
@@ -41,6 +41,7 @@ import java.time.Instant;
import java.time.LocalDate; import java.time.LocalDate;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit; import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalAdjusters; import java.time.temporal.TemporalAdjusters;
import java.time.temporal.WeekFields; import java.time.temporal.WeekFields;
@@ -58,6 +59,11 @@ public class ReadingSessionService {
private final UserRepository userRepository; private final UserRepository userRepository;
private final UserBookProgressRepository userBookProgressRepository; 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 @Transactional
public void recordSession(ReadingSessionRequest request) { public void recordSession(ReadingSessionRequest request) {
BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser();
@@ -91,7 +97,7 @@ public class ReadingSessionService {
BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser();
Long userId = authenticatedUser.getId(); Long userId = authenticatedUser.getId();
return readingSessionRepository.findSessionCountsByUserAndYear(userId, year) return readingSessionRepository.findSessionCountsByUserAndYear(userId, year, getTimezoneOffset())
.stream() .stream()
.map(dto -> ReadingSessionHeatmapResponse.builder() .map(dto -> ReadingSessionHeatmapResponse.builder()
.date(dto.getDate()) .date(dto.getDate())
@@ -105,7 +111,7 @@ public class ReadingSessionService {
BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser();
Long userId = authenticatedUser.getId(); Long userId = authenticatedUser.getId();
return readingSessionRepository.findSessionCountsByUserAndYearAndMonth(userId, year, month) return readingSessionRepository.findSessionCountsByUserAndYearAndMonth(userId, year, month, getTimezoneOffset())
.stream() .stream()
.map(dto -> ReadingSessionHeatmapResponse.builder() .map(dto -> ReadingSessionHeatmapResponse.builder()
.date(dto.getDate()) .date(dto.getDate())
@@ -158,7 +164,7 @@ public class ReadingSessionService {
BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser();
Long userId = authenticatedUser.getId(); Long userId = authenticatedUser.getId();
return readingSessionRepository.findPeakReadingHoursByUser(userId, year, month) return readingSessionRepository.findPeakReadingHoursByUser(userId, year, month, getTimezoneOffset())
.stream() .stream()
.map(dto -> PeakReadingHoursResponse.builder() .map(dto -> PeakReadingHoursResponse.builder()
.hourOfDay(dto.getHourOfDay()) .hourOfDay(dto.getHourOfDay())
@@ -175,7 +181,7 @@ public class ReadingSessionService {
String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; String[] dayNames = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
return readingSessionRepository.findFavoriteReadingDaysByUser(userId, year, month) return readingSessionRepository.findFavoriteReadingDaysByUser(userId, year, month, getTimezoneOffset())
.stream() .stream()
.map(dto -> FavoriteReadingDaysResponse.builder() .map(dto -> FavoriteReadingDaysResponse.builder()
.dayOfWeek(dto.getDayOfWeek()) .dayOfWeek(dto.getDayOfWeek())
@@ -414,7 +420,7 @@ public class ReadingSessionService {
BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser(); BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser();
Long userId = authenticatedUser.getId(); Long userId = authenticatedUser.getId();
return readingSessionRepository.findAllSessionCountsByUser(userId) return readingSessionRepository.findAllSessionCountsByUser(userId, getTimezoneOffset())
.stream() .stream()
.map(dto -> ReadingSessionHeatmapResponse.builder() .map(dto -> ReadingSessionHeatmapResponse.builder()
.date(dto.getDate()) .date(dto.getDate())