From 60e8ca8ae41ba376d5106031fd28d8b39242081b Mon Sep 17 00:00:00 2001 From: andreagerino Date: Tue, 14 Jun 2016 11:12:54 +0200 Subject: [PATCH] Adding explorations endpoint --- .../invisiblepuzzle/invisiblepuzzle.js | 179 +++++++----------- 1 file changed, 67 insertions(+), 112 deletions(-) diff --git a/routes/services/invisiblepuzzle/invisiblepuzzle.js b/routes/services/invisiblepuzzle/invisiblepuzzle.js index 493c623..175f755 100644 --- a/routes/services/invisiblepuzzle/invisiblepuzzle.js +++ b/routes/services/invisiblepuzzle/invisiblepuzzle.js @@ -114,82 +114,6 @@ router.get('/users/sighted', function(req, res, next) { }); -router.get('/explorations/headphones', function(req, res, next) { - - var collection = icarusDb.collection('resources'); - - var cursor = collection.aggregate( - - // Pipeline - [ - // Stage 1 - { - $match: { - "appdata.appname" : "Invisible Puzzle", - "userdata.event" : "exploration", - "debug" : false - } - }, - - // Stage 2 - { - $group: { - "_id" : 1, - "explorations" : { - "$sum" : 1 - }, - "with_hp" : { - "$sum" : { - "$cond" : [ - { - "$eq" : [ - "$userdata.headphones", - true - ] - }, - 1, - 0 - ] - } - } - } - } - - ] - - ); - - return_cursor(cursor, req, res, next); - -}); - -function return_cursor(cursor, req, res, next){ - - cursor.toArray(function(err, result) { - - if(err!=null){ - - debug(err); - next(err); - - }else{ - - if(result==undefined){ - - res.sendStatus(404); - - }else{ - - res.send(200,result); - - } - - } - - }); - -} - function user_ids_cursor(vib){ var filter_condition = {$eq: 0}; @@ -257,28 +181,60 @@ function user_ids_cursor(vib){ } -/*router.get('/users/', function(req, res, next) { +router.get('/explorations/hp', function(req, res, next) { - var refresh = req.query.refresh; - - if(refresh==1){ + var collection = icarusDb.collection('resources'); - rebuild_user_stats(req, res, next); + var cursor = collection.aggregate( - }else{ + // Pipeline + [ + // Stage 1 + { + $match: { + "appdata.appname" : "Invisible Puzzle", + "userdata.event" : "exploration", + "debug" : false + } + }, - res.sendStatus(500); + // Stage 2 + { + $group: { + "_id" : 1, + "explorations" : { + "$sum" : 1 + }, + "with_hp" : { + "$sum" : { + "$cond" : [ + { + "$eq" : [ + "$userdata.headphones", + true + ] + }, + 1, + 0 + ] + } + } + } + } - } + ] + ); + return_cursor(cursor, req, res, next); }); -function rebuild_user_stats(req, res, next){ +router.get('/explorations/hpbylevel', function(req, res, next) { var collection = icarusDb.collection('resources'); - collection.aggregate( + + var cursor = collection.aggregate( // Pipeline [ @@ -286,6 +242,7 @@ function rebuild_user_stats(req, res, next){ { $match: { "appdata.appname" : "Invisible Puzzle", + "userdata.event" : "exploration", "debug" : false } }, @@ -293,16 +250,16 @@ function rebuild_user_stats(req, res, next){ // Stage 2 { $group: { - "_id" : "$appdata.uuid", - "docs" : { + "_id" : "$userdata.level.id", + "explorations" : { "$sum" : 1 }, - "voiceover" : { + "with_hp" : { "$sum" : { "$cond" : [ { "$eq" : [ - "$appdata.voiceover", + "$userdata.headphones", true ] }, @@ -310,38 +267,36 @@ function rebuild_user_stats(req, res, next){ 0 ] } - }, - "levels" : { - "$addToSet" : "$userdata.level.id" - }, - "sonifications" : { - "$addToSet" : "$userdata.sonification" } } }, // Stage 3 { - $project: { - "docs" : 1, - "vo_ratio" : { - "$divide" : [ - "$voiceover", - "$docs" - ] - }, - "levels" : 1, - "sonifications" : 1 - } + "_id": 1, + "explorations": 1, + "hp_ratio": {$divide:["$with_hp","$explorations"]} }, // Stage 4 { - $out: "ip_user_stats" + $sort: { + "_id": 1 + } } + ] - ).toArray(function(err, result) { + ); + + + return_cursor(cursor, req, res, next); + +}); + +function return_cursor(cursor, req, res, next){ + + cursor.toArray(function(err, result) { if(err!=null){ @@ -356,7 +311,7 @@ function rebuild_user_stats(req, res, next){ }else{ - res.sendStatus(201, result); + res.send(200,result); } @@ -364,6 +319,6 @@ function rebuild_user_stats(req, res, next){ }); -}*/ +} -module.exports = router; +module.exports = router; \ No newline at end of file -- 2.18.1