A group of lights by

Does Firebase support grouped counting?
I would like to get the count for a specific key, grouped by value.

An example of my data structure:

"playbackPosition" : {
    "-JZ2c7v-imQXaEx2c4Cs" : {
        "data" : "0"
    },
    "-JZ2cDta73UGAgwIOejL" : {
        "data" : "25"
    },
    "-JZ2cJutDZu-7quLpLI2" : {
        "data" : "50"
    },
    "-JZ2cO-Me0nGK5XLTwd-" : {
        "data" : "75"
    },
    "-JZ2cSh-XnstYxbE_Zad" : {
        "data" : "100"
    },
    "-JZ2cWxya0-kmTPCze4u" : {
        "data" : "0"
    },
    "-JZ2c_wX-ODv43TKXkNG" : {
        "data" : "25"
    }
}

      

Required results based on the key data

:

0 => 2  
25 => 2  
50 => 1  
75 => 1  
100 => 1

      

And, of course, I must take into account that he will have thousands of children, not just 7 ...

Thanks in advance!

EDIT
A deeper explanation of the application and the problem we want to solve.

We have video scripts that run on different websites, each video session (user session) sends events and data, you can see an example here, check the Network tab - https://secure.bwebi.co/videostir /regular.html

Our goal is to collect this data and create a real-time analytics dashboard with multiple charts and graphs.

You can see our current data structure here

An example for the graphs we need:
Course Completion General - A histogram showing the total number of views per clip duration up to the specified periods.
Filters - date (start / end), unique / all, all urls / specific urls (inline), all config / specific config / ignore silent.
X axis - groups 0-25, 25-50.50-75.75-99, 100
Y axis - number of views

Views per Day (Terminated)
General - Multi-line graph showing the number of views per day over periods of duration.
Filters - date (start / end), unique / all, all urls / specific urls (inline), all config / specific config / ignore silent.
X-axis - time in days
Y axis - Number of views - Lines for:
Total daily views
Daily views with 100% duration
Daily views with 75-99% duration
Daily views with 50-75% duration
Daily views with 25-50% duration
Daily performances with duration 0-25%

Hopefully this becomes clearer now!

+3


source to share


1 answer


The by group is a SQL function. The reason SQL cannot execute data in real time is because this method does not scale. Mongo provides similar functionality, but once again, it doesn't scale . You can see the pattern here why Firebase doesn't provide such a request function.

It would be very helpful if you could provide some context for what you are actually trying to accomplish here, what the application rules are and which approaches you ruled out, not just your intended group decision.There are probably other, possibly better alternatives. See Problem XY .

Here are a couple of general alternatives obtained by making radical assumptions about your use case.

Saving totals

This is the most scalable solution. Save the data as follows:

/playbacks/$id/<playback data>
/group_totals/$group/<count>

      

When recording for playback, also update the counter for the corresponding group:



var fb = new Firebase(URL);
function addPlayback(rec) {
   var ref = fb.child('playbacks').push(rec, function(err) {
      if( err )  throw err;
      incrementCount(rec.data);
   });
}

function incrementCount(count) {
   fb.child('group_totals/' + count).transaction(function(currentVal) {
     return (currentVal||0)+1;
   });
}

      

Now when you want to get the total for a group, you can simply look at the value in group_totals / $ group. Likewise, you can store IDs for posts belonging to each group and use this index to grab only the posts for that group.

Use priorities for retrieval and grouping

A simpler approach is to give each record a priority based on the group / data value.

var fb = new Firebase(URL);
function addPlayback(rec) {
   rec['.priority'] = rec.data;
   var ref = fb.child('playbacks').push(rec, function(err) {
      if( err )  throw err;
   });
}

      

Now, to grab the recordset for a given group:

var fb = new Firebase(URL);
function getGroup(groupValue, callback) {
   fb.child('playbackPosition').startAt(groupValue).endAt(groupValue).once('value', callback);
}

function logGroupCount(groupValue, callback) {
   getGroup(groupValue, function(snap) {
       console.log('there are ' + snap.numChildren() + ' items in group ' +groupValue);
   });
}

      

+9


source







All Articles