Ejemplo n.º 1
0
        public async Task <StatisticsChartData> GetDonationStatisticsData(StatisticsQueryParams query)
        {
            var books = _bookRepository.GetAll();

            if (query.Cities?.Length > 0)
            {
                books = books.Where(b => query.Cities.Contains(b.User.UserRoom.Location.City));
            }

            if (query.Offices?.Length > 0)
            {
                books = books.Where(b => query.Offices.Contains(b.User.UserRoom.Location.OfficeName));
            }

            if (query.From.HasValue)
            {
                books = books.Where(b => b.DateAdded >= query.From.Value);
            }

            if (query.To.HasValue)
            {
                books = books.Where(b => b.DateAdded <= query.To.Value);
            }

            if (query.Genres?.Length > 0)
            {
                var predicate = PredicateBuilder.New <Book>();
                foreach (var id in query.Genres)
                {
                    var tempId = id;
                    predicate = predicate.Or(b => b.BookGenre.Any(g => g.Genre.Id == tempId));
                }

                books = books.Where(predicate);
            }


            DateTime from;

            if (query.From.HasValue)
            {
                from = query.From.Value;
            }
            else
            {
                try
                {
                    from = books.Min(t => t.DateAdded);
                }
                catch (Exception)
                {
                    from = DateTime.Today;
                }
            }
            DateTime to     = query.To ?? DateTime.Today;
            var      cities = query.Cities?.Length > 0
                ? _locationRepository.GetAll().Where(l => query.Cities.Contains(l.City)).Select(l => l.City).Distinct()
                : _locationRepository.GetAll().Select(l => l.City).Distinct();

            var data    = new Dictionary <string, List <int> >();
            var periods = new List <string>();

            if (to.Year - from.Year >= 2) // more than 2 years (years)
            {
                foreach (var city in cities)
                {
                    List <int> counts = new List <int>();
                    for (var currentYear = from.Year; currentYear < to.AddYears(1).Year; currentYear++)
                    {
                        if (!periods.Contains(currentYear.ToString()))
                        {
                            periods.Add(currentYear.ToString());
                        }

                        counts.Add(books
                                   .Where(b => b.DateAdded.Year == currentYear)
                                   .Count(b => b.User.UserRoom.Location.City == city)
                                   );
                    }

                    data.Add(city, counts);
                }
            }
            else if (from.AddMonths(1) < to) // more then month (months)
            {
                foreach (var city in cities)
                {
                    List <int> counts = new List <int>();

                    DateTime current = from;
                    for (; current <= to; current = current.AddMonths(1))
                    {
                        if (current.Year == to.Year && current.Month == to.Month && current < to)
                        {
                            current = to;
                        }

                        if (!periods.Contains(current.ToString("dd-MM-yyyy")))
                        {
                            periods.Add(current.ToString("dd-MM-yyyy"));
                        }

                        counts.Add(books
                                   .Where(b => b.DateAdded.Year == current.Year && b.DateAdded.Month == current.Month)
                                   .Count(b => b.User.UserRoom.Location.City == city)
                                   );
                    }

                    if (current.Month == to.Month && current > to)
                    {
                        if (!periods.Contains(to.ToString("dd-MM-yyyy")))
                        {
                            periods.Add(to.ToString("dd-MM-yyyy"));
                        }

                        counts.Add(books
                                   .Where(b => b.DateAdded.Year == current.Year && b.DateAdded.Month == current.Month)
                                   .Count(b => b.User.UserRoom.Location.City == city)
                                   );
                    }

                    data.Add(city, counts);
                }
            }
            else // less than month (days)
            {
                foreach (var city in cities)
                {
                    List <int> counts = new List <int>();
                    for (var current = from; current <= to; current = current.AddDays(1))
                    {
                        if (!periods.Contains(current.ToString("dd-MM-yyyy")))
                        {
                            periods.Add(current.ToString("dd-MM-yyyy"));
                        }

                        counts.Add(books
                                   .Where(b => b.DateAdded.Date == current.Date)
                                   .Count(b => b.User.UserRoom.Location.City == city)
                                   );
                    }

                    data.Add(city, counts);
                }
            }

            return(new StatisticsChartData(periods, data));
        }
