Exemplo n.º 1
0
        public IEnumerable <TimeStatisticItem> GetStatisticsPerDay(IEnumerable <PreGeneratedStatistic> preGeneratedStatistics, IEnumerable <Message> latestMessages, TimeRange expectedTimeRange)
        {
            latestMessages = this.FilterMessages(latestMessages, preGeneratedStatistics);
            var oldestLastMessagesDate = latestMessages.OrderBy(x => x.SentAt).FirstOrDefault()?.SentAt ?? DateTime.Today;
            var result = new List <TimeStatisticItem>();

            expectedTimeRange.ForeachDay((i, day) =>
            {
                var sum = 0;
                if (day >= oldestLastMessagesDate.Date)
                {
                    sum += latestMessages.Where(x => x.SentAt.Date == day).Count();
                }
                sum     += preGeneratedStatistics.Where(x => x.Period == Period.Day && x.TimeRange.Start == day).OrderBy(x => x.CreatedAt).FirstOrDefault()?.Count ?? 0;
                var item = new TimeStatisticItem(TimeRange.Create(day, day.AddDays(1).AddSeconds(-1)), sum);
                result.Add(item);
            });
            return(result);
        }