Skip to content

Commit

Permalink
Merge pull request nightscout#4 from nightscout/master
Browse files Browse the repository at this point in the history
14.06
  • Loading branch information
DarynGit authored Sep 28, 2020
2 parents 9ec0af0 + 1818810 commit bd0519d
Show file tree
Hide file tree
Showing 33 changed files with 901 additions and 403 deletions.
4 changes: 4 additions & 0 deletions .github/ISSUE_TEMPLATE/--bug-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ label: bug

---

**If you need support for Nightscout, PLEASE DO NOT FILE A TICKET HERE**
For support, please post a question to the "CGM in The Cloud" group in Facebook
(https://www.facebook.com/groups/cgminthecloud) or visit the WeAreNotWaiting Discord at https://discord.gg/zg7CvCQ

**Describe the bug**
A clear and concise description of what the bug is.

Expand Down
4 changes: 4 additions & 0 deletions .github/ISSUE_TEMPLATE/--feature-request--.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ about: Suggest an idea for this project

---

**If you need support for Nightscout, PLEASE DO NOT FILE A TICKET HERE**
For support, please post a question to the "CGM in The Cloud" group in Facebook
(https://www.facebook.com/groups/cgminthecloud) or visit the WeAreNotWaiting Discord at https://discord.gg/zg7CvCQ

**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Expand Down
4 changes: 3 additions & 1 deletion .github/ISSUE_TEMPLATE/--individual-troubleshooting-help.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ about: Getting help with your own individual setup of Nightscout

Having issues getting Nightscout up and running? Instead of creating an issue here, please use one of the existing support channels for Nightscout.

The documentation for Nightscout lives at (https://nightscout.github.io)

The main support channel is on Facebook: please join the CGM In The Cloud Facebook group (https://www.facebook.com/groups/cgminthecloud) and start a post there.

**Suggestions to include in your post when you are asking for help:**
1. Include what you are trying to do: ("*I am trying to set up Nightscout for the first time.*")
2. Include which step you are on and what the problem is: ("*I deployed on Heroku, but I'm not seeing any BG data.*")
3. If possible, include a link to the version of documentation you are following ("*I'm following the OpenAPS Nightscout setup docs (https://openaps.readthedocs.io/en/latest/docs/While%20You%20Wait%20For%20Gear/nightscout-setup.html#nightscout-setup-with-heroku)*")

Other places you can find support and assistance for Nightscout include Gitter's [nightscout/public](https://gitter.im/nightscout/public) channel.
Other places you can find support and assistance for Nightscout include our Discord channel at (https://discord.gg/zg7CvCQ)
49 changes: 18 additions & 31 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,25 @@ function create (env, ctx) {
));
});

// Allow static resources to be cached for week
var maxAge = 7 * 24 * 60 * 60 * 1000;

if (process.env.NODE_ENV === 'development') {
maxAge = 1;
console.log('Development environment detected, setting static file cache age to 1 second');
}

var staticFiles = express.static(env.static_files, {
maxAge
});

// serve the static content
app.use(staticFiles);

if (ctx.bootErrors && ctx.bootErrors.length > 0) {
app.get('*', require('./lib/server/booterror')(ctx));
const bootErrorView = require('./lib/server/booterror')(env, ctx);
bootErrorView.setLocals(app.locals);
app.get('*', bootErrorView);
return app;
}

Expand Down Expand Up @@ -256,36 +273,6 @@ function create (env, ctx) {
res.sendFile(__dirname + '/swagger.yaml');
});

/* // FOR DEBUGGING MEMORY LEEAKS
if (env.settings.isEnabled('dumps')) {
var heapdump = require('heapdump');
app.get('/api/v2/dumps/start', function(req, res) {
var path = new Date().toISOString() + '.heapsnapshot';
path = path.replace(/:/g, '-');
console.info('writing dump to', path);
heapdump.writeSnapshot(path);
res.send('wrote dump to ' + path);
});
}
*/

// app.get('/package.json', software);

// Allow static resources to be cached for week
var maxAge = 7 * 24 * 60 * 60 * 1000;

if (process.env.NODE_ENV === 'development') {
maxAge = 1;
console.log('Development environment detected, setting static file cache age to 1 second');
}

var staticFiles = express.static(env.static_files, {
maxAge
});

// serve the static content
app.use(staticFiles);

// API docs

const swaggerUi = require('swagger-ui-express');
Expand Down
23 changes: 17 additions & 6 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ var env = {
settings: require('./lib/settings')()
};

var shadowEnv;

// Module to constrain all config and environment parsing to one spot.
// See README.md for info about all the supported ENV VARs
function config ( ) {

// Assume users will typo whitespaces into keys and values

shadowEnv = {};

Object.keys(process.env).forEach((key, index) => {
shadowEnv[_trim(key)] = _trim(process.env[key]);
});

env.PORT = readENV('PORT', 1337);
env.HOSTNAME = readENV('HOSTNAME', null);
env.IMPORT_CONFIG = readENV('IMPORT_CONFIG', null);
Expand Down Expand Up @@ -122,7 +133,7 @@ function updateSettings() {
});

//should always find extended settings last
env.extendedSettings = findExtendedSettings(process.env);
env.extendedSettings = findExtendedSettings(shadowEnv);

if (!readENVTruthy('TREATMENTS_AUTH', true)) {
env.settings.authDefaultRoles = env.settings.authDefaultRoles || "";
Expand All @@ -132,10 +143,10 @@ function updateSettings() {

function readENV(varName, defaultValue) {
//for some reason Azure uses this prefix, maybe there is a good reason
var value = process.env['CUSTOMCONNSTR_' + varName]
|| process.env['CUSTOMCONNSTR_' + varName.toLowerCase()]
|| process.env[varName]
|| process.env[varName.toLowerCase()];
var value = shadowEnv['CUSTOMCONNSTR_' + varName]
|| shadowEnv['CUSTOMCONNSTR_' + varName.toLowerCase()]
|| shadowEnv[varName]
|| shadowEnv[varName.toLowerCase()];

if (varName == 'DISPLAY_UNITS') {
if (value && value.toLowerCase().includes('mmol')) {
Expand All @@ -162,7 +173,7 @@ function findExtendedSettings (envs) {
extended.devicestatus = {};
extended.devicestatus.advanced = true;
extended.devicestatus.days = 1;
if(process.env['DEVICESTATUS_DAYS'] && process.env['DEVICESTATUS_DAYS'] == '2') extended.devicestatus.days = 1;
if(shadowEnv['DEVICESTATUS_DAYS'] && shadowEnv['DEVICESTATUS_DAYS'] == '2') extended.devicestatus.days = 1;

function normalizeEnv (key) {
return key.toUpperCase().replace('CUSTOMCONNSTR_', '');
Expand Down
2 changes: 1 addition & 1 deletion lib/api/devicestatus/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function configure (app, wares, ctx, env) {
q.count = 10;
}

const inMemoryData = ctx.ddata.shadow.devicestatus ? ctx.ddata.shadow.devicestatus : [];
const inMemoryData = ctx.cache.devicestatus ? ctx.cache.devicestatus : [];
const canServeFromMemory = inMemoryData.length >= q.count && Object.keys(q).length == 1 ? true : false;

if (canServeFromMemory) {
Expand Down
19 changes: 11 additions & 8 deletions lib/api/entries/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ function configure (app, wares, ctx, env) {
function prepReqModel (req, model) {
var type = model || 'sgv';
if (!req.query.find) {

req.query.find = {
type: type
};
Expand Down Expand Up @@ -447,6 +448,7 @@ function configure (app, wares, ctx, env) {
Object.keys(query.find).forEach(function(key) {
if (key == 'type') {
typeQuery = query.find[key]["$eq"];
if (!typeQuery) typeQuery = query.find.type;
} else {
inMemoryPossible = false;
}
Expand All @@ -456,14 +458,16 @@ function configure (app, wares, ctx, env) {
let inMemoryCollection;

if (typeQuery) {
if (typeQuery == 'sgv') inMemoryCollection = ctx.ddata.shadow.sgvs;
if (typeQuery == 'mbg') inMemoryCollection = ctx.ddata.shadow.mbgs;
if (typeQuery == 'cal') inMemoryCollection = ctx.ddata.shadow.cals;
} else {
const merged = _.unionWith(ctx.ddata.shadow.sgvs, ctx.ddata.shadow.mbgs, ctx.ddata.shadow.cals, function(a, b) {
return a._id == b._id;
inMemoryCollection= _.filter(ctx.cache.entries, function checkType (object) {
if (typeQuery == 'sgv') return 'sgv' in object;
if (typeQuery == 'mbg') return 'mbg' in object;
if (typeQuery == 'cal') return object.type === 'cal';
return false;
});
inMemoryCollection = _.sortBy(merged, function(item) {
} else {
inMemoryCollection = ctx.cache.getData('entries');

inMemoryCollection = _.sortBy(inMemoryCollection, function(item) {
return item.mills;
}).reverse();
}
Expand All @@ -475,7 +479,6 @@ function configure (app, wares, ctx, env) {
}

// If we get this far, query the database

// bias to entries, but allow expressing a preference
var storage = req.storage || ctx.entries;
// perform the query
Expand Down
35 changes: 27 additions & 8 deletions lib/api/treatments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function configure (app, wares, ctx, env) {
query.count = query.find ? 1000 : 100;
}

const inMemoryData = ctx.ddata.shadow.treatments;
const inMemoryData = ctx.cache.treatments;
const canServeFromMemory = inMemoryData && inMemoryData.length >= query.count && Object.keys(query).length == 1 ? true : false;

if (canServeFromMemory) {
Expand All @@ -112,12 +112,35 @@ function configure (app, wares, ctx, env) {
treatments = [treatments];
}

for (let i = 0; i < treatments.length; i++) {
const t = treatments[i];

if (!t.created_at) {
t.created_at = new Date().toISOString();
}

/*
if (!t.created_at) {
console.log('Trying to create treatment without created_at field', t);
res.sendJSONStatus(res, constants.HTTP_VALIDATION_ERROR, 'Treatments must contain created_at');
return;
}
const d = moment(t.created_at);
if (!d.isValid()) {
console.log('Trying to insert date with invalid created_at', t);
res.sendJSONStatus(res, constants.HTTP_VALIDATION_ERROR, 'Treatments created_at must be an ISO-8601 date');
return;
}
*/

}

ctx.treatments.create(treatments, function(err, created) {
if (err) {
console.log('Error adding treatment', err);
res.sendJSONStatus(res, constants.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
} else {
console.log('Treatment created');
console.log('REST API treatment created', created);
res.json(created);
}
});
Expand All @@ -138,19 +161,16 @@ function configure (app, wares, ctx, env) {
query.count = 10
}

console.log('Delete records with query: ', query);

// remove using the query
ctx.treatments.remove(query, function(err, stat) {
if (err) {
console.log('treatments delete error: ', err);
return next(err);
}

// yield some information about success of operation
res.json(stat);

console.log('treatments records deleted');

return next();
});
}
Expand Down Expand Up @@ -180,8 +200,7 @@ function configure (app, wares, ctx, env) {
ctx.treatments.save(data, function(err, created) {
if (err) {
res.sendJSONStatus(res, constants.HTTP_INTERNAL_ERROR, 'Mongo Error', err);
console.log('Error saving treatment');
console.log(err);
console.log('Error saving treatment', err);
} else {
res.json(created);
console.log('Treatment saved', data);
Expand Down
2 changes: 2 additions & 0 deletions lib/client/careportal.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,8 @@ function init (client, $) {
data.eventTime = mergeDateAndTime().toDate();
}

data.created_at = data.eventTime ? data.eventTime.toISOString() : new Date().toISOString();

if (!inputMatrix[data.eventType].profile) {
delete data.profile;
}
Expand Down
1 change: 1 addition & 0 deletions lib/constants.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"HTTP_UNAUTHORIZED" : 401,
"HTTP_VALIDATION_ERROR" : 422,
"HTTP_INTERNAL_ERROR" : 500,
"HTTP_BAD_REQUEST": 400,
"ENTRIES_DEFAULT_COUNT" : 10,
"PROFILES_DEFAULT_COUNT" : 10,
"MMOL_TO_MGDL": 18,
Expand Down
Loading

0 comments on commit bd0519d

Please sign in to comment.