Skip to content

Commit 2e65b1d

Browse files
committed
Revert "Merge pull request #8026 from nightscout/less_frequent_db_updates"
This reverts commit 2757fe5, reversing changes made to 4a461e5.
1 parent b2a59ee commit 2e65b1d

25 files changed

+477
-194
lines changed

lib/api/activity/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ function configure(app, wares, ctx) {
8888
api.post('/activity/', ctx.authorization.isPermitted('api:activity:create'), post_response);
8989

9090
api.delete('/activity/:_id', ctx.authorization.isPermitted('api:activity:delete'), function(req, res) {
91-
ctx.activity.deleteOne(req.params._id, function() {
91+
ctx.activity.remove(req.params._id, function() {
9292
res.json({});
9393
});
9494
});

lib/api/food/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ function configure (app, wares, ctx) {
7171
});
7272
// delete record
7373
api.delete('/food/:_id', ctx.authorization.isPermitted('api:food:delete'), function(req, res) {
74-
ctx.food.deleteOne(req.params._id, function ( ) {
74+
ctx.food.remove(req.params._id, function ( ) {
7575
res.json({ });
7676
});
7777
});

lib/api/profile/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ function configure (app, wares, ctx) {
9292
});
9393

9494
api.delete('/profile/:_id', ctx.authorization.isPermitted('api:profile:delete'), function(req, res) {
95-
ctx.profile.deleteOne(req.params._id, function ( ) {
95+
ctx.profile.remove(req.params._id, function ( ) {
9696
res.json({ });
9797
});
9898
});

lib/api3/storage/mongoCollection/modify.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function updateOne (col, identifier, setFields) {
6969
if (err) {
7070
reject(err);
7171
} else {
72-
resolve({ updated: result.modifiedCount });
72+
resolve({ updated: result.result.nModified });
7373
}
7474
});
7575
});
@@ -91,7 +91,7 @@ function deleteOne (col, identifier) {
9191
if (err) {
9292
reject(err);
9393
} else {
94-
resolve({ deleted: result.deletedCount });
94+
resolve({ deleted: result.result.n });
9595
}
9696
});
9797
});

lib/api3/storage/mongoCollection/utils.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

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

88

@@ -112,7 +112,7 @@ function filterForOne (identifier) {
112112

113113
// fallback to "identifier = _id"
114114
if (checkForHexRegExp.test(identifier)) {
115-
filterOpts.push({ _id: new ObjectID(identifier) });
115+
filterOpts.push({ _id: ObjectID(identifier) });
116116
}
117117

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

138138
// fallback to "identifier = _id" (APIv1)
139139
if (checkForHexRegExp.test(identifier)) {
140-
filterItems.push({ identifier: { $exists: false }, _id: new ObjectID(identifier) });
140+
filterItems.push({ identifier: { $exists: false }, _id: ObjectID(identifier) });
141141
}
142142
}
143143

lib/authorization/storage.js

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
var _ = require('lodash');
44
var crypto = require('crypto');
55
var shiroTrie = require('shiro-trie');
6-
var ObjectID = require('mongodb-legacy').ObjectId;
6+
var ObjectID = require('mongodb').ObjectID;
77

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

@@ -27,22 +27,22 @@ function init (env, ctx) {
2727
if (!Object.prototype.hasOwnProperty.call(obj, 'created_at')) {
2828
obj.created_at = (new Date()).toISOString();
2929
}
30-
collection.insertOne(obj, function (err, doc) {
30+
collection.insert(obj, function (err, doc) {
3131
if (err != null && err.message) {
3232
console.log('Data insertion error', err.message);
3333
fn(err.message, null);
3434
return;
3535
}
3636
storage.reload(function loaded() {
37-
fn(null, obj);
37+
fn(null, doc.ops);
3838
});
3939
});
4040
}
4141
return doCreate;
4242
}
4343

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

68-
console.log('Loading',opts);
69-
7068
// now just stitch them all together
7169
limit.call(collection
7270
.find(query_for(opts))
@@ -79,7 +77,7 @@ function init (env, ctx) {
7977

8078
function remove (collection) {
8179
function doRemove (_id, callback) {
82-
collection.deleteOne({ '_id': new ObjectID(_id) }, function (err) {
80+
collection.remove({ '_id': new ObjectID(_id) }, function (err) {
8381
storage.reload(function loaded() {
8482
callback(err, null);
8583
});
@@ -94,7 +92,7 @@ function init (env, ctx) {
9492
if (!obj.created_at) {
9593
obj.created_at = (new Date()).toISOString();
9694
}
97-
collection.insertOne(obj, function (err) {
95+
collection.save(obj, function (err) {
9896
//id should be added for new docs
9997
storage.reload(function loaded() {
10098
callback(err, obj);
@@ -137,14 +135,8 @@ function init (env, ctx) {
137135

138136
storage.reload = function reload (callback) {
139137

140-
console.log('Reloading auth data');
141-
142138
storage.listRoles({sort: {name: 1}}, function listResults (err, results) {
143-
144-
console.log('Roles listed');
145-
146139
if (err) {
147-
console.log('Problem listing roles', err);
148140
return callback && callback(err);
149141
}
150142

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

161153
storage.listSubjects({sort: {name: 1}}, function listResults (err, results) {
162154
if (err) {
163-
console.log('Problem listing subjects', err);
164155
return callback && callback(err);
165156
}
166157

lib/client/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ client.init = function init (callback) {
6868
}).done(function success (serverSettings) {
6969
if (serverSettings.runtimeState !== 'loaded') {
7070
console.log('Server is still loading data');
71-
$('#loadingMessageText').html('Nightscout is still starting and should be available within about 15 seconds.');
71+
$('#loadingMessageText').html('Server is starting and still loading data, retrying load in 5 seconds');
7272
window.setTimeout(window.Nightscout.client.init, 5000);
7373
return;
7474
}

lib/server/activity.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ var find_options = require('./query');
44

55

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

99
function create (obj, fn) {
1010
obj.created_at = (new Date( )).toISOString( );
@@ -62,7 +62,7 @@ function storage (env, ctx) {
6262

6363
function remove (_id, fn) {
6464
var objId = new ObjectID(_id);
65-
return api( ).deleteOne({ '_id': objId }, fn);
65+
return api( ).remove({ '_id': objId }, fn);
6666
}
6767

6868
function api ( ) {

lib/server/bootevent.js

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

33
const _ = require('lodash');
4-
const UPDATE_THROTTLE = 15000;
4+
const UPDATE_THROTTLE = 5000;
55

66
function boot (env, language) {
77

88
function startBoot(ctx, next) {
99

10-
console.log('++++++++++++++++++++++++++++++');
11-
console.log('Nightscout Executing startBoot');
12-
console.log('++++++++++++++++++++++++++++++');
10+
console.log('Executing startBoot');
1311

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

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

43-
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'))) {
41+
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'))) {
4442
//Latest Node 14 LTS and Node 16 LTS are recommended and supported.
4543
//Require at least Node 14 without known security issues
4644
console.debug('Node LTS version ' + nodeVersion + ' is supported');
@@ -150,6 +148,16 @@ function boot (env, language) {
150148
}
151149

152150
try {
151+
if (_.startsWith(env.storageURI, 'openaps://')) {
152+
require('../storage/openaps-storage')(env, function ready (err, store) {
153+
if (err) {
154+
throw err;
155+
}
156+
ctx.store = store;
157+
console.log('OpenAPS Storage system ready');
158+
next();
159+
});
160+
} else {
153161
//TODO assume mongo for now, when there are more storage options add a lookup
154162
require('../storage/mongo-storage')(env, function ready(err, store) {
155163
// FIXME, error is always null, if there is an error, the index.js will throw an exception
@@ -162,6 +170,7 @@ function boot (env, language) {
162170
ctx.store = store;
163171
next();
164172
});
173+
}
165174
} catch (err) {
166175
console.info('ERROR CONNECTING TO MONGO', err);
167176
ctx.bootErrors = ctx.bootErrors || [ ];
@@ -286,7 +295,6 @@ function boot (env, language) {
286295

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

lib/server/devicestatus.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ function storage (collection, ctx) {
2424
obj.utcOffset = d.utcOffset();
2525

2626
api().insertOne(obj, function(err, results) {
27-
28-
if (err) {
27+
if (err !== null && err.message) {
2928
console.log('Error inserting the device status object', err.message);
3029
errorOccurred = true;
3130
fn(err.message, null);
3231
return;
3332
}
3433

35-
if (results) {
36-
if (!obj._id) obj._id = results.insertedId;
34+
if (!err) {
35+
36+
if (!obj._id) obj._id = results.insertedIds[0]._id;
3737
r.push(obj);
3838

3939
ctx.bus.emit('data-update', {
@@ -47,11 +47,6 @@ function storage (collection, ctx) {
4747
fn(null, r);
4848
ctx.bus.emit('data-received');
4949
}
50-
} else {
51-
console.log('Error inserting the device status object', err.message);
52-
errorOccurred = true;
53-
fn(err.message, null);
54-
return;
5550
}
5651
});
5752
};
@@ -105,19 +100,17 @@ function storage (collection, ctx) {
105100

106101
function removed (err, stat) {
107102

108-
console.log('removed', err, stat);
109-
110103
ctx.bus.emit('data-update', {
111104
type: 'devicestatus'
112105
, op: 'remove'
113-
, count: stat.deletedCount
106+
, count: stat.result.n
114107
, changes: opts.find._id
115108
});
116109

117110
fn(err, stat);
118111
}
119112

120-
return api().deleteMany(
113+
return api().remove(
121114
query_for(opts), removed);
122115
}
123116

0 commit comments

Comments
 (0)