Skip to content

Instantly share code, notes, and snippets.

@kennethkalmer
Created April 9, 2009 13:44
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 kennethkalmer/92476 to your computer and use it in GitHub Desktop.
Save kennethkalmer/92476 to your computer and use it in GitHub Desktop.
Experiments in couchdb
function(doc) {
if(doc.user && doc.service && doc.time && doc.action) {
if(doc.action == "LOGOUT" && parseInt(doc.retr) > 0) {
var date = new Date( doc.time );
var year = date.getFullYear();
var month = date.getMonth();
if( month.toString().length == 1 ) month = '0' + month;
var day = date.getDate();
if( day.toString().length == 1 ) day = '0' + day;
var key = year + '-' + month + '-' + day;
emit(key, {user: doc.user, size: parseInt(doc.retr)});
}
}
}
function( keys, values, rereduce ) {
var output = {};
if( !rereduce ) {
for each( var doc in values ) {
if( output[doc.user] ){
output[doc.user]['count'] += 1;
output[doc.user]['size'] += doc.size;
}
else
{
output[doc.user] = { 'count': 1, 'size': doc.size };
}
}
}
else {
for( var i in values ) {
for( var j in values[i]) {
output[j] = values[i][j];
}
}
}
return output;
}
function(doc) {
if(doc.user && doc.service && doc.time && doc.action) {
if(doc.action == "LOGOUT" && parseInt(doc.retr) > 0) {
var date = new Date( doc.time );
var year = date.getFullYear();
var month = date.getMonth();
if( month.toString().length == 1 ) month = '0' + month;
var day = date.getDate();
if( day.toString().length == 1 ) day = '0' + day;
var key = year + '-' + month + '-' + day;
emit(key, 1);
}
}
}
function( keys, values ) {
return sum(values);
}
{
"_id": "0000f68e73f3521f3ee8b3b51e0101d7",
"_rev": "1-3732031452",
"user": "user@example.com",
"host": "pop-5",
"time": "2009/03/13 05:47:08 +0000",
"action": "LOGOUT",
"service": "pop3d",
"ip": "[10.0.0.1]",
"top": "0",
"retr": "0"
}
function(doc) {
var months = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"];
if(doc.user && doc.service && doc.time && doc.action) {
if(doc.action == "LOGOUT" && parseInt(doc.retr) > 0) {
var date = new Date( doc.time );
var year = date.getFullYear();
var month = date.getMonth();
var month_s = month.toString();
if( month_s.length == 1 ) month_s = '0' + month_s;
var key = year + '-' + month_s + ' ' + months[ month - 1 ];
emit(key, {user: doc.user, size: parseInt(doc.retr) });
}
}
}
function( keys, values, rereduce ) {
var output = {};
if( !rereduce ) {
for each( var doc in values ) {
if( output[doc.user] ){
output[doc.user]['count'] += 1;
output[doc.user]['size'] += doc.size;
}
else
{
output[doc.user] = { 'count': 1, 'size': doc.size };
}
}
}
else {
for( var i in values ) {
for( var j in values[i]) {
output[j] = values[i][j];
}
}
}
return output;
}
function(doc) {
var months = ["January", "February", "March", "April",
"May", "June", "July", "August",
"September", "October", "November", "December"];
if(doc.user && doc.service && doc.time && doc.action) {
if(doc.action == "LOGOUT" && parseInt(doc.retr) > 0) {
var date = new Date( doc.time );
var year = date.getFullYear();
var month = date.getMonth();
var month_s = month.toString();
if( month_s.length == 1 ) month_s = '0' + month_s;
var key = year + '-' + month_s + ' ' + months[ month - 1 ];
emit(key, 1);
}
}
}
function( keys, values ) {
return sum(values);
}
{
"_id": "_design/usage",
"_rev": "11-4090663094",
"language": "javascript",
"views": {
"daily": {
"map": "function(doc) {\n if(doc.user && doc.service && doc.time && doc.action) {\n if(doc.action == \"LOGOUT\" && parseInt(doc.retr) > 0) {\n var date = new Date( doc.time );\n var year = date.getFullYear();\n var month = date.getMonth();\n if( month.toString().length == 1 ) month = '0' + month;\n var day = date.getDate();\n if( day.toString().length == 1 ) day = '0' + day;\n var key = year + '-' + month + '-' + day;\n emit(key, {user: doc.user, size: parseInt(doc.retr)});\n }\n }\n}",
"reduce": "function( keys, values, rereduce ) {\n\n var output = {};\n if( !rereduce ) {\n for each( var doc in values ) {\n if( output[doc.user] ){\n output[doc.user]['count'] += 1;\n output[doc.user]['size'] += doc.size;\n }\n else\n {\n output[doc.user] = { 'count': 1, 'size': doc.size };\n }\n }\n }\n else {\n for( var i in values ) {\n for( var j in values[i]) {\n output[j] = values[i][j];\n }\n }\n }\n return output;\n}"
},
"days": {
"map": "function(doc) {\n if(doc.user && doc.service && doc.time && doc.action) {\n if(doc.action == \"LOGOUT\" && parseInt(doc.retr) > 0) {\n var date = new Date( doc.time );\n var year = date.getFullYear();\n var month = date.getMonth();\n if( month.toString().length == 1 ) month = '0' + month;\n var day = date.getDate();\n if( day.toString().length == 1 ) day = '0' + day;\n var key = year + '-' + month + '-' + day;\n emit(key, 1);\n }\n }\n}",
"reduce": "function( keys, values ) {\n return sum(values);\n}"
},
"months": {
"map": "function(doc) {\n var months = [\"January\", \"February\", \"March\", \"April\", \n \"May\", \"June\", \"July\", \"August\",\n \"September\", \"October\", \"November\", \"December\"];\n\n if(doc.user && doc.service && doc.time && doc.action) {\n if(doc.action == \"LOGOUT\" && parseInt(doc.retr) > 0) {\n var date = new Date( doc.time );\n var year = date.getFullYear();\n var month = date.getMonth();\n var month_s = month.toString();\n if( month_s.length == 1 ) month_s = '0' + month_s;\n var key = year + '-' + month_s + ' ' + months[ month - 1 ];\n emit(key, 1);\n }\n }\n}",
"reduce": "function( keys, values ) {\n return sum(values);\n}"
},
"monthly": {
"map": "function(doc) {\n var months = [\"January\", \"February\", \"March\", \"April\", \n \"May\", \"June\", \"July\", \"August\",\n \"September\", \"October\", \"November\", \"December\"];\n\n if(doc.user && doc.service && doc.time && doc.action) {\n if(doc.action == \"LOGOUT\" && parseInt(doc.retr) > 0) {\n var date = new Date( doc.time );\n var year = date.getFullYear();\n var month = date.getMonth();\n var month_s = month.toString();\n if( month_s.length == 1 ) month_s = '0' + month_s;\n var key = year + '-' + month_s + ' ' + months[ month - 1 ];\n emit(key, {user: doc.user, size: parseInt(doc.retr) });\n }\n }\n}",
"reduce": "function( keys, values, rereduce ) {\n\n var output = {};\n if( !rereduce ) {\n for each( var doc in values ) {\n if( output[doc.user] ){\n output[doc.user]['count'] += 1;\n output[doc.user]['size'] += doc.size;\n }\n else\n {\n output[doc.user] = { 'count': 1, 'size': doc.size };\n }\n }\n }\n else {\n for( var i in values ) {\n for( var j in values[i]) {\n output[j] = values[i][j];\n }\n }\n }\n return output;\n}"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment