Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion lib/api/activity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function configure(app, wares, ctx) {
});

function config_authed(app, api, wares, ctx) {

function post_response(req, res) {
var activity = req.body;

Expand Down
4 changes: 2 additions & 2 deletions lib/api3/storage/mongoCollection/modify.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ function updateOne (col, identifier, setFields) {
if (err) {
reject(err);
} else {
resolve({ updated: result.result.nModified });
resolve({ updated: result.modifiedCount });
}
});
});
Expand All @@ -91,7 +91,7 @@ function deleteOne (col, identifier) {
if (err) {
reject(err);
} else {
resolve({ deleted: result.result.n });
resolve({ deleted: result.deletedCount });
}
});
});
Expand Down
6 changes: 3 additions & 3 deletions lib/api3/storage/mongoCollection/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const _ = require('lodash')
, checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$")
, ObjectID = require('mongodb').ObjectID
, ObjectID = require('mongodb-legacy').ObjectId
;


Expand Down Expand Up @@ -112,7 +112,7 @@ function filterForOne (identifier) {

// fallback to "identifier = _id"
if (checkForHexRegExp.test(identifier)) {
filterOpts.push({ _id: ObjectID(identifier) });
filterOpts.push({ _id: new ObjectID(identifier) });
}

return { $or: filterOpts };
Expand All @@ -137,7 +137,7 @@ function identifyingFilter (identifier, doc, dedupFallbackFields) {

// fallback to "identifier = _id" (APIv1)
if (checkForHexRegExp.test(identifier)) {
filterItems.push({ identifier: { $exists: false }, _id: ObjectID(identifier) });
filterItems.push({ identifier: { $exists: false }, _id: new ObjectID(identifier) });
}
}

Expand Down
21 changes: 15 additions & 6 deletions lib/authorization/storage.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
var _ = require('lodash');
var crypto = require('crypto');
var shiroTrie = require('shiro-trie');
var ObjectID = require('mongodb').ObjectID;
var ObjectID = require('mongodb-legacy').ObjectId;

var find_options = require('../server/query');

Expand All @@ -27,22 +27,22 @@ function init (env, ctx) {
if (!Object.prototype.hasOwnProperty.call(obj, 'created_at')) {
obj.created_at = (new Date()).toISOString();
}
collection.insert(obj, function (err, doc) {
collection.insertOne(obj, function (err, doc) {
if (err != null && err.message) {
console.log('Data insertion error', err.message);
fn(err.message, null);
return;
}
storage.reload(function loaded() {
fn(null, doc.ops);
fn(null, obj);
});
});
}
return doCreate;
}

function list (collection) {
function doList(opts, fn) {
function doList(opts, fn) {
// these functions, find, sort, and limit, are used to
// dynamically configure the request, based on the options we've
// been given
Expand All @@ -65,6 +65,8 @@ function init (env, ctx) {
fn(err, entries);
}

console.log('Loading',opts);

// now just stitch them all together
limit.call(collection
.find(query_for(opts))
Expand All @@ -77,7 +79,7 @@ function init (env, ctx) {

function remove (collection) {
function doRemove (_id, callback) {
collection.remove({ '_id': new ObjectID(_id) }, function (err) {
collection.deleteOne({ '_id': new ObjectID(_id) }, function (err) {
storage.reload(function loaded() {
callback(err, null);
});
Expand All @@ -92,7 +94,7 @@ function init (env, ctx) {
if (!obj.created_at) {
obj.created_at = (new Date()).toISOString();
}
collection.save(obj, function (err) {
collection.insertOne(obj, function (err) {
//id should be added for new docs
storage.reload(function loaded() {
callback(err, obj);
Expand Down Expand Up @@ -135,8 +137,14 @@ function init (env, ctx) {

storage.reload = function reload (callback) {

console.log('Reloading auth data');

storage.listRoles({sort: {name: 1}}, function listResults (err, results) {

console.log('Roles listed');

if (err) {
console.log('Problem listing roles', err);
return callback && callback(err);
}

Expand All @@ -152,6 +160,7 @@ function init (env, ctx) {

storage.listSubjects({sort: {name: 1}}, function listResults (err, results) {
if (err) {
console.log('Problem listing subjects', err);
return callback && callback(err);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ client.init = function init (callback) {
}).done(function success (serverSettings) {
if (serverSettings.runtimeState !== 'loaded') {
console.log('Server is still loading data');
$('#loadingMessageText').html('Server is starting and still loading data, retrying load in 5 seconds');
$('#loadingMessageText').html('Nightscout is still starting and should be available within about 15 seconds.');
window.setTimeout(window.Nightscout.client.init, 5000);
return;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/client/renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -757,6 +757,8 @@ function init (client, d3) {
chart().basals.attr('display', 'none');

operation = 'Move';
var x = Math.min(Math.max(0, d3.event.x), chart().charts.attr('width'));
newTime = new Date(chart().xScale.invert(x));
})
.on('drag', function() {
//console.log(d3.event);
Expand Down
45 changes: 29 additions & 16 deletions lib/server/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,37 @@ var find_options = require('./query');


function storage (env, ctx) {
var ObjectID = require('mongodb').ObjectID;

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
api().insert(obj, function (err, doc) {
if (err != null && err.message) {
console.log('Activity data insertion error', err.message);
fn(err.message, null);
return;
var ObjectID = require('mongodb-legacy').ObjectId;

function create (docs, fn) {
var firstErr = null
, numDocs = docs.length
, totalCreated = 0;

docs.forEach(function(doc) {
if (!Object.prototype.hasOwnProperty.call(doc, 'created_at')) {
doc.created_at = (new Date( )).toISOString( );
}
fn(null, doc.ops);
var query = (doc.created_at && doc._id) ? { _id: doc._id, created_at: doc.created_at } : doc;
api().replaceOne(query, doc, { upsert: true }, function(err, updateResults) {
firstErr = firstErr || err;

if (++totalCreated === numDocs) {
fn(firstErr, docs);
}
});
});
}


function save (obj, fn) {
obj._id = new ObjectID(obj._id);
obj.created_at = (new Date( )).toISOString( );
api().save(obj, function (err, doc) {
fn(err, doc);
if (!Object.prototype.hasOwnProperty.call(obj, 'created_at')) {
obj.created_at = (new Date( )).toISOString( );
}
api().insertOne(obj, function (err) {
//id should be added for new docs
fn(err, obj);
});
}

Expand Down Expand Up @@ -62,7 +74,7 @@ function storage (env, ctx) {

function remove (_id, fn) {
var objId = new ObjectID(_id);
return api( ).remove({ '_id': objId }, fn);
return api( ).deleteOne({ '_id': objId }, fn);
}

function api ( ) {
Expand All @@ -78,8 +90,9 @@ function storage (env, ctx) {
return api;
}

module.exports = storage;

storage.queryOpts = {
dateField: 'created_at'
};

module.exports = storage;

20 changes: 6 additions & 14 deletions lib/server/bootevent.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
'use strict';

const _ = require('lodash');
const UPDATE_THROTTLE = 5000;
const UPDATE_THROTTLE = 15000;

function boot (env, language) {

function startBoot(ctx, next) {

console.log('Executing startBoot');
console.log('++++++++++++++++++++++++++++++');
console.log('Nightscout Executing startBoot');
console.log('++++++++++++++++++++++++++++++');

ctx.bootErrors = [ ];
ctx.moment = require('moment-timezone');
Expand Down Expand Up @@ -38,7 +40,7 @@ function boot (env, language) {

const isLTS = process.release.lts ? true : false;

if (isLTS && (semver.satisfies(nodeVersion, '^20.0.0') || semver.satisfies(nodeVersion, '^18.0.0') || semver.satisfies(nodeVersion, '^16.0.0') || semver.satisfies(nodeVersion, '^14.0.0'))) {
if (isLTS || (semver.satisfies(nodeVersion, '^20.0.0') || semver.satisfies(nodeVersion, '^18.0.0') || semver.satisfies(nodeVersion, '^16.0.0') || semver.satisfies(nodeVersion, '^14.0.0'))) {
//Latest Node 14 LTS and Node 16 LTS are recommended and supported.
//Require at least Node 14 without known security issues
console.debug('Node LTS version ' + nodeVersion + ' is supported');
Expand Down Expand Up @@ -148,16 +150,6 @@ function boot (env, language) {
}

try {
if (_.startsWith(env.storageURI, 'openaps://')) {
require('../storage/openaps-storage')(env, function ready (err, store) {
if (err) {
throw err;
}
ctx.store = store;
console.log('OpenAPS Storage system ready');
next();
});
} else {
//TODO assume mongo for now, when there are more storage options add a lookup
require('../storage/mongo-storage')(env, function ready(err, store) {
// FIXME, error is always null, if there is an error, the index.js will throw an exception
Expand All @@ -170,7 +162,6 @@ function boot (env, language) {
ctx.store = store;
next();
});
}
} catch (err) {
console.info('ERROR CONNECTING TO MONGO', err);
ctx.bootErrors = ctx.bootErrors || [ ];
Expand Down Expand Up @@ -295,6 +286,7 @@ function boot (env, language) {

ctx.bus.on('data-received', function forceReloadData ( ) {
console.info('got data-received event, requesting reload');
ctx.bus.emit('data-loaded'); // Since we update local sandbox instantly, process data-loaded right away in case this gets debounced
updateData();
});

Expand Down
19 changes: 13 additions & 6 deletions lib/server/devicestatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ function storage (collection, ctx) {
obj.utcOffset = d.utcOffset();

api().insertOne(obj, function(err, results) {
if (err !== null && err.message) {

if (err) {
console.log('Error inserting the device status object', err.message);
errorOccurred = true;
fn(err.message, null);
return;
}

if (!err) {

if (!obj._id) obj._id = results.insertedIds[0]._id;
if (results) {
if (!obj._id) obj._id = results.insertedId;
r.push(obj);

ctx.bus.emit('data-update', {
Expand All @@ -47,6 +47,11 @@ function storage (collection, ctx) {
fn(null, r);
ctx.bus.emit('data-received');
}
} else {
console.log('Error inserting the device status object', err.message);
errorOccurred = true;
fn(err.message, null);
return;
}
});
};
Expand Down Expand Up @@ -100,17 +105,19 @@ function storage (collection, ctx) {

function removed (err, stat) {

console.log('removed', err, stat);

ctx.bus.emit('data-update', {
type: 'devicestatus'
, op: 'remove'
, count: stat.result.n
, count: stat.deletedCount
, changes: opts.find._id
});

fn(err, stat);
}

return api().remove(
return api().deleteMany(
query_for(opts), removed);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/server/entries.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

var es = require('event-stream');
var find_options = require('./query');
var ObjectID = require('mongodb').ObjectID;
var ObjectId = require('mongodb-legacy').ObjectId;
var moment = require('moment');

/**********\
Expand Down Expand Up @@ -46,12 +46,12 @@ function storage (env, ctx) {
}

function remove (opts, fn) {
api().remove(query_for(opts), function(err, stat) {
api().deleteMany(query_for(opts), function(err, stat) {

ctx.bus.emit('data-update', {
type: 'entries'
, op: 'remove'
, count: stat.result.n
, count: stat.deletedCount
, changes: opts.find._id
});

Expand Down Expand Up @@ -110,12 +110,12 @@ function storage (env, ctx) {
if (doc.dateString) doc.dateString = doc.sysTime;

var query = (doc.sysTime && doc.type) ? { sysTime: doc.sysTime, type: doc.type } : doc;
api().update(query, doc, { upsert: true }, function(err, updateResults) {
api().replaceOne(query, doc, { upsert: true }, function(err, updateResults) {
firstErr = firstErr || err;

if (!err) {
if (updateResults.result.upserted) {
doc._id = updateResults.result.upserted[0]._id
if (updateResults) {
if (updateResults.upsertedCount == 1) {
doc._id = updateResults.upsertedId
}

ctx.bus.emit('data-update', {
Expand All @@ -135,7 +135,7 @@ function storage (env, ctx) {
}

function getEntry (id, fn) {
api().findOne({ _id: ObjectID(id) }, function(err, entry) {
api().findOne({ "_id": new ObjectId(id) }, function(err, entry) {
if (err) {
fn(err);
} else {
Expand Down
Loading