Skip to content

Commit

Permalink
add schema validation and debug logging
Browse files Browse the repository at this point in the history
  • Loading branch information
freeman-lab committed Mar 28, 2016
1 parent fca4b21 commit d274250
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 3 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
"body-parser": "^1.15.0",
"browser-request": "^0.3.3",
"d3-scale": "^0.6.4",
"debug": "^2.2.0",
"drag-and-drop-files": "0.0.1",
"drag-drop": "^2.11.0",
"express": "^4.13.4",
"jsonschema": "^1.1.0",
"lodash": "^4.6.1",
"mongoose": "^4.4.7",
"object-assign": "^4.0.1",
Expand Down
29 changes: 26 additions & 3 deletions server/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ var express = require('express')
var mongoose = require('mongoose')
var parser = require('body-parser')
var timestamp = require('timestamp')
var jsonschema = require('jsonschema')
var spawn = require('child_process').spawn
var debug = require('debug')('neurofinder')
var evaluate = require('./evaluate')
var config = require('./config')
var schema = require('./schema')
var Dataset = require('./models/dataset')
var Answer = require('./models/answer')
var Submission = require('./models/submission')
Expand Down Expand Up @@ -38,19 +41,37 @@ var start = function (opts) {
app.post('/api/submit/', function(req, res){
var answers = req.body.answers

var v = new jsonschema.Validator()

console.log('')
debug('processing submission from ' + req.body.name)

function getDatasets (next) {
debug('fetching datasets')
Dataset.find({}, function (err, data) {
if (err) return next({stage: 'getting datasets', error: err})
return next(null, data)
})
}

function checkAnswers (datasets, next) {
if (datasets.length !== answers.length) return next({stage: 'checking answers', error: 'missing datasets'})
debug('checking answers')
var result = v.validate(answers, schema)
if (result.errors.length > 0) return next({stage: 'checking answers', error: 'invalid result format'})
if (datasets.length !== answers.length) {
return next({stage: 'checking answers', error: 'too few datasets in submission'})
}
var names = answers.map(function (answer) {return answer.dataset})
var missing = false
datasets.forEach(function (dataset) {
if (names.indexOf(dataset.name) === -1) missing = true
})
if (missing) return next({stage: 'checking answers', error: 'some dataset labels are missing'})
return next(null, datasets)
}

function computeResults (datasets, next) {
debug('computing results')
async.map(datasets, function (dataset, next) {
answers.forEach(function (answer) {
if (answer.dataset === dataset.name) {
Expand All @@ -76,6 +97,7 @@ var start = function (opts) {
}

function sendAnswers (results, next) {
debug('sending answers')
req.body.results = results
req.body.timestamp = timestamp()
var answer = new Answer(req.body)
Expand All @@ -86,6 +108,7 @@ var start = function (opts) {
}

function sendResults (next) {
debug('sending results')
delete req.body.answers
var submission = new Submission(req.body)
submission.save(function (err, data) {
Expand All @@ -98,14 +121,14 @@ var start = function (opts) {
getDatasets, checkAnswers, computeResults, sendAnswers, sendResults
], function (err) {
if (err) {
console.error(err)
debug('error processing results')
if (err.stage === 'getting datasets') return res.status(500).end('error fetching')
else if (err.stage === 'checking answers') return res.status(500).end(err.error)
else if (err.stage === 'computing results') return res.status(500).end('failure evaulating, check your file!')
else if (err.stage === 'sending results') return res.status(500).end('error posting')
else return res.status(500).end('error parsing results')
} else {
console.log('wrote result to db for name: ' + req.body.name)
debug('wrote result to db for ' + req.body.name + ' at time ' + req.body.timestamp)
return res.status(200).end('submission succeeeded')
}
})
Expand Down
30 changes: 30 additions & 0 deletions server/schema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// schema for results

module.exports = {
'type': 'array',
'required': true,
'items': {
'type': 'object',
'required': true,
'properties': {
'dataset': {
'type': 'string',
'required': true
},
'regions': {
'type': 'array',
'required': true,
'items': {
'type': 'object',
'required': true,
'properties': {
'coordinates' : {
'type': 'array',
'required': true
}
}
}
}
}
}
}
36 changes: 36 additions & 0 deletions server/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
var jsonschema = require('jsonschema')
var v = new jsonschema.Validator()
var schema = require('./schema')

var instance = [
{
"dataset": "00.00.test",
"regions": [{"coordinates": [[0, 0], [0, 1]]}]
},
{
"dataset": "00.01.test",
"regions": [{"coordinates": [[0, 0], [0, 1]]}]
},
{
"dataset": "01.00.test",
"regions": [{"coordinates": [[0, 0], [0, 1]]}]
},
{
"dataset": "01.01.test",
"regions": [{"coordinates": [[0, 0], [0, 1]]}]
},
{
"dataset": "02.00.test",
"regions": [{"coordinates": [[0, 0], [0, 1]]}]
},
{
"dataset": "02.01.test",
"regions": [{"coordinates": [[0, 0], [0, 1]]}]
},
{
"dataset": "03.00.test",
"regions": [{"coordinates": [[0, 0], [0, 1]]}]
}
]

console.log(v.validate(instance, schema).errors)

0 comments on commit d274250

Please sign in to comment.