Skip to content

Commit

Permalink
Merge branch 'dev' into wip/enable-iob
Browse files Browse the repository at this point in the history
  • Loading branch information
jasoncalabrese committed Mar 6, 2015
2 parents aa9233b + 5ac9978 commit 508d582
Show file tree
Hide file tree
Showing 5 changed files with 303 additions and 94 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,5 @@ static/bower_components/

# istanbul output
coverage/

npm-debug.log
8 changes: 3 additions & 5 deletions lib/api/entries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ function configure (app, wares, entries) {
// Middleware to format any response involving entries.
function format_entries (req, res, next) {
var output = es.readArray(res.entries || [ ]);
if (res.entries_err) return res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
if (res.entries_err) {
return res.sendJSONStatus(res, consts.HTTP_INTERNAL_ERROR, 'Mongo Error', res.entries_err);
}
return res.format({
text: function ( ) {
es.pipeline(output, sgvdata.format( ), res);
Expand Down Expand Up @@ -113,7 +115,6 @@ function configure (app, wares, entries) {
res.entries_err = err;
return next( );
});
return;
}, format_entries);

api.get('/entries/current', function(req, res, next) {
Expand All @@ -122,7 +123,6 @@ function configure (app, wares, entries) {
res.entries_err = err;
return next( );
});
return;
}, format_entries);


Expand All @@ -131,15 +131,13 @@ function configure (app, wares, entries) {
api.post('/entries/preview', function (req, res, next) {
req.persist_entries = false;
next( );
return;
}, insert_entries, format_entries);

if (app.enabled('api')) {
// Create and store new sgv entries
api.post('/entries/', wares.verifyAuthorization, function (req, res, next) {
req.persist_entries = true;
next( );
return;
}, insert_entries, format_entries);
}

Expand Down
119 changes: 58 additions & 61 deletions lib/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function storage(name, storage, pushover) {
with_collection(function (err, collection) {
// these functions, find, sort, and limit, are used to
// dynamically configure the request, based on the options we've
// been give
// been given

// determine find options
function find ( ) {
Expand Down Expand Up @@ -51,13 +51,9 @@ function storage(name, storage, pushover) {

// now just stitch them all together
limit.call(collection
.find(find( ))
.sort(sort( )))
// .limit(limit( ))
.toArray(toArray)
;
// limit.call(sort.call(find.call(collection))).toArray(toArray);

.find(find( ))
.sort(sort( ))
).toArray(toArray);
});
}

Expand Down Expand Up @@ -96,77 +92,79 @@ function storage(name, storage, pushover) {

// store new documents using the storage mechanism
function create (docs, fn) {
with_collection(function(err, collection) {
if (err) { fn(err); return; }
// potentially a batch insert
var firstErr = null,
totalCreated = 0;

docs.forEach(function(doc) {
collection.update(doc, doc, {upsert: true}, function (err, created) {
firstErr = firstErr || err;
totalCreated += created;
});
sendPushover(doc);
with_collection(function(err, collection) {
if (err) { fn(err); return; }
// potentially a batch insert
var firstErr = null,
numDocs = docs.length,
totalCreated = 0;

docs.forEach(function(doc) {
collection.update(doc, doc, {upsert: true}, function (err, created) {
firstErr = firstErr || err;
if (++totalCreated === numDocs) {
fn(firstErr, docs);
}
});
fn(firstErr, totalCreated, docs);
sendPushover(doc);
});
});
}

//currently the Android upload will send the last MBG over and over, make sure we get a single notification
var lastMBG = 0;

function sendPushover(doc) {
if (doc.type && doc.mbg && doc.type == 'mbg' && doc.date && doc.date != lastMBG && pushover) {
var offset = new Date().getTime() - doc.date;
if (offset > TEN_MINS) {
console.info('No MBG Pushover, offset: ' + offset + ' too big, doc.date: ' + doc.date + ', now: ' + new Date().getTime());
} else {
var msg = {
expire: 14400, // 4 hours
message: '\nMeter BG: ' + doc.mbg,
title: 'Calibration',
sound: 'magic',
timestamp: new Date(doc.date),
priority: 0,
retry: 30
};

pushover.send(msg, function (err, result) {
console.log(result);
});
}
lastMBG = doc.date;
if (doc.type && doc.mbg && doc.type == 'mbg' && doc.date && doc.date != lastMBG && pushover) {
var offset = new Date().getTime() - doc.date;
if (offset > TEN_MINS) {
console.info('No MBG Pushover, offset: ' + offset + ' too big, doc.date: ' + doc.date + ', now: ' + new Date().getTime());
} else {
var msg = {
expire: 14400, // 4 hours
message: '\nMeter BG: ' + doc.mbg,
title: 'Calibration',
sound: 'magic',
timestamp: new Date(doc.date),
priority: 0,
retry: 30
};

pushover.send(msg, function (err, result) {
console.log(result);
});
}
lastMBG = doc.date;
}
}

function getEntry(fn, id) {
console.info("trying to find entry for id: " + id);
with_collection(function(err, collection) {
console.info("trying to find entry for id: " + id);
with_collection(function(err, collection) {
if (err)
fn(err);
else
collection.findOne({"_id": ObjectID(id)}, function (err, entry) {
if (err)
fn(err);
fn(err);
else
collection.findOne({"_id": ObjectID(id)}, function (err, entry) {
if (err)
fn(err);
else
fn(null, entry);
});
});
fn(null, entry);
});
});
}

function getEntries(fn, count) {
with_collection(function(err, collection) {
with_collection(function(err, collection) {
if (err)
fn(err);
else
collection.find({ }).sort({"date": -1}).limit(count).toArray(function (err, entries) {
if (err)
fn(err);
fn(err);
else
collection.find({ }).sort({"date": -1}).limit(count).toArray(function (err, entries) {
if (err)
fn(err);
else
fn(null, entries);
});
});
fn(null, entries);
});
});
}

// closure to represent the API
Expand Down Expand Up @@ -202,4 +200,3 @@ module.exports = {
storage: storage,
ensureIndexes: ensureIndexes
};

55 changes: 28 additions & 27 deletions tests/api.entries.test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

var request = require('supertest');
var should = require('should');
var load = require('./fixtures/load');

describe('Entries REST api', function ( ) {
var entries = require('../lib/api/entries/');

before(function (done) {
var env = require('../env')( );
this.wares = require('../lib/middleware/')(env);
Expand All @@ -18,32 +18,39 @@ describe('Entries REST api', function ( ) {
self.archive.create(load('json'), done);
});
});

after(function (done) {
this.archive( ).remove({ }, done);
});

it('should be a module', function ( ) {
entries.should.be.ok;

});
it('/entries.json', function (done) {

// keep this test pinned at or near the top in order to validate all
// entries successfully uploaded. if res.body.length is short of the
// expected value, it may indicate a regression in the create
// function callback logic in entries.js.
it('gets requested number of entries', function (done) {
var count = 30;
request(this.app)
.get('/entries.json')
.get('/entries.json?count=' + count)
.expect(200)
.end(function (err, res) {
.end(function (err, res) {
// console.log('body', res.body);
res.body.length.should.equal(10);
res.body.length.should.equal(count);
done( );
});
});

it('/entries.json', function (done) {
it('gets default number of entries', function (done) {
var defaultCount = 10;
request(this.app)
.get('/entries.json?count=30')
.get('/entries.json')
.expect(200)
.end(function (err, res) {
.end(function (err, res) {
// console.log('body', res.body);
res.body.length.should.equal(30);
res.body.length.should.equal(defaultCount);
done( );
});
});
Expand All @@ -52,29 +59,23 @@ describe('Entries REST api', function ( ) {
request(this.app)
.get('/entries/current.json')
.expect(200)
.end(function (err, res) {
.end(function (err, res) {
res.body.length.should.equal(1);
done( );
// console.log('err', err, 'res', res);
});

});

it('/entries/preview', function (done) {

request(this.app)
.post('/entries/preview.json')
.send(load('json'))
.expect(201)
.end(function (err, res) {
// console.log(res.body);
res.body.length.should.equal(30);
done( );
// console.log('err', err, 'res', res);
})
;

request(this.app)
.post('/entries/preview.json')
.send(load('json'))
.expect(201)
.end(function (err, res) {
// console.log(res.body);
res.body.length.should.equal(30);
done( );
// console.log('err', err, 'res', res);
});
});

});

Loading

0 comments on commit 508d582

Please sign in to comment.