|
| 1 | +import db from '@codebuff/internal/db' |
| 2 | +import * as schema from '@codebuff/internal/db/schema' |
| 3 | +import { sql, eq, desc } from 'drizzle-orm' |
| 4 | + |
| 5 | +async function main() { |
| 6 | + const email = process.argv[2] |
| 7 | + if (!email) { |
| 8 | + console.error('usage: bun scripts/investigate-user.ts <email>') |
| 9 | + process.exit(1) |
| 10 | + } |
| 11 | + |
| 12 | + const users = await db |
| 13 | + .select() |
| 14 | + .from(schema.user) |
| 15 | + .where(sql`lower(${schema.user.email}) = ${email.toLowerCase()}`) |
| 16 | + |
| 17 | + if (users.length === 0) { |
| 18 | + console.log('user not found') |
| 19 | + return |
| 20 | + } |
| 21 | + const u = users[0] |
| 22 | + console.log('=== user ===') |
| 23 | + console.log(JSON.stringify({ |
| 24 | + id: u.id, |
| 25 | + email: u.email, |
| 26 | + name: u.name, |
| 27 | + handle: u.handle, |
| 28 | + banned: u.banned, |
| 29 | + created_at: u.created_at, |
| 30 | + emailVerified: u.emailVerified, |
| 31 | + image: u.image, |
| 32 | + }, null, 2)) |
| 33 | + |
| 34 | + const accounts = await db |
| 35 | + .select() |
| 36 | + .from(schema.account) |
| 37 | + .where(eq(schema.account.userId, u.id)) |
| 38 | + console.log('\n=== accounts ===') |
| 39 | + for (const a of accounts) { |
| 40 | + console.log(` provider=${a.provider} providerAccountId=${a.providerAccountId} scope=${a.scope ?? ''}`) |
| 41 | + } |
| 42 | + |
| 43 | + const stats = await db |
| 44 | + .select({ |
| 45 | + agent_id: schema.message.agent_id, |
| 46 | + count: sql<number>`COUNT(*)`, |
| 47 | + totalCost: sql<number>`SUM(${schema.message.cost})`, |
| 48 | + first: sql<string>`MIN(${schema.message.finished_at})`, |
| 49 | + last: sql<string>`MAX(${schema.message.finished_at})`, |
| 50 | + }) |
| 51 | + .from(schema.message) |
| 52 | + .where(eq(schema.message.user_id, u.id)) |
| 53 | + .groupBy(schema.message.agent_id) |
| 54 | + console.log('\n=== messages by agent ===') |
| 55 | + for (const s of stats) { |
| 56 | + console.log(` ${s.agent_id}: ${s.count} msgs, $${Number(s.totalCost).toFixed(2)}, ${s.first} → ${s.last}`) |
| 57 | + } |
| 58 | + |
| 59 | + const repos = await db |
| 60 | + .select({ |
| 61 | + repo_url: schema.message.repo_url, |
| 62 | + count: sql<number>`COUNT(*)`, |
| 63 | + }) |
| 64 | + .from(schema.message) |
| 65 | + .where(eq(schema.message.user_id, u.id)) |
| 66 | + .groupBy(schema.message.repo_url) |
| 67 | + .orderBy(desc(sql`COUNT(*)`)) |
| 68 | + .limit(20) |
| 69 | + console.log('\n=== repos touched ===') |
| 70 | + for (const r of repos) { |
| 71 | + console.log(` ${r.count.toString().padStart(5)} ${r.repo_url ?? '(null)'}`) |
| 72 | + } |
| 73 | + |
| 74 | + const sample = await db |
| 75 | + .select({ |
| 76 | + finished_at: schema.message.finished_at, |
| 77 | + agent_id: schema.message.agent_id, |
| 78 | + repo_url: schema.message.repo_url, |
| 79 | + input_tokens: schema.message.input_tokens, |
| 80 | + output_tokens: schema.message.output_tokens, |
| 81 | + cost: schema.message.cost, |
| 82 | + lastMessage: schema.message.lastMessage, |
| 83 | + }) |
| 84 | + .from(schema.message) |
| 85 | + .where(eq(schema.message.user_id, u.id)) |
| 86 | + .orderBy(desc(schema.message.finished_at)) |
| 87 | + .limit(5) |
| 88 | + console.log('\n=== 5 most recent messages (last user turn) ===') |
| 89 | + for (const m of sample) { |
| 90 | + console.log(`\n ${m.finished_at.toISOString()} agent=${m.agent_id} repo=${m.repo_url ?? ''} in=${m.input_tokens} out=${m.output_tokens} cost=$${Number(m.cost).toFixed(4)}`) |
| 91 | + const msg = m.lastMessage as any |
| 92 | + const content = typeof msg?.content === 'string' ? msg.content : JSON.stringify(msg?.content)?.slice(0, 500) |
| 93 | + console.log(` role=${msg?.role} content=${(content ?? '').slice(0, 500)}`) |
| 94 | + } |
| 95 | + |
| 96 | + // Session/CLI usage |
| 97 | + const sessions = await db |
| 98 | + .select({ |
| 99 | + type: schema.session.type, |
| 100 | + created_at: schema.session.created_at, |
| 101 | + fingerprint_id: schema.session.fingerprint_id, |
| 102 | + }) |
| 103 | + .from(schema.session) |
| 104 | + .where(eq(schema.session.userId, u.id)) |
| 105 | + .orderBy(desc(schema.session.created_at)) |
| 106 | + .limit(10) |
| 107 | + console.log('\n=== recent sessions ===') |
| 108 | + for (const s of sessions) { |
| 109 | + console.log(` ${s.created_at.toISOString()} type=${s.type} fp=${s.fingerprint_id ?? ''}`) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +main().then(() => process.exit(0)).catch((e) => { console.error(e); process.exit(1) }) |
0 commit comments