Added user_id and exp_id (disinction between both)

This commit is contained in:
Thomas FORGIONE 2015-06-04 16:52:58 +02:00
parent c82f735273
commit fa063a0f91
10 changed files with 185 additions and 89 deletions

View File

@ -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;

View File

@ -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');

View File

@ -10,4 +10,4 @@ block content
else
ol
each elt in users
li <a href="/prototype/replay/#{elt.id}">#{elt.name}</a>
li <a href="/prototype/replay/#{elt.id}">User <em>#{elt.username}</em> for scene <em>#{elt.scenename}</em></a>

View File

@ -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);
}

View File

@ -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("");
}

View File

@ -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("");
}

View File

@ -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("")
}

View File

@ -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("");
}

View File

@ -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("");
}

View File

@ -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
);