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

Function blastScaleFor

src/ui-server/api/stats.ts:55–88  ·  view source on GitHub ↗
(
  cg: CodeGraph,
  projectRoot: string,
  edgeCount: number
)

Source from the content-addressed store, hash-verified

53let cachedScale: { key: string; value: WireBlastScale } | null = null;
54
55export function blastScaleFor(
56 cg: CodeGraph,
57 projectRoot: string,
58 edgeCount: number
59): WireBlastScale {
60 // Keyed on the project AND the index's stamp AND its edge count, so a
61 // re-index (or a sync that only moved edges) invalidates it and two indexes
62 // opened by one process cannot share a denominator. A stale one would
63 // silently rescale every bar in the app.
64 const key = `${projectRoot}\u0000${cg.getLastIndexedAt() ?? 0}:${edgeCount}`;
65 if (cachedScale?.key === key) return cachedScale.value;
66
67 const top = cg.getTopDependedOn(BLAST_SCALE_SAMPLE);
68 let maxWithinHops = 0;
69 for (const candidate of top) {
70 try {
71 const subgraph = cg.getImpactRadius(candidate.nodeId, BLAST_DEPTH);
72 maxWithinHops = Math.max(maxWithinHops, subgraph.nodes.size - 1);
73 } catch {
74 // A candidate that cannot be traversed (a node the edge table names but
75 // the node table lost) narrows the sample; it must not fail the screen.
76 }
77 }
78
79 const value: WireBlastScale = {
80 maxDirect: top[0]?.dependents ?? 0,
81 maxWithinHops,
82 hops: BLAST_DEPTH,
83 sampled: top.length,
84 estimated: true,
85 };
86 cachedScale = { key, value };
87 return value;
88}
89
90/** Drop the memoised scale — for tests, which build a fresh index per case. */
91export function resetBlastScaleCache(): void {

Callers 1

buildStatsFunction · 0.85

Calls 3

getLastIndexedAtMethod · 0.45
getTopDependedOnMethod · 0.45
getImpactRadiusMethod · 0.45

Tested by

no test coverage detected