business-as-code 2.0.1 → 2.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/metrics.js ADDED
@@ -0,0 +1,324 @@
1
+ /**
2
+ * Standardized SaaS Metrics
3
+ *
4
+ * First-class types for common SaaS/subscription business metrics
5
+ * with auto-calculation over time periods.
6
+ *
7
+ * @packageDocumentation
8
+ */
9
+ // =============================================================================
10
+ // Calculation Functions
11
+ // =============================================================================
12
+ /**
13
+ * Calculate MRR from components
14
+ */
15
+ export function calculateMRR(input) {
16
+ const reactivationMRR = input.reactivationMRR || 0;
17
+ const netNewMRR = input.newMRR + input.expansionMRR - input.contractionMRR - input.churnedMRR + reactivationMRR;
18
+ const total = input.previousMRR + netNewMRR;
19
+ return {
20
+ total,
21
+ newMRR: input.newMRR,
22
+ expansionMRR: input.expansionMRR,
23
+ contractionMRR: input.contractionMRR,
24
+ churnedMRR: input.churnedMRR,
25
+ reactivationMRR,
26
+ netNewMRR,
27
+ currency: input.currency || 'USD',
28
+ period: input.period,
29
+ };
30
+ }
31
+ /**
32
+ * Calculate ARR from MRR
33
+ */
34
+ export function calculateARRFromMRR(mrr, currency = 'USD') {
35
+ return {
36
+ total: mrr * 12,
37
+ fromMRR: mrr * 12,
38
+ currency,
39
+ asOf: new Date(),
40
+ };
41
+ }
42
+ /**
43
+ * Calculate NRR
44
+ */
45
+ export function calculateNRR(input) {
46
+ const endingMRR = input.startingMRR + input.expansion - input.contraction - input.churn;
47
+ const rate = input.startingMRR > 0 ? (endingMRR / input.startingMRR) * 100 : 0;
48
+ return {
49
+ rate,
50
+ startingMRR: input.startingMRR,
51
+ endingMRR,
52
+ expansion: input.expansion,
53
+ contraction: input.contraction,
54
+ churn: input.churn,
55
+ period: input.period,
56
+ };
57
+ }
58
+ /**
59
+ * Calculate GRR
60
+ */
61
+ export function calculateGRR(input) {
62
+ const endingMRR = input.startingMRR - input.contraction - input.churn;
63
+ const rate = input.startingMRR > 0 ? Math.min((endingMRR / input.startingMRR) * 100, 100) : 0;
64
+ return {
65
+ rate,
66
+ startingMRR: input.startingMRR,
67
+ endingMRR,
68
+ contraction: input.contraction,
69
+ churn: input.churn,
70
+ period: input.period,
71
+ };
72
+ }
73
+ /**
74
+ * Calculate CAC
75
+ */
76
+ export function calculateCACMetric(input) {
77
+ const value = input.newCustomers > 0 ? input.salesMarketingSpend / input.newCustomers : 0;
78
+ let byChannel;
79
+ if (input.byChannel) {
80
+ byChannel = {};
81
+ for (const [channel, data] of Object.entries(input.byChannel)) {
82
+ byChannel[channel] = data.customers > 0 ? data.spend / data.customers : 0;
83
+ }
84
+ }
85
+ return {
86
+ value,
87
+ totalSalesMarketingSpend: input.salesMarketingSpend,
88
+ newCustomersAcquired: input.newCustomers,
89
+ currency: input.currency || 'USD',
90
+ period: input.period,
91
+ byChannel,
92
+ };
93
+ }
94
+ /**
95
+ * Calculate LTV
96
+ */
97
+ export function calculateLTVMetric(input) {
98
+ // LTV = (ARPU * Gross Margin) / Churn Rate
99
+ const averageLifetimeMonths = input.churnRate > 0 ? 1 / input.churnRate : 0;
100
+ const value = input.churnRate > 0 ? (input.arpu * input.grossMargin / 100) / input.churnRate : 0;
101
+ return {
102
+ value,
103
+ arpu: input.arpu,
104
+ grossMargin: input.grossMargin,
105
+ churnRate: input.churnRate,
106
+ averageLifetimeMonths,
107
+ currency: input.currency || 'USD',
108
+ };
109
+ }
110
+ /**
111
+ * Calculate LTV:CAC ratio
112
+ */
113
+ export function calculateLTVtoCACRatio(ltv, cac) {
114
+ const ratio = cac.value > 0 ? ltv.value / cac.value : 0;
115
+ const paybackMonths = ltv.arpu > 0 && ltv.grossMargin > 0
116
+ ? cac.value / (ltv.arpu * ltv.grossMargin / 100)
117
+ : 0;
118
+ return {
119
+ ratio,
120
+ ltv: ltv.value,
121
+ cac: cac.value,
122
+ paybackMonths,
123
+ healthy: ratio >= 3,
124
+ };
125
+ }
126
+ /**
127
+ * Calculate Quick Ratio
128
+ */
129
+ export function calculateQuickRatioMetric(mrr) {
130
+ const growth = mrr.newMRR + mrr.expansionMRR;
131
+ const loss = mrr.churnedMRR + mrr.contractionMRR;
132
+ const ratio = loss > 0 ? growth / loss : growth > 0 ? Infinity : 0;
133
+ return {
134
+ ratio,
135
+ newMRR: mrr.newMRR,
136
+ expansionMRR: mrr.expansionMRR,
137
+ churnedMRR: mrr.churnedMRR,
138
+ contractionMRR: mrr.contractionMRR,
139
+ healthy: ratio >= 4,
140
+ period: mrr.period,
141
+ };
142
+ }
143
+ /**
144
+ * Calculate Magic Number
145
+ */
146
+ export function calculateMagicNumberMetric(input) {
147
+ const value = input.salesMarketingSpend > 0 ? input.netNewARR / input.salesMarketingSpend : 0;
148
+ return {
149
+ value,
150
+ netNewARR: input.netNewARR,
151
+ salesMarketingSpend: input.salesMarketingSpend,
152
+ efficient: value >= 0.75,
153
+ period: input.period,
154
+ };
155
+ }
156
+ /**
157
+ * Calculate Burn Multiple
158
+ */
159
+ export function calculateBurnMultipleMetric(input) {
160
+ const value = input.netNewARR > 0 ? input.netBurn / input.netNewARR : Infinity;
161
+ return {
162
+ value,
163
+ netBurn: input.netBurn,
164
+ netNewARR: input.netNewARR,
165
+ efficient: value <= 1.5,
166
+ period: input.period,
167
+ };
168
+ }
169
+ /**
170
+ * Calculate Rule of 40
171
+ */
172
+ export function calculateRuleOf40Metric(input) {
173
+ const score = input.revenueGrowthRate + input.profitMargin;
174
+ return {
175
+ score,
176
+ revenueGrowthRate: input.revenueGrowthRate,
177
+ profitMargin: input.profitMargin,
178
+ passing: score >= 40,
179
+ period: input.period,
180
+ };
181
+ }
182
+ /**
183
+ * Calculate growth rates
184
+ */
185
+ export function calculateGrowthRates(input) {
186
+ const mom = input.previousMonth && input.previousMonth > 0
187
+ ? ((input.current - input.previousMonth) / input.previousMonth) * 100
188
+ : 0;
189
+ const qoq = input.previousQuarter && input.previousQuarter > 0
190
+ ? ((input.current - input.previousQuarter) / input.previousQuarter) * 100
191
+ : 0;
192
+ const yoy = input.previousYear && input.previousYear > 0
193
+ ? ((input.current - input.previousYear) / input.previousYear) * 100
194
+ : 0;
195
+ return {
196
+ mom,
197
+ qoq,
198
+ yoy,
199
+ metric: input.metric,
200
+ period: input.period,
201
+ };
202
+ }
203
+ /**
204
+ * Calculate churn metrics
205
+ */
206
+ export function calculateChurnMetrics(input) {
207
+ const customerChurnRate = input.customersStart > 0
208
+ ? (input.customersLost / input.customersStart) * 100
209
+ : 0;
210
+ const revenueChurnRate = input.mrrStart > 0
211
+ ? (input.mrrChurned / input.mrrStart) * 100
212
+ : 0;
213
+ const netRevenueChurnRate = input.mrrStart > 0
214
+ ? ((input.mrrChurned - input.expansionMRR) / input.mrrStart) * 100
215
+ : 0;
216
+ return {
217
+ customerChurnRate,
218
+ customersLost: input.customersLost,
219
+ customersStart: input.customersStart,
220
+ revenueChurnRate,
221
+ mrrChurned: input.mrrChurned,
222
+ netRevenueChurnRate,
223
+ period: input.period,
224
+ };
225
+ }
226
+ // =============================================================================
227
+ // Aggregation Functions
228
+ // =============================================================================
229
+ /**
230
+ * Aggregate time series data by period
231
+ */
232
+ export function aggregateTimeSeries(series, targetPeriod) {
233
+ const buckets = new Map();
234
+ for (const point of series.dataPoints) {
235
+ const key = getBucketKey(point.timestamp, targetPeriod);
236
+ const existing = buckets.get(key) || [];
237
+ buckets.set(key, [...existing, point]);
238
+ }
239
+ const aggregatedPoints = [];
240
+ const aggregation = series.aggregation || 'sum';
241
+ for (const [key, points] of buckets) {
242
+ const values = points.map(p => p.value);
243
+ let aggregatedValue;
244
+ switch (aggregation) {
245
+ case 'sum':
246
+ aggregatedValue = values.reduce((a, b) => a + b, 0);
247
+ break;
248
+ case 'avg':
249
+ aggregatedValue = values.reduce((a, b) => a + b, 0) / values.length;
250
+ break;
251
+ case 'min':
252
+ aggregatedValue = Math.min(...values);
253
+ break;
254
+ case 'max':
255
+ aggregatedValue = Math.max(...values);
256
+ break;
257
+ case 'last':
258
+ aggregatedValue = values[values.length - 1] ?? 0;
259
+ break;
260
+ case 'first':
261
+ aggregatedValue = values[0] ?? 0;
262
+ break;
263
+ default:
264
+ aggregatedValue = values.reduce((a, b) => a + b, 0);
265
+ }
266
+ aggregatedPoints.push({
267
+ timestamp: new Date(key),
268
+ value: aggregatedValue,
269
+ });
270
+ }
271
+ return {
272
+ ...series,
273
+ dataPoints: aggregatedPoints.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime()),
274
+ };
275
+ }
276
+ /**
277
+ * Get bucket key for time aggregation
278
+ */
279
+ function getBucketKey(date, period) {
280
+ switch (period) {
281
+ case 'daily':
282
+ return date.toISOString().split('T')[0] || date.toISOString();
283
+ case 'weekly':
284
+ const weekStart = new Date(date);
285
+ weekStart.setDate(date.getDate() - date.getDay());
286
+ return weekStart.toISOString().split('T')[0] || weekStart.toISOString();
287
+ case 'monthly':
288
+ return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, '0')}-01`;
289
+ case 'quarterly':
290
+ const quarter = Math.floor(date.getMonth() / 3);
291
+ return `${date.getFullYear()}-Q${quarter + 1}`;
292
+ case 'yearly':
293
+ return `${date.getFullYear()}-01-01`;
294
+ default:
295
+ return date.toISOString();
296
+ }
297
+ }
298
+ /**
299
+ * Create metric period from dates
300
+ */
301
+ export function createMetricPeriod(period, start, end, label) {
302
+ return {
303
+ period,
304
+ range: { start, end },
305
+ label: label || formatPeriodLabel(period, start, end),
306
+ };
307
+ }
308
+ /**
309
+ * Format period label
310
+ */
311
+ function formatPeriodLabel(period, start, end) {
312
+ const monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
313
+ switch (period) {
314
+ case 'monthly':
315
+ return `${monthNames[start.getMonth()]} ${start.getFullYear()}`;
316
+ case 'quarterly':
317
+ const quarter = Math.floor(start.getMonth() / 3) + 1;
318
+ return `Q${quarter} ${start.getFullYear()}`;
319
+ case 'yearly':
320
+ return `${start.getFullYear()}`;
321
+ default:
322
+ return `${start.toLocaleDateString()} - ${end.toLocaleDateString()}`;
323
+ }
324
+ }
package/src/okrs.js ADDED
@@ -0,0 +1,268 @@
1
+ /**
2
+ * Objectives and Key Results (OKRs) management
3
+ */
4
+ /**
5
+ * Define Objectives and Key Results for goal tracking
6
+ *
7
+ * @example
8
+ * ```ts
9
+ * const quarterlyOKRs = okrs([
10
+ * {
11
+ * objective: 'Achieve Product-Market Fit',
12
+ * description: 'Validate that our product solves a real problem for customers',
13
+ * period: 'Q2 2024',
14
+ * owner: 'CEO',
15
+ * keyResults: [
16
+ * {
17
+ * description: 'Increase Net Promoter Score',
18
+ * metric: 'NPS',
19
+ * startValue: 40,
20
+ * targetValue: 60,
21
+ * currentValue: 52,
22
+ * unit: 'score',
23
+ * progress: 60,
24
+ * },
25
+ * {
26
+ * description: 'Reduce monthly churn rate',
27
+ * metric: 'Churn Rate',
28
+ * startValue: 8,
29
+ * targetValue: 4,
30
+ * currentValue: 5.5,
31
+ * unit: 'percent',
32
+ * progress: 62.5,
33
+ * },
34
+ * {
35
+ * description: 'Achieve customer retention',
36
+ * metric: 'Customers with 3+ months',
37
+ * startValue: 50,
38
+ * targetValue: 200,
39
+ * currentValue: 125,
40
+ * unit: 'customers',
41
+ * progress: 50,
42
+ * },
43
+ * ],
44
+ * status: 'on-track',
45
+ * confidence: 75,
46
+ * },
47
+ * ])
48
+ * ```
49
+ */
50
+ export function okrs(definitions) {
51
+ return definitions.map(okr => validateAndNormalizeOKR(okr));
52
+ }
53
+ /**
54
+ * Define a single OKR
55
+ */
56
+ export function okr(definition) {
57
+ return validateAndNormalizeOKR(definition);
58
+ }
59
+ /**
60
+ * Validate and normalize an OKR definition
61
+ */
62
+ function validateAndNormalizeOKR(okr) {
63
+ if (!okr.objective) {
64
+ throw new Error('OKR objective is required');
65
+ }
66
+ // Calculate progress for key results if not set
67
+ const keyResults = okr.keyResults?.map(kr => ({
68
+ ...kr,
69
+ progress: kr.progress ?? calculateKeyResultProgress(kr),
70
+ }));
71
+ return {
72
+ ...okr,
73
+ keyResults,
74
+ status: okr.status || 'not-started',
75
+ confidence: okr.confidence ?? calculateConfidence(keyResults || []),
76
+ metadata: okr.metadata || {},
77
+ };
78
+ }
79
+ /**
80
+ * Calculate key result progress
81
+ */
82
+ export function calculateKeyResultProgress(kr) {
83
+ if (kr.currentValue === undefined || kr.startValue === undefined)
84
+ return 0;
85
+ const total = kr.targetValue - kr.startValue;
86
+ if (total === 0)
87
+ return 100;
88
+ const current = kr.currentValue - kr.startValue;
89
+ const progress = (current / total) * 100;
90
+ return Math.max(0, Math.min(100, progress));
91
+ }
92
+ /**
93
+ * Calculate overall OKR progress
94
+ */
95
+ export function calculateOKRProgress(okr) {
96
+ if (!okr.keyResults || okr.keyResults.length === 0)
97
+ return 0;
98
+ const totalProgress = okr.keyResults.reduce((sum, kr) => {
99
+ return sum + (kr.progress ?? calculateKeyResultProgress(kr));
100
+ }, 0);
101
+ return totalProgress / okr.keyResults.length;
102
+ }
103
+ /**
104
+ * Calculate confidence score based on key results
105
+ */
106
+ export function calculateConfidence(keyResults) {
107
+ if (keyResults.length === 0)
108
+ return 0;
109
+ const totalProgress = keyResults.reduce((sum, kr) => {
110
+ return sum + (kr.progress ?? calculateKeyResultProgress(kr));
111
+ }, 0);
112
+ const avgProgress = totalProgress / keyResults.length;
113
+ // Confidence tends to be slightly lower than actual progress
114
+ return Math.max(0, Math.min(100, avgProgress - 10));
115
+ }
116
+ /**
117
+ * Update key result current value
118
+ */
119
+ export function updateKeyResult(okr, krDescription, currentValue) {
120
+ const keyResults = okr.keyResults?.map(kr => {
121
+ if (kr.description === krDescription) {
122
+ const updatedKR = { ...kr, currentValue };
123
+ return {
124
+ ...updatedKR,
125
+ progress: calculateKeyResultProgress(updatedKR),
126
+ };
127
+ }
128
+ return kr;
129
+ });
130
+ // Recalculate overall status and confidence
131
+ const progress = calculateOKRProgress({ ...okr, keyResults });
132
+ const status = determineOKRStatus(progress, okr.confidence || 0);
133
+ return {
134
+ ...okr,
135
+ keyResults,
136
+ status,
137
+ confidence: calculateConfidence(keyResults || []),
138
+ };
139
+ }
140
+ /**
141
+ * Determine OKR status based on progress and confidence
142
+ */
143
+ function determineOKRStatus(progress, confidence) {
144
+ if (progress === 0)
145
+ return 'not-started';
146
+ if (progress === 100)
147
+ return 'completed';
148
+ if (confidence < 50 || progress < 30)
149
+ return 'at-risk';
150
+ return 'on-track';
151
+ }
152
+ /**
153
+ * Check if key result is on track
154
+ */
155
+ export function isKeyResultOnTrack(kr) {
156
+ const progress = kr.progress ?? calculateKeyResultProgress(kr);
157
+ return progress >= 70;
158
+ }
159
+ /**
160
+ * Check if OKR is on track
161
+ */
162
+ export function isOKROnTrack(okr) {
163
+ const progress = calculateOKRProgress(okr);
164
+ return progress >= 70 && (okr.confidence ?? 0) >= 60;
165
+ }
166
+ /**
167
+ * Get key results that are on track
168
+ */
169
+ export function getKeyResultsOnTrack(okr) {
170
+ return okr.keyResults?.filter(isKeyResultOnTrack) || [];
171
+ }
172
+ /**
173
+ * Get key results that are at risk
174
+ */
175
+ export function getKeyResultsAtRisk(okr) {
176
+ return okr.keyResults?.filter(kr => !isKeyResultOnTrack(kr)) || [];
177
+ }
178
+ /**
179
+ * Get OKRs by owner
180
+ */
181
+ export function getOKRsByOwner(okrs, owner) {
182
+ return okrs.filter(okr => okr.owner === owner);
183
+ }
184
+ /**
185
+ * Get OKRs by period
186
+ */
187
+ export function getOKRsByPeriod(okrs, period) {
188
+ return okrs.filter(okr => okr.period === period);
189
+ }
190
+ /**
191
+ * Get OKRs by status
192
+ */
193
+ export function getOKRsByStatus(okrs, status) {
194
+ return okrs.filter(okr => okr.status === status);
195
+ }
196
+ /**
197
+ * Calculate success rate across all OKRs
198
+ */
199
+ export function calculateSuccessRate(okrs) {
200
+ if (okrs.length === 0)
201
+ return 0;
202
+ const avgProgress = okrs.reduce((sum, okr) => {
203
+ return sum + calculateOKRProgress(okr);
204
+ }, 0) / okrs.length;
205
+ return avgProgress;
206
+ }
207
+ /**
208
+ * Format key result for display
209
+ */
210
+ export function formatKeyResult(kr) {
211
+ const progress = kr.progress ?? calculateKeyResultProgress(kr);
212
+ const current = kr.currentValue ?? kr.startValue ?? 0;
213
+ const target = kr.targetValue;
214
+ return `${kr.description}: ${current}/${target} ${kr.unit || ''} (${progress.toFixed(0)}%)`;
215
+ }
216
+ /**
217
+ * Compare OKR performance between periods
218
+ */
219
+ export function compareOKRPerformance(current, previous) {
220
+ const currentProgress = calculateOKRProgress(current);
221
+ const previousProgress = calculateOKRProgress(previous);
222
+ const progressDelta = currentProgress - previousProgress;
223
+ const currentConfidence = current.confidence ?? 0;
224
+ const previousConfidence = previous.confidence ?? 0;
225
+ const confidenceDelta = currentConfidence - previousConfidence;
226
+ const improved = progressDelta > 0 && confidenceDelta >= 0;
227
+ return { progressDelta, confidenceDelta, improved };
228
+ }
229
+ /**
230
+ * Validate OKR definitions
231
+ */
232
+ export function validateOKRs(okrs) {
233
+ const errors = [];
234
+ for (const okr of okrs) {
235
+ if (!okr.objective) {
236
+ errors.push('OKR objective is required');
237
+ }
238
+ if (okr.objective && okr.objective.length < 10) {
239
+ errors.push(`OKR objective "${okr.objective}" should be at least 10 characters`);
240
+ }
241
+ if (okr.confidence !== undefined && (okr.confidence < 0 || okr.confidence > 100)) {
242
+ errors.push(`OKR "${okr.objective}" confidence must be between 0 and 100`);
243
+ }
244
+ if (okr.keyResults) {
245
+ if (okr.keyResults.length === 0) {
246
+ errors.push(`OKR "${okr.objective}" must have at least one key result`);
247
+ }
248
+ if (okr.keyResults.length > 5) {
249
+ errors.push(`OKR "${okr.objective}" should have no more than 5 key results`);
250
+ }
251
+ for (const kr of okr.keyResults) {
252
+ if (!kr.description) {
253
+ errors.push(`Key result in OKR "${okr.objective}" must have a description`);
254
+ }
255
+ if (!kr.metric) {
256
+ errors.push(`Key result "${kr.description}" must have a metric`);
257
+ }
258
+ if (kr.progress !== undefined && (kr.progress < 0 || kr.progress > 100)) {
259
+ errors.push(`Key result "${kr.description}" progress must be between 0 and 100`);
260
+ }
261
+ }
262
+ }
263
+ }
264
+ return {
265
+ valid: errors.length === 0,
266
+ errors,
267
+ };
268
+ }