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