Skip to content

Commit

Permalink
add handling for days_to_display of the distribution diagram is > the…
Browse files Browse the repository at this point in the history
…n 365 (1 year), as the graphql has limit of 1 year
  • Loading branch information
gh0stintheshe11 committed Oct 6, 2024
1 parent e01456c commit 88ec0b3
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 10 deletions.
2 changes: 1 addition & 1 deletion config.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const config = {
ringThickness: 34
},
contribution_distribution: {
days_to_show: 60,
days_to_show: 60, // best to set to 60 days, if more than 60 days, the chart will be too crowded
border_color: "#00f0ff",
bullish_color: "#32cd32",
bearish_color: "#c5003c",
Expand Down
81 changes: 72 additions & 9 deletions src/fetch/fetch_github.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,14 +152,53 @@ async function fetchGitHubData(username) {
};
};

const fetchContributionsCalendar = async (username, fromDate, toDate) => {
const response = await http2Axios.post(url, {
query: GRAPHQL_QUERY_CONTRIBUTIONS_CALENDAR,
variables: { login: username, from: fromDate.toISOString(), to: toDate.toISOString() }
}, { headers });
return response.data?.data?.user?.contributionsCollection;
};

async function fetchContributionsCalendar(username, fromDate, toDate) {
const startDate = new Date(fromDate);
const endDate = new Date(toDate);

// Check if the date period is greater than 1 year
const isMoreThanOneYear = (endDate - startDate) > (365 * 24 * 60 * 60 * 1000);

if (isMoreThanOneYear) {
const promises = [];

// Separate the date range by year
for (let year = startDate.getFullYear(); year <= endDate.getFullYear(); year++) {
const yearStartDate = new Date(year, 0, 1); // January 1st of the current year
const yearEndDate = new Date(year, 11, 31); // December 31st of the current year

// Adjust the start and end dates if they fall outside the specified range
const from = yearStartDate < startDate ? startDate : yearStartDate;
const to = yearEndDate > endDate ? endDate : yearEndDate;

// Create a promise for the current year range
promises.push(fetchContributionsForYear(url, headers, username, from, to));
}

// Wait for all promises to resolve
const results = await Promise.all(promises);

// Combine results
const combinedResult = results.reduce((acc, curr) => {
for (const date in curr) {
if (!acc[date]) {
acc[date] = { total: 0 };
}
acc[date].total += curr[date].total;
}
return acc;
}, {});

// Log the length of the combined result
console.log(Object.keys(combinedResult).length);
return combinedResult;

} else {
// If the period is 1 year or less, run a normal query
return fetchContributionsForYear(url, headers, username, startDate, endDate);
}
}

const [userInfo, repositories, contributionsCalendar] = await Promise.all([
fetchUserInfo(),
fetchRepositories(),
Expand Down Expand Up @@ -206,7 +245,6 @@ async function fetchGitHubData(username) {
stats.language_percentages = calculateLanguagePercentage(stats.top_languages);

stats.contribution_distribution = await processContributionsCalendar(contributionsCalendar);
console.log(Object.keys(stats.contribution_distribution).length);

console.timeEnd('Data Processing');

Expand Down Expand Up @@ -242,6 +280,31 @@ function calculateTopLanguages(reposNodes) {
async function processContributionsCalendar(contributionsCollection) {
const result = {};

// Check if contributionsCollection is a valid object
if (contributionsCollection && typeof contributionsCollection === 'object') {
// Iterate over the keys (dates) in the contributionsCollection
for (const date in contributionsCollection) {
if (contributionsCollection.hasOwnProperty(date)) {
const total = contributionsCollection[date].total || 0; // Use 0 if total is undefined
result[date] = { total }; // Store the total contributions for the date
}
}
} else {
console.warn('Invalid contributionsCollection:', contributionsCollection);
}

return result;
}

async function fetchContributionsForYear(url, headers, username, fromDate, toDate) {
const response = await http2Axios.post(url, {
query: GRAPHQL_QUERY_CONTRIBUTIONS_CALENDAR,
variables: { login: username, from: fromDate.toISOString(), to: toDate.toISOString() }
}, { headers });

const contributionsCollection = response.data?.data?.user?.contributionsCollection;
const result = {};

// Check if contributionsCollection and contributionCalendar exist
if (contributionsCollection && contributionsCollection.contributionCalendar) {
for (const week of contributionsCollection.contributionCalendar.weeks) {
Expand Down

0 comments on commit 88ec0b3

Please sign in to comment.