private UserStatistic PopulateData(UserStatisticData statData)
 {
     UserStatistic stat = AutoMapper.Mapper.Map<UserStatisticData, UserStatistic>(statData);
     return stat;
 }
        private void RecalculateAll()
        {
            var operations = new BsonDocument[]
            {
                //new BsonDocument("$match", new BsonDocument("createdDate", new BsonDocument ( "$gte", DateTime.UtcNow.AddDays(-30) ))),
                new BsonDocument("$group", new BsonDocument { { "_id", new BsonDocument("user", "$user") }, { "userName", new BsonDocument("$first", "$userName") }, { "numReviews", new BsonDocument("$sum", 1) }, { "numLikes", new BsonDocument("$sum", 1) }}),

            };

            var operations2 = new BsonDocument[]
            {
                //new BsonDocument("$match", new BsonDocument("createdDate", new BsonDocument ( "$gte", DateTime.UtcNow.AddDays(-30) ))),
                new BsonDocument("$unwind", "$likes"),
                new BsonDocument("$group", new BsonDocument { { "_id", new BsonDocument("user", "$user") }, { "userName", new BsonDocument("$first", "$userName") }, { "numReviews", new BsonDocument("$sum", 1) }, { "numLikes", new BsonDocument("$sum", 1) }}),
            };

            var results = _reviewsRepository.Collection.Aggregate(operations);
            var results2 = _reviewsRepository.Collection.Aggregate(operations2);

            List<UserStatisticData> stats = new List<UserStatisticData>();
            //drop the table
            _allTimeStatsRepository.Collection.Drop();

            foreach (BsonDocument doc in results.ResultDocuments)
            {
                UserStatisticData stat = new UserStatisticData();

                BsonValue root; // = new BsonObjectId(;
                doc.TryGetValue("_id", out root);
                BsonDocument valreviewuser = root.AsBsonDocument;
                stat.userId = Guid.Parse(valreviewuser.GetValue("user").AsString);

                BsonValue valreviewusername; // = new BsonObjectId(;
                doc.TryGetValue("userName", out valreviewusername);
                stat.userName = valreviewusername.AsString;

                BsonValue valreviewnumreviews; // = new BsonObjectId(;
                doc.TryGetValue("numReviews", out valreviewnumreviews);
                stat.numReviews = valreviewnumreviews.AsInt32;

                foreach (BsonDocument doc2 in results2.ResultDocuments)
                {
                    BsonValue root2; // = new BsonObjectId(;
                    doc2.TryGetValue("_id", out root2);
                    BsonDocument valreviewuser2 = root2.AsBsonDocument;

                    if (valreviewuser.Equals(valreviewuser2))
                    {
                        BsonValue valreviewnumlikes; // = new BsonObjectId(;
                        doc2.TryGetValue("numLikes", out valreviewnumlikes);
                        stat.numLikes = valreviewnumlikes.AsInt32;
                        break;
                    }
                }
                stat.score = (stat.numReviews + stat.numLikes) * 100;
                stats.Add(stat);
            }

            stats.Sort((b, a) => a.score.CompareTo(b.score));

            //now calculate the rankings
            int rankCounter = 1;
            for (int i = 0; i < stats.Count(); i++)
            {
                UserStatisticData stat = stats.ElementAt(i);
                if (i > 0)
                {

                    UserStatisticData prvstat = stats.ElementAt(i - 1);
                    if (prvstat.score > stat.score)
                    {
                        rankCounter++;
                    }
                    stat.rank = rankCounter;
                }
                else
                {
                    stat.rank = rankCounter;
                }
            }
            _allTimeStatsRepository.Collection.InsertBatch(stats);

            //string[] indexes = new string[] { "userId" };

            //_allTimeStatsRepository.Collection.EnsureIndex(indexes);
        }