title: Group JSON data and filter them in ruby
body: <p>I have a JSON file which contains logs on our plateform
  Given this input data, we want to create a set of sessions of the incoming data. A sessions is defined as a group of events from a single visitor with no more than 10 minutes between each consecutive event. A visitor can have multiple sessions.</p>
  <pre><code>
      {
          &quot;events&quot;: [
               {
                   &quot;url&quot;: &quot;/pages/a-big-river&quot;,
                   &quot;userId&quot;: &quot;d1177368-2310-11e8-9e2a-9b860a0d9039&quot;,
                   &quot;timestamp&quot;: 1512754583000
               },
               {
                   &quot;url&quot;: &quot;/pages/a-small-dog&quot;,
                   &quot;userId&quot;: &quot;d1177368-2310-11e8-9e2a-9b860a0d9039&quot;,
                   &quot;timestamp&quot;: 1512754631000
               },
              {
                  &quot;url&quot;: &quot;/pages/a-big-talk&quot;,
                  &quot;userId&quot;: &quot;f877b96c-9969-4abc-bbe2-54b17d030f8b&quot;,
                  &quot;timestamp&quot;: 1512709065294
              },
              {
                  &quot;url&quot;: &quot;/pages/a-sad-story&quot;,
                  &quot;userId&quot;: &quot;f877b96c-9969-4abc-bbe2-54b17d030f8b&quot;,
                  &quot;timestamp&quot;: 1512711000000
              },
              {
                  &quot;url&quot;: &quot;/pages/a-big-river&quot;,
                  &quot;userId&quot;: &quot;d1177368-2310-11e8-9e2a-9b860a0d9039&quot;,
                  &quot;timestamp&quot;: 1512754436000
              },
              {
                  &quot;url&quot;: &quot;/pages/a-sad-story&quot;,
                  &quot;userId&quot;: &quot;f877b96c-9969-4abc-bbe2-54b17d030f8b&quot;,
                  &quot;timestamp&quot;: 1512709024000
              }
          ]
      }
  </code></pre>
  <p>Timestamps are in milliseconds.
  Events may not be given in chronological order.
  The visitors in sessionsByUser can be in any order.
  For each visitor, sessions to be in chronological order.
  For each session, the URLs should be sorted in chronological order
  For a session with only one event the duration should be zero
  Each event in a session (except the first event) must have occurred within 10 minutes of the preceding event in the session. This means that there can be more than 10 minutes between the first and the last event in the session.
  So given the example input data above, we would expect output which looks like:</p>
  <pre><code>
      {
          &quot;sessionsByUser&quot;: {
              &quot;f877b96c-9969-4abc-bbe2-54b17d030f8b&quot;: [
                  {
                      &quot;duration&quot;: 41294,
                      &quot;pages&quot;: [
                          &quot;/pages/a-sad-story&quot;,
                          &quot;/pages/a-big-talk&quot;
                      ],
                      &quot;startTime&quot;: 1512709024000
                  },
                  {
                      &quot;duratio`enter code here`n&quot;: 0,
                      &quot;pages&quot;: [
                          &quot;/pages/a-sad-story&quot;
                      ],
                      &quot;startTime&quot;: 1512711000000
                  }
              ],
              &quot;d1177368-2310-11e8-9e2a-9b860a0d9039&quot;: [
                  {
                      &quot;duration&quot;: 195000,
                      &quot;pages&quot;: [
                          &quot;/pages/a-big-river&quot;,
                          &quot;/pages/a-big-river&quot;,
                          &quot;/pages/a-small-dog&quot;
                      ],
                      &quot;startTime&quot;: 1512754436000
                  }
              ]
          }
      }
  
  </code></pre>