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

Method getTopCallingFiles

src/db/queries.ts:3210–3266  ·  view source on GitHub ↗

* 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
  )

Source from the content-addressed store, hash-verified

3208 * The product puts the file that does both at the top.
3209 */
3210 getTopCallingFiles(
3211 limit: number
3212 ): Array<{ nodeId: string; filePath: string; calls: number; reaches: number; score: number }> {
3213 if (limit <= 0) return [];
3214 return this.db
3215 .prepare(
3216 `WITH tops AS (
3217 SELECT n.id AS file_id, n.id AS src
3218 FROM nodes n
3219 WHERE n.kind = 'file'
3220 UNION ALL
3221 SELECT c.source AS file_id, c.target AS src
3222 FROM edges c
3223 JOIN nodes f ON f.id = c.source
3224 JOIN nodes v ON v.id = c.target
3225 WHERE c.kind = 'contains'
3226 AND f.kind = 'file'
3227 AND v.kind IN ('variable', 'constant')
3228 ),
3229 runs AS (
3230 SELECT t.file_id AS id, COUNT(*) AS calls
3231 FROM tops t
3232 JOIN edges e ON e.source = t.src
3233 WHERE e.kind IN ('calls', 'instantiates')
3234 GROUP BY t.file_id
3235 ),
3236 cand AS (
3237 SELECT r.id AS id, n.file_path AS fp, r.calls AS calls
3238 FROM runs r JOIN nodes n ON n.id = r.id
3239 ),
3240 wires AS (
3241 SELECT sn.file_path AS fp, COUNT(DISTINCT tn.file_path) AS reaches
3242 FROM edges e
3243 JOIN nodes sn ON sn.id = e.source
3244 JOIN nodes tn ON tn.id = e.target
3245 WHERE e.kind != 'contains'
3246 AND sn.file_path <> tn.file_path
3247 AND sn.file_path IN (SELECT fp FROM cand)
3248 GROUP BY sn.file_path
3249 )
3250 SELECT c.id AS nodeId,
3251 c.fp AS filePath,
3252 c.calls AS calls,
3253 COALESCE(w.reaches, 0) AS reaches,
3254 c.calls * (1 + COALESCE(w.reaches, 0)) AS score
3255 FROM cand c LEFT JOIN wires w ON w.fp = c.fp
3256 ORDER BY score DESC, calls DESC, filePath
3257 LIMIT ?`
3258 )
3259 .all(limit) as Array<{
3260 nodeId: string;
3261 filePath: string;
3262 calls: number;
3263 reaches: number;
3264 score: number;
3265 }>;
3266 }
3267

Callers 1

executableFilesFunction · 0.45

Calls 2

allMethod · 0.65
prepareMethod · 0.65

Tested by

no test coverage detected