Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save guilhermedecampo/542e30f2c77261dc5378 to your computer and use it in GitHub Desktop.
Save guilhermedecampo/542e30f2c77261dc5378 to your computer and use it in GitHub Desktop.
function agg_session_count(){
db.logs.mapReduce(
function(){
var local_zone = new Date().getTimezoneOffset()
var br_zone = 3*60;
var date_br = parseInt(this.when)+(br_zone-local_zone)*60;
var date = new Date(date_br*1000);
var date_format = date.getFullYear()+"-"+date.getMonth()+"-"+date.getDate();
emit(date_format,{event_ids: [this.event_id], count: 1});
},
function(key,values){
var res = {event_ids: [], count: 0}
var uniques = {};
values.forEach(function(value) {
value.event_ids.forEach(function(event_id) {
uniques[event_id] = 1
});
})
res.event_ids = Object.keys(uniques);
res.count = res.event_ids.length;
return res;
},
{
query: {event: 'created', type: 'stop', event_ids: {$ne: "unspecified"}},
out: 'agg_session_count'
},
function(err,res){
if(err){
console.log(err);
} else {
db.agg_session_count.count(function(err,val){
changes = val;
console.log("OK: "+val+" objects");
});
}
}
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment