Skip to content

Commit

Permalink
chore: optimized stats calculation by fetching contribution data in p…
Browse files Browse the repository at this point in the history
…arallel using `Promise.all` (#416)

- Replaced sequential for loop with Promise.all to fetch contributions for each year in parallel.
- Utilized Array.reduce to aggregate total commit contributions, restricted contributions, and pull request review contributions.
- Improved performance by reducing the overall time to retrieve data from GitHub API.
  • Loading branch information
FajarKim authored Aug 31, 2024
1 parent 7454601 commit 312c38c
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/fetcher/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,17 +252,26 @@ async function stats(username: string): Promise<User> {
const startYear = await getUserJoinYear(username);
const endYear = new Date().getFullYear();

let TotalCommitContributions = 0;
let RestrictedContributionsCount = 0;
let TotalPullRequestReviewContributions = 0;

const contributionPromises = [];
for (let year = startYear; year <= endYear; year++) {
const contributions = await fetchContributions(username, year);
TotalCommitContributions += contributions.totalCommitContributions;
RestrictedContributionsCount += contributions.restrictedContributionsCount;
TotalPullRequestReviewContributions += contributions.totalPullRequestReviewContributions;
contributionPromises.push(fetchContributions(username, year));
}

const contributions = await Promise.all(contributionPromises);

const TotalCommitContributions = contributions.reduce(
(total, contribution) => total + contribution.totalCommitContributions,
0
);
const RestrictedContributionsCount = contributions.reduce(
(total, contribution) => total + contribution.restrictedContributionsCount,
0
);
const TotalPullRequestReviewContributions = contributions.reduce(
(total, contribution) => total + contribution.totalPullRequestReviewContributions,
0
);

const data = await axios({
method: "post",
url: "https://api.github.com/graphql",
Expand Down

1 comment on commit 312c38c

@vercel
Copy link

@vercel vercel bot commented on 312c38c Aug 31, 2024

Choose a reason for hiding this comment

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

Please sign in to comment.