Skip to content

Instantly share code, notes, and snippets.

@Bouhnosaure
Created May 25, 2015 20:44
Show Gist options
  • Save Bouhnosaure/0c19bb22b3793481b436 to your computer and use it in GitHub Desktop.
Save Bouhnosaure/0c19bb22b3793481b436 to your computer and use it in GitHub Desktop.
couchdb map reduce brightness
//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