Browse Source

added generate network map data of user connections (likes, travels)

master
arnaucode 7 years ago
parent
commit
1e3bd06b9f
2 changed files with 154 additions and 1 deletions
  1. +152
    -1
      controllers/adminController.js
  2. +2
    -0
      server.js

+ 152
- 1
controllers/adminController.js

@ -288,6 +288,7 @@ exports.network = function(req, res) {
var edge = { var edge = {
from: users[i]._id, from: users[i]._id,
to: users[i].likes[j]._id, to: users[i].likes[j]._id,
label: "like",
arrows: "to", arrows: "to",
color: { color: {
color: "#E57373" //red300 color: "#E57373" //red300
@ -318,7 +319,10 @@ exports.network = function(req, res) {
} }
var edge = { var edge = {
from: users[i]._id, from: users[i]._id,
to: users[i].travels[j]._id
to: users[i].travels[j]._id,
color: {
color: "#616161" //grey
}
}; };
edges.push(edge); edges.push(edge);
@ -358,3 +362,150 @@ exports.network = function(req, res) {
} }
}); });
}; };
exports.getUserNetwork = function(req, res) {
adminModel.findOne({
'token': req.headers['x-access-token']
})
.exec(function(err, admin) {
if (!admin) {
res.json({
success: false,
message: 'Admin not found'
});
} else if (admin) {
if (err) return res.send(500, err.message);
userModel.findOne({
_id: req.params.userid
})
.lean()
.populate('travels', 'title type joins')
.populate('likes', 'username avatar')
.exec(function(err, user) {
if (err) return res.send(500, err.message);
/*res.status(200).jsonp(users);*/
var nodes = [];
var edges = [];
//for (var i = 0; i < users.length; i++) {
var node = {
title: user.username,
label: user.username,
image: user.avatar,
shape: "image",
id: user._id,
group: user._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
nodes.push(node);
var uNode = nodes.length - 1;
}
for (var j = 0; j < user.likes.length; j++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: user.likes[j].username,
label: user.likes[j].username,
image: user.likes[j].avatar,
shape: "image",
id: user.likes[j]._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
}
var edge = {
from: user._id,
to: user.likes[j]._id,
label: "like",
arrows: "to",
color: {
color: "#E57373" //red300
}
};
edges.push(edge);
}
for (var j = 0; j < user.travels.length; j++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: user.travels[j].title,
label: user.travels[j].title,
image: "img/" + user.travels[j].type + ".png",
shape: "image",
id: user.travels[j]._id,
value: "0.5",
group: user._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
}
var edge = {
from: user._id,
to: user.travels[j]._id,
color: {
color: "#616161" //grey
}
};
edges.push(edge);
}
//add users that has add like to the user
userModel.find({
likes: req.params.userid
})
.exec(function(err, users) {
for (var i = 0; i < users.length; i++) {
/*console.log(i + ", " + j);
console.log(nodes);*/
var node = {
title: users[i].username,
label: users[i].username,
image: users[i].avatar,
shape: "image",
id: users[i]._id
};
var lNode = isNodeInNodes(node, nodes);
if (lNode < 0) {
//node no exist
nodes.push(node);
lNode = nodes.length - 1;
} else {
//node already exist
}
var edge = {
from: users[i]._id,
to: req.params.userid,
label: "like",
arrows: "to",
color: {
color: "#E57373" //red300
}
};
edges.push(edge);
}
var resp = {
nodes: nodes,
edges: edges
};
res.status(200).jsonp(resp);
});
//}
});
}
});
};

+ 2
- 0
server.js

@ -118,6 +118,8 @@ apiRoutes.use(function(req, res, next) {
//admin //admin
apiRoutes.route('/admin/network') apiRoutes.route('/admin/network')
.get(adminCtrl.network); .get(adminCtrl.network);
apiRoutes.route('/admin/user/network/:userid')
.get(adminCtrl.getUserNetwork);
apiRoutes.route('/admin/travels/id/:travelid') apiRoutes.route('/admin/travels/id/:travelid')
.delete(adminCtrl.deleteTravel); .delete(adminCtrl.deleteTravel);
apiRoutes.route('/admin/users/id/:userid') apiRoutes.route('/admin/users/id/:userid')

Loading…
Cancel
Save