From 376c1e3ba11dc1aab223df3ba6a5117e44d9a4ae Mon Sep 17 00:00:00 2001 From: andreagerino Date: Sun, 8 May 2016 18:40:50 +0200 Subject: [PATCH] Adding user stats download to IP service --- .../invisiblepuzzle/invisiblepuzzle.js | 106 ++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/routes/services/invisiblepuzzle/invisiblepuzzle.js b/routes/services/invisiblepuzzle/invisiblepuzzle.js index ac8c341..af9d28f 100644 --- a/routes/services/invisiblepuzzle/invisiblepuzzle.js +++ b/routes/services/invisiblepuzzle/invisiblepuzzle.js @@ -1,6 +1,26 @@ var express = require('express'); var router = express.Router(); +var debug = require('debug')('Icarus:server'); + +var mongoClient = require('mongodb').MongoClient; + +var assert = assert = require('assert'); + +// Connection URL +var mongoUrl = 'mongodb://icarus:icarus@localhost:27017/icarus'; + +//Connect to the server and intialise the pool +var icarusDb; +mongoClient.connect(mongoUrl, function(err, db) { + + assert.equal(null, err, err); + debug("Connected to MongoDB database: "+db.databaseName); + + icarusDb = db; + +}); + /* GET root . */ router.get('/', function(req, res, next) { @@ -8,4 +28,90 @@ router.get('/', function(req, res, next) { }); +router.get('/users', function(req, res, next) { + + var collection = icarusDb.collection('resources'); + + collection.aggregate( + + // Pipeline + [ + // Stage 1 + { + $match: { + "appdata.appname":"Invisible Puzzle", + "debug":false + } + }, + + // Stage 2 + { + $group: { + "_id": "$appdata.uuid", + "docs": {$sum: 1}, + "voiceover": {$sum : {$cond: [{$eq:["$appdata.voiceover", true]},1,0]}} + } + }, + + // Stage 3 + { + $project: { + + "docs": 1, + "vo_ratio": {$divide:["$voiceover","$docs"]} + + } + }, + + // Stage 4 + { + $group: { + + "_id": 1, + "users": {$sum: 1}, + "vib": {$sum : {$cond: [{$gt:["$vo_ratio", 0]},1,0]}} + + } + }, + + // Stage 5 + { + + $project: { + + "_id": 0, + "users": 1, + "vib": 1 + + } + + } + ] + + ).toArray(function(err, result) { + + if(err!=null){ + + debug(err); + next(err); + + }else{ + + if(result==undefined){ + + res.sendStatus(404); + + }else{ + + res.send(200,result); + + } + + } + + }); + + +}); + module.exports = router; -- 2.18.1