MCPcopy Create free account
hub / github.com/colbymchenry/codegraph / activation

Function activation

telemetry-dashboard/src/api.ts:906–985  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

904 * gaps, like every other counted number on the page.
905 */
906async 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,

Callers 1

handleApiFunction · 0.70

Calls 14

NumberInterface · 0.85
dayListFunction · 0.85
firstOfFunction · 0.85
newUsersFromFunction · 0.85
bindMethod · 0.80
mapMethod · 0.80
failFunction · 0.70
rowsOfFunction · 0.70
addDaysFunction · 0.70
utcDayFunction · 0.70
countedFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected