I built a simple NodeJS server that emits client events. An event is a JSON object that looks like this:
{
"date": "2019-12-12T09:55:05.679Z",
"clientId": "Client01",
"ip": "10.1.1.1",
"event": "someEvent"
}
An event can be emitted anytime. I get many messages with different timestamps (date
).
Currently, I store all the data in memory.
How I can aggregate the client events by date?
let's say in 15-minute bulks, so I end up with a total number of client events for each time frame, for example:
const timestamps = ["2019-12-12T09:00:00.000Z", "2019-12-12T09:15:00.000Z", "2019-12-12T09:30:00.000Z"];
const totalNumEvents = [50, 27, 82];
const clientIds = ["Client01", "Client02", "Client03"];
Sorry if the question is too generic, I tried to look for direction by googling but couldn't find any solution without using a framework / DB like MongoDB.
So far, what I did is, creating an object for each clientId and push the event into it. (I have a callback for each event)
const onEventArrived = (client) => {
if (!clients[client.clientId]) {
console.log(`[New] Client Added - ${client.clientId}`);
clients[client.clientId] = [{ date, client}];
} else {
console.log(`[Exist] New Client Message for ${client.clientId}`);
clients[client.clientId].push({ date, client});
}
});
Back to question, I have all the events for each client, but how do I aggregate the random times into fixed 15 minute windows?