Skip to content

Commit

Permalink
Remove timestamp from leaderboard dates
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Sep 21, 2024
1 parent 9034afc commit b76ed6d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package de.tum.in.www1.hephaestus.leaderboard;

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

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -21,8 +22,9 @@ public LeaderboardController(LeaderboardService leaderboardService) {
}

@GetMapping
public ResponseEntity<List<LeaderboardEntry>> getLeaderboard(@RequestParam Optional<OffsetDateTime> after,
@RequestParam Optional<OffsetDateTime> before) {
public ResponseEntity<List<LeaderboardEntry>> getLeaderboard(
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Optional<LocalDate> after,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Optional<LocalDate> before) {
return ResponseEntity.ok(leaderboardService.createLeaderboard(after, before));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package de.tum.in.www1.hephaestus.leaderboard;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -37,17 +38,17 @@ public LeaderboardService(UserService userService) {
this.userService = userService;
}

public List<LeaderboardEntry> createLeaderboard(Optional<OffsetDateTime> after, Optional<OffsetDateTime> before) {
public List<LeaderboardEntry> createLeaderboard(Optional<LocalDate> after, Optional<LocalDate> before) {
logger.info("Creating leaderboard dataset");

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

OffsetDateTime afterCutOff = after.isPresent() ? after.get()
: new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * timeframe).toInstant()
.atOffset(ZoneOffset.UTC);
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 "
+ before);
+ beforeCutOff);

List<LeaderboardEntry> leaderboard = users.stream().map(user -> {
if (user.getType() != UserType.USER) {
Expand All @@ -59,12 +60,12 @@ public List<LeaderboardEntry> createLeaderboard(Optional<OffsetDateTime> after,
Set<PullRequestReviewDTO> commentSet = new HashSet<>();

user.getReviews().stream()
.filter(review -> isInTimeframe(review.getCreatedAt(), afterCutOff, before)
|| isInTimeframe(review.getUpdatedAt(), afterCutOff, before))
.filter(review -> isInTimeframe(review.getCreatedAt(), afterCutOff, beforeCutOff))
.forEach(review -> {
if (review.getPullRequest().getAuthor().getLogin().equals(user.getLogin())) {
return;
}

PullRequestReviewDTO reviewDTO = new PullRequestReviewDTO(review.getId(), review.getCreatedAt(),
review.getUpdatedAt(), review.getSubmittedAt(), review.getState());

Expand Down Expand Up @@ -103,8 +104,9 @@ public List<LeaderboardEntry> createLeaderboard(Optional<OffsetDateTime> after,
return leaderboard;
}

private boolean isInTimeframe(OffsetDateTime date, OffsetDateTime after, Optional<OffsetDateTime> before) {
return date != null && (before.isPresent() && date.isAfter(before.get()) || date.isBefore(after));
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)));
}

/**
Expand Down
6 changes: 3 additions & 3 deletions webapp/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export class HomeComponent {
leaderboardService = inject(LeaderboardService);

// timeframe for leaderboard
// example: 2024-09-19T10:15:30+01:00
// example: 2024-09-19
private readonly route = inject(ActivatedRoute);
private queryParams = toSignal(this.route.queryParamMap, { requireSync: true });
protected after = computed(() => this.queryParams().get('after')?.replace(' ', '+') ?? undefined);
protected before = computed(() => this.queryParams().get('before')?.replace(' ', '+') ?? undefined);
protected after = computed(() => this.queryParams().get('after') ?? undefined);
protected before = computed(() => this.queryParams().get('before') ?? undefined);

query = injectQuery(() => ({
queryKey: ['leaderboard', { after: this.after(), before: this.before() }],
Expand Down

0 comments on commit b76ed6d

Please sign in to comment.