-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathbatch.js
More file actions
90 lines (83 loc) · 2.89 KB
/
Copy pathbatch.js
File metadata and controls
90 lines (83 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/**
* Batch query orchestration — run the same query command against multiple targets
* and return all results in a single JSON payload.
*
* Designed for multi-agent swarms that need to dispatch 20+ queries in one call.
*/
import { complexityData } from './complexity.js';
import { flowData } from './flow.js';
import {
contextData,
explainData,
fileDepsData,
fnDepsData,
fnImpactData,
impactAnalysisData,
queryNameData,
whereData,
} from './queries.js';
/**
* Map of supported batch commands → their data function + first-arg semantics.
* `sig` describes how the target string is passed to the data function:
* - 'name' → dataFn(target, dbPath, opts)
* - 'target' → dataFn(target, dbPath, opts)
* - 'file' → dataFn(target, dbPath, opts)
* - 'dbOnly' → dataFn(dbPath, { target, ...opts }) (target goes into opts)
*/
export const BATCH_COMMANDS = {
'fn-impact': { fn: fnImpactData, sig: 'name' },
context: { fn: contextData, sig: 'name' },
explain: { fn: explainData, sig: 'target' },
where: { fn: whereData, sig: 'target' },
query: { fn: queryNameData, sig: 'name' },
fn: { fn: fnDepsData, sig: 'name' },
impact: { fn: impactAnalysisData, sig: 'file' },
deps: { fn: fileDepsData, sig: 'file' },
flow: { fn: flowData, sig: 'name' },
complexity: { fn: complexityData, sig: 'dbOnly' },
};
/**
* Run a query command against multiple targets, returning all results.
*
* @param {string} command - One of the keys in BATCH_COMMANDS
* @param {string[]} targets - List of target names/paths
* @param {string} [customDbPath] - Path to graph.db
* @param {object} [opts] - Shared options passed to every invocation
* @returns {{ command: string, total: number, succeeded: number, failed: number, results: object[] }}
*/
export function batchData(command, targets, customDbPath, opts = {}) {
const entry = BATCH_COMMANDS[command];
if (!entry) {
throw new Error(
`Unknown batch command "${command}". Valid commands: ${Object.keys(BATCH_COMMANDS).join(', ')}`,
);
}
const results = [];
let succeeded = 0;
let failed = 0;
for (const target of targets) {
try {
let data;
if (entry.sig === 'dbOnly') {
// complexityData(dbPath, { target, ...opts })
data = entry.fn(customDbPath, { ...opts, target });
} else {
// All other: dataFn(target, dbPath, opts)
data = entry.fn(target, customDbPath, opts);
}
results.push({ target, ok: true, data });
succeeded++;
} catch (err) {
results.push({ target, ok: false, error: err.message });
failed++;
}
}
return { command, total: targets.length, succeeded, failed, results };
}
/**
* CLI wrapper — calls batchData and prints JSON to stdout.
*/
export function batch(command, targets, customDbPath, opts = {}) {
const data = batchData(command, targets, customDbPath, opts);
console.log(JSON.stringify(data, null, 2));
}