Commit 60e8ca8a authored by andreagerino's avatar andreagerino

Adding explorations endpoint

parent 5bae4b56
...@@ -114,82 +114,6 @@ router.get('/users/sighted', function(req, res, next) { ...@@ -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){ function user_ids_cursor(vib){
var filter_condition = {$eq: 0}; var filter_condition = {$eq: 0};
...@@ -257,28 +181,60 @@ function user_ids_cursor(vib){ ...@@ -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){
rebuild_user_stats(req, res, next); var collection = icarusDb.collection('resources');
}else{ var cursor = collection.aggregate(
res.sendStatus(500); // 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 rebuild_user_stats(req, res, next){ router.get('/explorations/hpbylevel', function(req, res, next) {
var collection = icarusDb.collection('resources'); var collection = icarusDb.collection('resources');
collection.aggregate(
var cursor = collection.aggregate(
// Pipeline // Pipeline
[ [
...@@ -286,6 +242,7 @@ function rebuild_user_stats(req, res, next){ ...@@ -286,6 +242,7 @@ function rebuild_user_stats(req, res, next){
{ {
$match: { $match: {
"appdata.appname" : "Invisible Puzzle", "appdata.appname" : "Invisible Puzzle",
"userdata.event" : "exploration",
"debug" : false "debug" : false
} }
}, },
...@@ -293,16 +250,16 @@ function rebuild_user_stats(req, res, next){ ...@@ -293,16 +250,16 @@ function rebuild_user_stats(req, res, next){
// Stage 2 // Stage 2
{ {
$group: { $group: {
"_id" : "$appdata.uuid", "_id" : "$userdata.level.id",
"docs" : { "explorations" : {
"$sum" : 1 "$sum" : 1
}, },
"voiceover" : { "with_hp" : {
"$sum" : { "$sum" : {
"$cond" : [ "$cond" : [
{ {
"$eq" : [ "$eq" : [
"$appdata.voiceover", "$userdata.headphones",
true true
] ]
}, },
...@@ -310,38 +267,36 @@ function rebuild_user_stats(req, res, next){ ...@@ -310,38 +267,36 @@ function rebuild_user_stats(req, res, next){
0 0
] ]
} }
},
"levels" : {
"$addToSet" : "$userdata.level.id"
},
"sonifications" : {
"$addToSet" : "$userdata.sonification"
} }
} }
}, },
// Stage 3 // Stage 3
{ {
$project: { "_id": 1,
"docs" : 1, "explorations": 1,
"vo_ratio" : { "hp_ratio": {$divide:["$with_hp","$explorations"]}
"$divide" : [
"$voiceover",
"$docs"
]
},
"levels" : 1,
"sonifications" : 1
}
}, },
// Stage 4 // 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){ if(err!=null){
...@@ -356,7 +311,7 @@ function rebuild_user_stats(req, res, next){ ...@@ -356,7 +311,7 @@ function rebuild_user_stats(req, res, next){
}else{ }else{
res.sendStatus(201, result); res.send(200,result);
} }
...@@ -364,6 +319,6 @@ function rebuild_user_stats(req, res, next){ ...@@ -364,6 +319,6 @@ function rebuild_user_stats(req, res, next){
}); });
}*/ }
module.exports = router; module.exports = router;
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment