Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongo driver update & less frequent database polls #8026

Merged
merged 11 commits into from
Oct 12, 2023
2 changes: 1 addition & 1 deletion lib/api/activity/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function configure(app, wares, ctx) {
api.post('/activity/', ctx.authorization.isPermitted('api:activity:create'), post_response);

api.delete('/activity/:_id', ctx.authorization.isPermitted('api:activity:delete'), function(req, res) {
ctx.activity.remove(req.params._id, function() {
ctx.activity.deleteOne(req.params._id, function() {
res.json({});
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/api/food/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function configure (app, wares, ctx) {
});
// delete record
api.delete('/food/:_id', ctx.authorization.isPermitted('api:food:delete'), function(req, res) {
ctx.food.remove(req.params._id, function ( ) {
ctx.food.deleteOne(req.params._id, function ( ) {
res.json({ });
});
});
Expand Down
2 changes: 1 addition & 1 deletion lib/api/profile/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ function configure (app, wares, ctx) {
});

api.delete('/profile/:_id', ctx.authorization.isPermitted('api:profile:delete'), function(req, res) {
ctx.profile.remove(req.params._id, function ( ) {
ctx.profile.deleteOne(req.params._id, function ( ) {
res.json({ });
});
});
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
4 changes: 2 additions & 2 deletions lib/server/activity.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var find_options = require('./query');


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

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
Expand Down Expand Up @@ -62,7 +62,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 Down
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
4 changes: 2 additions & 2 deletions lib/server/food.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

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

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
Expand Down Expand Up @@ -42,7 +42,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);
}


Expand Down
4 changes: 2 additions & 2 deletions lib/server/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var find_options = require('./query');
var consts = require('../constants');

function storage (collection, ctx) {
var ObjectID = require('mongodb').ObjectID;
var ObjectID = require('mongodb-legacy').ObjectId;

function create (obj, fn) {
obj.created_at = (new Date( )).toISOString( );
Expand Down Expand Up @@ -79,7 +79,7 @@ function storage (collection, ctx) {

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

ctx.bus.emit('data-received');
}
Expand Down
2 changes: 1 addition & 1 deletion lib/server/query.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const traverse = require('traverse');
const ObjectID = require('mongodb').ObjectID;
const ObjectID = require('mongodb-legacy').ObjectId;
const moment = require('moment');

const TWO_DAYS = 172800000;
Expand Down
Loading