Skip to content

Instantly share code, notes, and snippets.

@minimal
Created May 4, 2010 17:58
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 minimal/389731 to your computer and use it in GitHub Desktop.
Save minimal/389731 to your computer and use it in GitHub Desktop.
/* Calc number of reports per service per week */
m = function() {
var getWeek = function(d) {
dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 1;
var newYear = new Date(d.getFullYear(),0,1);
var day = newYear.getDay() - dowOffset;
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((d.getTime() - newYear.getTime() - (d.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
var weeknum;
if (day < 4) {
weeknum = Math.floor((daynum+day-1)/7) + 1;
if(weeknum > 52) {
nYear = new Date(d.getFullYear() + 1,0,1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
weeknum = nday < 4 ? 1 : 53;
}
} else {
weeknum = Math.floor((daynum+day-1)/7);
}
return weeknum;
};
emit(getWeek(this.metadata.publishdate) + this.metadata.serviceref , 1);
};
r = function(previous, current){
var sum = 0;
for (index in current) {
sum += current[index];
}
return sum;
};
use publications
res = db.reports.files.mapReduce(m, r, {query: {'metadata.publishdate': {$gte: new Date(2010, 00, 01), $lte:new Date(2010, 05, 01)}}})
db[res.result].find()
{ "_id" : "10usa", "value" : 1 }
{ "_id" : "12asia", "value" : 9 }
{ "_id" : "12cg", "value" : 8 }
{ "_id" : "12fledg", "value" : 4 }
{ "_id" : "12gl", "value" : 15 }
{ "_id" : "12gp", "value" : 8 }
{ "_id" : "12jp", "value" : 14 }
{ "_id" : "12sfkcg", "value" : 6 }
{ "_id" : "12usa", "value" : 30 }
{ "_id" : "12wmcg", "value" : 7 }
{ "_id" : "13cg", "value" : 2 }
{ "_id" : "13gp", "value" : 2 }
{ "_id" : "13mbcg", "value" : 2 }
{ "_id" : "13wmcg", "value" : 24 }
{ "_id" : "14ccla", "value" : 2 }
{ "_id" : "14cfbcg", "value" : 6 }
{ "_id" : "14cig", "value" : 4 }
{ "_id" : "14mbcg", "value" : 4 }
{ "_id" : "14sfkcg", "value" : 2 }
{ "_id" : "14wmcg", "value" : 9 }
has more
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment