Browse files

aggregate visits and forks by user

  • Loading branch information...
1 parent e23f99d commit 2f4009d58ccee9f4f67443b53240e686ee82f4e1 @enjalot committed Jan 18, 2013
Showing with 19 additions and 3 deletions.
  1. +14 −2 reports/aggregators/inletsByUser.js
  2. +5 −1 server.js
View
16 reports/aggregators/inletsByUser.js
@@ -23,20 +23,30 @@ db.open(function(err, db) {
if(this.user) {
emit(this.user.id, {
type: "user",
- count: 1
+ count: 1,
+ visits: this.visits || 0,
+ nforks: this.nforks || 0
})
}
}
function reduceUsers(key, values) {
var result = {
- count: 0
+ count: 0,
+ visits: 0,
+ nforks: 0
};
values.forEach(function(value) {
if(value.count) {
result.count += value.count;
} else {
result.count += 1;
}
+ if(value.nforks) {
+ result.nforks += value.nforks;
+ }
+ if(value.visits) {
+ result.visits += value.visits;
+ }
});
return result;
}
@@ -68,6 +78,8 @@ db.open(function(err, db) {
$users.findOne({id: mr_user._id}, function(error, user) {
if(error || !user ) return finish();
user.inlets = mr_user.value.count || 1;
+ user.visits = mr_user.value.visits || 1;
+ user.nforks = mr_user.value.nforks || 0;
$users.update({id: user.id}, user, {safe: true}, function(error) {
if(error) console.log(error)
finish()
View
6 server.js
@@ -81,6 +81,8 @@ app.get('/inlet', inlet)
app.get('/inlet/:gistid', inlet)
app.get('/tributary', inlet)
app.get('/tributary/:gistid', inlet)
+//backwards compatible endpoint...
+app.get('/tributary/:gistid/:filename', inlet)
function inlet(req,res,next) {
var gistid = req.params['gistid'];
var user = req.session.user;
@@ -484,7 +486,9 @@ function api_users(req,res,next) {
id: 1,
avatar_url: 1,
html_url: 1,
- inlets: 1
+ inlets: 1,
+ visits: 1,
+ nforks: 1
}
var opts = {
limit: limit

0 comments on commit 2f4009d

Please sign in to comment.