-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Leaderboard: Custom Sliding Time Window #100
Changes from 3 commits
2504b75
3ab086f
833a340
b83c893
00ed5c4
171a58b
ad71f16
dbf2b02
cc94b30
81fedd1
a62318d
4136de1
9034afc
b76ed6d
d592150
63c434d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.concurrent.atomic.AtomicInteger; | ||
import java.util.stream.Collectors; | ||
|
@@ -36,14 +37,17 @@ public LeaderboardService(UserService userService) { | |
this.userService = userService; | ||
} | ||
|
||
public List<LeaderboardEntry> createLeaderboard() { | ||
public List<LeaderboardEntry> createLeaderboard(Optional<OffsetDateTime> before, Optional<OffsetDateTime> after) { | ||
logger.info("Creating leaderboard dataset"); | ||
|
||
List<User> users = userService.getAllUsers(); | ||
logger.info("Leaderboard has " + users.size() + " users"); | ||
|
||
OffsetDateTime cutOffTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * timeframe) | ||
.toInstant().atOffset(ZoneOffset.UTC); | ||
OffsetDateTime afterCutOff = after.isPresent() ? after.get() | ||
: new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * timeframe).toInstant() | ||
.atOffset(ZoneOffset.UTC); | ||
|
||
logger.info("Leaderboard has " + users.size() + " users with cut-off time " + afterCutOff + " and before time " | ||
+ before); | ||
|
||
List<LeaderboardEntry> leaderboard = users.stream().map(user -> { | ||
if (user.getType() != UserType.USER) { | ||
|
@@ -55,8 +59,8 @@ public List<LeaderboardEntry> createLeaderboard() { | |
Set<PullRequestReviewDTO> commentSet = new HashSet<>(); | ||
|
||
user.getReviews().stream() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if I understand your idea correctly, you would like to do the filtering in the SQL-Query already? That's a great idea for performance improvements, I will take a look at it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried out a Query like this: SELECT u
FROM User u
JOIN FETCH u.pullRequests
JOIN FETCH u.issueComments
JOIN FETCH u.reviewComments
JOIN FETCH u.reviews re
WHERE re.createdAt between :after and :before
OR re.updatedAt between :after and :before but it doesn't seem to return any results, most likely I'm missing something regarding the format conversion. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Managed to find the correct SQL query to filter the dates. |
||
.filter(review -> (review.getCreatedAt() != null && review.getCreatedAt().isAfter(cutOffTime)) | ||
|| (review.getUpdatedAt() != null && review.getUpdatedAt().isAfter(cutOffTime))) | ||
.filter(review -> isInTimeframe(review.getCreatedAt(), before, afterCutOff) | ||
|| isInTimeframe(review.getUpdatedAt(), before, afterCutOff)) | ||
.forEach(review -> { | ||
if (review.getPullRequest().getAuthor().getLogin().equals(user.getLogin())) { | ||
return; | ||
|
@@ -99,6 +103,10 @@ public List<LeaderboardEntry> createLeaderboard() { | |
return leaderboard; | ||
} | ||
|
||
private boolean isInTimeframe(OffsetDateTime date, Optional<OffsetDateTime> before, OffsetDateTime after) { | ||
GODrums marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return date != null && (before.isPresent() && date.isAfter(before.get()) || date.isBefore(after)); | ||
} | ||
|
||
/** | ||
* Calculates the score for a given pull request. | ||
* Possible values: 1, 3, 7, 17, 33. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should round to the beginning of the day?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or what happens with
2024-09-10
? I guess it will use midnight, then it might be important for theafter
case. If you for example use2024-09-15
asbefore
I think it should get all timestamps before the end of day. So actually we would need to round up?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If no timestamp is provided, it should default to midnight, yes.
While I personally find different default timestamps for
before
andafter
confusing, I do see why this would be expected behavior for users. Will change it 👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had a look through Java best practices and found this sheet: https://i.sstatic.net/WyAl2.png
Equivalently to your idea, after and before are now only dates (
LocalDate
- without timestamp) with an interpretation as the start and end of the day on the server respectively. Also looks much cleaner in the URL (example: http://localhost:4200/?after=2024-09-21&before=2024-09-23).Implemented this change in b76ed6d.