Add 10 advanced stats charts (#2850)
* Add 10 advanced stats charts with new API endpoints and visualizations * Fix chart rendering and visual readability across all new stats charts * Remove 6 charts, fix session archetypes outliers, make book flow half-width * Revert book flow to full width * Remove avg rating from book flow chart, reorder charts, cleanup unused imports
This commit is contained in:
@@ -171,4 +171,16 @@ public class UserStatsController {
|
||||
List<ReadingSessionHeatmapResponse> data = readingSessionService.getReadingDates();
|
||||
return ResponseEntity.ok(data);
|
||||
}
|
||||
|
||||
@Operation(summary = "Get session scatter data", description = "Returns individual session data points for scatter plot visualization")
|
||||
@ApiResponses({
|
||||
@ApiResponse(responseCode = "200", description = "Session scatter data retrieved successfully"),
|
||||
@ApiResponse(responseCode = "401", description = "Unauthorized")
|
||||
})
|
||||
@GetMapping("/session-scatter")
|
||||
@PreAuthorize("@securityUtil.canAccessUserStats() or @securityUtil.isAdmin()")
|
||||
public ResponseEntity<List<SessionScatterResponse>> getSessionScatter(@RequestParam int year) {
|
||||
List<SessionScatterResponse> data = readingSessionService.getSessionScatter(year);
|
||||
return ResponseEntity.ok(data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package org.booklore.model.dto;
|
||||
|
||||
public interface SessionScatterDto {
|
||||
Double getHourOfDay();
|
||||
Double getDurationMinutes();
|
||||
Integer getDayOfWeek();
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
package org.booklore.model.dto.response;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class SessionScatterResponse {
|
||||
private Double hourOfDay;
|
||||
private Double durationMinutes;
|
||||
private Integer dayOfWeek;
|
||||
}
|
||||
@@ -1,8 +1,6 @@
|
||||
package org.booklore.repository;
|
||||
|
||||
import org.booklore.model.dto.*;
|
||||
import org.booklore.model.dto.CompletionRaceSessionDto;
|
||||
import org.booklore.model.dto.PageTurnerSessionDto;
|
||||
|
||||
import org.booklore.model.entity.ReadingSessionEntity;
|
||||
import org.springframework.data.domain.Page;
|
||||
@@ -198,4 +196,21 @@ public interface ReadingSessionRepository extends JpaRepository<ReadingSessionEn
|
||||
List<ReadingSessionCountDto> findAllSessionCountsByUser(
|
||||
@Param("userId") Long userId,
|
||||
@Param("tzOffset") String tzOffset);
|
||||
|
||||
@Query(value = """
|
||||
SELECT
|
||||
HOUR(CONVERT_TZ(rs.start_time, '+00:00', :tzOffset))
|
||||
+ MINUTE(CONVERT_TZ(rs.start_time, '+00:00', :tzOffset)) / 60.0 as hourOfDay,
|
||||
rs.duration_seconds / 60.0 as durationMinutes,
|
||||
DAYOFWEEK(CONVERT_TZ(rs.start_time, '+00:00', :tzOffset)) as dayOfWeek
|
||||
FROM reading_sessions rs
|
||||
WHERE rs.user_id = :userId
|
||||
AND YEAR(CONVERT_TZ(rs.start_time, '+00:00', :tzOffset)) = :year
|
||||
ORDER BY rs.start_time DESC
|
||||
LIMIT 500
|
||||
""", nativeQuery = true)
|
||||
List<SessionScatterDto> findSessionScatterByUserAndYear(
|
||||
@Param("userId") Long userId,
|
||||
@Param("year") int year,
|
||||
@Param("tzOffset") String tzOffset);
|
||||
}
|
||||
|
||||
@@ -6,18 +6,7 @@ import org.booklore.model.dto.BookLoreUser;
|
||||
import org.booklore.model.dto.CompletionRaceSessionDto;
|
||||
import org.booklore.model.dto.request.ReadingSessionRequest;
|
||||
import org.booklore.model.dto.PageTurnerSessionDto;
|
||||
import org.booklore.model.dto.response.BookCompletionHeatmapResponse;
|
||||
import org.booklore.model.dto.response.CompletionRaceResponse;
|
||||
import org.booklore.model.dto.response.CompletionTimelineResponse;
|
||||
import org.booklore.model.dto.response.FavoriteReadingDaysResponse;
|
||||
import org.booklore.model.dto.response.GenreStatisticsResponse;
|
||||
import org.booklore.model.dto.response.PageTurnerScoreResponse;
|
||||
import org.booklore.model.dto.response.PeakReadingHoursResponse;
|
||||
|
||||
import org.booklore.model.dto.response.ReadingSessionHeatmapResponse;
|
||||
import org.booklore.model.dto.response.ReadingSessionResponse;
|
||||
import org.booklore.model.dto.response.ReadingSessionTimelineResponse;
|
||||
import org.booklore.model.dto.response.ReadingSpeedResponse;
|
||||
import org.booklore.model.dto.response.*;
|
||||
import org.booklore.model.entity.BookEntity;
|
||||
import org.booklore.model.entity.BookLoreUserEntity;
|
||||
import org.booklore.model.entity.CategoryEntity;
|
||||
@@ -429,6 +418,21 @@ public class ReadingSessionService {
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Transactional(readOnly = true)
|
||||
public List<SessionScatterResponse> getSessionScatter(int year) {
|
||||
BookLoreUser authenticatedUser = authenticationService.getAuthenticatedUser();
|
||||
Long userId = authenticatedUser.getId();
|
||||
|
||||
return readingSessionRepository.findSessionScatterByUserAndYear(userId, year, getTimezoneOffset())
|
||||
.stream()
|
||||
.map(dto -> SessionScatterResponse.builder()
|
||||
.hourOfDay(dto.getHourOfDay())
|
||||
.durationMinutes(dto.getDurationMinutes())
|
||||
.dayOfWeek(dto.getDayOfWeek())
|
||||
.build())
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
private double linearRegressionSlope(List<Double> values) {
|
||||
int n = values.size();
|
||||
if (n < 2) return 0.0;
|
||||
|
||||
Reference in New Issue
Block a user