Skip to content

Commit 09e407c

Browse files
committed
update: report.js auto generate the user edges graph
1 parent 2f60dbd commit 09e407c

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

src/commands/setup/.foam/templates/report.js

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,10 +167,14 @@ function generateFoamGraph(foam) {
167167
nodeInfo: {},
168168
edges: new Set(),
169169
userEdges: new Set(),
170+
hostEdges: new Set(),
170171
};
171172
foam.workspace.list().forEach((n) => {
172173
const type = n.type === "note" ? n.properties.type ?? "note" : n.type;
173174
const title = n.type === "note" ? n.title : n.uri.getBasename();
175+
if (type === "report") {
176+
return // ignore all report type notes
177+
}
174178
graph.nodeInfo[n.uri.path] = {
175179
id: n.uri.path,
176180
type: type,
@@ -189,12 +193,18 @@ function generateFoamGraph(foam) {
189193
});
190194
const sourceNode = graph.nodeInfo[sourcePath];
191195
const targetNode = graph.nodeInfo[targetPath];
192-
if (sourceNode && targetNode && targetNode.type === "user") {
196+
if (sourceNode && targetNode && (targetNode.type === "user" || sourceNode.type === "user")) {
193197
graph.userEdges.add({
194198
source: sourceNode.id,
195199
target: targetNode.id,
196200
});
197201
}
202+
if (sourceNode && targetNode && (targetNode.type === "host" || sourceNode.type === "host")) {
203+
graph.hostEdges.add({
204+
source: sourceNode.id,
205+
target: targetNode.id,
206+
});
207+
}
198208
});
199209
// console.log("Graph nodes:", graph.nodeInfo);
200210
// console.log("Graph edges: ", foam.graph.getAllConnections());
@@ -203,13 +213,41 @@ function generateFoamGraph(foam) {
203213
return graph;
204214
}
205215

216+
function calcTheMermaid(foam, graph) {
217+
function getId(uri) {
218+
return foam.workspace.getIdentifier(uri) || "";
219+
}
220+
let ret = {
221+
hostEdges: [],
222+
userEdges: []
223+
}
224+
for (const hostEdge of Array.from(graph.hostEdges)) {
225+
// Perform calculations or modifications based on host edges
226+
const { source, target } = hostEdge;
227+
let sourceNode = graph.nodeInfo[source];
228+
let targetNode = graph.nodeInfo[target];
229+
if (sourceNode && targetNode && targetNode.type === "host" && sourceNode.type === "host") {
230+
ret.hostEdges.push(`${getId(sourceNode.uri)} ---> ${getId(targetNode.uri)}`);
231+
}
232+
}
233+
for (const userEdge of Array.from(graph.userEdges)) {
234+
// Perform calculations or modifications based on user edges
235+
const { source, target } = userEdge;
236+
let sourceNode = graph.nodeInfo[source];
237+
let targetNode = graph.nodeInfo[target];
238+
if (sourceNode && targetNode) {
239+
ret.userEdges.push(`${getId(sourceNode.uri)} ---> ${getId(targetNode.uri)}`);
240+
}
241+
}
242+
return ret;
243+
}
244+
206245
let meta = `---
207246
title: Final Penetration Testing Report
208247
type: report
209248
---
210249
211250
# Final Penetration Testing Report
212-
213251
`;
214252

215253
function checkArrayDiffElements(arr1, arr2) {
@@ -233,7 +271,7 @@ async function createNote({ trigger, foam, resolver, foamDate }) {
233271
function getId(uri) {
234272
return foam.workspace.getIdentifier(uri) || "";
235273
}
236-
console.log("Creating note for trigger:", trigger);
274+
console.log("BEGIN ======= Creating note for trigger:", trigger);
237275
console.log("Foam instance:", Object.keys(foam));
238276
const graph = generateFoamGraph(foam);
239277
console.log("Generated graph!");
@@ -305,10 +343,27 @@ async function createNote({ trigger, foam, resolver, foamDate }) {
305343
});
306344
userInformation.concat(extraNotes);
307345

308-
let body = `## Hosts Information
346+
let grapher = calcTheMermaid(foam, graph);
347+
console.log("Grapher:", grapher);
348+
349+
// You can use the grapher object to generate a mermaid diagram
350+
// For example:
351+
let mermaidDiagram = `graph TD;\n`;
352+
for (const edge of grapher.userEdges) {
353+
mermaidDiagram += ` ${edge}\n`;
354+
}
355+
mermaidDiagram = "```mermaid\n" + mermaidDiagram + "```";
356+
console.log("Mermaid Diagram:", mermaidDiagram);
357+
358+
359+
let body = `${meta}
360+
## Hosts Information
309361
310362
${hostInformation.join("\n")}
311363
364+
## Users Relation graph
365+
${mermaidDiagram}
366+
312367
${userInformation.join("\n")}
313368
`;
314369

@@ -319,6 +374,6 @@ ${userInformation.join("\n")}
319374

320375
return {
321376
filepath: "report.md",
322-
content: meta + body,
377+
content: body,
323378
};
324379
}

0 commit comments

Comments
 (0)