Ejemplo n.º 2
0
        public async Task <StatisticsChartData> GetReadingStatisticsData(StatisticsQueryParams query)
        {
            var transitions = _bookService.GetBooksTransitions();

            if (query.Cities?.Length > 0)
            {
                transitions = transitions.Where(r => query.Cities.Contains(r.User.UserRoom.Location.City));
            }

            if (query.Offices?.Length > 0)
            {
                transitions = transitions.Where(r => query.Offices.Contains(r.User.UserRoom.Location.OfficeName));
            }

            if (query.From.HasValue)
            {
                transitions = transitions.Where(r => r.RequestDate >= query.From.Value);
            }

            if (query.To.HasValue)
            {
                transitions = transitions.Where(r => r.RequestDate <= query.To.Value);
            }

            if (query.Genres?.Length > 0)
            {
                var predicate = PredicateBuilder.New <Request>();
                foreach (var id in query.Genres)
                {
                    var tempId = id;
                    predicate = predicate.Or(g => g.Book.BookGenre.Any(g => g.Genre.Id == tempId));
                }

                transitions = transitions.Where(predicate);
            }

            DateTime from;

            if (query.From.HasValue)
            {
                from = query.From.Value;
            }
            else
            {
                try
                {
                    from = transitions.Min(t => t.RequestDate);
                }
                catch (Exception)
                {
                    from = DateTime.Today;
                }
            }
            DateTime to     = query.To ?? DateTime.Today;
            var      genres = query.Genres?.Length > 0
                ? _genreRepository.GetAll().Where(g => query.Genres.Contains(g.Id)).Select(g => g.Name).ToList()
                : _genreRepository.GetAll().Select(g => g.Name).ToList();


            var data    = new Dictionary <string, List <int> >();
            var periods = new List <string>();

            if (to.Year - from.Year >= 2) // more than 2 years (years)
            {
                foreach (var genre in genres)
                {
                    List <int> counts = new List <int>();


                    for (var currentYear = from.Year; currentYear < to.AddYears(1).Year; currentYear++)
                    {
                        if (!periods.Contains(currentYear.ToString()))
                        {
                            periods.Add(currentYear.ToString());
                        }

                        counts.Add(transitions
                                   .Where(r => r.RequestDate.Year == currentYear)
                                   .Count(r => r.Book.BookGenre.Exists(g => g.Genre.Name == genre))
                                   );
                    }

                    data.Add(genre, counts);
                }
            }
            else if (from.AddMonths(1) < to) // more then month (months)
            {
                foreach (var genre in genres)
                {
                    List <int> counts = new List <int>();

                    DateTime current = from;
                    for (; current <= to; current = current.AddMonths(1))
                    {
                        if (current.Year == to.Year && current.Month == to.Month && current < to)
                        {
                            current = to;
                        }

                        if (!periods.Contains(current.ToString("dd-MM-yyyy")))
                        {
                            periods.Add(current.ToString("dd-MM-yyyy"));
                        }

                        counts.Add(transitions
                                   .Where(r => r.RequestDate.Year == current.Year && r.RequestDate.Month == current.Month)
                                   .Count(r => r.Book.BookGenre.Exists(g => g.Genre.Name == genre))
                                   );
                    }

                    if (current.Month == to.Month && current > to)
                    {
                        if (!periods.Contains(to.ToString("dd-MM-yyyy")))
                        {
                            periods.Add(to.ToString("dd-MM-yyyy"));
                        }

                        counts.Add(transitions
                                   .Where(r => r.RequestDate.Year == to.Year && r.RequestDate.Month == to.Month)
                                   .Count(r => r.Book.BookGenre.Exists(g => g.Genre.Name == genre))
                                   );
                    }

                    data.Add(genre, counts);
                }
            }
            else // less than month (days)
            {
                foreach (var genre in genres)
                {
                    List <int> counts = new List <int>();
                    for (var current = from; current <= to; current = current.AddDays(1))
                    {
                        if (!periods.Contains(current.ToString("dd-MM-yyyy")))
                        {
                            periods.Add(current.ToString("dd-MM-yyyy"));
                        }

                        counts.Add(transitions
                                   .Where(r => r.RequestDate.Date == current.Date)
                                   .Count(r => r.Book.BookGenre.Exists(g => g.Genre.Name == genre))
                                   );
                    }

                    data.Add(genre, counts);
                }
            }

            return(new StatisticsChartData(periods, data));
        }
Ejemplo n.º 3
0
 public async Task <ActionResult <StatisticsChartData> > GetDonationStatisticsAsync([FromQuery] StatisticsQueryParams query)
 {
     return(await _statisticsService.GetDonationStatisticsData(query));
 }