Skip to content

Commit

Permalink
Time filter directly in SQL
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Sep 22, 2024
1 parent d592150 commit 63c434d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.in.www1.hephaestus.codereview.user;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -39,4 +40,12 @@ SELECT new UserDTO(u.id, u.login, u.email, u.name, u.url)
JOIN FETCH u.reviews
""")
List<User> findAllWithRelations();

@Query("""
SELECT u
FROM User u
JOIN FETCH u.reviews re
WHERE re.createdAt BETWEEN :after AND :before
""")
List<User> findAllInTimeframe(@Param("after") OffsetDateTime after, @Param("before") OffsetDateTime before);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.tum.in.www1.hephaestus.codereview.user;

import java.time.OffsetDateTime;
import java.util.List;
import java.util.Optional;

Expand Down Expand Up @@ -31,4 +32,9 @@ public List<User> getAllUsers() {
logger.info("Getting all users");
return userRepository.findAll().stream().toList();
}

public List<User> getAllUsersInTimeframe(OffsetDateTime after, OffsetDateTime before) {
logger.info("Getting all users in timeframe between " + after + " and " + before);
return userRepository.findAllInTimeframe(after, before);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,17 @@ public LeaderboardService(UserService userService) {
public List<LeaderboardEntry> createLeaderboard(Optional<LocalDate> after, Optional<LocalDate> before) {
logger.info("Creating leaderboard dataset");

List<User> users = userService.getAllUsers();

LocalDateTime afterCutOff = after.isPresent() ? after.get().atStartOfDay()
: LocalDate.now().minusDays(timeframe).atStartOfDay();
Optional<LocalDateTime> beforeCutOff = before.map(date -> date.plusDays(1).atStartOfDay());

logger.info("Leaderboard has " + users.size() + " users with cut-off time " + afterCutOff + " and before time "
+ beforeCutOff);
List<User> users = userService.getAllUsersInTimeframe(afterCutOff.atOffset(ZoneOffset.UTC),
beforeCutOff.map(b -> b.atOffset(ZoneOffset.UTC)).orElse(OffsetDateTime.now()));

logger.info("Found " + users.size() + " users for the leaderboard");

List<LeaderboardEntry> leaderboard = users.stream().map(user -> {
// ignore non-users, e.g. bots
if (user.getType() != UserType.USER) {
return null;
}
Expand All @@ -60,7 +61,6 @@ public List<LeaderboardEntry> createLeaderboard(Optional<LocalDate> after, Optio
Set<PullRequestReviewDTO> commentSet = new HashSet<>();

user.getReviews().stream()
.filter(review -> isInTimeframe(review.getCreatedAt(), afterCutOff, beforeCutOff))
.forEach(review -> {
if (review.getPullRequest().getAuthor().getLogin().equals(user.getLogin())) {
return;
Expand Down Expand Up @@ -104,11 +104,6 @@ public List<LeaderboardEntry> createLeaderboard(Optional<LocalDate> after, Optio
return leaderboard;
}

private boolean isInTimeframe(OffsetDateTime date, LocalDateTime after, Optional<LocalDateTime> before) {
return date != null && (before.isPresent() && date.isBefore(before.get().atOffset(ZoneOffset.UTC))
|| date.isAfter(after.atOffset(ZoneOffset.UTC)));
}

/**
* Calculates the score for a given pull request.
* Possible values: 1, 3, 7, 17, 33.
Expand Down

0 comments on commit 63c434d

Please sign in to comment.