title: data aggregation by time window with JS
body: <p>I built a simple NodeJS server that emits client events. An event is a JSON object that looks like this:</p>
  <pre class="lang-js prettyprint-override"><code>{
    &quot;date&quot;: &quot;2019-12-12T09:55:05.679Z&quot;,
    &quot;clientId&quot;: &quot;Client01&quot;,
    &quot;ip&quot;: &quot;10.1.1.1&quot;,
    &quot;event&quot;: &quot;someEvent&quot;
  }
  </code></pre>
  <p>An event can be emitted anytime. I get many messages with different timestamps (<code>date</code>).</p>
  <p>Currently, I store all the data in memory.</p>
  <p><strong>How I can aggregate the client events by date?</strong></p>
  <p>let's say in 15-minute bulks, so I end up with a total number of client events for each time frame, for example:</p>
  <pre class="lang-js prettyprint-override"><code>const timestamps = [&quot;2019-12-12T09:00:00.000Z&quot;, &quot;2019-12-12T09:15:00.000Z&quot;, &quot;2019-12-12T09:30:00.000Z&quot;];
  const totalNumEvents = [50, 27, 82];
  const clientIds = [&quot;Client01&quot;, &quot;Client02&quot;, &quot;Client03&quot;];
  </code></pre>
  <p>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.</p>
  <p>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)</p>
  <pre><code>const onEventArrived = (client) =&gt; {
        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});
        }
  });
  </code></pre>
  <p>Back to question, I have all the events for each client, but how do I aggregate the random times into fixed 15 minute windows?</p>