invisiblepuzzle.js 2.16 KB
Newer Older
1 2 3
var express = require('express');
var router = express.Router();

4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
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;

});

24 25 26 27 28 29 30
/* GET root . */
router.get('/', function(req, res, next) {

    res.sendStatus(200);

});

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
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);

            }

        }

    });


});

117
module.exports = router;