From fa063a0f915c905dadc2222aa04d1ba0d7559332 Mon Sep 17 00:00:00 2001 From: Thomas FORGIONE Date: Thu, 4 Jun 2015 16:52:58 +0200 Subject: [PATCH] Added user_id and exp_id (disinction between both) --- controllers/prototype/dbrequests.js | 136 +++++++++++++++--- controllers/prototype/index.js | 19 +-- controllers/prototype/views/replay_index.jade | 2 +- posts/arrow-clicked/index.js | 9 +- posts/coin-clicked/index.js | 10 +- posts/hovered/index.js | 9 +- posts/keyboard-event/index.js | 22 ++- posts/previous-next-clicked/index.js | 21 ++- posts/reset-clicked/index.js | 9 +- sql/backup.pgsql | 37 ++++- 10 files changed, 185 insertions(+), 89 deletions(-) diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index 9f803f7..62894bf 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -88,7 +88,7 @@ Info.prototype.loadCameras = function() { "((camera).target).y AS ty, " + "((camera).target).z AS tz, " + "time AS time " + - "FROM keyboardevent WHERE user_id = $1 ORDER BY time;", + "FROM keyboardevent WHERE exp_id = $1 ORDER BY time;", [self.id], function(err, result) { self.results.cameras = []; @@ -119,7 +119,7 @@ Info.prototype.loadCameras = function() { Info.prototype.loadCoins = function() { var self = this; this.client.query( - "SELECT coin_id, time FROM coinclicked WHERE user_id = $1 ORDER BY time;", + "SELECT coin_id, time FROM coinclicked WHERE exp_id = $1 ORDER BY time;", [self.id], function(err,result) { self.results.coins = []; @@ -141,7 +141,7 @@ Info.prototype.loadCoins = function() { Info.prototype.loadArrows = function() { var self = this; this.client.query( - "SELECT arrow_id, time FROM arrowclicked WHERE user_id = $1 ORDER BY time;", + "SELECT arrow_id, time FROM arrowclicked WHERE exp_id = $1 ORDER BY time;", [self.id], function(err, result) { self.results.arrows = []; @@ -163,7 +163,7 @@ Info.prototype.loadArrows = function() { Info.prototype.loadResets = function() { var self = this; this.client.query( - "SELECT time FROM resetclicked WHERE user_id = $1 ORDER BY time;", + "SELECT time FROM resetclicked WHERE exp_id = $1 ORDER BY time;", [self.id], function(err, result) { self.results.resets = []; @@ -191,7 +191,7 @@ Info.prototype.loadPreviousNext = function () { "((camera).target).y AS ty, " + "((camera).target).z AS tz, " + "time AS time " + - "FROM previousnextclicked WHERE user_id = $1 ORDER BY time;", + "FROM previousnextclicked WHERE exp_id = $1 ORDER BY time;", [self.id], function(err, result) { self.results.previousNext = []; @@ -223,7 +223,7 @@ Info.prototype.loadPreviousNext = function () { Info.prototype.loadHovered = function() { var self = this; this.client.query( - "SELECT start, time, arrow_id FROM hovered WHERE user_id = $1 ORDER BY time;", + "SELECT start, time, arrow_id FROM hovered WHERE exp_id = $1 ORDER BY time;", [self.id], function(err, result) { self.results.hovered = []; @@ -243,7 +243,7 @@ Info.prototype.loadHovered = function() { ); } -var IdCreator = function(finishAction) { +var UserCreator = function(finishAction) { this.finishAction = finishAction; // Connect to db @@ -255,7 +255,7 @@ var IdCreator = function(finishAction) { }); } -IdCreator.prototype.execute = function() { +UserCreator.prototype.execute = function() { var self = this; this.client.query( "INSERT INTO users(name) VALUES('anonymous'); SELECT currval('users_id_seq');", @@ -267,7 +267,7 @@ IdCreator.prototype.execute = function() { ); } -IdCreator.prototype.finish = function() { +UserCreator.prototype.finish = function() { this.release(); this.client = null; this.release = null; @@ -275,7 +275,43 @@ IdCreator.prototype.finish = function() { this.finishAction(this.finalResult); } -var IdChecker = function(id, finishAction) { +var ExpCreator = function(user_id, finishAction) { + this.finishAction = finishAction; + this.user_id = user_id; + + // Connect to db + var self = this; + pg.connect(pgc.url, function(err, client, release) { + self.client = client; + self.release = release; + self.execute(); + }); +} + +ExpCreator.prototype.execute = function() { + var self = this; + this.client.query( + // TODO this is ugly, we should not do that... + "INSERT INTO experiment(user_id, scene_id) VALUES(" + this.user_id + " , 1); SELECT currval('experiment_id_seq');", + [], + function(err, result) { + console.log(self.user_id); + console.log(err); + self.finalResult = result.rows[0].currval; + self.finish(); + } + ); +} + +ExpCreator.prototype.finish = function() { + this.release(); + this.client = null; + this.release = null; + + this.finishAction(this.finalResult); +} + +var UserIdChecker = function(id, finishAction) { this.id = id; this.finishAction = finishAction; @@ -287,7 +323,7 @@ var IdChecker = function(id, finishAction) { }); } -IdChecker.prototype.execute = function() { +UserIdChecker.prototype.execute = function() { var self = this; this.client.query( "SELECT count(id) > 0 AS answer FROM users WHERE id = $1;", @@ -299,7 +335,7 @@ IdChecker.prototype.execute = function() { ); } -IdChecker.prototype.finish = function() { +UserIdChecker.prototype.finish = function() { this.release(); this.client = null; this.release = null; @@ -307,7 +343,8 @@ IdChecker.prototype.finish = function() { this.finishAction(this.finalResult); } -var UserGetter = function(finishAction) { +var ExpIdChecker = function(id, finishAction) { + this.id = id; this.finishAction = finishAction; var self = this; @@ -318,19 +355,19 @@ var UserGetter = function(finishAction) { }); } -UserGetter.prototype.execute = function() { +ExpIdChecker.prototype.execute = function() { var self = this; this.client.query( - "SELECT id, name FROM users", - [], + "SELECT count(id) > 0 AS answer FROM experiment WHERE id = $1;", + [self.id], function(err, result) { - self.finalResult = result.rows; + self.finalResult = result.rows[0].answer; self.finish(); } ); } -UserGetter.prototype.finish = function() { +ExpIdChecker.prototype.finish = function() { this.release(); this.client = null; this.release = null; @@ -338,7 +375,62 @@ UserGetter.prototype.finish = function() { this.finishAction(this.finalResult); } -module.exports.getInfo = function(id, callback) { new Info(id, callback); }; -module.exports.createId = function(callback) { new IdCreator(callback); }; -module.exports.checkId = function(id, callback) { new IdChecker(id, callback); }; -module.exports.getAllUsers = function(callback) { new UserGetter(callback); }; +var ExpGetter = function(finishAction) { + this.finishAction = finishAction; + + var self = this; + pg.connect(pgc.url, function(err, client, release) { + self.client = client; + self.release = release; + self.execute(); + }); +} + +ExpGetter.prototype.execute = function() { + var self = this; + this.client.query( + "SELECT " + + "experiment.id as exp_id, " + + "users.name as username, " + + "scene.name as scenename " + + "FROM experiment, users, scene " + + "WHERE experiment.user_id = user_id and scene.id = experiment.scene_id " + + "ORDER BY experiment.id;", + [], + function(err, result) { + console.log(err); + self.finalResult = result.rows; + self.finish(); + } + ); +} + +ExpGetter.prototype.finish = function() { + this.release(); + this.client = null; + this.release = null; + + this.finishAction(this.finalResult); +} + +var tryUser = function(id, callback) { + if (id !== undefined && id !== null) { + new UserIdChecker(id, function(clear) { + if (clear) { + callback(id); + } else { + new UserCreator(callback); + } + }); + } else { + new UserCreator(callback); + } +} + +module.exports.getInfo = function(id, callback) { new Info(id, callback); }; +module.exports.createUser = function(callback) { new UserCreator(callback); }; +module.exports.createExp = function(id, callback) { new ExpCreator(id, callback); }; +module.exports.checkUserId = function(id, callback) { new UserIdChecker(id, callback); }; +module.exports.checkExpId = function(id, callback) { new ExpIdChecker(id, callback); }; +module.exports.getAllExps = function(callback) { new ExpGetter(callback); }; +module.exports.tryUser = tryUser; diff --git a/controllers/prototype/index.js b/controllers/prototype/index.js index fe64c88..2b604ae 100644 --- a/controllers/prototype/index.js +++ b/controllers/prototype/index.js @@ -13,13 +13,16 @@ module.exports.index = function(req, res) { var protoHelper = function(template) { return function(req, res) { - db.createId(function(id) { - req.session.user_id = id; - req.session.save(); + db.tryUser(req.session.user_id, function(id) { + db.createExp(id, function(id) { + req.session.exp_id = id; + req.session.user_id = id; + req.session.save(); - res.setHeader('Content-Type','text/html'); - res.render(template, res.locals, function(err, result) { - res.send(result); + res.setHeader('Content-Type','text/html'); + res.render(template, res.locals, function(err, result) { + res.send(result); + }); }); }); }; @@ -44,7 +47,7 @@ module.exports.replay = function(req, res, next) { // Get id parameter res.locals.id = tools.filterInt(req.params.id); - db.checkId(res.locals.id, function(idExist) { + db.checkExpId(res.locals.id, function(idExist) { if (!idExist) { var err = new Error("This replay does not exist"); err.status = 404; @@ -59,7 +62,7 @@ module.exports.replay = function(req, res, next) { } module.exports.replay_index = function(req, res, next) { - db.getAllUsers(function(result) { + db.getAllExps(function(result) { res.locals.users = result; res.setHeader('Content-Type', 'text/html'); diff --git a/controllers/prototype/views/replay_index.jade b/controllers/prototype/views/replay_index.jade index 92e806f..9a51aaf 100644 --- a/controllers/prototype/views/replay_index.jade +++ b/controllers/prototype/views/replay_index.jade @@ -10,4 +10,4 @@ block content else ol each elt in users - li #{elt.name} + li User #{elt.username} for scene #{elt.scenename} diff --git a/posts/arrow-clicked/index.js b/posts/arrow-clicked/index.js index 8dd0a2b..cec1d8d 100644 --- a/posts/arrow-clicked/index.js +++ b/posts/arrow-clicked/index.js @@ -3,13 +3,10 @@ var secret = require('../../private'); module.exports.index = function(req, res) { - var user_id = req.session.user_id; - var arrow_id = req.body.arrow_id; - pg.connect(secret.url, function(err, client, release) { client.query( - "INSERT INTO arrowclicked(user_id, arrow_id, time) VALUES($1,$2, to_timestamp($3));", - [user_id, arrow_id, req.body.time], + "INSERT INTO arrowclicked(exp_id, arrow_id, time) VALUES($1,$2, to_timestamp($3));", + [req.session.exp_id, req.body.arrow_id, req.body.time], function(err, result) { release(); } @@ -17,6 +14,4 @@ module.exports.index = function(req, res) { }); res.setHeader('Content-Type', 'text/html'); - res.send("user_id = " + user_id); - } diff --git a/posts/coin-clicked/index.js b/posts/coin-clicked/index.js index 12edfb8..3956b76 100644 --- a/posts/coin-clicked/index.js +++ b/posts/coin-clicked/index.js @@ -3,13 +3,10 @@ var secret = require('../../private'); module.exports.index = function(req, res) { - var user_id = req.session.user_id; - var coin_id = req.body.coin_id; - pg.connect(secret.url, function(err, client, release) { client.query( - "INSERT INTO coinclicked(user_id, coin_id, time) VALUES($1,$2, to_timestamp($3));", - [user_id, coin_id, req.body.time], + "INSERT INTO coinclicked(exp_id, coin_id, time) VALUES($1,$2, to_timestamp($3));", + [req.session.exp_id, req.body.coin_id, req.body.time], function(err, result) { release(); } @@ -17,6 +14,5 @@ module.exports.index = function(req, res) { }); res.setHeader('Content-Type', 'text/html'); - res.send("user_id = " + user_id); - + res.send(""); } diff --git a/posts/hovered/index.js b/posts/hovered/index.js index 00e633f..6cad008 100644 --- a/posts/hovered/index.js +++ b/posts/hovered/index.js @@ -3,14 +3,12 @@ var secret = require('../../private'); module.exports.index = function(req, res) { - var user_id = req.session.user_id; - pg.connect(secret.url, function(err, client, release) { client.query( - "INSERT INTO hovered(user_id, time, start, arrow_id)" + + "INSERT INTO hovered(exp_id, time, start, arrow_id)" + "VALUES($1, to_timestamp($2), $3, $4);" , [ - user_id, + req.session.exp_id, req.body.time, req.body.start ? true : false, req.body.arrow_id @@ -22,6 +20,5 @@ module.exports.index = function(req, res) { }); res.setHeader('Content-Type', 'text/html'); - res.send("user_id = " + user_id); - + res.send(""); } diff --git a/posts/keyboard-event/index.js b/posts/keyboard-event/index.js index 6623cee..6812aef 100644 --- a/posts/keyboard-event/index.js +++ b/posts/keyboard-event/index.js @@ -3,22 +3,19 @@ var secret = require('../../private'); module.exports.index = function(req, res) { - var user_id = req.session.user_id; - var camera = req.body.camera; - pg.connect(secret.url, function(err, client, release) { client.query( - "INSERT INTO keyboardevent(user_id, camera, time)" + + "INSERT INTO keyboardevent(exp_id, camera, time)" + "VALUES($1, ROW(ROW($2,$3,$4),ROW($5,$6,$7)), to_timestamp($8));" , [ - user_id, - camera.position.x, - camera.position.y, - camera.position.z, + req.session.exp_id, + req.body.camera.position.x, + req.body.camera.position.y, + req.body.camera.position.z, - camera.target.x, - camera.target.y, - camera.target.z, + req.body.camera.target.x, + req.body.camera.target.y, + req.body.camera.target.z, req.body.time ], @@ -29,6 +26,5 @@ module.exports.index = function(req, res) { }); res.setHeader('Content-Type', 'text/html'); - res.send("user_id = " + user_id); - + res.send("") } diff --git a/posts/previous-next-clicked/index.js b/posts/previous-next-clicked/index.js index 0bb693d..fa6a684 100644 --- a/posts/previous-next-clicked/index.js +++ b/posts/previous-next-clicked/index.js @@ -3,23 +3,20 @@ var secret = require('../../private'); module.exports.index = function(req, res) { - var user_id = req.session.user_id; - var camera = req.body.camera; - pg.connect(secret.url, function(err, client, release) { client.query( - "INSERT INTO previousnextclicked(user_id, previousnext, time, camera)" + + "INSERT INTO previousnextclicked(exp_id, previousnext, time, camera)" + "VALUES($1, $2, to_timestamp($3), ROW(ROW($4,$5,$6), ROW($7,$8,$9)));" , [ - user_id, + req.session.exp_id, req.body.previous ? 'p' : 'n', req.body.time, - camera.position.x, - camera.position.y, - camera.position.z, - camera.target.x, - camera.target.y, - camera.target.z + req.body.camera.position.x, + req.body.camera.position.y, + req.body.camera.position.z, + req.body.camera.target.x, + req.body.camera.target.y, + req.body.camera.target.z ], function(err, result) { release(); @@ -28,6 +25,6 @@ module.exports.index = function(req, res) { }); res.setHeader('Content-Type', 'text/html'); - res.send("user_id = " + user_id); + res.send(""); } diff --git a/posts/reset-clicked/index.js b/posts/reset-clicked/index.js index bdb8985..3bef20b 100644 --- a/posts/reset-clicked/index.js +++ b/posts/reset-clicked/index.js @@ -3,15 +3,12 @@ var secret = require('../../private'); module.exports.index = function(req, res) { - var user_id = req.session.user_id; - var camera = req.body.camera; - pg.connect(secret.url, function(err, client, release) { client.query( - "INSERT INTO resetclicked(user_id, time)" + + "INSERT INTO resetclicked(exp_id, time)" + "VALUES($1, to_timestamp($2));" , [ - user_id, + req.session.exp_id, req.body.time ], function(err, result) { @@ -21,6 +18,6 @@ module.exports.index = function(req, res) { }); res.setHeader('Content-Type', 'text/html'); - res.send("user_id = " + user_id); + res.send(""); } diff --git a/sql/backup.pgsql b/sql/backup.pgsql index 49e4e0f..57dae05 100644 --- a/sql/backup.pgsql +++ b/sql/backup.pgsql @@ -1,3 +1,4 @@ +-- Clear database from previous tables (just in case...) DROP TABLE IF EXISTS users CASCADE; DROP TABLE IF EXISTS arrowclicked CASCADE; DROP TABLE IF EXISTS coinclicked CASCADE; @@ -5,11 +6,14 @@ DROP TABLE IF EXISTS keyboardevent CASCADE; DROP TABLE IF EXISTS resetclicked CASCADE; DROP TABLE IF EXISTS previousnextclicked CASCADE; DROP TABLE IF EXISTS hovered CASCADE; +DROP TABLE IF EXISTS scene CASCADE; +DROP TABLE IF EXISTS experiment CASCADE; DROP TYPE IF EXISTS VECTOR3 CASCADE; DROP TYPE IF EXISTS CAMERA CASCADE; DROP TYPE IF EXISTS PREVIOUSNEXT CASCADE; +-- Elementary types CREATE TYPE PREVIOUSNEXT AS ENUM( 'p', 'n' ); @@ -25,41 +29,60 @@ CREATE TYPE CAMERA AS( target VECTOR3 ); +-- Base tables CREATE TABLE users( id SERIAL PRIMARY KEY, name CHAR(50) ); -CREATE TABLE arrowclicked( +CREATE TABLE scene( + id SERIAL PRIMARY KEY, + name CHAR(50) +); + +CREATE TABLE experiment( id SERIAL PRIMARY KEY, user_id SERIAL REFERENCES users (id), + scene_id SERIAL REFERENCES scene (id) +); + +-- Init scene table +INSERT INTO scene(name) VALUES ('peachcastle'); +INSERT INTO scene(name) VALUES ('bobomb'); +INSERT INTO scene(name) VALUES ('coolcoolmountain'); +INSERT INTO scene(name) VALUES ('whomp'); + +-- Events +CREATE TABLE arrowclicked( + id SERIAL PRIMARY KEY, + exp_id SERIAL REFERENCES experiment (id), time TIMESTAMP DEFAULT NOW(), arrow_id INTEGER ); CREATE TABLE coinclicked( id SERIAL PRIMARY KEY, - user_id SERIAL REFERENCES users (id), + exp_id SERIAL REFERENCES experiment (id), time TIMESTAMP DEFAULT NOW(), coin_id INTEGER ); CREATE TABLE keyboardevent( id SERIAL PRIMARY KEY, - user_id SERIAL REFERENCES users (id), + exp_id SERIAL REFERENCES experiment (id), time TIMESTAMP DEFAULT NOW(), camera CAMERA ); CREATE TABLE resetclicked( id SERIAL PRIMARY KEY, - user_id SERIAL REFERENCES users (id), + exp_id SERIAL REFERENCES experiment (id), time TIMESTAMP DEFAULT NOW() ); CREATE TABLE previousnextclicked( id SERIAL PRIMARY KEY, - user_id SERIAL REFERENCES users (id), + exp_id SERIAL REFERENCES experiment (id), previousnext PREVIOUSNEXT NOT NULL, time TIMESTAMP DEFAULT NOW(), camera CAMERA @@ -67,8 +90,8 @@ CREATE TABLE previousnextclicked( CREATE TABLE hovered( id SERIAL PRIMARY KEY, - user_id SERIAL REFERENCES users (id), + exp_id SERIAL REFERENCES experiment (id), start BOOLEAN NOT NULL, time TIMESTAMP DEFAULT NOW(), - arrow_id INT + arrow_id INTEGER );