Example #1
0
        public WidgetStatistics Average()
        {
            var result = new WidgetStatistics();

            try
            {
                using (var ctx = _dbContextScopeFactory.CreateReadOnly())
                {
                    #region Get Incomes
                    result.IncomeCompany = _incomeRepo.Find(x =>
                                                            x.IncomeType == Enums.IncomeType.CompanyTotal
                                                            ).GroupBy(x => new {
                        Year  = x.CreatedAt.Value.Year,
                        Month = x.CreatedAt.Value.Month
                    }).Select(x => new {
                        Year  = x.Key.Year,
                        Month = x.Key.Month,
                        Total = x.Sum(y => y.Total)
                    }).OrderByDescending(x => x.Year)
                                           .ThenByDescending(x => x.Month)
                                           .Select(x => x.Total).DefaultIfEmpty(0).Average();
                    #endregion

                    #region Get Students
                    result.NewStudents = (decimal)_userPerCourseRepo.AsQueryable().GroupBy(x => new {
                        Year  = x.CreatedAt.Value.Year,
                        Month = x.CreatedAt.Value.Month
                    }).Select(x => new
                    {
                        Year  = x.Key.Year,
                        Month = x.Key.Month,
                        Total = x.Count()
                    }).OrderByDescending(x => x.Year)
                                         .ThenByDescending(x => x.Month)
                                         .Select(x => x.Total).DefaultIfEmpty(0).Average();
                    #endregion

                    #region Get Courses
                    result.NewCourses = (decimal)_courseRepo.AsQueryable().GroupBy(x => new {
                        Year  = x.CreatedAt.Value.Year,
                        Month = x.CreatedAt.Value.Month
                    }).Select(x => new {
                        Year  = x.Key.Year,
                        Month = x.Key.Month,
                        Total = x.Count()
                    }).OrderByDescending(x => x.Year)
                                        .ThenByDescending(x => x.Month)
                                        .Select(x => x.Total).DefaultIfEmpty(0).Average();
                    #endregion
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
            }

            return(result);
        }
Example #2
0
        public WidgetStatistics Variance()
        {
            var result = new WidgetStatistics();

            try
            {
                using (var ctx = _dbContextScopeFactory.CreateReadOnly())
                {
                    var dateEnd   = DateTime.Today;
                    var dateStart = DateTime.Today.AddMonths(-1);

                    #region Get Incomes
                    var incomes = _incomeRepo.Find(x => (
                                                       (x.CreatedAt.Value.Year == dateStart.Year || x.CreatedAt.Value.Year == dateEnd.Year) &&
                                                       (x.CreatedAt.Value.Month == dateStart.Month || x.CreatedAt.Value.Month == dateEnd.Month) &&
                                                       x.IncomeType == Enums.IncomeType.CompanyTotal
                                                       )).GroupBy(x => new {
                        Year  = x.CreatedAt.Value.Year,
                        Month = x.CreatedAt.Value.Month
                    }).Select(x => new {
                        Year  = x.Key.Year,
                        Month = x.Key.Month,
                        Total = x.Sum(y => y.Total)
                    }).OrderByDescending(x => x.Year)
                                  .ThenByDescending(x => x.Month)
                                  .ToList();

                    // Si no hay nada, no hubo variación
                    if (incomes == null)
                    {
                        result.IncomeCompany = 0;
                    }
                    else
                    {
                        var priorMonth = incomes.Where(x =>
                                                       x.Year == dateStart.Year && x.Month == dateStart.Month
                                                       ).Select(x => x.Total).DefaultIfEmpty(0).SingleOrDefault();

                        var currentMonth = incomes.Where(x =>
                                                         x.Year == dateEnd.Year && x.Month == dateEnd.Month
                                                         ).Select(x => x.Total).DefaultIfEmpty(0).SingleOrDefault();

                        if (priorMonth == 0)
                        {
                            result.IncomeCompany = 0;
                        }
                        else
                        {
                            result.IncomeCompany = Math.Round(((currentMonth - priorMonth) / priorMonth) * 100, 2);
                        }
                    }
                    #endregion

                    #region Get Students
                    var students = _userPerCourseRepo.Find(x => (
                                                               (x.CreatedAt.Value.Year == dateStart.Year || x.CreatedAt.Value.Year == dateEnd.Year) &&
                                                               (x.CreatedAt.Value.Month == dateStart.Month || x.CreatedAt.Value.Month == dateEnd.Month)
                                                               )).GroupBy(x => new {
                        Year  = x.CreatedAt.Value.Year,
                        Month = x.CreatedAt.Value.Month
                    }).Select(x => new {
                        Year  = x.Key.Year,
                        Month = x.Key.Month,
                        Total = x.Count()
                    }).OrderByDescending(x => x.Year)
                                   .ThenByDescending(x => x.Month)
                                   .ToList();

                    // Si no hay nada, no hubo variación
                    if (students == null)
                    {
                        result.NewStudents = 0;
                    }
                    else
                    {
                        var priorMonth = students.Where(x =>
                                                        x.Year == dateStart.Year && x.Month == dateStart.Month
                                                        ).Select(x => x.Total).DefaultIfEmpty(0).SingleOrDefault();

                        var currentMonth = students.Where(x =>
                                                          x.Year == dateEnd.Year && x.Month == dateEnd.Month
                                                          ).Select(x => x.Total).DefaultIfEmpty(0).SingleOrDefault();

                        if (priorMonth == 0)
                        {
                            result.NewStudents = 0;
                        }
                        else
                        {
                            result.NewStudents = ((Convert.ToDecimal(currentMonth) - priorMonth) / priorMonth) * 100;
                        }
                    }
                    #endregion

                    #region Get Courses
                    var courses = _courseRepo.Find(x => (
                                                       (x.CreatedAt.Value.Year == dateStart.Year || x.CreatedAt.Value.Year == dateEnd.Year) &&
                                                       (x.CreatedAt.Value.Month == dateStart.Month || x.CreatedAt.Value.Month == dateEnd.Month)
                                                       )).GroupBy(x => new {
                        Year  = x.CreatedAt.Value.Year,
                        Month = x.CreatedAt.Value.Month
                    }).Select(x => new {
                        Year  = x.Key.Year,
                        Month = x.Key.Month,
                        Total = x.Count()
                    }).OrderByDescending(x => x.Year)
                                  .ThenByDescending(x => x.Month)
                                  .ToList();

                    // Si no hay nada, no hubo variación
                    if (courses == null)
                    {
                        result.NewCourses = 0;
                    }
                    else
                    {
                        var priorMonth = courses.Where(x =>
                                                       x.Year == dateStart.Year && x.Month == dateStart.Month
                                                       ).Select(x => x.Total).DefaultIfEmpty(0).SingleOrDefault();

                        var currentMonth = courses.Where(x =>
                                                         x.Year == dateEnd.Year && x.Month == dateEnd.Month
                                                         ).Select(x => x.Total).DefaultIfEmpty(0).SingleOrDefault();

                        if (priorMonth == 0)
                        {
                            result.NewCourses = 0;
                        }
                        else
                        {
                            result.NewCourses = ((Convert.ToDecimal(currentMonth) - priorMonth) / priorMonth) * 100;
                        }
                    }
                    #endregion
                }
            }
            catch (Exception e)
            {
                logger.Error(e.Message);
            }

            return(result);
        }