|
| 1 | +import { and, eq, sql } from 'drizzle-orm' |
| 2 | +import { db } from '../src/db/client' |
| 3 | +import { githubContentCache } from '../src/db/schema' |
| 4 | + |
| 5 | +async function main() { |
| 6 | + const repo = 'tanstack/router' |
| 7 | + const gitRef = 'main' |
| 8 | + |
| 9 | + console.log(`\n=== ${repo}@${gitRef} ===`) |
| 10 | + |
| 11 | + const [{ total }] = await db |
| 12 | + .select({ total: sql<number>`count(*)::int` }) |
| 13 | + .from(githubContentCache) |
| 14 | + .where( |
| 15 | + and( |
| 16 | + eq(githubContentCache.repo, repo), |
| 17 | + eq(githubContentCache.gitRef, gitRef), |
| 18 | + ), |
| 19 | + ) |
| 20 | + console.log(`total: ${total}`) |
| 21 | + |
| 22 | + console.log('\nby contentKind:') |
| 23 | + const byKind = await db |
| 24 | + .select({ |
| 25 | + kind: githubContentCache.contentKind, |
| 26 | + rows: sql<number>`count(*)::int`, |
| 27 | + present: sql<number>`count(*) filter (where ${githubContentCache.isPresent})::int`, |
| 28 | + absent: sql<number>`count(*) filter (where not ${githubContentCache.isPresent})::int`, |
| 29 | + }) |
| 30 | + .from(githubContentCache) |
| 31 | + .where( |
| 32 | + and( |
| 33 | + eq(githubContentCache.repo, repo), |
| 34 | + eq(githubContentCache.gitRef, gitRef), |
| 35 | + ), |
| 36 | + ) |
| 37 | + .groupBy(githubContentCache.contentKind) |
| 38 | + console.table(byKind) |
| 39 | + |
| 40 | + console.log('\ntop-level path segment distribution:') |
| 41 | + const byTopSegment = await db.execute(sql` |
| 42 | + select |
| 43 | + content_kind, |
| 44 | + split_part(path, '/', 1) as top, |
| 45 | + count(*)::int as rows |
| 46 | + from github_content_cache |
| 47 | + where repo = ${repo} and git_ref = ${gitRef} |
| 48 | + group by content_kind, top |
| 49 | + order by count(*) desc |
| 50 | + limit 30 |
| 51 | + `) |
| 52 | + console.table(byTopSegment) |
| 53 | + |
| 54 | + console.log('\nfile extension distribution (file kind only):') |
| 55 | + const byExt = await db.execute(sql` |
| 56 | + select |
| 57 | + case |
| 58 | + when path ~ '\\.[a-zA-Z0-9]+$' then regexp_replace(path, '.*\\.([a-zA-Z0-9]+)$', '\\1') |
| 59 | + else '(none)' |
| 60 | + end as ext, |
| 61 | + count(*)::int as rows |
| 62 | + from github_content_cache |
| 63 | + where repo = ${repo} and git_ref = ${gitRef} and content_kind = 'file' |
| 64 | + group by ext |
| 65 | + order by count(*) desc |
| 66 | + limit 30 |
| 67 | + `) |
| 68 | + console.table(byExt) |
| 69 | + |
| 70 | + console.log('\nsample 20 random file paths:') |
| 71 | + const samples = await db.execute(sql` |
| 72 | + select path, is_present, length(coalesce(text_content, '')) as text_len |
| 73 | + from github_content_cache |
| 74 | + where repo = ${repo} and git_ref = ${gitRef} and content_kind = 'file' |
| 75 | + order by random() |
| 76 | + limit 20 |
| 77 | + `) |
| 78 | + console.table(samples) |
| 79 | + |
| 80 | + console.log('\nsample 10 random dir paths:') |
| 81 | + const dirSamples = await db.execute(sql` |
| 82 | + select path, is_present, jsonb_array_length(coalesce(json_content, '[]'::jsonb)) as entries |
| 83 | + from github_content_cache |
| 84 | + where repo = ${repo} and git_ref = ${gitRef} and content_kind = 'dir' |
| 85 | + order by random() |
| 86 | + limit 10 |
| 87 | + `) |
| 88 | + console.table(dirSamples) |
| 89 | + |
| 90 | + console.log('\npath length distribution:') |
| 91 | + const lenBuckets = await db.execute(sql` |
| 92 | + select |
| 93 | + case |
| 94 | + when length(path) < 50 then '< 50' |
| 95 | + when length(path) < 100 then '50-100' |
| 96 | + when length(path) < 200 then '100-200' |
| 97 | + when length(path) < 500 then '200-500' |
| 98 | + else '500+' |
| 99 | + end as bucket, |
| 100 | + count(*)::int as rows |
| 101 | + from github_content_cache |
| 102 | + where repo = ${repo} and git_ref = ${gitRef} |
| 103 | + group by bucket |
| 104 | + order by min(length(path)) |
| 105 | + `) |
| 106 | + console.table(lenBuckets) |
| 107 | +} |
| 108 | + |
| 109 | +main() |
| 110 | + .then(() => process.exit(0)) |
| 111 | + .catch((err) => { |
| 112 | + console.error(err) |
| 113 | + process.exit(1) |
| 114 | + }) |
0 commit comments