Skip to content

Commit

Permalink
Revert "Merge pull request #8026 from nightscout/less_frequent_db_upd…
Browse files Browse the repository at this point in the history
…ates"

This reverts commit 2757fe5, reversing
changes made to 4a461e5.
  • Loading branch information
bewest committed Oct 19, 2023
1 parent b2a59ee commit 2e65b1d
Show file tree
Hide file tree
Showing 25 changed files with 477 additions and 194 deletions.
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.deleteOne(req.params._id, function() {
ctx.activity.remove(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.deleteOne(req.params._id, function ( ) {
ctx.food.remove(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.deleteOne(req.params._id, function ( ) {
ctx.profile.remove(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.modifiedCount });
resolve({ updated: result.result.nModified });
}
});
});
Expand All @@ -91,7 +91,7 @@ function deleteOne (col, identifier) {
if (err) {
reject(err);
} else {
resolve({ deleted: result.deletedCount });
resolve({ deleted: result.result.n });
}
});
});
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-legacy').ObjectId
, ObjectID = require('mongodb').ObjectID
;


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

// fallback to "identifier = _id"
if (checkForHexRegExp.test(identifier)) {
filterOpts.push({ _id: new ObjectID(identifier) });
filterOpts.push({ _id: 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: new ObjectID(identifier) });
filterItems.push({ identifier: { $exists: false }, _id: ObjectID(identifier) });
}
}

Expand Down
21 changes: 6 additions & 15 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-legacy').ObjectId;
var ObjectID = require('mongodb').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.insertOne(obj, function (err, doc) {
collection.insert(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, obj);
fn(null, doc.ops);
});
});
}
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,8 +65,6 @@ 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 @@ -79,7 +77,7 @@ function init (env, ctx) {

function remove (collection) {
function doRemove (_id, callback) {
collection.deleteOne({ '_id': new ObjectID(_id) }, function (err) {
collection.remove({ '_id': new ObjectID(_id) }, function (err) {
storage.reload(function loaded() {
callback(err, null);
});
Expand All @@ -94,7 +92,7 @@ function init (env, ctx) {
if (!obj.created_at) {
obj.created_at = (new Date()).toISOString();
}
collection.insertOne(obj, function (err) {
collection.save(obj, function (err) {
//id should be added for new docs
storage.reload(function loaded() {
callback(err, obj);
Expand Down Expand Up @@ -137,14 +135,8 @@ 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 @@ -160,7 +152,6 @@ 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('Nightscout is still starting and should be available within about 15 seconds.');
$('#loadingMessageText').html('Server is starting and still loading data, retrying load in 5 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-legacy').ObjectId;
var ObjectID = require('mongodb').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( ).deleteOne({ '_id': objId }, fn);
return api( ).remove({ '_id': objId }, fn);
}

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

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

function boot (env, language) {

function startBoot(ctx, next) {

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

ctx.bootErrors = [ ];
ctx.moment = require('moment-timezone');
Expand Down Expand Up @@ -40,7 +38,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 @@ -150,6 +148,16 @@ 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 @@ -162,6 +170,7 @@ 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 @@ -286,7 +295,6 @@ 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: 6 additions & 13 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) {
if (err !== null && err.message) {
console.log('Error inserting the device status object', err.message);
errorOccurred = true;
fn(err.message, null);
return;
}

if (results) {
if (!obj._id) obj._id = results.insertedId;
if (!err) {

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

ctx.bus.emit('data-update', {
Expand All @@ -47,11 +47,6 @@ 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 @@ -105,19 +100,17 @@ function storage (collection, ctx) {

function removed (err, stat) {

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

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

fn(err, stat);
}

return api().deleteMany(
return api().remove(
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-legacy').ObjectId;
var ObjectID = require('mongodb').ObjectID;
var moment = require('moment');

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

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

ctx.bus.emit('data-update', {
type: 'entries'
, op: 'remove'
, count: stat.deletedCount
, count: stat.result.n
, 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().replaceOne(query, doc, { upsert: true }, function(err, updateResults) {
api().update(query, doc, { upsert: true }, function(err, updateResults) {
firstErr = firstErr || err;

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

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

function getEntry (id, fn) {
api().findOne({ "_id": new ObjectId(id) }, function(err, entry) {
api().findOne({ _id: 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-legacy').ObjectId;
var ObjectID = require('mongodb').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( ).deleteOne({ '_id': objId }, fn);
return api( ).remove({ '_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-legacy').ObjectId;
var ObjectID = require('mongodb').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( ).deleteOne({ '_id': objId }, fn);
api( ).remove({ '_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-legacy').ObjectId;
const ObjectID = require('mongodb').ObjectID;
const moment = require('moment');

const TWO_DAYS = 172800000;
Expand Down

0 comments on commit 2e65b1d

Please sign in to comment.