73 lines · 4.6 KBRaw
| 1 | import { now } from "../auth.ts"; |
| 2 | import type { AdminDay, AdminOverview, AdminPerson, AdminTotals } from "../contract/views.ts"; |
| 3 | |
| 4 | const DAY = 86400; |
| 5 | const WINDOW_DAYS = 30; |
| 6 | |
| 7 | const ACTIVITY = `WITH activity(user_id, at) AS ( |
| 8 | SELECT CASE actor_kind WHEN 'human' THEN actor_id ELSE on_behalf_of END, created_at FROM events WHERE created_at >= ?1 |
| 9 | UNION ALL SELECT user_id, created_at FROM agent_sessions WHERE created_at >= ?1 |
| 10 | UNION ALL SELECT user_id, last_seen_at FROM agent_sessions WHERE last_seen_at >= ?1 |
| 11 | UNION ALL SELECT id, last_seen_at FROM users WHERE last_seen_at >= ?1 |
| 12 | )`; |
| 13 | |
| 14 | const PEOPLE = `SELECT u.id, u.name, u.email, u.handle, u.status, u.created_at AS joinedAt, u.last_seen_at AS lastSeenAt, |
| 15 | s.last AS lastAgentAt, coalesce(s.n, 0) AS agentSessions, coalesce(r.n, 0) AS repos, coalesce(p.n, 0) AS pulls, coalesce(e.n, 0) AS events |
| 16 | FROM users u |
| 17 | LEFT JOIN (SELECT user_id, count(*) AS n, max(coalesce(last_seen_at, created_at)) AS last FROM agent_sessions GROUP BY user_id) s ON s.user_id = u.id |
| 18 | LEFT JOIN (SELECT creator_id, count(*) AS n FROM repos GROUP BY creator_id) r ON r.creator_id = u.id |
| 19 | LEFT JOIN (SELECT author_id, count(*) AS n FROM pulls GROUP BY author_id) p ON p.author_id = u.id |
| 20 | LEFT JOIN (SELECT coalesce(on_behalf_of, actor_id) AS uid, count(*) AS n FROM events GROUP BY uid) e ON e.uid = u.id |
| 21 | ORDER BY u.created_at DESC`; |
| 22 | |
| 23 | type Counted = { day: string; n: number }; |
| 24 | |
| 25 | export async function adminOverview(db: D1Database): Promise<AdminOverview> { |
| 26 | const at = now(); |
| 27 | const since = at - WINDOW_DAYS * DAY; |
| 28 | const [totals, lastActive, active, signups, sessions, events, runtimes, actions, people] = await db.batch([ |
| 29 | db |
| 30 | .prepare( |
| 31 | `SELECT (SELECT count(*) FROM users) AS people, |
| 32 | (SELECT count(*) FROM users WHERE created_at >= ?1) AS signups7d, |
| 33 | (SELECT count(*) FROM users WHERE created_at >= ?2) AS signups30d, |
| 34 | (SELECT count(*) FROM namespaces WHERE kind = 'org') AS orgs, |
| 35 | (SELECT count(*) FROM repos) AS repos, |
| 36 | (SELECT count(*) FROM pulls) AS pulls, |
| 37 | (SELECT count(*) FROM pulls WHERE status = 'merged') AS mergedPulls, |
| 38 | (SELECT count(*) FROM agent_sessions) AS agentSessions, |
| 39 | (SELECT count(*) FROM decisions) AS decisions`, |
| 40 | ) |
| 41 | .bind(at - 7 * DAY, since), |
| 42 | db.prepare(`${ACTIVITY} SELECT max(at) AS last FROM activity WHERE user_id IS NOT NULL GROUP BY user_id`).bind(since), |
| 43 | db.prepare(`${ACTIVITY} SELECT date(at, 'unixepoch') AS day, count(DISTINCT user_id) AS n FROM activity WHERE user_id IS NOT NULL GROUP BY day`).bind(since), |
| 44 | db.prepare("SELECT date(created_at, 'unixepoch') AS day, count(*) AS n FROM users WHERE created_at >= ? GROUP BY day").bind(since), |
| 45 | db.prepare("SELECT date(created_at, 'unixepoch') AS day, count(*) AS n FROM agent_sessions WHERE created_at >= ? GROUP BY day").bind(since), |
| 46 | db.prepare("SELECT date(created_at, 'unixepoch') AS day, count(*) AS n FROM events WHERE created_at >= ? GROUP BY day").bind(since), |
| 47 | db.prepare("SELECT runtime, count(*) AS sessions FROM agent_sessions GROUP BY runtime ORDER BY sessions DESC"), |
| 48 | db.prepare("SELECT kind, count(*) AS count FROM events WHERE created_at >= ? GROUP BY kind ORDER BY count DESC LIMIT 20").bind(since), |
| 49 | db.prepare(PEOPLE), |
| 50 | ]); |
| 51 | const lasts = (lastActive.results as { last: number }[]).map((row) => row.last); |
| 52 | const activeWithin = (days: number) => lasts.filter((last) => last >= at - days * DAY).length; |
| 53 | return { |
| 54 | totals: { ...(totals.results[0] as Omit<AdminTotals, "activeToday" | "active7d" | "active30d">), activeToday: activeWithin(1), active7d: activeWithin(7), active30d: lasts.length }, |
| 55 | days: fillDays(at, { |
| 56 | active: active.results as Counted[], |
| 57 | signups: signups.results as Counted[], |
| 58 | agentSessions: sessions.results as Counted[], |
| 59 | events: events.results as Counted[], |
| 60 | }), |
| 61 | runtimes: runtimes.results as { runtime: string; sessions: number }[], |
| 62 | actions: actions.results as { kind: string; count: number }[], |
| 63 | people: people.results as AdminPerson[], |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | export function fillDays(at: number, series: Record<Exclude<keyof AdminDay, "day">, Counted[]>): AdminDay[] { |
| 68 | const lookup = Object.fromEntries(Object.entries(series).map(([key, rows]) => [key, new Map(rows.map((row) => [row.day, row.n]))])); |
| 69 | return Array.from({ length: WINDOW_DAYS }, (_, index) => { |
| 70 | const day = new Date((at - (WINDOW_DAYS - 1 - index) * DAY) * 1000).toISOString().slice(0, 10); |
| 71 | return { day, active: lookup.active.get(day) ?? 0, signups: lookup.signups.get(day) ?? 0, agentSessions: lookup.agentSessions.get(day) ?? 0, events: lookup.events.get(day) ?? 0 }; |
| 72 | }); |
| 73 | } |