Skip to content

Commit

Permalink
Add time to leaderboard date
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Nov 8, 2024
1 parent d7bf83a commit e424ed0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 30 deletions.
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.OffsetDateTime;
import java.util.List;
import java.util.Optional;

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

@GetMapping
public ResponseEntity<List<LeaderboardEntryDTO>> getLeaderboard(
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Optional<LocalDate> after,
@RequestParam @DateTimeFormat(pattern = "yyyy-MM-dd") Optional<LocalDate> before,
@RequestParam @DateTimeFormat(iso = ISO.DATE_TIME) Optional<OffsetDateTime> after,
@RequestParam @DateTimeFormat(iso = ISO.DATE_TIME) Optional<OffsetDateTime> before,
@RequestParam Optional<String> repository) {
return ResponseEntity.ok(leaderboardService.createLeaderboard(after, before, repository));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.stream.IntStream;
import java.util.Map;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Comparator;
Expand Down Expand Up @@ -43,16 +42,12 @@ public LeaderboardService(
}

@Transactional
public List<LeaderboardEntryDTO> createLeaderboard(Optional<LocalDate> after, Optional<LocalDate> before,
public List<LeaderboardEntryDTO> createLeaderboard(Optional<OffsetDateTime> after, Optional<OffsetDateTime> before,
Optional<String> repository) {
logger.info("Creating leaderboard dataset");

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

var afterOffset = afterCutOff.atOffset(ZoneOffset.UTC);
var beforeOffset = beforeCutOff.map(b -> b.atOffset(ZoneOffset.UTC)).orElse(OffsetDateTime.now());
var afterOffset = after.orElse(LocalDate.now().minusDays(timeframe).atStartOfDay().atOffset(ZoneOffset.UTC));
var beforeOffset = before.orElse(OffsetDateTime.now());
logger.info("Timeframe: {} - {}", afterOffset, beforeOffset);
List<PullRequestReview> reviews = pullRequestReviewRepository.findAllInTimeframe(afterOffset, beforeOffset,
repository);
List<IssueComment> issueComments = issueCommentRepository.findAllInTimeframe(afterOffset, beforeOffset,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand Down Expand Up @@ -70,9 +70,10 @@ public void sendMessage(String channelId, List<LayoutBlock> blocks, String fallb
}

private List<User> getTop3SlackReviewers() {
LocalDate after = LocalDate.now().minusDays(9);
LocalDate before = LocalDate.now().minusDays(3);
var leaderboard = leaderboardService.createLeaderboard(Optional.of(after), Optional.of(before),
// exactly 7 days ago
OffsetDateTime after = OffsetDateTime.of(LocalDate.now().minusDays(7), OffsetDateTime.now().toLocalTime(),
OffsetDateTime.now().getOffset());
var leaderboard = leaderboardService.createLeaderboard(Optional.of(after), Optional.empty(),
Optional.empty());
var top3 = leaderboard.subList(0, Math.min(3, leaderboard.size()));
logger.debug("Top 3 Users of the last week: " + top3.stream().map(e -> e.user().name()).toList());
Expand Down Expand Up @@ -109,7 +110,7 @@ public void sendScheduledLeaderboard() {
}

// get date in unix format
var currentDate = LocalDateTime.now().toEpochSecond(java.time.ZoneOffset.UTC);
var currentDate = OffsetDateTime.now().toEpochSecond();

var top3reviewers = getTop3SlackReviewers();

Expand Down
4 changes: 2 additions & 2 deletions webapp/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ export class HomeComponent {

private readonly route = inject(ActivatedRoute);
private queryParams = toSignal(this.route.queryParamMap, { requireSync: true });
protected after = computed(() => this.queryParams().get('after') ?? dayjs().isoWeekday(1).format('YYYY-MM-DD'));
protected before = computed(() => this.queryParams().get('before') ?? dayjs().format('YYYY-MM-DD'));
protected after = computed(() => this.queryParams().get('after') ?? dayjs().isoWeekday(1).startOf('hour').hour(9).format());
protected before = computed(() => this.queryParams().get('before') ?? dayjs().format());
protected repository = computed(() => this.queryParams().get('repository') ?? 'all');

query = injectQuery(() => ({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, computed, effect, signal } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { Router, RouterLink } from '@angular/router';
import { Router } from '@angular/router';
import dayjs from 'dayjs';
import isoWeek from 'dayjs/plugin/isoWeek';
import { BrnSelectModule } from '@spartan-ng/ui-select-brain';
Expand All @@ -18,21 +18,21 @@ dayjs.extend(isoWeek);
function formatLabel(startDate: dayjs.Dayjs, endDate: dayjs.Dayjs | undefined) {
const calendarWeek = startDate.isoWeek();
if (!endDate || endDate.isSame(dayjs(), 'day')) {
return `CW\xa0${calendarWeek}:\xa0${startDate.format('MMM D')}\xa0-\xa0Today`;
return `CW\xa0${calendarWeek}:\xa0${startDate.format('MMM D, h[am]')}\xa0-\xa0Today`;
}

const sameMonth = startDate.month() === endDate.month();
if (sameMonth) {
return `CW\xa0${calendarWeek}:\xa0${startDate.format('MMM D')}\xa0-\xa0${endDate.format('D')}`;
return `CW\xa0${calendarWeek}:\xa0${startDate.format('MMM D, h[am]')}\xa0-\xa0${endDate.format('D, h[am]')}`;
} else {
return `CW\xa0${calendarWeek}:\xa0${startDate.format('MMM D')}\xa0-\xa0${endDate.format('MMM D')}`;
return `CW\xa0${calendarWeek}:\xa0${startDate.format('MMM D, h[am]')}\xa0-\xa0${endDate.format('MMM D, h[am]')}`;
}
}

@Component({
selector: 'app-leaderboard-filter-timeframe',
standalone: true,
imports: [RouterLink, BrnSelectModule, HlmSelectModule, HlmLabelModule, FormsModule],
imports: [BrnSelectModule, HlmSelectModule, HlmLabelModule, FormsModule],
templateUrl: './timeframe.component.html'
})
export class LeaderboardFilterTimeframeComponent {
Expand All @@ -41,16 +41,16 @@ export class LeaderboardFilterTimeframeComponent {
value = signal<string>(`${this.after()}.${this.before()}`);

placeholder = computed(() => {
return formatLabel(dayjs(this.after()) ?? dayjs().day(1), !this.before() ? undefined : dayjs(this.before()));
return formatLabel(dayjs(this.after()) ?? dayjs().day(2).startOf('hour').hour(9), !this.before() ? undefined : dayjs(this.before()));
});

options = computed(() => {
const now = dayjs();
let currentDate = dayjs().isoWeekday(1);
let currentDate = dayjs().isoWeekday(2).startOf('hour').hour(9);
const options: SelectOption[] = [
{
id: now.unix(),
value: `${currentDate.format('YYYY-MM-DD')}.${now.format('YYYY-MM-DD')}`,
value: `${currentDate.format()}.${now.format()}`,
label: formatLabel(currentDate, undefined)
}
];
Expand All @@ -60,7 +60,7 @@ export class LeaderboardFilterTimeframeComponent {
const endDate = currentDate.subtract(1, 'day');
options.push({
id: startDate.unix(),
value: `${startDate.format('YYYY-MM-DD')}.${endDate.format('YYYY-MM-DD')}`,
value: `${startDate.format()}.${endDate.format()}`,
label: formatLabel(startDate, endDate)
});
currentDate = startDate;
Expand All @@ -72,8 +72,8 @@ export class LeaderboardFilterTimeframeComponent {
constructor(private router: Router) {
// init params
const queryParams = this.router.parseUrl(this.router.url).queryParams;
this.after.set(queryParams['after'] ?? dayjs().isoWeekday(1).format('YYYY-MM-DD'));
this.before.set(queryParams['before'] ?? dayjs().format('YYYY-MM-DD'));
this.after.set(queryParams['after'] ?? dayjs().isoWeekday(2).hour(9).format());
this.before.set(queryParams['before'] ?? dayjs().format());

// persist changes in url
effect(() => {
Expand Down

0 comments on commit e424ed0

Please sign in to comment.