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

Function timeseries

telemetry-dashboard/src/api.ts:640–694  ·  view source on GitHub ↗
(env: Env, url: URL, range: Range)

Source from the content-addressed store, hash-verified

638};
639
640async function timeseries(env: Env, url: URL, range: Range): Promise<ApiResult> {
641 const metric = url.searchParams.get('metric') ?? 'installs_uninstalls';
642
643 // The one metric whose series are data-driven rather than fixed: one line per
644 // duration bucket, in bucket order (an ordered scale, so the order is meaning).
645 if (metric === 'duration_buckets') return durationBucketSeries(env, range);
646
647 const spec = SERIES[metric];
648 if (!spec) {
649 return fail(`unknown metric — one of: ${[...Object.keys(SERIES), 'duration_buckets'].join(', ')}`);
650 }
651
652 const batch = await env.DB.batch([
653 env.DB.prepare(spec.sql).bind(...spec.binds(range)),
654 env.DB.prepare(spec.coverage ?? COVERAGE_SQL),
655 ]);
656 const results = rowsOf<DayValueRow>(batch[0]);
657 const coveredThrough = spec.live ? null : (firstOf<{ day: string | null }>(batch[1])?.day ?? null);
658
659 const labels = dayList(range);
660 const a = new Map<string, number>();
661 const b = new Map<string, number>();
662 const notCounted = new Set<string>();
663 for (const row of results) {
664 if (row.a === null && spec.gaps) notCounted.add(row.day);
665 a.set(row.day, row.a ?? 0);
666 b.set(row.day, row.b ?? 0);
667 }
668
669 const fill = (byDay: Map<string, number>): (number | null)[] =>
670 spec.live ? densify(labels, byDay) : densifyCovered(labels, byDay, coveredThrough);
671 const usersFrom = spec.newUsers ? newUsersFrom(env) : '';
672 const first = fill(a).map((value, i) => (notCounted.has(labels[i]!) || labels[i]! < usersFrom ? null : value));
673 const datasets = [{ label: spec.labels[0], data: first }];
674 if (spec.labels.length === 2) datasets.push({ label: spec.labels[1], data: fill(b) });
675
676 return {
677 body: {
678 range,
679 metric,
680 title: spec.title,
681 /** Last day with real numbers; null for a live series, which runs through today. */
682 covered_through: spec.live ? null : coveredThrough,
683 /** The first day this series is counted from, when that is after the start of the range. */
684 counted_from: usersFrom > range.from ? usersFrom : null,
685 labels,
686 datasets,
687 rows: labels.map((day, i) => ({
688 day,
689 ...Object.fromEntries(datasets.map((d) => [d.label, d.data[i] ?? null])),
690 })),
691 },
692 cacheControl: CACHE_CONTROL,
693 };
694}
695
696/** "Session run length over time": one series per duration bucket, bucket-ordered. */
697async function durationBucketSeries(env: Env, range: Range): Promise<ApiResult> {

Callers 1

handleApiFunction · 0.85

Calls 15

durationBucketSeriesFunction · 0.85
firstOfFunction · 0.85
dayListFunction · 0.85
newUsersFromFunction · 0.85
bindMethod · 0.80
mapMethod · 0.80
hasMethod · 0.80
pushMethod · 0.80
failFunction · 0.70
rowsOfFunction · 0.70
fillFunction · 0.70
getMethod · 0.65

Tested by

no test coverage detected