Skip to content
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

Merged
merged 16 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions server/application-server/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,13 @@ paths:
- leaderboard
operationId: getLeaderboard
parameters:
- name: before
- name: after
in: query
required: false
schema:
type: string
format: date-time
- name: after
- name: before
in: query
required: false
schema:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public LeaderboardController(LeaderboardService leaderboardService) {
}

@GetMapping
public ResponseEntity<List<LeaderboardEntry>> getLeaderboard(@RequestParam Optional<OffsetDateTime> before,
@RequestParam Optional<OffsetDateTime> after) {
return ResponseEntity.ok(leaderboardService.createLeaderboard(before, after));
public ResponseEntity<List<LeaderboardEntry>> getLeaderboard(@RequestParam Optional<OffsetDateTime> after,
@RequestParam Optional<OffsetDateTime> before) {
return ResponseEntity.ok(leaderboardService.createLeaderboard(after, before));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public LeaderboardService(UserService userService) {
this.userService = userService;
}

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

List<User> users = userService.getAllUsers();
Expand All @@ -59,8 +59,8 @@ public List<LeaderboardEntry> createLeaderboard(Optional<OffsetDateTime> before,
Set<PullRequestReviewDTO> commentSet = new HashSet<>();

user.getReviews().stream()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think user.getReviews could already support querying by timeframe.

Copy link
Collaborator Author

@GODrums GODrums Sep 20, 2024

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Managed to find the correct SQL query to filter the dates.
With 63c434d, we now filter directly in the DB.

.filter(review -> isInTimeframe(review.getCreatedAt(), before, afterCutOff)
|| isInTimeframe(review.getUpdatedAt(), before, afterCutOff))
.filter(review -> isInTimeframe(review.getCreatedAt(), afterCutOff, before)
|| isInTimeframe(review.getUpdatedAt(), afterCutOff, before))
.forEach(review -> {
if (review.getPullRequest().getAuthor().getLogin().equals(user.getLogin())) {
return;
Expand Down Expand Up @@ -103,7 +103,7 @@ public List<LeaderboardEntry> createLeaderboard(Optional<OffsetDateTime> before,
return leaderboard;
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ springdoc:
monitoring:
runOnStartup: true
# Fetching timeframe in days
timeframe: 1
timeframe: 7
repository-sync-cron: "0 0 * * * *"
repositories: ls1intum/Artemis

Expand Down
18 changes: 9 additions & 9 deletions webapp/src/app/core/modules/openapi/api/leaderboard.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,25 @@ export class LeaderboardService implements LeaderboardServiceInterface {
}

/**
* @param before
* @param after
* @param before
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public getLeaderboard(before?: string, after?: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<Array<LeaderboardEntry>>;
public getLeaderboard(before?: string, after?: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpResponse<Array<LeaderboardEntry>>>;
public getLeaderboard(before?: string, after?: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpEvent<Array<LeaderboardEntry>>>;
public getLeaderboard(before?: string, after?: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<any> {
public getLeaderboard(after?: string, before?: string, observe?: 'body', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<Array<LeaderboardEntry>>;
public getLeaderboard(after?: string, before?: string, observe?: 'response', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpResponse<Array<LeaderboardEntry>>>;
public getLeaderboard(after?: string, before?: string, observe?: 'events', reportProgress?: boolean, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<HttpEvent<Array<LeaderboardEntry>>>;
public getLeaderboard(after?: string, before?: string, observe: any = 'body', reportProgress: boolean = false, options?: {httpHeaderAccept?: 'application/json', context?: HttpContext, transferCache?: boolean}): Observable<any> {

let localVarQueryParameters = new HttpParams({encoder: this.encoder});
if (before !== undefined && before !== null) {
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
<any>before, 'before');
}
if (after !== undefined && after !== null) {
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
<any>after, 'after');
}
if (before !== undefined && before !== null) {
localVarQueryParameters = this.addToHttpParams(localVarQueryParameters,
<any>before, 'before');
}

let localVarHeaders = this.defaultHeaders;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ export interface LeaderboardServiceInterface {
/**
*
*
* @param before
* @param after
* @param before
*/
getLeaderboard(before?: string, after?: string, extraHttpRequestParams?: any): Observable<Array<LeaderboardEntry>>;
getLeaderboard(after?: string, before?: string, extraHttpRequestParams?: any): Observable<Array<LeaderboardEntry>>;

}
20 changes: 8 additions & 12 deletions webapp/src/app/home/home.component.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, inject } from '@angular/core';
import { Component, computed, inject } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { injectQuery } from '@tanstack/angular-query-experimental';
import { LeaderboardService } from 'app/core/modules/openapi/api/leaderboard.service';
import { LeaderboardComponent } from 'app/home/leaderboard/leaderboard.component';
import { lastValueFrom } from 'rxjs';
import { toSignal } from '@angular/core/rxjs-interop';

@Component({
selector: 'app-home',
Expand All @@ -16,19 +17,14 @@ export class HomeComponent {

// timeframe for leaderboard
// example: 2024-09-19T10:15:30+01:00
GODrums marked this conversation as resolved.
Show resolved Hide resolved
protected after: string | undefined = undefined;
protected before: string | undefined = undefined;

constructor() {
inject(ActivatedRoute).queryParamMap.subscribe((params) => {
this.after = params.get('after')?.replace(' ', '+') ?? undefined;
this.before = params.get('before')?.replace(' ', '+') ?? undefined;
});
}
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);

query = injectQuery(() => ({
queryKey: ['leaderboard'],
queryFn: async () => lastValueFrom(this.leaderboardService.getLeaderboard(this.before, this.after)),
queryKey: ['leaderboard', { after: this.after, before: this.before }],
GODrums marked this conversation as resolved.
Show resolved Hide resolved
queryFn: async () => lastValueFrom(this.leaderboardService.getLeaderboard(this.after(), this.before())),
gcTime: Infinity
GODrums marked this conversation as resolved.
Show resolved Hide resolved
}));
}