diff --git a/helyos_server/src/event_handlers/rabbitmq_event_handlers/database_request_handler.js b/helyos_server/src/event_handlers/rabbitmq_event_handlers/database_request_handler.js index 99084c1d..3e349e54 100644 --- a/helyos_server/src/event_handlers/rabbitmq_event_handlers/database_request_handler.js +++ b/helyos_server/src/event_handlers/rabbitmq_event_handlers/database_request_handler.js @@ -2,10 +2,10 @@ const databaseServices = require('../../services/database/database_services.js'); const {inMemDB } = require('../../services/in_mem_database/mem_database_service.js'); const rabbitMQServices = require('../../services/message_broker/rabbitMQ_services.js'); +const DB_BUFFER_TIME = parseInt(process.env.DB_BUFFER_TIME || 1000); async function queryDataBase(uuid, objMsg, msgProps) { - console.log(objMsg, msgProps); inMemDB.agents_stats[uuid]['updtPerSecond'].countMessage(); let replyTo = msgProps.replyTo? msgProps.replyTo : uuid; @@ -53,7 +53,9 @@ async function queryDataBase(uuid, objMsg, msgProps) { break; case 'allMapObjects': - response = await databaseServices.map_objects.select(objMsg.body['conditions'] || {}); + let conditions = objMsg.body['conditions'] || {deleted_at: null}; + conditions = {deleted_at: null, ...conditions}; + response = await databaseServices.map_objects.select(conditions); break; } @@ -65,9 +67,20 @@ async function queryDataBase(uuid, objMsg, msgProps) { console.log(r); const newObjects = await databaseServices.map_objects.list_in(newIds); newObjects.forEach( obj => { inMemDB.update('map_objects', 'id', obj, new Date(), 'realtime'); }); + return newIds; }); break; + + case 'updateMapObjects': + const patches = objMsg.body['data']; + patches.forEach( patch => { + inMemDB.update('map_objects', 'id', patch, new Date()); + }); + response = "data saved"; + break; + + case 'deleteMapObjects': response = await databaseServices.map_objects.delete(objMsg.body['condition']) .then( (r) => { diff --git a/helyos_server/src/services/database/postg_access_layer.js b/helyos_server/src/services/database/postg_access_layer.js index 5497236e..b29b8090 100644 --- a/helyos_server/src/services/database/postg_access_layer.js +++ b/helyos_server/src/services/database/postg_access_layer.js @@ -17,9 +17,19 @@ const parseConditions = (tableName, conditions) => { return this.list(); } - let fromTableStatements = []; + + const notNullConditions = {}; Object.keys(conditions).forEach((key, idx) => { + if (conditions[key] === null) { + null_conditions.push(` AND ${key} IS NULL `); + } else { + notNullConditions[key]=conditions[key]; + } + }); + + + Object.keys(notNullConditions).forEach((key, idx) => { if (key.includes('.')) { const [table, field] = key.split('.'); @@ -38,9 +48,7 @@ const parseConditions = (tableName, conditions) => { } else { in_conditions.push(`AND ${key.slice(0, -4)} IN ('${conditions[key].join("','")}') `); } - } else if (conditions[key] === null) { - null_conditions.push(`AND ${key} IS NULL `); - } else { + } else { names.push(key); values.push(conditions[key]); masks.push('$' + (idx + 1)); @@ -487,7 +495,7 @@ class AgentDataLayer extends DatabaseLayer { aggregatedFollowerConnections(agent) { return this.client.query(` - SELECT A.id, A.uuid, A.geometry, B.connection_geometry FROM public.agents as A + SELECT A.id, A.uuid, A.geometry, A.yard_id, B.connection_geometry FROM public.agents as A JOIN public.agents_interconnections as B ON A.id = B.follower_id WHERE A.id IN (SELECT follower_id FROM public.agents_interconnections WHERE leader_id = $1) @@ -497,7 +505,7 @@ class AgentDataLayer extends DatabaseLayer { aggregatedLeaderConnections(agent) { return this.client.query(` - SELECT A.id, A.uuid, A.geometry, B.connection_geometry FROM public.agents as A + SELECT A.id, A.uuid, A.geometry, A.yard_id, B.connection_geometry FROM public.agents as A JOIN public.agents_interconnections as B ON A.id = B.leader_id WHERE A.id IN (SELECT leader_id FROM public.agents_interconnections WHERE follower_id = $1)