2024-09-24 10:56:04 -04:00
|
|
|
const { JSDOM } = require("jsdom");
|
|
|
|
const graphology = require("graphology");
|
|
|
|
const { circular } = require('graphology-layout');
|
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
|
|
|
|
const graphQueryString = `
|
|
|
|
select p.number, p.html, p.time, a.contents
|
|
|
|
from pages p
|
|
|
|
left join attributes a on a.number = p.number
|
|
|
|
where time >= (
|
|
|
|
select max(s.time)
|
|
|
|
from pages s
|
|
|
|
where s.number = p.number
|
|
|
|
)
|
|
|
|
order by p.time desc
|
|
|
|
`;
|
|
|
|
|
2024-09-24 10:56:04 -04:00
|
|
|
function graphFromList(allTheStuff) {
|
|
|
|
const graph = new graphology.Graph();
|
2024-10-05 12:01:19 -04:00
|
|
|
for (const {number, html} of allTheStuff) {
|
2024-10-05 17:38:48 -04:00
|
|
|
if (!graph.hasNode(number)) graph.addNode(number);
|
2024-09-24 10:56:04 -04:00
|
|
|
}
|
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
for (const {number, html, contents} of allTheStuff) {
|
2024-09-24 10:56:04 -04:00
|
|
|
const { document } = (new JSDOM(html)).window;
|
|
|
|
const links = document.querySelectorAll('a');
|
|
|
|
links.forEach((link) => {
|
|
|
|
const referent = link.href.replace("/","");
|
2024-10-25 17:48:12 -04:00
|
|
|
graph.mergeEdge(number, referent, {color: "white"});
|
2024-09-24 10:56:04 -04:00
|
|
|
});
|
2024-10-25 17:48:12 -04:00
|
|
|
|
|
|
|
if (typeof contents?.verbs?.forEach === 'function') {
|
|
|
|
contents?.verbs?.forEach((verbNumber) => {
|
|
|
|
graph.mergeEdge(number, verbNumber, {color: "red"})
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof contents?.contents?.forEach === 'function') {
|
|
|
|
contents?.contents?.forEach((nounNumber) => {
|
|
|
|
graph.mergeEdge(number, nounNumber, {color: "green"});
|
|
|
|
});
|
|
|
|
}
|
2024-09-24 10:56:04 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
circular.assign(graph);
|
|
|
|
|
|
|
|
return graph;
|
|
|
|
}
|
|
|
|
|
2024-10-25 17:48:12 -04:00
|
|
|
module.exports = { graphFromList, graphQueryString };
|