Avoid multiple connections if possible

This commit is contained in:
Thomas FORGIONE 2015-05-20 10:47:27 +02:00
parent 66a2159972
commit 7a6d4b0247
1 changed files with 82 additions and 88 deletions

View File

@ -36,89 +36,80 @@ var checkId = function(req, res, next, callback, id) {
}); });
} }
var addCamerasFromId = function(req, res, callback, id) { var addCamerasFromId = function(client, req, res, callback, id) {
pg.connect(pgc.url, function(err, client, release) { client.query(
client.query( "SELECT ((camera).position).x AS px, " +
"SELECT ((camera).position).x AS px, " + "((camera).position).y AS py, " +
"((camera).position).y AS py, " + "((camera).position).z AS pz, " +
"((camera).position).z AS pz, " + "((camera).target).x AS tx, " +
"((camera).target).x AS tx, " + "((camera).target).y AS ty, " +
"((camera).target).y AS ty, " + "((camera).target).z AS tz, " +
"((camera).target).z AS tz, " + "time AS time " +
"time AS time " +
"FROM keyboardevent WHERE user_id = $1 ORDER BY time;", "FROM keyboardevent WHERE user_id = $1 ORDER BY time;",
[id], [id],
function(err, result) { function(err, result) {
res.locals.path = res.locals.path || []; res.locals.path = res.locals.path || [];
for (var i in result.rows) { for (var i in result.rows) {
res.locals.path.push( res.locals.path.push(
{ {
type: 'camera', type: 'camera',
position : { position : {
x: result.rows[i].px, x: result.rows[i].px,
y: result.rows[i].py, y: result.rows[i].py,
z: result.rows[i].pz z: result.rows[i].pz
}, },
target : { target : {
x: result.rows[i].tx, x: result.rows[i].tx,
y: result.rows[i].ty, y: result.rows[i].ty,
z: result.rows[i].tz z: result.rows[i].tz
}, },
time: result.rows[i].time time: result.rows[i].time
} }
); );
}
callback();
release();
} }
); callback();
}); }
);
} }
var addCoinsFromId = function(req, res, callback, id) { var addCoinsFromId = function(client, req, res, callback, id) {
pg.connect(pgc.url, function(err, client, release) { client.query(
client.query( "SELECT coin_id, time FROM coinclicked WHERE user_id = $1",
"SELECT coin_id, time FROM coinclicked WHERE user_id = $1", [id],
[id], function(err,result) {
function(err,result) { res.locals.path = res.locals.path || [];
res.locals.path = res.locals.path || []; for (var i in result.rows) {
for (var i in result.rows) { res.locals.path.push(
res.locals.path.push( {
{ type: 'coin',
type: 'coin', time: result.rows[i].time,
time: result.rows[i].time, id: result.rows[i].coin_id
id: result.rows[i].coin_id }
} );
);
}
callback();
release();
} }
); callback();
}); }
);
} }
var addArrowsFromId = function(req, res, callback, id) { var addArrowsFromId = function(client, req, res, callback, id) {
pg.connect(pgc.url, function(err, client, release) { client.query(
client.query( "SELECT arrow_id, time FROM arrowclicked WHERE user_id = $1",
"SELECT arrow_id, time FROM arrowclicked WHERE user_id = $1", [id],
[id], function(err, result) {
function(err, result) { res.locals.path = res.locals.path || [];
res.locals.path = res.locals.path || []; for (var i in result.rows) {
for (var i in result.rows) { res.locals.path.push(
res.locals.path.push( {
{ type: 'arrow',
type: 'arrow', time: result.rows[i].time,
time: result.rows[i].time, id: result.rows[i].arrow_id
id: result.rows[i].arrow_id }
} );
);
}
callback();
release();
} }
); callback();
}); }
);
} }
module.exports.index = function(req, res) { module.exports.index = function(req, res) {
@ -171,21 +162,24 @@ module.exports.replay_info = function(req, res) {
// Parse id // Parse id
var id = tools.filterInt(req.params.id); var id = tools.filterInt(req.params.id);
addCamerasFromId(req, res, function() { pg.connect(pgc.url, function(err, client, release) {
addCoinsFromId(req, res, function() { addCamerasFromId(client, req, res, function() {
addArrowsFromId(req, res, function() { addCoinsFromId(client, req, res, function() {
res.locals.path.sort(function(elt1, elt2) { addArrowsFromId(client, req, res, function() {
// Dates as string can be compared res.locals.path.sort(function(elt1, elt2) {
if (elt1.time < elt2.time) // Dates as string can be compared
return -1; if (elt1.time < elt2.time)
if (elt1.time > elt2.time) return -1;
return 1; if (elt1.time > elt2.time)
return 0; return 1;
}); return 0;
res.send(JSON.stringify(res.locals.path)); });
res.send(JSON.stringify(res.locals.path));
}, id);
}, id); }, id);
}, id); }, id);
}, id); release();
});
} }
module.exports.replay = function(req, res, next) { module.exports.replay = function(req, res, next) {