DB aggregation for downloads stats

I moved the aggregation into the database.
Still need to test this, so please don't merge, just wanted to get an early feedback.