feat: add book distributions endpoint for mobile stats (#2855)

* feat: add book distributions endpoint for mobile stats

Add GET /api/v1/user-stats/book-distributions returning rating,
reading progress, and read status distributions. Progress percentages
are coalesced across all sync sources and bucketed into 6 ranges.

* fix(test): use stable date for thisPeriod_week test to avoid Monday boundary flake

The test used Instant.now().minus(1 day) which falls in the previous
ISO week when CI runs on Monday (UTC). Use a date guaranteed to be
within the current week instead.

---------

Co-authored-by: acx10 <acx10@users.noreply.github.com>
This commit is contained in:
ACX
2026-02-22 19:29:21 -07:00
committed by GitHub
co-authored by acx10
parent 4e14b7a8e8
commit 88622a66dd
8 changed files with 202 additions and 4 deletions
@@ -1481,9 +1481,15 @@ class BookRuleEvaluatorServiceIntegrationTest {
@Test
void thisPeriod_week_matchesThisWeekBook() {
BookEntity yesterday = createBook("Yesterday Book");
yesterday.setAddedOn(Instant.now().minus(1, ChronoUnit.DAYS));
em.merge(yesterday);
// Use midweek (Wednesday) of the current week to avoid boundary issues on Monday
LocalDate thisWeekWednesday = LocalDate.now().with(java.time.temporal.TemporalAdjusters.previousOrSame(java.time.DayOfWeek.MONDAY)).plusDays(2);
// If Wednesday is in the future (we're Mon/Tue), just use today
LocalDate safeDate = thisWeekWednesday.isAfter(LocalDate.now()) ? LocalDate.now() : thisWeekWednesday;
Instant thisWeekInstant = safeDate.atStartOfDay(java.time.ZoneId.systemDefault()).toInstant().plus(12, ChronoUnit.HOURS);
BookEntity thisWeekBook = createBook("This Week Book");
thisWeekBook.setAddedOn(thisWeekInstant);
em.merge(thisWeekBook);
BookEntity twoWeeksAgo = createBook("Two Weeks Ago Book");
twoWeeksAgo.setAddedOn(Instant.now().minus(14, ChronoUnit.DAYS));
@@ -1492,7 +1498,7 @@ class BookRuleEvaluatorServiceIntegrationTest {
em.clear();
List<Long> ids = findMatchingIds(singleRule(RuleField.ADDED_ON, RuleOperator.THIS_PERIOD, "week"));
assertThat(ids).contains(yesterday.getId());
assertThat(ids).contains(thisWeekBook.getId());
assertThat(ids).doesNotContain(twoWeeksAgo.getId());
}
}