From 7a6d4b02474088137cb68442f0d85126bfada8bb Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Wed, 20 May 2015 10:47:27 +0200 Subject: [PATCH] Avoid multiple connections if possible --- controllers/prototype/index.js | 170 ++++++++++++++++----------------- 1 file changed, 82 insertions(+), 88 deletions(-) diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index a5bc10f..50129a9 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -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) {