Created
May 4, 2010 17:32
-
-
Save minimal/389702 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// calculate n reports 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; | |
}; | |
var data = {serviceref: this.metadata.serviceref, count: 1}; | |
emit(getWeek(this.metadata.publishdate), 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment