From b76ed6dd13098a415d894cefe82d0188814a333e Mon Sep 17 00:00:00 2001 From: GODrums Date: Sun, 22 Sep 2024 01:50:23 +0200 Subject: [PATCH] Remove timestamp from leaderboard dates --- .../leaderboard/LeaderboardController.java | 8 ++++--- .../leaderboard/LeaderboardService.java | 22 ++++++++++--------- webapp/src/app/home/home.component.ts | 6 ++--- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardController.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardController.java index eb4ff149..12d7d922 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardController.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardController.java @@ -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; @@ -21,8 +22,9 @@ public LeaderboardController(LeaderboardService leaderboardService) { } @GetMapping - public ResponseEntity> getLeaderboard(@RequestParam Optional after, - @RequestParam Optional before) { + public ResponseEntity> getLeaderboard( + @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Optional after, + @RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Optional before) { return ResponseEntity.ok(leaderboardService.createLeaderboard(after, before)); } } diff --git a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardService.java b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardService.java index 1c4de0d2..852ac6b7 100644 --- a/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardService.java +++ b/server/application-server/src/main/java/de/tum/in/www1/hephaestus/leaderboard/LeaderboardService.java @@ -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; @@ -37,17 +38,17 @@ public LeaderboardService(UserService userService) { this.userService = userService; } - public List createLeaderboard(Optional after, Optional before) { + public List createLeaderboard(Optional after, Optional before) { logger.info("Creating leaderboard dataset"); List 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 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 leaderboard = users.stream().map(user -> { if (user.getType() != UserType.USER) { @@ -59,12 +60,12 @@ public List createLeaderboard(Optional after, Set 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()); @@ -103,8 +104,9 @@ public List createLeaderboard(Optional after, return leaderboard; } - private boolean isInTimeframe(OffsetDateTime date, OffsetDateTime after, Optional before) { - return date != null && (before.isPresent() && date.isAfter(before.get()) || date.isBefore(after)); + private boolean isInTimeframe(OffsetDateTime date, LocalDateTime after, Optional before) { + return date != null && (before.isPresent() && date.isBefore(before.get().atOffset(ZoneOffset.UTC)) + || date.isAfter(after.atOffset(ZoneOffset.UTC))); } /** diff --git a/webapp/src/app/home/home.component.ts b/webapp/src/app/home/home.component.ts index 6f43fea2..09a5efa3 100644 --- a/webapp/src/app/home/home.component.ts +++ b/webapp/src/app/home/home.component.ts @@ -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() }],