diff --git a/analysis/analyse.js b/analysis/analyse.js index dcdbda8..b015861 100644 --- a/analysis/analyse.js +++ b/analysis/analyse.js @@ -1,6 +1,42 @@ -var pgc = require('../private.js'); -var db = require('./loadTables.js')(pgc.url, function() { +function main() { - console.log(db.users); + var db = JSON.parse(require('fs').readFileSync('./data.json', 'utf8')); -}); + console.log('There were ' + db.users.length + ' users for ' + db.experiments.length + ' experiments'); + + var meanTimeNoReco = 0; + var meanTimeArrow = 0; + var meanTimeViewport = 0; + + for (var i = 0; i < db.experiments.length; i++) { + + var exp = db.experiments[i]; + var events = exp.elements.events; + + if (events.length === 0 || exp.user.worker_id === null) { + + continue; + + } + + console.log(exp.user.worker_id + ' : ' + exp.user.rating + ' -> ' + timeToString(timeDifference(events[0].time, events[events.length-1].time))); + + } + +} + +function timeDifference(time1, time2) { + + return new Date(time2).getTime() - new Date(time1).getTime(); + +} + +function timeToString(_time) { + + var time = _time / 1000; + + return Math.floor(time / 3600) + 'h' + Math.floor((time % 3600) / 60) + 'm' + Math.floor(time % 60); + +} + +main(); diff --git a/analysis/generateData.js b/analysis/generateData.js new file mode 100644 index 0000000..9cb354f --- /dev/null +++ b/analysis/generateData.js @@ -0,0 +1,9 @@ +var pgc = require('../private.js'); + +var url = pgc.url; + +require('./loadTables.js')(url, function(db) { + + console.log(JSON.stringify(db)); + +}); diff --git a/analysis/loadTables.js b/analysis/loadTables.js index 0fb658a..9789b00 100644 --- a/analysis/loadTables.js +++ b/analysis/loadTables.js @@ -1,14 +1,21 @@ var pg = require('pg'); var async = require('async'); +var DBReq = require('../controllers/prototype/dbrequests.js'); -var users, client, release, scenes, coinCombinations, experiments, callback, url; +var users, client, release, scenes, coinCombinations, experiments, callback, url, db = {}; + +function write(str) { + process.stderr.write('\033[31m' + str + '\033[0m'); +} function start() { client = new pg.Client(url); + write("Connecting to the database..."); client.connect( function() { + write(" done !\n"); client.query( 'SELECT * FROM Users', function(err, result) { @@ -27,6 +34,8 @@ function main() { // Init function(done) { + write("Getting scenes and coin combinations..."); + async.parallel([ function(callback) { client.query( @@ -64,13 +73,16 @@ function main() { }, ], function() { + write(" done !\n"); done(); }); }, function(done) { - async.map( + + write("Getting experiments for each user..."); + async.each( users, function(user, callback) { client.query( @@ -83,6 +95,7 @@ function main() { ); }, function(err, result) { + write(' done !\n'); done(); } ); @@ -90,7 +103,9 @@ function main() { function(done) { - async.map( + write('Getting experiments...'); + + async.each( experiments, function(exp, callback) { client.query( @@ -103,7 +118,31 @@ function main() { } ); }, - done + function() { + write(' done !\n'); + done(); + } + ); + + }, + + function(done) { + + write('Getting interactions from experiments (might be long)'); + + async.each( // Don't know why each doesn't work + experiments, + function(exp, callback) { + DBReq.getInfo(exp.id, function(result) { + exp.elements = result; + write('.'); + callback(); + }); + }, + function () { + write(' done !\n'); + done(); + } ); }, @@ -112,17 +151,15 @@ function main() { function(done) { client.end(); - console.log("Finished"); - done(); }, function(done) { - module.exports.users = users; - module.exports.experiments = experiments; - module.exports.coinCombinations = coinCombinations; - callback(); + db.users = users; + db.experiments = experiments; + db.coinCombinations = coinCombinations; + callback(db); done(); } diff --git a/controllers/prototype/dbrequests.js b/controllers/prototype/dbrequests.js index 1d557f2..fc44c77 100644 --- a/controllers/prototype/dbrequests.js +++ b/controllers/prototype/dbrequests.js @@ -73,9 +73,9 @@ DBReq.Info = function(id, finishAction) { // Connect to db var self = this; - pg.connect(pgc.url, function(err, client, release) { - self.client = client; - self.release = release; + self.client = new pg.Client(pgc.url); + + self.client.connect(function() { self.execute(); }); }; @@ -108,8 +108,9 @@ DBReq.Info.prototype.tryMerge = function() { } // Release db connection - this.release(); - this.release = null; + // this.release(); + // this.release = null; + this.client.end(); this.client = null; this.merge();