Website for visualizing a persons github network.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

335 lines
7.3 KiB

  1. var nodes;
  2. var edges;
  3. var options = {
  4. nodes: {
  5. borderWidth:4,
  6. size:30,
  7. color: {
  8. border: '#222222',
  9. background: '#666666'
  10. },
  11. font:{color:'#eeeeee'}
  12. },
  13. edges: {
  14. color: 'lightgray'
  15. }
  16. };
  17. /**
  18. * Checks if a user is a node in the graph
  19. *
  20. * @param userID
  21. * @returns {boolean}
  22. */
  23. function alreadyInGraph(userID)
  24. {
  25. for(var i = 0; i < nodes.length; i++)
  26. {
  27. if(nodes[i].id === userID)
  28. {
  29. return true;
  30. }
  31. }
  32. return false;
  33. }
  34. /**
  35. * adds a person to the nodes list
  36. *
  37. * @param profileData
  38. */
  39. function addPersonToGraph(profileData)
  40. {
  41. nodes.push(
  42. {
  43. id:profileData.id,
  44. name:profileData.login,
  45. shape: 'circularImage',
  46. image:profileData.avatar_url
  47. });
  48. }
  49. /**
  50. * Adds the followers/following of a person
  51. * to the graph
  52. *
  53. * @param username
  54. * @param apiPath
  55. * @returns {Promise<any>}
  56. */
  57. function addFriends(username, apiPath, page)
  58. {
  59. console.log(username + " page=" + page);
  60. return new Promise(function(resolve, reject)
  61. {
  62. queryAPIByUser(apiPath + "?page=" + page, username, function(data)
  63. {
  64. console.log(data);
  65. console.log(data.length);
  66. for(var i = 0; i < data.length; i++)
  67. {
  68. if(!alreadyInGraph(data[i].id))
  69. {
  70. addPersonToGraph(data[i]);
  71. }
  72. }
  73. if(data.length === 30)
  74. {
  75. addFriends(username, apiPath, page+ 1).then(function()
  76. {
  77. resolve();
  78. })
  79. }
  80. else
  81. {
  82. resolve();
  83. }
  84. },
  85. function(error)
  86. {
  87. reject(error);
  88. })
  89. });
  90. }
  91. /**
  92. * Greedy function which checks to see if a edge is in the graphs
  93. *
  94. * @param id1
  95. * @param id2
  96. * @returns {boolean}
  97. */
  98. function edgeInGraph(id1, id2)
  99. {
  100. console.log("edge check");
  101. for(var i = 0;i < edges.length; i++)
  102. {
  103. if(edges[i].from === id1 && edges[i].to === id2)
  104. {
  105. return true;
  106. }
  107. if(edges[i].to === id1 && edges[i].from === id2)
  108. {
  109. return true;
  110. }
  111. }
  112. return false;
  113. }
  114. /**
  115. * Adds a connection to the graph
  116. *
  117. * @param person1
  118. * @param person2
  119. */
  120. function addConnection(person1, person2)
  121. {
  122. if(person1.id !== person2.id)
  123. {
  124. if(alreadyInGraph(person2.id) && !edgeInGraph(person1.id, person2.id))
  125. {
  126. edges.push(
  127. {
  128. from: person1.id,
  129. to: person2.id
  130. });
  131. }
  132. }
  133. }
  134. function processConnections(user, apiPoint, page)
  135. {
  136. return new Promise(function(resolve, reject)
  137. {
  138. queryAPIByUser(apiPoint + "?page=" + page, user.name,
  139. function(data)
  140. {
  141. for(var i = 0; i < data.length; i++)
  142. {
  143. addConnection(user, data[i])
  144. }
  145. if(data.length === 30)
  146. {
  147. processConnections(user, apiPoint, page + 1).then(function()
  148. {
  149. resolve();
  150. });
  151. }
  152. else
  153. {
  154. resolve();
  155. }
  156. }, function(error)
  157. {
  158. console.log(error);
  159. resolve();
  160. })
  161. })
  162. }
  163. /**
  164. * Processes all the connections of a user and adds them to the graph
  165. *
  166. * @param user has .id and .name
  167. * @returns {Promise<any>}
  168. */
  169. function processUserConnections(user)
  170. {
  171. return new Promise(function(resolve, reject)
  172. {
  173. processConnections(user, API_FOLLOWING, 1).then(function()
  174. {
  175. processConnections(user, API_FOLLOWERS, 1).then(function()
  176. {
  177. resolve();
  178. })
  179. })
  180. // queryAPIByUser(API_FOLLOWING, user.name,
  181. // function(data)
  182. // {
  183. // for(var i = 0; i < data.length; i++)
  184. // {
  185. // addConnection(user, data[i])
  186. // }
  187. //
  188. // queryAPIByUser(API_FOLLOWERS, user.name, function(data2)
  189. // {
  190. // for(var i = 0; i < data2.length; i++)
  191. // {
  192. // addConnection(user, data2[i]);
  193. // }
  194. // resolve();
  195. // },
  196. // function(error)
  197. // {
  198. // // reject(error);
  199. // resolve();
  200. // });
  201. // },
  202. // function(error)
  203. // {
  204. // // reject(error);
  205. // resolve();
  206. // })
  207. });
  208. }
  209. /**
  210. * Creates connections between all the nodes in
  211. * the graph.
  212. *
  213. * @returns {Promise<any>}
  214. */
  215. function createConnections()
  216. {
  217. return new Promise(function(resolve, reject)
  218. {
  219. var prom = [];
  220. for(var i = 0; i < nodes.length; i++)
  221. {
  222. prom.push(processUserConnections(nodes[i]));
  223. }
  224. Promise.all(prom).then(function()
  225. {
  226. resolve();
  227. }).catch(function(error)
  228. {
  229. console.log(error);
  230. resolve();
  231. });
  232. });
  233. }
  234. /**
  235. * Adds the base person to the graph.
  236. *
  237. * @param username
  238. * @returns {Promise<any>}
  239. */
  240. function addSelfToGraph(username)
  241. {
  242. return new Promise(function(resolve, reject)
  243. {
  244. queryAPIByUser("", username, function(data)
  245. {
  246. addPersonToGraph(data);
  247. resolve();
  248. },
  249. function(error)
  250. {
  251. reject(error);
  252. });
  253. });
  254. }
  255. function bringUpProfileView(id)
  256. {
  257. for(var i = 0; i < nodes.length; i++)
  258. {
  259. if(nodes[i].id === id)
  260. {
  261. profileGen(nodes[i].name, "profileGen");
  262. }
  263. }
  264. }
  265. /**
  266. * Creates a graph
  267. * @param username
  268. * @param containerName
  269. * @param graphsTitle
  270. */
  271. function createFriendsGraph(username, containerName, graphsTitle)
  272. {
  273. nodes = [];
  274. edges = [];
  275. addSelfToGraph(username).then(function()
  276. {
  277. addFriends(username, API_FOLLOWERS,1).then(function()
  278. {
  279. addFriends(username, API_FOLLOWING,1).then(function()
  280. {
  281. createConnections().then(function()
  282. {
  283. var container = document.getElementById(containerName);
  284. var data =
  285. {
  286. nodes: nodes,
  287. edges: edges
  288. };
  289. var network = new vis.Network(container, data, options);
  290. network.on("click", function (params)
  291. {
  292. params.event = "[original event]";
  293. if(Number(this.getNodeAt(params.pointer.DOM)) !== NaN)
  294. {
  295. bringUpProfileView(Number(this.getNodeAt(params.pointer.DOM)));
  296. }
  297. });
  298. });
  299. });
  300. })
  301. }).catch(function(error)
  302. {
  303. $("#" + graphsTitle).html("Error Fetching Data From API");
  304. alert("Invalid User");
  305. });
  306. }