3d-interface/analysis/loadTables.js

179 lines
4.5 KiB
JavaScript
Raw Normal View History

2015-09-14 16:35:16 +02:00
var pg = require('pg');
var async = require('async');
2015-09-28 09:36:49 +02:00
var DBReq = require('../controllers/prototype/dbrequests.js');
2015-09-14 16:35:16 +02:00
2015-09-28 09:36:49 +02:00
var users, client, release, scenes, coinCombinations, experiments, callback, url, db = {};
function write(str) {
process.stderr.write('\033[31m' + str + '\033[0m');
}
2015-09-14 16:35:16 +02:00
function start() {
client = new pg.Client(url);
2015-09-28 09:36:49 +02:00
write("Connecting to the database...");
2015-09-14 16:35:16 +02:00
client.connect(
function() {
2015-09-28 09:36:49 +02:00
write(" done !\n");
2015-09-14 16:35:16 +02:00
client.query(
'SELECT * FROM Users',
function(err, result) {
users = result.rows;
main();
}
);
}
);
}
function main() {
async.series([
// Init
function(done) {
2015-09-28 09:36:49 +02:00
write("Getting scenes and coin combinations...");
2015-09-14 16:35:16 +02:00
async.parallel([
function(callback) {
client.query(
'SELECT * FROM Scene;',
[],
function(err, result) {
scenes = result.rows;
callback();
}
);
},
function(callback) {
client.query(
'SELECT * FROM CoinCombination;',
function(err, result) {
coinCombinations = result.rows;
callback();
}
);
},
function(callback) {
client.query(
'SELECT * FROM Experiment;',
function(err, result) {
experiments = result.rows;
callback();
}
);
},
], function() {
2015-09-28 09:36:49 +02:00
write(" done !\n");
2015-09-14 16:35:16 +02:00
done();
});
},
function(done) {
2015-09-28 09:36:49 +02:00
write("Getting experiments for each user...");
async.each(
2015-09-14 16:35:16 +02:00
users,
function(user, callback) {
client.query(
'SELECT * FROM Experiment WHERE user_id = $1',
[user.id],
function(err, result) {
user.experiments = result.rows;
callback();
}
);
},
function(err, result) {
2015-09-28 09:36:49 +02:00
write(' done !\n');
2015-09-14 16:35:16 +02:00
done();
}
);
},
function(done) {
2015-09-28 09:36:49 +02:00
write('Getting experiments...');
async.each(
2015-09-14 16:35:16 +02:00
experiments,
function(exp, callback) {
client.query(
'SELECT user_id, coin_combination_id FROM Experiment WHERE id = $1',
[exp.id],
function(err, result) {
exp.coinCombination = coinCombinations[result.rows[0].coin_combination_id - 1];
exp.user = users[result.rows[0].user_id - 1];
callback();
}
);
},
2015-09-28 09:36:49 +02:00
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();
}
2015-09-14 16:35:16 +02:00
);
},
// Finish
function(done) {
client.end();
done();
},
function(done) {
2015-09-28 09:36:49 +02:00
db.users = users;
db.experiments = experiments;
db.coinCombinations = coinCombinations;
callback(db);
2015-09-14 16:35:16 +02:00
done();
}
]);
}
module.exports = function(_url, _callback) {
callback = _callback;
url = _url;
start();
return module.exports;
};