invisiblepuzzle.js 8.08 KB
Newer Older
1 2 3 4 5 6
// PROTOCOL SPECIFICATION
// -----------------------
// /users/count - returns the number of users and how many of them are using VoiceOver
// /users/vib - returns objects representing who are using VoiceOver
// /users/sighted - returns objects representing who are not using VoiceOver

7 8 9
var express = require('express');
var router = express.Router();

10 11 12 13 14 15 16
var debug = require('debug')('Icarus:server');

var mongoClient = require('mongodb').MongoClient;

var assert = assert = require('assert');

// Connection URL
17
var mongoUrl = 'mongodb://readOnly:readOnly@localhost:27017/icarus';
18 19 20 21 22 23 24 25 26 27 28 29

//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;

});

30 31 32 33 34 35 36
/* GET root . */
router.get('/', function(req, res, next) {

    res.sendStatus(200);

});

37
router.get('/users/count', function(req, res, next) {
38 39 40

    var collection = icarusDb.collection('resources');

41
    var cursor = collection.aggregate(
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

        // 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

                }

            }
        ]

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    )

    return_cursor(cursor, req, res, next);

});

router.get('/users/vib', function(req, res, next) {

    var cursor = user_ids_cursor(true);
    return_cursor(cursor, req, res, next);

});

router.get('/users/sighted', function(req, res, next) {

    var cursor = user_ids_cursor(false);
    return_cursor(cursor, req, res, next);

});

function user_ids_cursor(vib){

    var filter_condition = {$eq: 0};

    if(vib==true){

        filter_condition  = {$gt: 0}

    }

    var collection = icarusDb.collection('resources');

    return 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]}},
                    "levels" : {
                        "$addToSet" : "$userdata.level.id"
                    },
                    "sonifications" : {
                        "$addToSet" : "$userdata.sonification"
                    }
                }

            },

            // Stage 3
            {
                $project: {

                    "docs": 1,
                    "vo_ratio": {$divide:["$voiceover","$docs"]},
                    "levels": 1,
                    "sonification": 1

                }
            },

            // Stage 4
            {
                $match: {

                    "vo_ratio": filter_condition

                }
            }

        ]

    )

}

184
router.get('/explorations/hp', function(req, res, next) {
185

186
    var collection = icarusDb.collection('resources');
187

188
    var cursor = collection.aggregate(
189

190 191 192 193 194 195 196
        // Pipeline
        [
            // Stage 1
            {
                $match: {
                    "appdata.appname" : "Invisible Puzzle",
                    "userdata.event" : "exploration",
197
                    "appdata.voiceover": true,
198 199 200
                    "debug" : false
                }
            },
201

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
            // Stage 2
            {
                $group: {
                    "_id" : 1,
                    "explorations" : {
                        "$sum" : 1
                    },
                    "with_hp" : {
                        "$sum" : {
                            "$cond" : [
                                {
                                    "$eq" : [
                                        "$userdata.headphones",
                                        true
                                    ]
                                },
                                1,
                                0
                            ]
                        }
                    }
                }
            }
225

226
        ]
227

228
    );
229

230
    return_cursor(cursor, req, res, next);
231 232 233

});

234
router.get('/explorations/hpbylevel', function(req, res, next) {
235 236

    var collection = icarusDb.collection('resources');
237 238

    var cursor = collection.aggregate(
239 240 241 242 243 244 245

        // Pipeline
        [
            // Stage 1
            {
                $match: {
                    "appdata.appname" : "Invisible Puzzle",
246
                    "userdata.event" : "exploration",
247
                    "appdata.voiceover": true,
248 249 250 251 252 253 254
                    "debug" : false
                }
            },

            // Stage 2
            {
                $group: {
255 256
                    "_id" : "$userdata.level.id",
                    "explorations" : {
257 258
                        "$sum" : 1
                    },
259
                    "with_hp" : {
260 261 262 263
                        "$sum" : {
                            "$cond" : [
                                {
                                    "$eq" : [
264
                                        "$userdata.headphones",
265 266 267 268 269 270 271 272 273 274 275 276 277
                                        true
                                    ]
                                },
                                1,
                                0
                            ]
                        }
                    }
                }
            },

            // Stage 3
            {
278 279 280 281 282
                $project: {
                    "_id": 1,
                    "explorations": 1,
                    "hp_ratio": {$divide:["$with_hp","$explorations"]}
                }
283 284 285 286
            },

            // Stage 4
            {
287 288 289
                $sort: {
                    "_id": 1
                }
290
            }
291

292 293
        ]

294 295 296 297 298 299 300
    );


    return_cursor(cursor, req, res, next);

});

301 302 303 304 305 306 307 308 309 310 311 312 313
router.get('/sonifications/bylevel', function(req, res, next) {

    var collection = icarusDb.collection('resources');

    var cursor = collection.aggregate(

        // Pipeline
        [
            // Stage 1
            {
                $match: {
                    "appdata.appname" : "Invisible Puzzle",
                    "userdata.event" : "exploration",
314
                    "appdata.voiceover": true,
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
                    "debug" : false
                }
            },

            // Stage 2
            {
                $project: {
                    "identifier": {
                        "lvl": "$userdata.level.id",
                        "son": "$userdata.sonification"
                    }
                }
            },

            // Stage 3
            {
                $group: {
                    "_id": "$identifier",
                    "count": {$sum: 1}
                }
            }

        ]

    );

    return_cursor(cursor, req, res, next);

});

345 346 347
function return_cursor(cursor, req, res, next){

    cursor.toArray(function(err, result) {
348 349 350 351 352 353 354 355 356 357 358 359 360 361

        if(err!=null){

            debug(err);
            next(err);

        }else{

            if(result==undefined){

                res.sendStatus(404);

            }else{

362
                res.send(200,result);
363 364 365 366 367 368 369

            }

        }

    });

370
}
371

372
module.exports = router;