* Of the machines whose FIRST day falls in the range, how many ran an index * within `window` days of it. * * The cohort key is `machine_first_seen`, not install events: a machine that * reinstalls does not re-enter the funnel, which is what makes this a * conversion rate rather than an install
(env: Env, url: URL, range: Range)
| 904 | * gaps, like every other counted number on the page. |
| 905 | */ |
| 906 | async function activation(env: Env, url: URL, range: Range): Promise<ApiResult> { |
| 907 | const rawWindow = url.searchParams.get('window'); |
| 908 | const window = rawWindow === null ? DEFAULT_ACTIVATION_WINDOW : Number(rawWindow); |
| 909 | if (!Number.isInteger(window) || window < 1 || window > MAX_ACTIVATION_WINDOW) { |
| 910 | return fail(`window must be an integer between 1 and ${MAX_ACTIVATION_WINDOW}`); |
| 911 | } |
| 912 | |
| 913 | const batch = await env.DB.batch([ |
| 914 | env.DB.prepare( |
| 915 | `SELECT first_day AS day, |
| 916 | sum(machines) AS installs, |
| 917 | sum(CASE WHEN delay BETWEEN 0 AND ? THEN machines ELSE 0 END) AS activated |
| 918 | FROM cohort_activation |
| 919 | WHERE first_day BETWEEN ? AND ? |
| 920 | GROUP BY first_day`, |
| 921 | ).bind(window, range.from, range.to), |
| 922 | env.DB.prepare(COHORT_COVERAGE_SQL), |
| 923 | ]); |
| 924 | |
| 925 | const rows = rowsOf<ActivationRow>(batch[0]); |
| 926 | const byDay = new Map(rows.map((r) => [r.day, r])); |
| 927 | const labels = dayList(range); |
| 928 | |
| 929 | // Cohorts younger than the window have not finished converting yet, so their |
| 930 | // rate is a floor, not a result. Marked rather than dropped: hiding the last |
| 931 | // week of a conversion chart is its own kind of lie. |
| 932 | const coveredThrough = firstOf<{ day: string | null }>(batch[1])?.day ?? null; |
| 933 | const incompleteFrom = addDays(coveredThrough ?? utcDay(Date.now()), -(window - 1)); |
| 934 | // Nor is a cohort from before new users can be told from existing ones: a machine |
| 935 | // that had been using the tool for months "indexed within a day" of the records |
| 936 | // starting, and a funnel of such machines reads as near-perfect. |
| 937 | const usersFrom = newUsersFrom(env); |
| 938 | const counted = (day: string): boolean => coveredThrough !== null && day <= coveredThrough && day >= usersFrom; |
| 939 | |
| 940 | let installs = 0; |
| 941 | let activated = 0; |
| 942 | const detail = labels.map((day) => { |
| 943 | const row = byDay.get(day); |
| 944 | const dayInstalls = row?.installs ?? 0; |
| 945 | if (!counted(day)) { |
| 946 | return { day, installs: dayInstalls, activated: null, rate: null, complete: false }; |
| 947 | } |
| 948 | const dayActivated = row?.activated ?? 0; |
| 949 | installs += dayInstalls; |
| 950 | activated += dayActivated; |
| 951 | return { |
| 952 | day, |
| 953 | installs: dayInstalls, |
| 954 | activated: dayActivated, |
| 955 | rate: dayInstalls > 0 ? dayActivated / dayInstalls : null, |
| 956 | complete: day < incompleteFrom, |
| 957 | }; |
| 958 | }); |
| 959 | |
| 960 | return { |
| 961 | body: { |
| 962 | range, |
| 963 | window_days: window, |
no test coverage detected