Skip to content

Commit

Permalink
Merge pull request #1 from developmentseed/aggregate_stats
Browse files Browse the repository at this point in the history
Aggregate stats
  • Loading branch information
kamicut committed Jan 22, 2016
2 parents e64d4bc + 5d9f131 commit dbc1a44
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
69 changes: 64 additions & 5 deletions routes/hashtag.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,68 @@ module.exports = [
if (!req.params.id) {
res(Boom.badRequest('Not a valid hashtag.'));
}
Hashtag.where({hashtag: req.params.id})
.fetch({withRelated: 'changesets'})
.then(res)
var subquery = bookshelf.knex('changesets_hashtags')
.join('hashtags', 'hashtags.id', 'changesets_hashtags.hashtag_id')
.select('changeset_id')
.where('hashtags.hashtag', req.params.id);

bookshelf.knex('users')
.join('changesets', 'users.id', 'changesets.user_id')
.where('changesets.id', 'in', subquery)
.then(function (changesets) {

var times = {};
var users = {};
var roads = 0;
var buildings = 0;
var waterways = 0;
var pois = 0;
var currentRoads = 0;
var currentBuildings = 0;
var currentWaterways = 0;
var currentPois = 0;
var currentTotal = 0;

changesets.forEach(function (changeset) {
currentRoads = Number(changeset.road_count_add) + Number(changeset.road_count_mod);
currentBuildings = Number(changeset.building_count_add) + Number(changeset.building_count_mod);
currentWaterways = Number(changeset.waterway_count_add);
currentPois = Number(changeset.poi_count_add);
times[changeset.created_at] = {
roads: currentRoads,
buildings: currentBuildings,
waterways: currentWaterways,
pois: currentPois
};

roads += currentRoads;
buildings += currentBuildings;
waterways += currentWaterways;
pois += currentPois;

userId = changeset.user_id;
currentTotal = currentRoads + currentBuildings + currentWaterways + currentPois;
if (!users[userId]) {
users[userId] = {name: changeset.name, total: currentTotal};
}
else {
users[userId]['total'] += currentTotal;
};

});

return {
total: {
roads: roads,
buildings: buildings,
waterways: waterways,
pois: pois
},
users: users,
times: times
};
})
.then(res);
}
},
{
Expand All @@ -26,7 +85,7 @@ module.exports = [
var serialized = hashtags.toJSON();
return R.map(R.prop('hashtag'), serialized);
})
.then(res)
.then(res);
}
}
]
];
3 changes: 3 additions & 0 deletions routes/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,18 @@ module.exports = [
bookshelf.knex('changesets')
.select('created_at')
.where('user_id', req.params.id),
user.getNumCountries()
])
.then(function (results) {
var hashtags = results[0]
var latest = results[1];
var edit_times = R.map(R.prop('created_at'), results[2]);
var countryCount = results[3];

var serialized = user.toJSON();
serialized.latest = latest[0].id;
serialized.edit_times = edit_times;
serialized.country_count = countryCount;
serialized.hashtags = hashtags;
return serialized;
})
Expand Down

0 comments on commit dbc1a44

Please sign in to comment.