Skip to content

Commit

Permalink
Improve loop performance
Browse files Browse the repository at this point in the history
  • Loading branch information
GODrums committed Sep 18, 2024
1 parent c5b1e00 commit 6da0d83
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public List<LeaderboardEntry> createLeaderboard() {
List<User> users = userService.getAllUsers();
logger.info("Found " + users.size() + " users");

OffsetDateTime cutOffTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * timeframe)
.toInstant().atOffset(ZoneOffset.UTC);

List<LeaderboardEntry> leaderboard = users.stream().map(user -> {
if (user.getType() != UserType.USER) {
return null;
Expand All @@ -48,8 +51,6 @@ public List<LeaderboardEntry> createLeaderboard() {
AtomicInteger changesRequested = new AtomicInteger(0);
AtomicInteger changesApproved = new AtomicInteger(0);
AtomicInteger score = new AtomicInteger(0);
OffsetDateTime cutOffTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * timeframe)
.toInstant().atOffset(ZoneOffset.UTC);
user.getReviews().stream()
.filter(review -> (review.getCreatedAt() != null && review.getCreatedAt().isAfter(cutOffTime))
|| (review.getUpdatedAt() != null && review.getUpdatedAt().isAfter(cutOffTime)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Collectors;

import org.kohsuke.github.GHDirection;
import org.kohsuke.github.GHIssueState;
import org.kohsuke.github.GHObject;
import org.kohsuke.github.GHPullRequest;
import org.kohsuke.github.GHPullRequestReviewComment;
Expand Down Expand Up @@ -56,6 +58,7 @@ public class GitHubDataSyncService {

@Value("${monitoring.timeframe}")
private int timeframe;
private Date cutOffTime;

private GitHub github;

Expand Down Expand Up @@ -102,6 +105,8 @@ public GitHubDataSyncService(RepositoryRepository repositoryRepository, PullRequ

public void syncData() {
int successfullySyncedRepositories = 0;
this.cutOffTime = new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * timeframe);
logger.info("Cut-off time for the data sync: " + cutOffTime);
for (String repositoryName : repositoriesToMonitor) {
try {
syncRepository(repositoryName);
Expand Down Expand Up @@ -169,20 +174,23 @@ public Repository fetchRepository(String nameWithOwner) throws IOException {

private Set<PullRequest> getPullRequestsFromGHRepository(GHRepository ghRepo, Repository repository)
throws IOException {
// Iterator allows us to handle pullrequests without knowing the next ones
PagedIterator<GHPullRequest> pullRequests = ghRepo.queryPullRequests()
// .state(GHIssueState.ALL)
.state(GHIssueState.ALL)
.sort(Sort.UPDATED)
.direction(GHDirection.DESC)
.list().withPageSize(100).iterator();
Set<PullRequest> prs = new HashSet<>();
while (pullRequests.hasNext()) {

// Only fetch next page if all PRs are still within the timeframe
AtomicBoolean fetchStillInTimeframe = new AtomicBoolean(true);

while (fetchStillInTimeframe.get() && pullRequests.hasNext()) {
List<GHPullRequest> nextPage = pullRequests.nextPage();
if (!isResourceRecent(nextPage.get(0))) {
break;
}
logger.info("Fetched " + nextPage.size() + " PRs");
logger.info("Fetched " + nextPage.size() + " PRs from Github");
prs.addAll(nextPage.stream().map(pr -> {
if (!isResourceRecent(pr)) {
fetchStillInTimeframe.set(false);
return null;
}
PullRequest pullRequest = pullRequestRepository.save(pullRequestConverter.convert(pr));
Expand Down Expand Up @@ -332,18 +340,9 @@ private PullRequestReview getPRRFromReviewId(Long reviewId) {
* @return
*/
private boolean isResourceRecent(GHObject obj) {
if (obj.getClass().equals(GHPullRequest.class)) {
try {
logger.info("Checking if PR is recent: " + obj.getUpdatedAt() + ", "
+ obj.getCreatedAt());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
return obj.getUpdatedAt() != null
&& obj.getUpdatedAt().after(new Date(System.currentTimeMillis() - 1000 * 60 * 60 * 24 * timeframe));
&& obj.getUpdatedAt().after(cutOffTime);
} catch (IOException e) {
logger.error("Error while fetching createdAt! Resource ID: " + obj.getId());
return false;
Expand Down

0 comments on commit 6da0d83

Please sign in to comment.