Created
May 25, 2015 20:44
-
-
Save Bouhnosaure/0c19bb22b3793481b436 to your computer and use it in GitHub Desktop.
couchdb map reduce brightness
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
//brightness : http://couchdb.ovh:5984/database/_design/mesures/_view/brightness?group=true | |
//map : | |
function(doc, req) { | |
var date = dateFromISO8601(doc.datetime); | |
emit(date.getDate(), doc.brightness); | |
} | |
function dateFromISO8601(isostr) { | |
var parts = isostr.match(/\d+/g); | |
return new Date(parts[0], parts[1] - 1, parts[2], parts[3], parts[4], parts[5]); | |
} | |
//reduce : | |
avg_function = function(keys, values, rereduce) { | |
var avg, length; | |
if (!rereduce) { | |
length = values.length; | |
return [sum(values) / length, length]; | |
} else { | |
length = sum(values.map(function(v) { | |
return v[1]; | |
})); | |
avg = sum(values.map(function(v) { | |
return v[0] * (v[1] / length); | |
})); | |
return [avg, length]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment