forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgrowth-metrics.ts
More file actions
267 lines (243 loc) · 7.85 KB
/
Copy pathgrowth-metrics.ts
File metadata and controls
267 lines (243 loc) · 7.85 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
const DAY_MS = 86_400_000;
export interface DailyNewUsersPoint {
date: string;
users: number;
}
export interface WeeklyNewUsersPoint {
week: string;
users: number;
}
export interface DailyActivationPoint {
date: string;
signups: number;
activated: number;
/**
* Subset of `signups` running a desktop build that is able to emit the
* `Memory Created` event the numerator counts. Signups on older builds cannot
* activate no matter what the user does, so pooling them silently deflates the
* rate during a rollout.
*/
capableSignups?: number;
capableActivated?: number;
}
export interface WeeklyActivationPoint {
week: string;
signups: number;
activated: number;
/** Pooled rate over every signup, including those that cannot report. */
rate: number;
/**
* Rate over signups whose build can report activation, and null when none
* could. This is the honest read: a week nobody could report is a blind spot,
* not a week of zero activation.
*/
capableRate: number | null;
telemetryCoverage: number | null;
}
export interface ActivationSummary {
/** Signups whose 7-day activation window has fully elapsed. */
signups: number;
activated: number;
rate: number | null;
/** Same, restricted to signups whose build can report activation at all. */
capableSignups: number;
capableActivated: number;
capableRate: number | null;
/**
* Percentage of matured signups on a reporting-capable build. Below 100 means
* the headline `rate` is diluted by users who physically cannot report, not by
* users failing to activate.
*/
telemetryCoverage: number | null;
}
function utcDate(value: string | Date): Date | null {
const date =
value instanceof Date
? new Date(value.getTime())
: new Date(`${value.slice(0, 10)}T00:00:00Z`);
return Number.isNaN(date.getTime()) ? null : date;
}
export function mondayKey(value: string | Date): string | null {
const date = utcDate(value);
if (!date) return null;
date.setUTCHours(0, 0, 0, 0);
const daysSinceMonday = (date.getUTCDay() + 6) % 7;
date.setUTCDate(date.getUTCDate() - daysSinceMonday);
return date.toISOString().slice(0, 10);
}
export function completedWeeklyNewUsers(
points: readonly DailyNewUsersPoint[],
today = new Date(),
): WeeklyNewUsersPoint[] {
const currentWeek = mondayKey(today);
if (!currentWeek) return [];
const totals = new Map<string, number>();
for (const point of points) {
const week = mondayKey(point.date);
if (!week || week >= currentWeek || !Number.isFinite(point.users)) continue;
totals.set(week, (totals.get(week) ?? 0) + point.users);
}
return Array.from(totals, ([week, users]) => ({ week, users })).sort((a, b) =>
a.week.localeCompare(b.week),
);
}
export function maturedWeeklyActivation(
points: readonly DailyActivationPoint[],
today = new Date(),
maturityDays = 7,
): WeeklyActivationPoint[] {
const todayUtc = utcDate(today);
if (!todayUtc) return [];
todayUtc.setUTCHours(0, 0, 0, 0);
const totals = new Map<
string,
{
signups: number;
activated: number;
capableSignups: number;
capableActivated: number;
}
>();
for (const point of points) {
const week = mondayKey(point.date);
if (
!week ||
!Number.isFinite(point.signups) ||
!Number.isFinite(point.activated)
) {
continue;
}
const current = totals.get(week) ?? {
signups: 0,
activated: 0,
capableSignups: 0,
capableActivated: 0,
};
current.signups += point.signups;
current.activated += point.activated;
// A series recorded before capability was tracked carries no capable
// counts; treat those days as fully capable rather than as zero coverage.
current.capableSignups += point.capableSignups ?? point.signups;
current.capableActivated += point.capableActivated ?? point.activated;
totals.set(week, current);
}
return Array.from(totals, ([week, totalsForWeek]) => {
const weekStart = utcDate(week)!;
const fullyMatureAt = new Date(
weekStart.getTime() + (7 + maturityDays) * DAY_MS,
);
return {
week,
fullyMatureAt,
...totalsForWeek,
};
})
.filter((point) => point.fullyMatureAt <= todayUtc)
.sort((a, b) => a.week.localeCompare(b.week))
.map(({ week, signups, activated, capableSignups, capableActivated }) => ({
week,
signups,
activated,
rate: signups > 0 ? Math.round((activated / signups) * 1000) / 10 : 0,
capableRate: percent(capableActivated, capableSignups),
telemetryCoverage: percent(capableSignups, signups),
}));
}
function percent(numerator: number, denominator: number): number | null {
if (denominator <= 0) return null;
return Math.round((numerator / denominator) * 1000) / 10;
}
export interface ActivationCohortMember {
signupAt: string;
activated: boolean;
}
export interface ActivationSeries {
weeks: { week: string; signups: number; activated: number; rate: number }[];
signups: number;
activated: number;
rate: number | null;
}
/**
* Roll a per-user activation cohort into weekly buckets plus a pooled rate.
*
* Unlike the PostHog series this has no telemetry-coverage dimension: it is
* derived from the conversation records themselves, which exist regardless of
* what the client managed to report. Callers must pass only members whose
* activation window has already elapsed.
*/
export function rollUpActivationCohort(
members: readonly ActivationCohortMember[],
): ActivationSeries {
const totals = new Map<string, { signups: number; activated: number }>();
let signups = 0;
let activated = 0;
for (const member of members) {
const week = mondayKey(member.signupAt);
if (!week) continue;
const current = totals.get(week) ?? { signups: 0, activated: 0 };
current.signups += 1;
signups += 1;
if (member.activated) {
current.activated += 1;
activated += 1;
}
totals.set(week, current);
}
const weeks = Array.from(totals, ([week, t]) => ({
week,
signups: t.signups,
activated: t.activated,
rate: percent(t.activated, t.signups) ?? 0,
})).sort((a, b) => a.week.localeCompare(b.week));
return { weeks, signups, activated, rate: percent(activated, signups) };
}
/**
* Pool a daily activation series into the single headline rate.
*
* Only signup days whose activation window has fully elapsed are counted. A
* signup from yesterday has not had its 7 days yet, so including it would count
* a guaranteed-zero numerator against a real denominator and drag the rate down
* every single day.
*/
export function summarizeActivation(
points: readonly DailyActivationPoint[],
today = new Date(),
maturityDays = 7,
): ActivationSummary {
const todayUtc = utcDate(today);
const empty: ActivationSummary = {
signups: 0,
activated: 0,
rate: null,
capableSignups: 0,
capableActivated: 0,
capableRate: null,
telemetryCoverage: null,
};
if (!todayUtc) return empty;
todayUtc.setUTCHours(0, 0, 0, 0);
const totals = { ...empty };
for (const point of points) {
const day = utcDate(point.date);
if (
!day ||
!Number.isFinite(point.signups) ||
!Number.isFinite(point.activated)
) {
continue;
}
if (new Date(day.getTime() + maturityDays * DAY_MS) > todayUtc) continue;
totals.signups += point.signups;
totals.activated += point.activated;
// A series recorded before capability was tracked carries no capable
// counts; treat those days as fully capable rather than as zero coverage.
totals.capableSignups += point.capableSignups ?? point.signups;
totals.capableActivated += point.capableActivated ?? point.activated;
}
return {
...totals,
rate: percent(totals.activated, totals.signups),
capableRate: percent(totals.capableActivated, totals.capableSignups),
telemetryCoverage: percent(totals.capableSignups, totals.signups),
};
}