Skip to content

Commit

Permalink
adjust websockets usage for mongo update
Browse files Browse the repository at this point in the history
  • Loading branch information
bewest committed Nov 27, 2024
1 parent 5a36647 commit e60e093
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
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
25 changes: 16 additions & 9 deletions lib/server/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ function init (env, ctx, server) {
// field_2: another_value
// }
// }
// TODO: tests, data models
socket.on('dbAdd', function dbAdd (data, callback) {
console.log(LOG_WS + 'dbAdd client ID: ', socket.client.id, ' data: ', data);
var collection = supportedCollections[data.collection];
Expand Down Expand Up @@ -405,20 +406,22 @@ function init (env, ctx, server) {
}
// if not found create new record
console.log(LOG_DEDUP + 'Adding new record');
ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) {
ctx.store.collection(collection).insertOne(data.data, function insertResult (err, ops) {
if (err != null && err.message) {
console.log('treatments data insertion error: ', err.message);
return;
}

var doc = data.data;
doc._id = ops.insertedId;
ctx.bus.emit('data-update', {
type: data.collection
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime(doc.ops)
, changes: ctx.ddata.processRawDataForRuntime([doc])
});

if (callback) {
callback(doc.ops);
callback([doc]);
}
ctx.bus.emit('data-received');
});
Expand Down Expand Up @@ -453,38 +456,42 @@ function init (env, ctx, server) {

});

ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) {
ctx.store.collection(collection).insertOne(data.data, function insertResult (err, ops) {
if (err != null && err.message) {
console.log('devicestatus insertion error: ', err.message);
return;
}

var doc = data.data;
doc._id = ops.insertedId;
ctx.bus.emit('data-update', {
type: 'devicestatus'
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime(doc.ops)
, changes: ctx.ddata.processRawDataForRuntime([doc])
});

if (callback) {
callback(doc.ops);
callback([doc]);
}
ctx.bus.emit('data-received');
});
} else {
ctx.store.collection(collection).insert(data.data, function insertResult (err, doc) {
ctx.store.collection(collection).insertOne(data.data, function insertResult (err, ops) {
if (err != null && err.message) {
console.log(data.collection + ' insertion error: ', err.message);
return;
}

var doc = data.data;
doc._id = ops.insertedId;
ctx.bus.emit('data-update', {
type: data.collection
, op: 'update'
, changes: ctx.ddata.processRawDataForRuntime(doc.ops)
, changes: ctx.ddata.processRawDataForRuntime([doc])
});

if (callback) {
callback(doc.ops);
callback([doc]);
}
ctx.bus.emit('data-received');
});
Expand Down

0 comments on commit e60e093

Please sign in to comment.