forked from CodebuffAI/codebuff
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculate-mrr.ts
More file actions
116 lines (99 loc) · 3.6 KB
/
calculate-mrr.ts
File metadata and controls
116 lines (99 loc) · 3.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import { stripeServer } from '@codebuff/common/util/stripe'
import type Stripe from 'stripe'
async function calculateMRR() {
console.log('Calculating MRR...')
let totalMRR = 0
let totalPastDueMRR = 0
let totalPastDueInvoices = 0
let totalSubscriptions = 0
try {
// First get active subscriptions
let hasMore = true
let startingAfter: string | undefined = undefined
while (hasMore) {
const subscriptions: Stripe.Response<
Stripe.ApiList<Stripe.Subscription>
> = await stripeServer.subscriptions.list({
limit: 100,
starting_after: startingAfter,
status: 'active',
expand: ['data.items.data.price'],
})
// Process each subscription
for (const subscription of subscriptions.data) {
// Skip subscriptions that are scheduled to be canceled
if (subscription.cancel_at_period_end) {
continue
}
totalSubscriptions++
// Get the base subscription price (licensed item)
const basePriceItem = subscription.items.data.find(
(item: Stripe.SubscriptionItem) =>
item.price.recurring?.usage_type === 'licensed',
)
if (basePriceItem?.price.unit_amount) {
totalMRR += basePriceItem.price.unit_amount
console.log(
`Active base MRR for customer ${subscription.customer}: $${(basePriceItem.price.unit_amount / 100).toFixed(2)}`,
)
}
}
hasMore = subscriptions.has_more
if (hasMore && subscriptions.data.length > 0) {
startingAfter = subscriptions.data[subscriptions.data.length - 1].id
}
}
// Now get past_due subscriptions
hasMore = true
startingAfter = undefined
while (hasMore) {
const subscriptions: Stripe.Response<
Stripe.ApiList<Stripe.Subscription>
> = await stripeServer.subscriptions.list({
limit: 100,
starting_after: startingAfter,
status: 'past_due',
expand: ['data.items.data.price'],
})
// Process each subscription
for (const subscription of subscriptions.data) {
totalSubscriptions++
// Get the base subscription price (licensed item)
const basePriceItem = subscription.items.data.find(
(item: Stripe.SubscriptionItem) =>
item.price.recurring?.usage_type === 'licensed',
)
if (basePriceItem?.price.unit_amount) {
totalPastDueMRR += basePriceItem.price.unit_amount
console.log(
`Past due base MRR for customer ${subscription.customer}: $${(basePriceItem.price.unit_amount / 100).toFixed(2)}`,
)
totalPastDueInvoices++
}
}
hasMore = subscriptions.has_more
if (hasMore && subscriptions.data.length > 0) {
startingAfter = subscriptions.data[subscriptions.data.length - 1].id
}
}
// Convert from cents to dollars
const mrrInDollars = totalMRR / 100
const pastDueMRRInDollars = totalPastDueMRR / 100
console.log(`\nProcessed ${totalSubscriptions} total subscriptions`)
console.log(`Found ${totalPastDueInvoices} past due subscriptions`)
console.log(
`Base MRR (from active subscriptions): $${mrrInDollars.toFixed(2)}`,
)
console.log(`Past Due Base MRR: $${pastDueMRRInDollars.toFixed(2)}`)
console.log(
`Total Base MRR: $${(mrrInDollars + pastDueMRRInDollars).toFixed(2)}`,
)
console.log(
`Annual Base Run Rate (ARR): $${((mrrInDollars + pastDueMRRInDollars) * 12).toFixed(2)}`,
)
} catch (error) {
console.error('Error calculating MRR:', error)
}
}
// Run the script
calculateMRR()