From 312c38c4b19070249b4ca8bfbef941232b602c89 Mon Sep 17 00:00:00 2001 From: Rangga Fajar Oktariansyah <86386385+FajarKim@users.noreply.github.com> Date: Sat, 31 Aug 2024 14:33:15 +0700 Subject: [PATCH] chore: optimized stats calculation by fetching contribution data in parallel 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. --- src/fetcher/stats.ts | 25 +++++++++++++++++-------- 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/src/fetcher/stats.ts b/src/fetcher/stats.ts index a0c76ce..623d1cf 100644 --- a/src/fetcher/stats.ts +++ b/src/fetcher/stats.ts @@ -252,17 +252,26 @@ async function stats(username: string): Promise { 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",