Skip to content

Instantly share code, notes, and snippets.

@nelstrom
Created August 31, 2011 13:43
Show Gist options
  • Save nelstrom/1183561 to your computer and use it in GitHub Desktop.
Save nelstrom/1183561 to your computer and use it in GitHub Desktop.
A map reduce function that counts the requests for each path on each day.
db.receipts.ensureIndex({issued_at: 1})
map = ->
day = @issued_at;
data =
count: 1
paths: {}
data.paths[@path] = {count:1}
emit(day, data);
reduce = (key, values) ->
result =
count: 0,
paths: {}
values.forEach (value) ->
result.count++
for path of value["paths"]
result.paths[path] ||= {count: 0}
result.paths[path].count++
result
stats = db.receipts.mapReduce(map, reduce,
query:
status: '200'
sort:
issued_at: 1
out: 'daily_hits_by_path'
limit: 3000
)
var map, reduce, stats;
db.receipts.ensureIndex({
issued_at: 1
});
map = function() {
var data, day;
day = this.issued_at;
data = {
count: 1,
paths: {}
};
data.paths[this.path] = {
count: 1
};
return emit(day, data);
};
reduce = function(key, values) {
var result;
result = {
count: 0,
paths: {}
};
values.forEach(function(value) {
var path, _base, _results;
result.count++;
_results = [];
for (path in value["paths"]) {
(_base = result.paths)[path] || (_base[path] = {
count: 0
});
_results.push(result.paths[path].count++);
}
return _results;
});
return result;
};
stats = db.receipts.mapReduce(map, reduce, {
query: {
status: '200'
},
sort: {
issued_at: 1
},
out: 'daily_hits_by_path',
limit: 3000
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment