Beispiel #1
0
        public virtual async Task <UserStatistics> GetStatisticsAsync()
        {
            int totalUsers = await _db.Users.CountAsync();

            int totalAdmins = (await GetUsersInRole("Admin")).Count;
            var data        = await _db.Users.Where(p => p.CreatedOn >= DateTime.Now.AddYears(-1)).Select(c => new { date = c.CreatedOn.Date, month = c.CreatedOn.Month }).ToListAsync();

            var createdByDate  = data.GroupBy(p => p.date).Select(g => new { name = g.Key, count = g.Count() });
            var createdByMonth = data.GroupBy(p => p.month).Select(g => new { name = g.Key, count = g.Count() });

            List <KeyValuePair <string, int> > days = new List <KeyValuePair <string, int> >();

            foreach (DateTime day in DateTimeExtensions.EachDay(DateTime.UtcNow.AddDays(-89), DateTime.UtcNow))
            {
                var dayvalue = createdByDate.SingleOrDefault(c => c.name == day.Date);
                int count    = dayvalue != null ? dayvalue.count : 0;
                days.Add(new KeyValuePair <string, int>(day.ToString("dd MMM"), count));
            }

            List <KeyValuePair <string, int> > months = new List <KeyValuePair <string, int> >();

            for (DateTime dt = DateTime.UtcNow.AddMonths(-11); dt <= DateTime.UtcNow; dt = dt.AddMonths(1))
            {
                var monthvalue = createdByMonth.SingleOrDefault(c => c.name == dt.Month);
                int count      = monthvalue != null ? monthvalue.count : 0;
                months.Add(new KeyValuePair <string, int>(dt.ToString("dd MMM"), count));
            }

            return(new UserStatistics(totalUsers, totalAdmins, days, months));
        }
Beispiel #2
0
        public static string CalculateAverageCommitsPerDay(IEnumerable <Commit> commits,
                                                           DateTime start,
                                                           DateTime end)
        {
            var commitsPerDay = new List <int>();
            var hasCommits    = false;

            foreach (
                var count in
                DateTimeExtensions.EachDay(start, end)
                .TakeWhile(day => day.Date <= DateTime.Now.Date)
                .Select(day => commits.Count(c => c.Committer.When.DateTime.Date == day.Date)))
            {
                if (count <= 0 && !hasCommits)
                {
                }
                if (count <= 0)
                {
                    continue;
                }
                hasCommits = true;
                commitsPerDay.Add(count);
            }

            return(commitsPerDay.Any()
                ? Math.Round(commitsPerDay.Average(), 2).ToString(CultureInfo.CurrentCulture)
                : "0");
        }
