Skip to content

Instantly share code, notes, and snippets.

@dch
Created March 25, 2012 09:17
Show Gist options
  • Save dch/2192516 to your computer and use it in GitHub Desktop.
Save dch/2192516 to your computer and use it in GitHub Desktop.
couchdb map/reduce to compute browserling and testling usage data by month
{
map : function (doc) {
if (doc.type === 'usage' && doc.service && doc.time) {
var d = new Date(doc.time);
var y = d.getFullYear();
var m = d.getMonth() + 1;
var stamp = y + '/' + m;
var rec = {
stamp : stamp,
seconds : doc.seconds,
service : doc.service,
};
emit(doc.email, rec);
if (doc.group) emit(doc.group, rec);
}
},
reduce : function (keys, values) {
return keys.reduce(function (acc, key, ix) {
var v = values[ix];
var email = key[0];
acc[email] = acc[email] || {};
acc[email][v.service] = acc[email][v.service] || {};
acc[email][v.service][v.stamp] =
(acc[email][v.service][v.stamp] || 0) + v.seconds
;
return acc;
}, {});
}
}
$ curl -X GET 'http://localhost:5984/browserling/_design/usage/_view/by-month?startkey="[email protected]"&endkey="[email protected]"'
{"rows":[
{"key":null,"value":{"[email protected]":{"testling":{"2012/2":100,"2012/3":11}}}}
]}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment