Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. var container = {
  2. application: {
  3. id: "awesome-app",
  4. state: {
  5. id: "running"
  6. },
  7. solution: {
  8. id: "awesome-solution",
  9. application: [
  10. {
  11. id: "super-app",
  12. state: {
  13. id: "running"
  14. },
  15. environment: [
  16. {
  17. id: "big-ass-datacentrum"
  18. },
  19. {
  20. id: "small-ass-datacentrum"
  21. },
  22. {
  23. id: "bigger-ass-datacentrum"
  24. },
  25. {
  26. id: "biggest-ass-datacentrum"
  27. }
  28.  
  29. ]
  30. },
  31. {
  32. id: "uber-app",
  33. state: {
  34. id: "running"
  35. },
  36. environment: {
  37. id: "big-ass-datacentrum"
  38. }
  39. },
  40. {
  41. id: "fun-app",
  42. state: {
  43. id: "running"
  44. },
  45. environment: {
  46. id: "big-ass-datacentrum"
  47. }
  48. },
  49. {
  50. id: "boring-app",
  51. state: {
  52. id: "frozen"
  53. },
  54. environment: {
  55. id: "big-ass-datacentrum"
  56. }
  57. }
  58. ]
  59. },
  60. environment: {
  61. id: "big-ass-datacentrum"
  62. }
  63. }
  64. }
  65.  
  66.  
  67. function isVertex(field) {
  68. return ["application", "state", "environment", "solution"].includes(field)
  69. }
  70.  
  71. function mapVertices(parent, data, arrayParentName, arrayParentId) {
  72. let vertices = data.vertices
  73. let edges = data.edges
  74.  
  75. Object.keys(parent).forEach(
  76. key => {
  77.  
  78.  
  79. const fieldValue = parent[key]
  80. let realKey = Array.isArray(parent) ? arrayParentName : key // in an array our key should be our parent's key
  81.  
  82. if (Array.isArray(fieldValue)) {
  83. let result = mapVertices(fieldValue, {vertices: vertices, edges: edges}, key, parent.id)
  84. //let idExists = vertices.filter(vertex => vertex.id === fieldValue.id).length >= 1
  85.  
  86. //vertices = vertices.concat(result.vertices)
  87. vertices = result.vertices
  88. edges = result.edges//edges.concat(result.edges)
  89. }
  90.  
  91. else {
  92. if (isVertex(realKey)) {
  93.  
  94.  
  95. /*result.vertices.forEach(vertex => {
  96. let idExists = vertices.filter(vertex => vertex.id === fieldValue.id).length >= 1
  97. if (!idExists)
  98. vertices.push(vertex)
  99. })*/
  100.  
  101.  
  102. let idExists = vertices.filter(vertex => vertex.id === fieldValue.id).length >= 1
  103. if (!idExists)
  104. vertices.push({label: realKey + "\n" + fieldValue.id, id: fieldValue.id})
  105.  
  106. if (Array.isArray(parent))
  107. edges.push({from: arrayParentId, to: fieldValue.id})
  108. else if (parent.id != undefined)
  109. edges.push({from: parent.id, to: fieldValue.id})
  110.  
  111. if (typeof fieldValue === "object") {
  112. let result = mapVertices(fieldValue, {vertices: vertices, edges: edges})
  113. vertices = result.vertices// vertices.concat(result.vertices)
  114. edges = result.edges// edges.concat(result.edges)
  115. }
  116. }
  117. }
  118. })
  119. return {vertices: vertices, edges: edges}
  120. }
  121.  
  122. graphData = mapVertices(container, {vertices: [], edges: []})
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement