Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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)
- //let idExists = vertices.filter(vertex => vertex.id === fieldValue.id).length >= 1
- //vertices = vertices.concat(result.vertices)
- vertices = result.vertices
- edges = result.edges//edges.concat(result.edges)
- }
- else {
- if (isVertex(realKey)) {
- /*result.vertices.forEach(vertex => {
- let idExists = vertices.filter(vertex => vertex.id === fieldValue.id).length >= 1
- if (!idExists)
- vertices.push(vertex)
- })*/
- 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// vertices.concat(result.vertices)
- edges = result.edges// edges.concat(result.edges)
- }
- }
- }
- })
- return {vertices: vertices, edges: edges}
- }
- graphData = mapVertices(container, {vertices: [], edges: []})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement