-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
fix 503 error from opencollective #4214
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
Merged
chenxsan
merged 7 commits into
webpack:master
from
chenxsan:bugfix/fix-opencollective-503
Nov 28, 2020
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
58e8e0b
use api key for opencollective
chenxsan e767fe1
remove from testing action
chenxsan 3400d69
add OPENCOLLECTIVE_API_KEY
chenxsan 4d48585
add comments
chenxsan 7a8a864
make graphqlPageSize smaller
chenxsan ea86652
optimize transaction querying
chenxsan 804855d
remove api key
chenxsan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ jobs: | |
uses: JamesIves/[email protected] | ||
with: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | ||
OPENCOLLECTIVE_API_KEY: ${{ secrets.OPENCOLLECTIVE_API_KEY }} | ||
BRANCH: gh-pages | ||
FOLDER: dist | ||
CLEAN: true | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,19 @@ const asyncWriteFile = promisify(fs.writeFile); | |
|
||
const REQUIRED_KEYS = ['totalDonations', 'slug', 'name']; | ||
const filename = '_supporters.json'; | ||
const absoluteFilename = path.resolve(__dirname, '..', 'components', 'Support', filename); | ||
const absoluteFilename = path.resolve( | ||
__dirname, | ||
'..', | ||
'components', | ||
'Support', | ||
filename | ||
); | ||
|
||
const graphqlEndpoint = 'https://api.opencollective.com/graphql/v2'; | ||
const graphqlEndpoint = | ||
'https://api.opencollective.com/graphql/v2'; | ||
|
||
// https://github.com/opencollective/opencollective-api/blob/master/server/graphql/v2/query/TransactionsQuery.ts#L81 | ||
const graphqlPageSize = 1000; | ||
|
||
const membersGraphqlQuery = `query account($limit: Int, $offset: Int) { | ||
account(slug: "webpack") { | ||
|
@@ -32,10 +42,12 @@ const membersGraphqlQuery = `query account($limit: Int, $offset: Int) { | |
} | ||
}`; | ||
|
||
const transactionsGraphqlQuery = `query account($limit: Int, $offset: Int) { | ||
account(slug: "webpack") { | ||
transactions(limit: $limit, offset: $offset, includeIncognitoTransactions: false) { | ||
nodes { | ||
// only query transactions in last year | ||
const transactionsGraphqlQuery = `query transactions($dateFrom: ISODateTime, $limit: Int, $offset: Int) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Now we query only data in last year. |
||
transactions(account: { | ||
slug: "webpack" | ||
}, dateFrom: $dateFrom, limit: $limit, offset: $offset, includeIncognitoTransactions: false) { | ||
nodes { | ||
amountInHostCurrency { | ||
value | ||
} | ||
|
@@ -47,28 +59,34 @@ const transactionsGraphqlQuery = `query account($limit: Int, $offset: Int) { | |
} | ||
createdAt | ||
} | ||
} | ||
} | ||
}`; | ||
|
||
const graphqlPageSize = 5000; | ||
|
||
const nodeToSupporter = node => ({ | ||
const nodeToSupporter = (node) => ({ | ||
name: node.account.name, | ||
slug: node.account.slug, | ||
website: node.account.website, | ||
avatar: node.account.imageUrl, | ||
firstDonation: node.createdAt, | ||
totalDonations: node.totalDonations.value * 100, | ||
monthlyDonations: 0 | ||
monthlyDonations: 0, | ||
}); | ||
|
||
const getAllNodes = async (graphqlQuery, getNodes) => { | ||
const requestOptions = { | ||
method: 'POST', | ||
uri: graphqlEndpoint, | ||
body: { query: graphqlQuery, variables: { limit: graphqlPageSize, offset: 0 } }, | ||
json: true | ||
body: { | ||
query: graphqlQuery, | ||
variables: { | ||
limit: graphqlPageSize, | ||
offset: 0, | ||
dateFrom: new Date( | ||
new Date().setFullYear(new Date().getFullYear() - 1) | ||
).toISOString(), // data from last year | ||
}, | ||
}, | ||
json: true, | ||
}; | ||
|
||
let allNodes = []; | ||
|
@@ -82,15 +100,22 @@ const getAllNodes = async (graphqlQuery, getNodes) => { | |
requestOptions.body.variables.offset += graphqlPageSize; | ||
if (nodes.length < graphqlPageSize) { | ||
return allNodes; | ||
} else { | ||
// sleep for a while | ||
await new Promise((resolve) => setTimeout(resolve, 100)); | ||
} | ||
} | ||
}; | ||
|
||
const oneYearAgo = Date.now() - 365 * 24 * 60 * 60 * 1000; | ||
|
||
(async () => { | ||
const members = await getAllNodes(membersGraphqlQuery, data => data.account.members.nodes); | ||
let supporters = members.map(nodeToSupporter).sort((a, b) => b.totalDonations - a.totalDonations); | ||
const members = await getAllNodes( | ||
membersGraphqlQuery, | ||
(data) => data.account.members.nodes | ||
); | ||
let supporters = members | ||
.map(nodeToSupporter) | ||
.sort((a, b) => b.totalDonations - a.totalDonations); | ||
|
||
// Deduplicating supporters with multiple orders | ||
supporters = uniqBy(supporters, 'slug'); | ||
|
@@ -99,37 +124,43 @@ const oneYearAgo = Date.now() - 365 * 24 * 60 * 60 * 1000; | |
for (const supporter of supporters) { | ||
for (const key of REQUIRED_KEYS) { | ||
if (!supporter || typeof supporter !== 'object') { | ||
throw new Error(`Supporters: ${JSON.stringify(supporter)} is not an object.`); | ||
throw new Error( | ||
`Supporters: ${JSON.stringify(supporter)} is not an object.` | ||
); | ||
} | ||
if (!(key in supporter)) { | ||
throw new Error(`Supporters: ${JSON.stringify(supporter)} doesn't include ${key}.`); | ||
throw new Error( | ||
`Supporters: ${JSON.stringify(supporter)} doesn't include ${key}.` | ||
); | ||
} | ||
} | ||
supportersBySlug.set(supporter.slug, supporter); | ||
} | ||
|
||
// Calculate monthly amount from transactions | ||
const transactions = await getAllNodes(transactionsGraphqlQuery, data => data.account.transactions.nodes); | ||
const transactions = await getAllNodes( | ||
transactionsGraphqlQuery, | ||
(data) => data.transactions.nodes | ||
); | ||
for (const transaction of transactions) { | ||
if (!transaction.amountInHostCurrency) continue; | ||
const amount = transaction.amountInHostCurrency.value; | ||
if (!amount || amount <= 0) continue; | ||
const date = +new Date(transaction.createdAt); | ||
if (date < oneYearAgo) continue; | ||
const supporter = supportersBySlug.get(transaction.fromAccount.slug); | ||
if (!supporter) continue; | ||
supporter.monthlyDonations += amount * 100 / 12; | ||
supporter.monthlyDonations += (amount * 100) / 12; | ||
} | ||
|
||
for (const supporter of supporters) { | ||
supporter.monthlyDonations = Math.round(supporter.monthlyDonations); | ||
} | ||
|
||
// Write the file | ||
return asyncWriteFile(absoluteFilename, JSON.stringify(supporters, null, 2)).then(() => | ||
console.log(`Fetched 1 file: ${filename}`) | ||
); | ||
})().catch(error => { | ||
return asyncWriteFile( | ||
absoluteFilename, | ||
JSON.stringify(supporters, null, 2) | ||
).then(() => console.log(`Fetched 1 file: ${filename}`)); | ||
})().catch((error) => { | ||
console.error('utilities/fetch-supporters:', error); | ||
process.exitCode = 1; | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which account holds this key?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer used, could be removed. PR here #4390