Skip to content

Instantly share code, notes, and snippets.

@Kattoor
Last active January 24, 2017 07:03
Show Gist options
  • Save Kattoor/70052e2a1f31253857b8ebeb9f242799 to your computer and use it in GitHub Desktop.
Save Kattoor/70052e2a1f31253857b8ebeb9f242799 to your computer and use it in GitHub Desktop.
var container = {
application: {
id: "awesome-app",
state: {
id: "running"
},
solution: {
id: "awesome-solution",
application: [
{
id: "super-app",
state: {
id: "running"
},
environment: [
{
id: "big-ass-datacentrum"
},
{
id: "small-ass-datacentrum"
},
{
id: "bigger-ass-datacentrum"
},
{
id: "biggest-ass-datacentrum"
}
]
},
{
id: "uber-app",
state: {
id: "running"
},
environment: {
id: "big-ass-datacentrum"
}
},
{
id: "fun-app",
state: {
id: "running"
},
environment: {
id: "big-ass-datacentrum"
}
},
{
id: "boring-app",
state: {
id: "frozen"
},
environment: {
id: "big-ass-datacentrum"
}
}
]
},
environment: {
id: "big-ass-datacentrum"
}
}
}
function isVertex(field) {
return ["application", "state", "environment", "solution"].includes(field)
}
function mapVertices(parent, data, arrayParentName, arrayParentId) {
let vertices = data.vertices
let edges = data.edges
Object.keys(parent).forEach(
key => {
const fieldValue = parent[key]
let realKey = Array.isArray(parent) ? arrayParentName : key // in an array our key should be our parent's key
if (Array.isArray(fieldValue)) {
let result = mapVertices(fieldValue, {vertices: vertices, edges: edges}, key, parent.id)
vertices = result.vertices
edges = result.edges
}
else {
if (isVertex(realKey)) {
let idExists = vertices.filter(vertex => vertex.id === fieldValue.id).length >= 1
if (!idExists)
vertices.push({label: realKey + "\n" + fieldValue.id, id: fieldValue.id})
if (Array.isArray(parent))
edges.push({from: arrayParentId, to: fieldValue.id})
else if (parent.id != undefined)
edges.push({from: parent.id, to: fieldValue.id})
if (typeof fieldValue === "object") {
let result = mapVertices(fieldValue, {vertices: vertices, edges: edges})
vertices = result.vertices
edges = result.edges
}
}
}
})
return {vertices: vertices, edges: edges}
}
graphData = mapVertices(container, {vertices: [], edges: []})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment