Skip to content

Commit

Permalink
Support uploading device statuses in batches (nightscout#6147)
Browse files Browse the repository at this point in the history
* Support uploading device statuses in batches
* Correctly report batch insertion results
  • Loading branch information
sulkaharo authored and tanja3981 committed Nov 15, 2020
1 parent 14f60ed commit 2463c02
Showing 1 changed file with 45 additions and 25 deletions.
70 changes: 45 additions & 25 deletions lib/server/devicestatus.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,34 +5,51 @@ var find_options = require('./query');

function storage (collection, ctx) {

function create (obj, fn) {

// Normalize all dates to UTC
const d = moment(obj.created_at).isValid() ? moment.parseZone(obj.created_at) : moment();
obj.created_at = d.toISOString();
obj.utcOffset = d.utcOffset();

api().insertOne(obj, function(err, results) {
if (err !== null && err.message) {
console.log('Error inserting the device status object', err.message);
fn(err.message, null);
return;
}
function create (statuses, fn) {

if (!err) {
if (!Array.isArray(statuses)) { statuses = [statuses]; }

if (!obj._id) obj._id = results.insertedIds[0]._id;
const r = [];
let errorOccurred = false;

ctx.bus.emit('data-update', {
type: 'devicestatus'
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime([obj])
});
}
for (let i = 0; i < statuses.length; i++) {

fn(null, results.ops);
ctx.bus.emit('data-received');
});
const obj = statuses[i];

if (errorOccurred) return;

// Normalize all dates to UTC
const d = moment(obj.created_at).isValid() ? moment.parseZone(obj.created_at) : moment();
obj.created_at = d.toISOString();
obj.utcOffset = d.utcOffset();

api().insertOne(obj, function(err, results) {
if (err !== null && err.message) {
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;
r.push(obj);

ctx.bus.emit('data-update', {
type: 'devicestatus'
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime([obj])
});

// Last object! Return results
if (i == statuses.length - 1) {
fn(null, r);
ctx.bus.emit('data-received');
}
}
});
};
}

function last (fn) {
Expand Down Expand Up @@ -109,7 +126,10 @@ function storage (collection, ctx) {
api.aggregate = require('./aggregate')({}, api);
api.indexedFields = [
'created_at'





, 'NSCLIENT_ID'
];
return api;
Expand Down

0 comments on commit 2463c02

Please sign in to comment.