Skip to content

Commit 6893781

Browse files
committed
Merge pull request #207 from nightscout/release/0.5.0
Cookie Monster (0.5.0)
2 parents adc24df + 6e8cacb commit 6893781

22 files changed

+336
-67
lines changed

.gitattributes

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Auto detect text files and perform LF normalization
2+
* text=auto
3+
4+
# Standard to msysgit
5+
*.doc diff=astextplain
6+
*.DOC diff=astextplain
7+
*.docx diff=astextplain
8+
*.DOCX diff=astextplain
9+
*.dot diff=astextplain
10+
*.DOT diff=astextplain
11+
*.pdf diff=astextplain
12+
*.PDF diff=astextplain
13+
*.rtf diff=astextplain
14+
*.RTF diff=astextplain

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,4 @@ static/bower_components/
1111
.DS_Store
1212

1313
.vagrant
14-
/iisnode
15-
/web.config
14+
/iisnode

bin/post-mbg.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/sh
2+
# "date": "1413782506964"
3+
4+
curl -H "Content-Type: application/json" -H "api-secret: $API_SECRET" -XPOST 'http://localhost:1337/api/v1/entries/' -d '{
5+
"mbg": 100,
6+
"type": "mbg",
7+
"date": "1413779270000"
8+
}'

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nightscout",
3-
"version": "0.4.3",
3+
"version": "0.5.0",
44
"dependencies": {
55
"angularjs": "1.3.0-beta.19",
66
"bootstrap": "~3.2.0",

env.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,14 @@ function config ( ) {
2323
if (readENV('SCM_GIT_EMAIL') == 'windowsazure' && readENV('ScmType') == 'GitHub') {
2424
git.cwd('/home/site/repository');
2525
}
26-
git.short(function record_git_head (head) {
27-
console.log("GIT HEAD", head);
28-
env.head = head;
29-
});
26+
if (readENV('SCM_COMMIT_ID')) {
27+
env.head = readENV('SCM_COMMIT_ID');
28+
} else {
29+
git.short(function record_git_head (head) {
30+
console.log("GIT HEAD", head);
31+
env.head = head;
32+
});
33+
}
3034
env.version = software.version;
3135
env.name = software.name;
3236

lib/api/entries/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function configure (app, wares, entries) {
4848
var incoming = [ ];
4949
// Potentially a single json encoded body.
5050
// This can happen from either an url-encoded or json content-type.
51-
if ('sgv' in req.body) {
51+
if ('date' in req.body) {
5252
// add it to the incoming list
5353
incoming.push(req.body);
5454
}

lib/devicestatus.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
function configure (collection, storage) {
3+
function storage (collection, storage) {
44

55
function create (obj, fn) {
66
obj.created_at = (new Date( )).toISOString( );
@@ -31,4 +31,18 @@ function configure (collection, storage) {
3131
api.last = last;
3232
return api;
3333
}
34-
module.exports = configure;
34+
35+
function ensureIndexes(name, storage) {
36+
storage.with_collection(name)(function (err, collection) {
37+
if (err) {
38+
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
39+
} else {
40+
storage.ensureIndexes(collection, ['created_at']);
41+
}
42+
});
43+
}
44+
45+
module.exports = {
46+
storage: storage,
47+
ensureIndexes: ensureIndexes
48+
};

lib/entries.js

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
var es = require('event-stream');
44
var sgvdata = require('sgvdata');
55

6+
var TEN_MINS = 10 * 60 * 1000;
7+
68
/**********\
79
* Entries
810
* Encapsulate persistent storage of sgv entries.
911
\**********/
1012

11-
function entries (name, storage) {
13+
function storage(name, storage, pushover) {
1214

1315
// TODO: Code is a little redundant.
1416

@@ -61,7 +63,13 @@ function entries (name, storage) {
6163

6264
// return writable stream to lint each sgv record passing through it
6365
function map ( ) {
64-
return sgvdata.lint( );
66+
function iter (item, next) {
67+
if (item && item.type) {
68+
return next(null, item);
69+
}
70+
return next(null, sgvdata.sync.json.echo(item));
71+
}
72+
return es.map(iter);
6573
}
6674

6775
// writable stream that persists all records
@@ -73,7 +81,6 @@ function entries (name, storage) {
7381
if (err) return fn(err, result);
7482
// batch insert a list of records
7583
create(result, fn);
76-
return;
7784
}
7885
// lint and store the entire list
7986
return es.pipeline(map( ), es.writeArray(done));
@@ -100,11 +107,39 @@ function entries (name, storage) {
100107
firstErr = firstErr || err;
101108
totalCreated += created;
102109
});
110+
sendPushover(doc);
103111
});
104112
fn(firstErr, totalCreated, docs);
105113
});
106114
}
107115

116+
//currently the Android upload will send the last MBG over and over, make sure we get a single notification
117+
var lastMBG = 0;
118+
119+
function sendPushover(doc) {
120+
if (doc.type && doc.mbg && doc.type == 'mbg' && doc.date && doc.date != lastMBG && pushover) {
121+
var offset = new Date().getTime() - doc.date;
122+
if (offset > TEN_MINS) {
123+
console.info('No MBG Pushover, offset: ' + offset + ' too big, doc.date: ' + doc.date + ', now: ' + new Date().getTime());
124+
} else {
125+
var msg = {
126+
expire: 14400, // 4 hours
127+
message: '\nMeter BG: ' + doc.mbg,
128+
title: 'Calibration',
129+
sound: 'magic',
130+
timestamp: new Date(doc.date),
131+
priority: 0,
132+
retry: 30
133+
};
134+
135+
pushover.send(msg, function (err, result) {
136+
console.log(result);
137+
});
138+
}
139+
lastMBG = doc.date;
140+
}
141+
}
142+
108143
function getEntry(fn, id) {
109144
console.info("trying to find entry for id: " + id);
110145
with_collection(function(err, collection) {
@@ -152,6 +187,19 @@ function entries (name, storage) {
152187
return api;
153188
}
154189

190+
function ensureIndexes(name, storage) {
191+
storage.with_collection(name)(function(err, collection) {
192+
if (err) {
193+
console.error("ensureIndexes, unable to get collection for: " + name + " - " + err);
194+
} else {
195+
storage.ensureIndexes(collection, ['date', 'type', 'sgv']);
196+
}
197+
});
198+
}
199+
155200
// expose module
156-
module.exports = entries;
201+
module.exports = {
202+
storage: storage,
203+
ensureIndexes: ensureIndexes
204+
};
157205

lib/pebble.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ function pebble (req, res) {
8080
console.error("req.devicestatus.tail", err);
8181
}
8282

83-
req.entries.list({count: 2}, get_latest);
83+
req.entries.list({count: 2, find: { "sgv": { $exists: true }}}, get_latest);
8484
});
8585
}
8686
function configure (entries, devicestatus) {

lib/storage.js

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22
var mongodb = require('mongodb');
33

4-
function init (env) {
4+
function init (env, cb) {
55
var MongoClient = mongodb.MongoClient;
66

77
var my = { };
@@ -13,8 +13,12 @@ function init (env) {
1313
}
1414
console.log("Connecting to mongo");
1515
MongoClient.connect(env.mongo, function connected (err, db) {
16-
console.log("Connected to mongo, ERROR: %j", err);
17-
if (err) { throw err; }
16+
if (err) {
17+
console.log("Error connecting to mongo, ERROR: %j", err);
18+
throw err;
19+
} else {
20+
console.log("Connected to mongo");
21+
}
1822
my.db = db;
1923
mongo.pool.db = my.db = db;
2024

@@ -30,7 +34,7 @@ function init (env) {
3034

3135
mongo.pool = function ( ) {
3236
return my;
33-
}
37+
};
3438

3539
mongo.collection = function get_collection (name) {
3640
return mongo.pool( ).db.collection(name);
@@ -42,7 +46,18 @@ function init (env) {
4246
};
4347
};
4448

45-
return mongo( );
49+
mongo.ensureIndexes = function(collection, fields) {
50+
fields.forEach(function (field) {
51+
console.info("ensuring index for: " + field);
52+
collection.ensureIndex(field, function (err) {
53+
if (err) {
54+
console.error("unable to ensureIndex for: " + field + " - " + err);
55+
}
56+
});
57+
});
58+
};
59+
60+
return mongo(cb);
4661
}
4762

4863
module.exports = init;

0 commit comments

Comments
 (0)