* The graph's executable roots — files that RUN something at module level, * ranked by how much of the project they set in motion. * * The engine records a statement at the top level of a file as an edge from * the *file* node, so `src/bin/codegraph.ts` calling `program.parse()` at *
(
limit: number
)
| 3242 | * The product puts the file that does both at the top. |
| 3243 | */ |
| 3244 | getTopCallingFiles( |
| 3245 | limit: number |
| 3246 | ): Array<{ nodeId: string; filePath: string; calls: number; reaches: number; score: number }> { |
| 3247 | if (limit <= 0) return []; |
| 3248 | return this.db |
| 3249 | .prepare( |
| 3250 | `WITH tops AS ( |
| 3251 | SELECT n.id AS file_id, n.id AS src |
| 3252 | FROM nodes n |
| 3253 | WHERE n.kind = 'file' |
| 3254 | UNION ALL |
| 3255 | SELECT c.source AS file_id, c.target AS src |
| 3256 | FROM edges c |
| 3257 | JOIN nodes f ON f.id = c.source |
| 3258 | JOIN nodes v ON v.id = c.target |
| 3259 | WHERE c.kind = 'contains' |
| 3260 | AND f.kind = 'file' |
| 3261 | AND v.kind IN ('variable', 'constant') |
| 3262 | ), |
| 3263 | runs AS ( |
| 3264 | SELECT t.file_id AS id, COUNT(*) AS calls |
| 3265 | FROM tops t |
| 3266 | JOIN edges e ON e.source = t.src |
| 3267 | WHERE e.kind IN ('calls', 'instantiates') |
| 3268 | GROUP BY t.file_id |
| 3269 | ), |
| 3270 | cand AS ( |
| 3271 | SELECT r.id AS id, n.file_path AS fp, r.calls AS calls |
| 3272 | FROM runs r JOIN nodes n ON n.id = r.id |
| 3273 | ), |
| 3274 | wires AS ( |
| 3275 | SELECT sn.file_path AS fp, COUNT(DISTINCT tn.file_path) AS reaches |
| 3276 | FROM edges e |
| 3277 | JOIN nodes sn ON sn.id = e.source |
| 3278 | JOIN nodes tn ON tn.id = e.target |
| 3279 | WHERE e.kind != 'contains' |
| 3280 | AND sn.file_path <> tn.file_path |
| 3281 | AND sn.file_path IN (SELECT fp FROM cand) |
| 3282 | GROUP BY sn.file_path |
| 3283 | ) |
| 3284 | SELECT c.id AS nodeId, |
| 3285 | c.fp AS filePath, |
| 3286 | c.calls AS calls, |
| 3287 | COALESCE(w.reaches, 0) AS reaches, |
| 3288 | c.calls * (1 + COALESCE(w.reaches, 0)) AS score |
| 3289 | FROM cand c LEFT JOIN wires w ON w.fp = c.fp |
| 3290 | ORDER BY score DESC, calls DESC, filePath |
| 3291 | LIMIT ?` |
| 3292 | ) |
| 3293 | .all(limit) as Array<{ |
| 3294 | nodeId: string; |
| 3295 | filePath: string; |
| 3296 | calls: number; |
| 3297 | reaches: number; |
| 3298 | score: number; |
| 3299 | }>; |
| 3300 | } |
| 3301 |
no test coverage detected