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

Function meta

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

* How fresh the data is, and whether either writer has stopped. * * The page's ranges end on today; this is what tells it how much of that range * each kind of number actually covers. Machine counts (`machine_days`, * `machine_first_seen`) are written by the ingest worker as events arrive, so th

(env: Env)

Source from the content-addressed store, hash-verified

245 * for seven weeks. Each flag is a fact the page states, not a tuned threshold.
246 */
247async function meta(env: Env): Promise<ApiResult> {
248 const now = Date.now();
249 const today = utcDay(now);
250 const yesterday = utcDay(now - DAY_MS);
251 const row = await env.DB.prepare(
252 `SELECT (SELECT max(day) FROM daily_event_counts) AS latest_rollup_day,
253 (SELECT min(day) FROM daily_event_counts) AS earliest_rollup_day,
254 (SELECT max(day) FROM machine_days) AS latest_active_day,
255 (SELECT min(day) FROM machine_days) AS earliest_active_day,
256 (SELECT min(day) FROM events) AS earliest_raw_day,
257 (SELECT max(day) FROM events) AS latest_raw_day,
258 (SELECT max(day) FROM usage_daily) AS latest_usage_day,
259 (SELECT count(*) FROM machine_days WHERE day = ?) AS machines_yesterday`,
260 )
261 .bind(yesterday)
262 .first<MetaRow>();
263
264 // How the cron's last run ended, which is what to read first when the numbers are
265 // behind. A query of its own: a dashboard deployed ahead of the worker's migration
266 // has no such table yet, and must still say everything else.
267 let rollupError: { at: string; message: string } | null = null;
268 let rollupLastOk: string | null = null;
269 // What the cron's last run left for the next one: after an outage it fills the
270 // weeks behind the newest days over several runs, and until it has, a chart shows
271 // a day not counted yet as a day with nothing on it.
272 let catchingUp: { days: number; windows: number; cohorts: boolean } | null = null;
273 try {
274 const { results } = await env.DB.prepare(
275 `SELECT key, value FROM rollup_state WHERE key IN ('last_error', 'last_ok', 'last_run')`,
276 ).all<{ key: string; value: string }>();
277 for (const state of results) {
278 if (state.key === 'last_ok') rollupLastOk = state.value;
279 else if (state.key === 'last_run') {
280 const run = JSON.parse(state.value) as Record<string, unknown>;
281 const count = (key: string): number => (typeof run[key] === 'number' ? (run[key] as number) : 0);
282 const left = {
283 days: count('days_left'),
284 windows: count('windows_left'),
285 cohorts: run.cohorts_complete === false,
286 };
287 if (left.days > 0 || left.windows > 0 || left.cohorts) catchingUp = left;
288 } else if (state.value !== '') {
289 const parsed = JSON.parse(state.value) as { at?: unknown; message?: unknown };
290 if (typeof parsed.at === 'string' && typeof parsed.message === 'string') {
291 rollupError = { at: parsed.at, message: parsed.message };
292 }
293 }
294 }
295 } catch (err) {
296 console.error(JSON.stringify({ msg: 'rollup state unreadable', err: String(err) }));
297 }
298
299 const latestRollup = row?.latest_rollup_day ?? null;
300 const latestRaw = row?.latest_raw_day ?? null;
301 const latestUsage = row?.latest_usage_day ?? null;
302 const latestActive = row?.latest_active_day ?? null;
303 // Ingest stores lifecycle events in `events` and usage counters in `usage_daily`, so
304 // either one arriving means it is working — a day of usage and no installs or index

Callers 1

handleApiFunction · 0.70

Calls 7

bindMethod · 0.80
parseMethod · 0.80
utcDayFunction · 0.70
countFunction · 0.70
addDaysFunction · 0.70
prepareMethod · 0.65
errorMethod · 0.65

Tested by

no test coverage detected