-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add scripts + chart re. sponsorship activities
- Loading branch information
Showing
8 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
101 changes: 101 additions & 0 deletions
101
packages/docs/scripts/get-monthly-sponsorships-github.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { graphql } from '@octokit/graphql'; | ||
|
||
const START_DATE = new Date('2023-11-01'); | ||
|
||
interface SponsorActivity { | ||
action: 'NEW_SPONSORSHIP' | 'CANCELLED_SPONSORSHIP'; | ||
timestamp: string; | ||
sponsorsTier: { | ||
monthlyPriceInDollars: number; | ||
isOneTime: boolean; | ||
}; | ||
sponsor: { | ||
login: string; | ||
}; | ||
} | ||
|
||
interface GraphQLResponse { | ||
viewer: { | ||
sponsorsActivities: { | ||
nodes: SponsorActivity[]; | ||
}; | ||
}; | ||
} | ||
|
||
const getMonthlyTotals = async (token: string) => { | ||
const { viewer } = await graphql<GraphQLResponse>({ | ||
query: ` | ||
query { | ||
viewer { | ||
sponsorsActivities(first: 100, period: ALL) { | ||
nodes { | ||
action | ||
timestamp | ||
sponsorsTier { | ||
monthlyPriceInDollars | ||
isOneTime | ||
} | ||
sponsor { | ||
... on User { login } | ||
... on Organization { login } | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`, | ||
headers: { | ||
authorization: `token ${token}`, | ||
}, | ||
}); | ||
|
||
const activities = [...viewer.sponsorsActivities.nodes].sort( | ||
(a, b) => new Date(a.timestamp).getTime() - new Date(b.timestamp).getTime() | ||
); | ||
|
||
const activeRecurring = new Map<string, number>(); | ||
const monthlyTotals = new Map<string, number>(); | ||
const now = new Date(); | ||
|
||
for (let d = new Date(START_DATE); d <= now; d.setMonth(d.getMonth() + 1)) { | ||
monthlyTotals.set(d.toISOString().substring(0, 7), 0); | ||
} | ||
|
||
for (const activity of activities) { | ||
const { action, sponsor, sponsorsTier, timestamp } = activity; | ||
const amount = sponsorsTier?.monthlyPriceInDollars || 0; | ||
const monthYear = new Date(timestamp).toISOString().substring(0, 7); | ||
|
||
if (sponsorsTier?.isOneTime) { | ||
if (action === 'NEW_SPONSORSHIP' && monthYear >= START_DATE.toISOString().substring(0, 7)) { | ||
monthlyTotals.set(monthYear, (monthlyTotals.get(monthYear) || 0) + amount); | ||
} | ||
} else { | ||
if (action === 'NEW_SPONSORSHIP') activeRecurring.set(sponsor.login, amount); | ||
else if (action === 'CANCELLED_SPONSORSHIP') activeRecurring.delete(sponsor.login); | ||
const recurringTotal = Array.from(activeRecurring.values()).reduce((sum, a) => sum + a, 0); | ||
for (const [month] of monthlyTotals) if (month >= monthYear) monthlyTotals.set(month, recurringTotal); | ||
} | ||
} | ||
|
||
return monthlyTotals; | ||
}; | ||
|
||
const main = async () => { | ||
const token = process.env.GITHUB_TOKEN; | ||
if (!token) { | ||
throw new Error('GITHUB_TOKEN environment variable is not set'); | ||
} | ||
const monthlyData = await getMonthlyTotals(token); | ||
|
||
let grandTotal = 0; | ||
|
||
for (const [month, amount] of [...monthlyData.entries()].sort()) { | ||
console.log(`${month} ${amount}`); | ||
grandTotal += amount; | ||
} | ||
|
||
console.log(`\nGrand total: ${grandTotal} (${monthlyData.size} months)`); | ||
}; | ||
|
||
main().catch(console.error); |
119 changes: 119 additions & 0 deletions
119
packages/docs/scripts/get-monthly-sponsorships-opencollective.ts
This file contains 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 |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { graphql } from '@octokit/graphql'; | ||
|
||
const START_DATE = new Date('2023-11-01'); | ||
const RATE_EUR_TO_USD = 1.08; | ||
|
||
interface Transaction { | ||
id: string; | ||
type: string; | ||
kind: string; | ||
amount: { | ||
value: number; | ||
currency: string; | ||
}; | ||
createdAt: string; | ||
fromAccount: { | ||
name: string; | ||
}; | ||
} | ||
|
||
interface Expense { | ||
id: string; | ||
amount: number; | ||
currency: string; | ||
createdAt: string; | ||
type: string; | ||
status: string; | ||
} | ||
|
||
interface GraphQLResponse { | ||
account: { | ||
transactions: { | ||
nodes: Transaction[]; | ||
}; | ||
}; | ||
expenses: { | ||
nodes: Expense[]; | ||
}; | ||
} | ||
|
||
const getMonthlyTotals = async (token: string): Promise<Map<string, number>> => { | ||
const { account, expenses } = await graphql<GraphQLResponse>({ | ||
query: ` | ||
query { | ||
account(slug: "knip") { | ||
transactions(type: CREDIT) { | ||
nodes { | ||
id | ||
type | ||
kind | ||
amount { | ||
value | ||
currency | ||
} | ||
createdAt | ||
fromAccount { | ||
name | ||
} | ||
} | ||
} | ||
} | ||
expenses(fromAccount: { slug: "webpro" }) { | ||
nodes { | ||
id | ||
amount | ||
currency | ||
createdAt | ||
type | ||
status | ||
} | ||
} | ||
} | ||
`, | ||
url: 'https://api.opencollective.com/graphql/v2', | ||
headers: { | ||
'Api-Key': token, | ||
Accept: 'application/json', | ||
}, | ||
}); | ||
|
||
const monthlyTotals = new Map<string, number>(); | ||
const now = new Date(); | ||
|
||
for (let d = new Date(START_DATE); d <= now; d.setMonth(d.getMonth() + 1)) { | ||
monthlyTotals.set(d.toISOString().substring(0, 7), 0); | ||
} | ||
|
||
for (const transaction of account.transactions.nodes) { | ||
const month = new Date(transaction.createdAt).toISOString().substring(0, 7); | ||
const amount = Math.round(transaction.amount.value); | ||
monthlyTotals.set(month, (monthlyTotals.get(month) || 0) + amount); | ||
} | ||
|
||
for (const expense of expenses.nodes) { | ||
const month = new Date(expense.createdAt).toISOString().substring(0, 7); | ||
const amount = | ||
expense.currency === 'EUR' | ||
? Math.round((expense.amount / 100) * RATE_EUR_TO_USD) | ||
: Math.round(expense.amount / 100); | ||
monthlyTotals.set(month, (monthlyTotals.get(month) || 0) + amount); | ||
} | ||
|
||
return monthlyTotals; | ||
}; | ||
|
||
const main = async () => { | ||
const token = process.env.OPENCOLLECTIVE_TOKEN; | ||
if (!token) throw new Error('OPENCOLLECTIVE_TOKEN is not set'); | ||
const monthlyData = await getMonthlyTotals(token); | ||
|
||
let grandTotal = 0; | ||
for (const [month, amount] of [...monthlyData.entries()].sort()) { | ||
console.log(`${month} ${amount}`); | ||
grandTotal += amount; | ||
} | ||
|
||
console.log(`\nGrand total: ${grandTotal} (${monthlyData.size} months)`); | ||
}; | ||
|
||
main().catch(console.error); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
import Chart from '../assets/venz-chart.svg'; | ||
--- | ||
|
||
<Chart /> |
This file contains 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