Beispiel #3
0
        public async Task <ContentStatitsics> GetStatisticsAsync()
        {
            int totalPosts = await _db.Content.CountAsync();

            int totalPublished = await _db.Content.Where(c => c.Status == ContentStatus.Published && c.PublishDate < DateTime.UtcNow).CountAsync();

            var data = await _db.Content.Where(p => p.CreatedOn >= DateTime.Now.AddYears(-1)).Where(c => c.Status == ContentStatus.Published && c.PublishDate < DateTime.UtcNow).Select(c => new { type = c.ContentType, date = c.CreatedOn.Date, month = c.CreatedOn.Month, pubdate = c.PublishDate.Date, pubmonth = c.PublishDate.Month }).ToListAsync();

            var createdByDate    = data.GroupBy(p => p.date).Select(g => new { name = g.Key, count = g.Count() });
            var createdByMonth   = data.GroupBy(p => p.month).Select(g => new { name = g.Key, count = g.Count() });
            var publishedByDate  = data.GroupBy(p => p.pubdate).Select(g => new { name = g.Key, count = g.Count() });
            var publishedByMonth = data.GroupBy(p => p.pubmonth).Select(g => new { name = g.Key, count = g.Count() });
            var byType           = data.GroupBy(p => p.type).Select(g => new ContentTypeStat()
            {
                Type = Engine.Settings.Content.GetContentType(g.Key), Total = g.Count(), Name = g.Key
            });

            List <KeyValuePair <string, int> > days        = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > publishDays = new List <KeyValuePair <string, int> >();

            foreach (DateTime day in DateTimeExtensions.EachDay(DateTime.UtcNow.AddDays(-89), DateTime.UtcNow))
            {
                var dayvalue = createdByDate.SingleOrDefault(c => c.name == day.Date);
                int count    = dayvalue != null ? dayvalue.count : 0;
                days.Add(new KeyValuePair <string, int>(day.ToString("dd MMM"), count));

                dayvalue = publishedByDate.SingleOrDefault(c => c.name == day.Date);
                count    = dayvalue != null ? dayvalue.count : 0;
                publishDays.Add(new KeyValuePair <string, int>(day.ToString("dd MMM"), count));
            }

            List <KeyValuePair <string, int> > months        = new List <KeyValuePair <string, int> >();
            List <KeyValuePair <string, int> > publishMonths = new List <KeyValuePair <string, int> >();

            for (DateTime dt = DateTime.UtcNow.AddMonths(-11); dt <= DateTime.UtcNow; dt = dt.AddMonths(1))
            {
                var monthvalue = createdByMonth.SingleOrDefault(c => c.name == dt.Month);
                int count      = monthvalue != null ? monthvalue.count : 0;
                months.Add(new KeyValuePair <string, int>(dt.ToString("MMMM, yyyy"), count));

                monthvalue = publishedByMonth.SingleOrDefault(c => c.name == dt.Month);
                count      = monthvalue != null ? monthvalue.count : 0;
                publishMonths.Add(new KeyValuePair <string, int>(dt.ToString("MMMM, yyyy"), count));
            }

            return(new ContentStatitsics(totalPosts, totalPublished, days, months, publishDays, publishMonths, byType));
        }
        /// <summary>
        ///     Exports git log to an Excel spreadsheet
        /// </summary>
        /// <param name="path">The path to the git repositiory</param>
        /// <param name="fileName">The desired filename of the generated spreadsheet</param>
        /// <param name="from">The starting date</param>
        /// <param name="to">The ending date</param>
        public void ExportGitLog(string path,
                                 string fileName,
                                 DateTime from,
                                 DateTime to)
        {
            _start      = from;
            _end        = to;
            ProjectName = string.Empty;

            using (_repo = new Repository(path))
            {
                ProjectName = _repo.Config.Get <string>("core.ProjectName").Value;

                _commits = (from c in _repo.Commits
                            where c.Committer.When.DateTime >= _start && c.Committer.When.DateTime <= _end
                            orderby c.Committer.When.DateTime descending
                            select c).ToList();

                var report = new FileInfo(Path.Combine(fileName));
                if (report.Exists)
                {
                    report.Delete();
                    report = new FileInfo(fileName);
                }

                using (var package = new ExcelPackage(report))
                {
                    if (_commits.Any())
                    {
                        foreach (var day in DateTimeExtensions.EachDay(_start, _end))
                        {
                            var currentCommits =
                                _commits.Where(c => c.Committer.When.DateTime.Date == day.Date).ToList();

                            if (!currentCommits.Any())
                            {
                                continue;
                            }

                            var ws = package.Workbook.Worksheets.Add(day.ToString("dddd, MM-dd"));

                            // Headers
                            ws.Cells[1, 1, 1, 2].Merge = true;
                            var titleCell = ws.Cells[1, 1];
                            titleCell.Value           = currentCommits.First().Committer.When.DateTime.ToString("D");
                            titleCell.Style.Font.Bold = true;

                            ws.Cells[2, 1, 2, 2].Merge = true;
                            ws.Cells[2, 1].Value       = $"Commits: {currentCommits.Count}";

                            ws.Cells[2, 3, 2, 4].Merge = true;
                            ws.Cells[2, 3].Value       = $"Total Commits: {_commits.Count}";

                            ws.Cells[2, 5, 2, 6].Merge = true;
                            ws.Cells[2, 5].Value       =
                                $"Avg Per Day: {Commits.CalculateAverageCommitsPerDay(_commits, _start, _end)}";

                            ws.View.FreezePanes(3, 1);

                            BuildCommits(currentCommits, ws, 4);

                            ws.Cells[ws.Dimension.Address].AutoFitColumns();

                            ws.Column(2).Width = 60;
                        }
                    }
                    else
                    {
                        var ws = package.Workbook.Worksheets.Add("No Commits Found");

                        ws.Cells[1, 1, 1, 2].Merge = true;
                        ws.Cells[1, 1].Value       = "No Commits Found";

                        ws.Cells[1, 4, 1, 5].Merge = true;
                        ws.Cells[1, 4].Value       = $"Total Commits: {_commits.Count}";

                        ws.Cells[2, 4, 2, 5].Merge = true;
                        ws.Cells[2, 4].Value       = $"Avg Per Day: 0";
                    }

                    package.SaveAs(report);
                }
            }
        }