I try to minimise the load of data that I get from the backend (w/ an axios get request), by filtering it down to a certain amount. It is data that is comes from different sensors. I only want from the last 24 hours and not from measured so far. That goes into tens of thousands of measured values (stored in the MongoDB database).
On the server side, I tried the following code:
sensorRoutes.get(
'/',
expressAsyncHandler( async(req, res) => {
const sensorData = await Sensor.find();
if(sensorData){
const length = sensorData.length;
const timeNow = Date.now();
const last24h = timeNow - 86400000;
for(let i = 0; i < length; i++){
let array = sensorData[i].values.map(item => [ item.value, item.timestamp ]);
let oneDayData = array.filter(item=> item[1] >= last24h);
sensorData[i].values = oneDayData;
}
res.send(sensorData);
}else{
res.status(404).send({message: 'Sensor not found'});
}
})
);
This slows down the whole process considerably. The above is on the front-end side now, before using it anywhere else in the App. Doing it on the front-end does not slow down the process as much as doing this at the backend.
My question to you, how can I do this on the backend, to minimise the computing load (and memory usage!) on the frond-end. Your help is much appreciated.