I can't find a way to do this with LoopBack, I though someone could maybe help, or point me to an alternative/recommended way to do this.
I have a 'Traffic' model that looks like this:
{
"fromTime": "string",
"toTime": "string",
"trafficTypeId": 0,
"totalPackets": 0,
"totalSize": "string",
"id": 0
}
Now, each model is made with certain interval, say from 16:00 to 16:05, and it keeps the total packets and size that was processed in this time.
I have a need to display a graph, that changes it's display from hourly/daily/weekly etc.
How would I make it so the summing won't happen in the client side, as I am afraid performance issues may arise (when trying to sum a month using 5 minute intervals)? Instead I would like to make a function that takes 2 arguments: startTime
and endTime
, and returns 1 object with the sum of packets and total size for this time?
I've looked at remote methods, but it doesn't seem to fit my needs. Am I wrong in understanding this?
Is there any other recommended way to try?
Edit: Is it possible to call something like this:
traffic.find({'where': {`fromTime': {gt: SOME_TIME}, 'toTime': {lt: SOME_OTHER_TIME } }); // (using AngularJS)
and have the returned data passed to a remote method that I will code, to sum up the values I want?
A remote method would work for this, why do you think it wouldn't? Instead of calling the built-in query methods, simply use the Node syntax to query your database and do the summing all on the backend. You don't need to first call for data and then send that data from the client back into a remote method, just package it all up inside a single remote method:
Something like this inside traffic.js:
module.exports = function(Traffic) {
Traffic.getGraphData = function (fromTime, toTime, cb) {
let filter = {
where: {
fromTime: {gt: fromTime},
toTime: {lt: toTime}
}
};
Traffic.find(filter, function(err, trafficResults) {
if(err) return cb(err); // error out and return err msg to client
let graphData = {
// properties to support graph display
};
// loop on trafficResults to do Summation here
// collect results inside graphData
// return clean graphData to client
cb(null, graphData);
});
}
Traffic.remoteMethod('getGraphData', {
accepts: [
{ arg: 'fromTime', type: 'number' },
{ arg: 'toTime', type: 'number' }
],
returns: { arg: 'graphData', type: 'object' }
});
};