Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:Simplify error handling in build-dashboard.js(#3305) #3309

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/edit-page-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
},
{
"value": "",
"href": "https://github.com/asyncapi/website/blob/master/pages"
"href": "https://github.com/asyncapi/website/blob/master/markdown"
},
{
"value": "reference/extensions/",
Expand Down
46 changes: 26 additions & 20 deletions scripts/dashboard/build-dashboard.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const { writeFileSync } = require('fs');
const { resolve } = require('path');
const { graphql } = require('@octokit/graphql');
const { Promise } = require('node-fetch');
const { Queries } = require('./issue-queries');

async function getHotDiscussions(discussions) {
Expand All @@ -24,12 +23,13 @@ async function getHotDiscussions(discussions) {

const finalInteractionsCount = isPR
? interactionsCount +
discussion.reviews.totalCount +
discussion.reviews.nodes.reduce(
(acc, curr) => acc + curr.comments.totalCount,
0
)
discussion.reviews.totalCount +
discussion.reviews.nodes.reduce(
(acc, curr) => acc + curr.comments.totalCount,
0
)
: interactionsCount;

return {
id: discussion.id,
isPR,
Expand All @@ -45,7 +45,7 @@ async function getHotDiscussions(discussions) {
};
} catch (e) {
console.error(
`there was some issues while parsing this item: ${JSON.stringify(
`There were some issues while parsing this item: ${JSON.stringify(
discussion
)}`
);
Expand All @@ -57,12 +57,14 @@ async function getHotDiscussions(discussions) {
const filteredResult = result.filter(issue => issue.author !== 'asyncapi-bot');
return filteredResult.slice(0, 12);
}

async function writeToFile(content) {
writeFileSync(
resolve(__dirname, '..', '..', 'dashboard.json'),
JSON.stringify(content, null, ' ')
);
}

Comment on lines +60 to +67
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider using asynchronous file writing

The newline addition improves code readability. However, the change from fs.writeFile to writeFileSync makes the operation synchronous, which could potentially block the Node.js event loop, especially for large files.

Consider using the asynchronous fs.promises.writeFile instead:

const { promises: fsPromises } = require('fs');

async function writeToFile(content) {
  await fsPromises.writeFile(
    resolve(__dirname, '..', '..', 'dashboard.json'),
    JSON.stringify(content, null, '  ')
  );
}

This change would maintain the asynchronous nature of the operation and prevent potential blocking of the event loop.

async function mapGoodFirstIssues(issues) {
return issues.map((issue) => ({
id: issue.id,
Expand All @@ -87,11 +89,9 @@ function getLabel(issue, filter) {
return result && result.name.split('/')[1];
}


function monthsSince(date) {
const seconds = Math.floor((new Date() - new Date(date)) / 1000);
// 2592000 = number of seconds in a month = 30 * 24 * 60 * 60
const months = seconds / 2592000;
const months = seconds / 2592000; // 2592000 = seconds in a month
return Math.floor(months);
}

Expand All @@ -112,7 +112,7 @@ async function getDiscussions(query, pageSize, endCursor = null) {
`limit = ${result.rateLimit.limit}`,
`remaining = ${result.rateLimit.remaining}`,
`resetAt = ${result.rateLimit.resetAt}`
)
);
}

const hasNextPage = result.search.pageInfo.hasNextPage;
Expand All @@ -125,42 +125,48 @@ async function getDiscussions(query, pageSize, endCursor = null) {
);
}
} catch (e) {
console.error(e);
console.error('Error fetching discussions:', e);
throw e;
}
}

async function getDiscussionByID(isPR, id) {
try {
let result = await graphql(isPR ? Queries.pullRequestById : Queries.issueById, {
id,
headers: {
authorization: `token ${process.env.GITHUB_TOKEN}`,
},

}
);
});
return result;
} catch (e) {
console.error(e);
console.error(`Error fetching discussion by ID: ${id}`, e);
throw e;
}
}

async function start() {
try {
const [issues, PRs, rawGoodFirstIssues] = await Promise.all([
getDiscussions(Queries.hotDiscussionsIssues, 20),
getDiscussions(Queries.hotDiscussionsPullRequests, 20),
getDiscussions(Queries.goodFirstIssues, 20),
]);

const discussions = issues.concat(PRs);
const [hotDiscussions, goodFirstIssues] = await Promise.all([
getHotDiscussions(discussions),
mapGoodFirstIssues(rawGoodFirstIssues),
]);
writeToFile({ hotDiscussions, goodFirstIssues });

await writeToFile({ hotDiscussions, goodFirstIssues });
} catch (e) {
console.log('There were some issues parsing data from github.')
console.log(e);
console.log('There were some issues parsing data from GitHub.');
console.error(e);
throw e;
}
}

start();

module.exports = { getLabel, monthsSince, mapGoodFirstIssues, getHotDiscussions, getDiscussionByID }
module.exports = { getLabel, monthsSince, mapGoodFirstIssues, getHotDiscussions, getDiscussionByID };
Loading