Skip to content

Commit f97e11c

Browse files
committed
feat: major improvements on efficiency
- Added an internal cache to avoid executing the same query when there are many teams in the same repository. This requests the same pull requests for each team, which translates on fetching the same comments and commits which is a really demanding task. - Added logged so that we are aware of what is being done in the background. We can add an option to make it silent by default. - Other minor improvements.
1 parent 601e245 commit f97e11c

File tree

10 files changed

+67
-33
lines changed

10 files changed

+67
-33
lines changed

src/main/java/io/pakland/mdas/githubstats/domain/entity/Commit.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ public class Commit {
2626
// when adding a commit to a set like structure
2727
@Override
2828
public int hashCode() {
29-
int result = sha.hashCode();
30-
result = 31 * result + (date != null ? date.hashCode() : 0);
31-
result = 31 * result + additions;
32-
result = 31 * result + deletions;
33-
return result;
29+
return sha.hashCode();
3430
}
3531
}

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/model/GitHubPullRequestDTO.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class GitHubPullRequestDTO implements PullRequestDTO {
1616
private Integer number;
1717

1818
@JsonProperty("state")
19-
private PullRequestStateDTO state;
19+
private GitHubPullRequestStateDTO state;
2020

2121
@JsonProperty("closed_at")
2222
private Instant closedAt;

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/model/GitHubPullRequestStateDTO.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import io.pakland.mdas.githubstats.application.dto.PullRequestStateDTO;
55
import io.pakland.mdas.githubstats.domain.enums.PullRequestState;
6+
import lombok.AllArgsConstructor;
67

8+
@AllArgsConstructor
79
public class GitHubPullRequestStateDTO implements PullRequestStateDTO {
810

911
@JsonProperty("state")

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/CommentGitHubRepository.java

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,48 @@
55
import io.pakland.mdas.githubstats.domain.entity.Comment;
66
import io.pakland.mdas.githubstats.domain.repository.CommentExternalRepository;
77
import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubCommentDTO;
8+
import java.util.*;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
1011
import org.springframework.web.reactive.function.client.WebClientResponseException;
1112

12-
import java.util.List;
13-
1413
public class CommentGitHubRepository implements CommentExternalRepository {
14+
1515
private final WebClientConfiguration webClientConfiguration;
1616
private final Logger logger = LoggerFactory.getLogger(CommentGitHubRepository.class);
1717

18+
private final Map<Integer, Integer> internalQueryCache = new HashMap<>();
19+
private final List<List<Comment>> commentStore = new ArrayList<>();
20+
1821
public CommentGitHubRepository(WebClientConfiguration webClientConfiguration) {
1922
this.webClientConfiguration = webClientConfiguration;
2023
}
2124

2225
@Override
2326
public List<Comment> fetchCommentsFromPullRequest(
24-
String repositoryOwner, String repositoryName, Integer pullRequestNumber, Integer page, Integer perPage)
25-
throws HttpException {
27+
String repositoryOwner, String repositoryName, Integer pullRequestNumber, Integer page,
28+
Integer perPage)
29+
throws HttpException {
30+
String query = String.format("/repos/%s/%s/pulls/%s/comments?%s", repositoryOwner,
31+
repositoryName, pullRequestNumber, getRequestParams(page, perPage));
32+
Integer dataPosition = internalQueryCache.get(query.hashCode());
33+
if (dataPosition != null) {
34+
return commentStore.get(dataPosition);
35+
}
2636

2737
try {
28-
29-
return this.webClientConfiguration.getWebClient().get()
30-
.uri(String.format("/repos/%s/%s/pulls/%s/comments?%s", repositoryOwner,
31-
repositoryName, pullRequestNumber, getRequestParams(page, perPage)))
32-
.retrieve()
33-
.bodyToFlux(GitHubCommentDTO.class)
34-
.map(CommentMapper::dtoToEntity)
35-
.collectList()
36-
.block();
38+
logger.info(" - Fetching comments from pull request: " + pullRequestNumber);
39+
List<Comment> result = this.webClientConfiguration.getWebClient().get()
40+
.uri(query)
41+
.retrieve()
42+
.bodyToFlux(GitHubCommentDTO.class)
43+
.map(CommentMapper::dtoToEntity)
44+
.collectList()
45+
.block();
46+
commentStore.add(result);
47+
internalQueryCache.put(query.hashCode(), commentStore.size() - 1);
48+
49+
return result;
3750

3851
} catch (WebClientResponseException ex) {
3952
logger.error(ex.toString());

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/CommitGitHubRepository.java

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,41 +5,57 @@
55
import io.pakland.mdas.githubstats.domain.entity.Commit;
66
import io.pakland.mdas.githubstats.domain.repository.CommitExternalRepository;
77
import io.pakland.mdas.githubstats.infrastructure.github.model.GitHubCommitDTO;
8+
import java.util.*;
89
import org.slf4j.Logger;
910
import org.slf4j.LoggerFactory;
1011
import org.springframework.web.reactive.function.client.WebClientResponseException;
1112

12-
import java.util.List;
13-
1413
public class CommitGitHubRepository implements CommitExternalRepository {
14+
1515
private final WebClientConfiguration webClientConfiguration;
1616
private final Logger logger = LoggerFactory.getLogger(CommitGitHubRepository.class);
1717

18+
private final Map<Integer, Integer> internalQueryCache = new HashMap<>();
19+
private final List<List<Commit>> commitStore = new ArrayList<>();
20+
1821
public CommitGitHubRepository(WebClientConfiguration webClientConfiguration) {
1922
this.webClientConfiguration = webClientConfiguration;
2023
}
2124

2225
@Override
23-
public List<Commit> fetchCommitsFromPullRequest(FetchCommitsFromPullRequestRequest request) throws HttpException {
26+
public List<Commit> fetchCommitsFromPullRequest(FetchCommitsFromPullRequestRequest request)
27+
throws HttpException {
28+
String query = String.format("/repos/%s/%s/pulls/%s/commits?%s",
29+
request.getRepositoryOwner(),
30+
request.getRepositoryName(), request.getPullRequestNumber(),
31+
getRequestParams(request));
32+
Integer resultPosition = internalQueryCache.get(query.hashCode());
33+
if (resultPosition != null) {
34+
return commitStore.get(resultPosition);
35+
}
2436

2537
try {
26-
27-
return this.webClientConfiguration.getWebClient().get()
28-
.uri(String.format("/repos/%s/%s/pulls/%s/commits?%s", request.getRepositoryOwner(),
29-
request.getRepositoryName(), request.getPullRequestNumber(), getRequestParams(request)))
30-
.retrieve()
31-
.bodyToFlux(GitHubCommitDTO.class)
32-
.map(CommitMapper::dtoToEntity)
33-
.collectList()
34-
.block();
35-
38+
logger.info(" - Fetching commits for pull request: " + request.getPullRequestNumber()
39+
.toString());
40+
List<Commit> result = this.webClientConfiguration.getWebClient().get()
41+
.uri(query)
42+
.retrieve()
43+
.bodyToFlux(GitHubCommitDTO.class)
44+
.map(CommitMapper::dtoToEntity)
45+
.collectList()
46+
.block();
47+
commitStore.add(result);
48+
internalQueryCache.put(query.hashCode(), commitStore.size() - 1);
49+
50+
return result;
3651
} catch (WebClientResponseException ex) {
3752
logger.error(ex.toString());
3853
throw new HttpException(ex.getRawStatusCode(), ex.getMessage());
3954
}
4055
}
4156

4257
private String getRequestParams(FetchCommitsFromPullRequestRequest request) {
43-
return String.format("per_page=%d&page=%d", request.getPerPage(), request.getPage() < 0 ? 1 : request.getPage());
58+
return String.format("per_page=%d&page=%d", request.getPerPage(),
59+
request.getPage() < 0 ? 1 : request.getPage());
4460
}
4561
}

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/OrganizationGitHubRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public OrganizationGitHubRepository(WebClientConfiguration webClientConfiguratio
2222
@Override
2323
public List<Organization> fetchAvailableOrganizations() throws HttpException {
2424
try {
25+
logger.info(" - Fetching organizations");
2526
return this.webClientConfiguration.getWebClient().get()
2627
.uri("/user/orgs")
2728
.retrieve()

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/PullRequestGitHubRepository.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ public PullRequestGitHubRepository(WebClientConfiguration webClientConfiguration
2323
public List<PullRequest> fetchPullRequestsFromRepository(
2424
FetchPullRequestFromRepositoryRequest request) throws HttpException {
2525
try {
26+
logger.info(
27+
" - Fetching pull requests from repository: " + request.getRepositoryOwner() + "/"
28+
+ request.getRepository());
2629
return this.webClientConfiguration.getWebClient().get()
2730
.uri(String.format("/repos/%s/%s/pulls?%s", request.getRepositoryOwner(),
2831
request.getRepository(), getRequestParams(request)))

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/RepositoryGitHubRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public RepositoryGitHubRepository(WebClientConfiguration webClientConfiguration)
2323
public List<Repository> fetchTeamRepositories(String organizationLogin, String teamSlug)
2424
throws HttpException {
2525
try {
26+
logger.info(" - Fetching repositories for team: " + organizationLogin + "/" + teamSlug);
2627
return this.webClientConfiguration.getWebClient().get()
2728
.uri(String.format("/orgs/%s/teams/%s/repos", organizationLogin, teamSlug))
2829
.retrieve()

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/TeamGitHubRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public TeamGitHubRepository(WebClientConfiguration webClientConfiguration) {
2222
@Override
2323
public List<Team> fetchTeamsFromOrganization(String organizationName) throws HttpException {
2424
try {
25+
logger.info(" - Fetching teams from organization: " + organizationName);
2526
return this.webClientConfiguration.getWebClient().get()
2627
.uri(String.format("/orgs/%s/teams", organizationName))
2728
.retrieve()

src/main/java/io/pakland/mdas/githubstats/infrastructure/github/repository/UserGitHubRepository.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ public UserGitHubRepository(WebClientConfiguration webClientConfiguration) {
2525
public List<User> fetchUsersFromTeam(String organizationName, String teamName)
2626
throws HttpException {
2727
try {
28+
logger.info(" - Fetching users from team: " + organizationName + "/" + teamName);
2829
return this.webClientConfiguration.getWebClient().get()
2930
.uri(String.format("/orgs/%s/teams/%s/members", organizationName, teamName))
3031
.retrieve()

0 commit comments

Comments
 (0)