Skip to content

Instantly share code, notes, and snippets.

@andypetrella
Last active March 31, 2019 11:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypetrella/6ebbdcfcb92a80372484c36abec13665 to your computer and use it in GitHub Desktop.
Save andypetrella/6ebbdcfcb92a80372484c36abec13665 to your computer and use it in GitHub Desktop.
Test WebCOLA
numberOfNodes = 10000
avgDegree = 10
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min;
}
function generateId() {
return 'k-' + Math.random().toString(36).substr(2, 32);
}
function getRandomItem(items) {
return items[getRandomInt(0, items.length)];
}
function generateNodes(numberOfNodes, nodes, links) {
let remaining = numberOfNodes - nodes.length
let newNodes = [].concat(nodes)
while (remaining > 0) {
newNodes.push({
id: generateId(),
group: 1
})
remaining--
}
return newNodes
}
function generateLinks (numberOfLinks, nodes, links) {
let remaining = numberOfLinks - links.length
let newLinks = [].concat(links)
while (remaining > 0) {
var source = getRandomItem(nodes)
var target = getRandomItem(nodes)
// check unicity...
newLinks.push({
source: source,
target: target,
value: Math.ceil(Math.random() * 10)+1
})
remaining--
}
return newLinks
}
newGraph = fetch("https://gist.githubusercontent.com/andypetrella/6ebbdcfcb92a80372484c36abec13665/raw/db0f2c8b91bd93f57ba4d3ec59159a8772885512/graph.json")
.then(res => res.json())
.then(data => {
const nodes = generateNodes(numberOfNodes, data.nodes)
const links = generateLinks(numberOfNodes * avgDegree, nodes, data.links)
result = {
nodes: nodes,
links: links
}
return JSON.stringify(result);
})
newGraph.then(console.log)
function work() {
var origins = "https://www.sandbox.kensu.io/services/v1/rtbfs/data/datasources";
const grabGraph = uuid => fetch("https://www.sandbox.kensu.io/services/v1/lineage/"+uuid+"/export-by-datasource-id?granularity=per-table")
.then(res => res.json())
.then(json => buildGraph(json))
.catch(function(error) {
console.log(error);
return [[],[]];
})
const buildGraph = data => {
const nodes = {};
const edges = [];
if (!data) return [nodes, edges];
for (node of data.nodes) {
nodes[node.table] = {
"id": node.tableId,
"group": 1
};
}
for (edge of data.links) {
edges.push({
"source": nodes[edge.source].id,
"target": nodes[edge.target].id,
"value": Math.ceil(Math.random() * 10)+1
});
}
return [nodes, edges];
}
const globalGraph = fetch(origins)
.then((dss) => dss.json())
.then((dss)=>dss.map((ds)=>ds.uuid))
.then((dss) => Promise.all(dss.map((uuid) => grabGraph(uuid))))
.then((gs) => {
const r = gs.reduce(([an, ae], [bn, be]) => {
return [Object.assign({}, an, bn), ae.concat(be)]
})
return r
})
return globalGraph;
}
result = work()
resultString = ""
result.then(([n, e]) => {
resultString = JSON.stringify({
"nodes": Object.values(nodes),
"links": e
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment