var mongoose = require('mongoose'),
  async = require('async');

var Article = mongoose.model('Article');

module.exports = function (req, res, next) {
  async.parallel([
      aggregateRevisions,
      aggregateRevisedArticlesByUniqueUsers
    ],
    function (err, results) {
      if (err) {
        next(err);
      }

      res.render('home.pug', {
        data: {
          revisions: results[0],
          uniqueRevisions: results[1]
        }
      });
    });
};

var aggregateRevisions = function (cb) {
  // Aggregates the articles and gets a count of each article
  // This count gives us the number of times this article was edited
  // We also sort these articles based on count
  // As a result the first article in the array is the most edited one
  // The last article in the array is least edited
  Article.aggregate([{
    $group: {
      _id: "$title",
      count: {
        $sum: 1
      }
    }
  }, {
    $sort: {
      count: -1
    }
  }], function (err, data) {
    if (err) {
      cb(err);
    } else {
      cb(null, {
        most: data[0],
        least: data[data.length - 1]
      });
    }
  });
};

var aggregateRevisedArticlesByUniqueUsers = function (cb) {
  Article.aggregate([{
      $group: {
        _id: "$title",
        users: {
          $addToSet: "$user"
        }
      }
    }, {
      $unwind: "$users"
    },
    {
      $group: {
        _id: "$_id",
        count: {
          $sum: 1
        }
      }
    }, {
      $sort: {
        count: -1
      }
    }
  ], function (err, data) {
    if (err) {
      cb(err);
    } else {
      cb(null, {
        most: data[0],
        least: data[data.length - 1]
      });
    }
  });
};
@@ -98,16 +98,51 @@
 ticle');
+%0Avar User = mongoose.model('User');
 %0A%0Amodule
@@ -1167,91 +1167,386 @@
 %7D;%0A%0A
-var aggregateRevisedArticlesByUniqueUsers = function (cb) %7B%0A  Article.aggregate(%5B%7B%0A
+// It only returns registered users which excludes bots and admins%0Avar aggregateRevisedArticlesByUniqueUsers = function (cb) %7B%0A  User.distinct('name', function (err, botsAndAdmins) %7B%0A    if (err) %7B%0A      cb(err);%0A    %7D else %7B%0A      Article.aggregate(%5B%7B%0A          $match: %7B%0A            user: %7B%0A              $nin: botsAndAdmins%0A            %7D%0A          %7D%0A        %7D,%0A        %7B%0A    
     
@@ -1561,24 +1561,28 @@
 : %7B%0A        
+    
 _id: %22$title
@@ -1588,24 +1588,28 @@
 e%22,%0A        
+   
+ 
 users: %7B%0A   
@@ -1601,24 +1601,28 @@
    users: %7B%0A
+    
           $a
@@ -1642,34 +1642,42 @@
 er%22%0A        
+ 
-%7D%0A
+   %7D%0A    
       %7D%0A
     %7D, %7B%0A   
@@ -1656,32 +1656,36 @@
   %7D%0A          %7D%0A
+    
     %7D, %7B%0A      $
@@ -1683,16 +1683,20 @@
 %7B%0A      
+  
+  
 $unwind:
@@ -1705,16 +1705,20 @@
 $users%22%0A
+    
     %7D,%0A 
@@ -1716,26 +1716,34 @@
      %7D,%0A    
+ 
-%7B%0A
+   %7B%0A    
       $group
@@ -1750,24 +1750,28 @@
 : %7B%0A        
+    
 _id: %22$_id%22,
@@ -1775,24 +1775,28 @@
 d%22,%0A        
+   
+ 
 count: %7B%0A   
@@ -1794,32 +1794,36 @@
 nt: %7B%0A          
+    
 $sum: 1%0A        
@@ -1826,18 +1826,26 @@
     
+ 
-%7D%0A
+   %7D%0A    
       %7D%0A
     
@@ -1836,24 +1836,28 @@
           %7D%0A
+    
     %7D, %7B%0A   
@@ -1855,24 +1855,28 @@
  %7D, %7B%0A      
+  
+  
 $sort: %7B%0A   
@@ -1868,24 +1868,28 @@
    $sort: %7B%0A
+    
         coun
@@ -1900,24 +1900,36 @@
 1%0A      
-%7D%0A
+    %7D%0A   
     
+ 
 %7D%0A
+    
   %5D, fun
@@ -1944,32 +1944,36 @@
 rr, data) %7B%0A    
+  
+  
 if (err) %7B%0A     
@@ -1965,32 +1965,36 @@
 f (err) %7B%0A      
+    
 cb(err);%0A    %7D e
@@ -1982,32 +1982,36 @@
    cb(err);%0A    
+ 
+   
 %7D else %7B%0A      c
@@ -1995,32 +1995,36 @@
        %7D else %7B%0A
+    
       cb(null, %7B
@@ -2024,32 +2024,36 @@
 null, %7B%0A        
+    
 most: data%5B0%5D,%0A 
@@ -2051,32 +2051,36 @@
 ata%5B0%5D,%0A        
+  
+  
 least: data%5Bdata
@@ -2084,32 +2084,56 @@
 ata.length - 1%5D%0A
+          %7D);%0A        %7D%0A
       %7D);%0A    %7D%0A

Fix query for home page