Exemplo n.º 1
0
        public void MonthSplitterTest(int months, int items, int shouldItemsPerMonth)
        {
            //Arrange
            var splitter       = new MonthSplitter();
            var timeRange      = TimeRange.Create(DateTime.UtcNow, DateTime.UtcNow.AddMonths(months));
            var testCollection = TestEntityGenerator
                                 .Generate(timeRange, i =>
            {
                if (i == 0 || items % i == 0)
                {
                    return(null);
                }

                var time   = DateTime.UtcNow.AddDays(i);
                var entity = new TestEntity();
                entity.SetCreatedAt(time);
                return(entity);
            });

            //Act
            var splitted = splitter.Split(testCollection).ToList();

            //Assert
            var timeRanges    = splitted.Select(x => x.Key);
            var itemsPerMonth = splitted.First().Value.Count();
            var daysAreEqual  = splitted.All(x => x.Value.Count() == itemsPerMonth);

            Assert.That(timeRanges.Count(), Is.EqualTo(months));
            Assert.That(itemsPerMonth, Is.EqualTo(shouldItemsPerMonth));
            Assert.That(daysAreEqual, Is.True);
        }
Exemplo n.º 2
0
 private IEnumerable <TimeStatisticItem> SumItems(IEnumerable <TimeStatisticItem> statisticsPerDay, int daysPerItem, TimeRange expectedTimeRange)
 {
     for (var i = 0; i <= expectedTimeRange.DaysBetween / daysPerItem; i++)
     {
         var itemsToSum = statisticsPerDay.Skip(i * daysPerItem).Take(daysPerItem);
         var sum        = itemsToSum.Sum(x => x.Value);
         var item       = new TimeStatisticItem(TimeRange.Create(itemsToSum.First().Time.Start, itemsToSum.Last().Time.End), sum);
         yield return(item);
     }
 }
Exemplo n.º 3
0
        public IEnumerable <KeyValuePair <TimeRange, IEnumerable <T> > > Split <T>(IEnumerable <T> collection) where T : ISplittable
        {
            var groupedByDay = collection.GroupBy(x => x.GetSplittable().Date);

            foreach (var day in groupedByDay)
            {
                var timeRange = TimeRange.Create(day.Key, day.Key);
                yield return(new KeyValuePair <TimeRange, IEnumerable <T> >(timeRange, day));
            }
        }
Exemplo n.º 4
0
        public IEnumerable <TimeRange> GetTimeRangeMovePerPeriod(string period, DateTime oldestMessageDatetime) //TODO test
        {
            var startOfCurrentPeriod = this.GetStartOfCurrentPeriod(period);
            var moveForward          = this.GetMoveForward(period);
            var moveBackward         = this.GetMoveBackward(period);
            var daysAtEnd            = this.GetDaysAtEnd(period);

            return(TimeRange.Create(startOfCurrentPeriod, startOfCurrentPeriod.AddDays(moveForward.Invoke(startOfCurrentPeriod) - daysAtEnd).AddMilliseconds(-1))
                   .Move(TimeSpan.FromDays(-moveBackward.Invoke(startOfCurrentPeriod)))
                   .MoveWhile(x => !x.Contains(oldestMessageDatetime), x => TimeSpan.FromDays(-moveBackward.Invoke(x.Start))));
        }
Exemplo n.º 5
0
        public IEnumerable <TimeStatisticItem> GetStatisticsPerMinute(IEnumerable <Message> latestMessages, TimeRange expectedTimeRange)
        {
            var result = new List <TimeStatisticItem>();

            expectedTimeRange.ForeachMinute((i, minute) =>
            {
                var sum  = latestMessages.Where(x => x.SentAt.Date == minute.Date && x.SentAt.Hour == minute.Hour && x.SentAt.Minute == minute.Minute).Count();
                var item = new TimeStatisticItem(TimeRange.Create(minute, minute.AddMinutes(1).AddSeconds(-1)), sum);
                result.Add(item);
            });
            return(result);
        }
Exemplo n.º 6
0
        public IEnumerable <TimeStatisticItem> GetStatisticsPerHour(IEnumerable <Message> latestMessages, TimeRange expectedTimeRange)
        {
            var result = new List <TimeStatisticItem>();

            expectedTimeRange.ForeachHour((i, hour) =>
            {
                var sum  = latestMessages.Where(x => x.SentAt.Date == hour.Date && x.SentAt.Hour == hour.Hour).Count();
                var item = new TimeStatisticItem(TimeRange.Create(hour, hour.AddHours(1).AddSeconds(-1)), sum);
                result.Add(item);
            });
            return(result);
        }
Exemplo n.º 7
0
        public IEnumerable <KeyValuePair <TimeRange, IEnumerable <T> > > Split <T>(IEnumerable <T> collection) where T : ISplittable
        {
            var groupedByYear = collection.GroupBy(x => x.GetSplittable().Date.Year).Select(x => (Year: x.Key, Month: x.GroupBy(y => y.GetSplittable().Month))).ToList();

            foreach (var year in groupedByYear)
            {
                foreach (var month in year.Month)
                {
                    var timeRange = TimeRange.Create(new DateTime(year.Year, month.Key, 1), new DateTime(year.Year, month.Key + 1, 1).AddMilliseconds(-1));
                    yield return(new KeyValuePair <TimeRange, IEnumerable <T> >(timeRange, month));
                }
            }
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
        public void DaySplitterTest(int days, int items, int shouldItemsPerDay)
        {
            //Arrange
            var splitter       = new DaySplitter();
            var testCollection = TestEntityGenerator
                                 .Generate(TimeRange.Create(DateTime.Now.Date.AddHours(12).AddDays(-days), DateTime.Now.Date.AddHours(12)), items);

            //Act
            var splitted = splitter.Split(testCollection).ToList();

            //Assert
            var timeRanges   = splitted.Select(x => x.Key);
            var itemsPerDay  = splitted.First().Value.Count();
            var daysAreEqual = splitted.All(x => x.Value.Count() == itemsPerDay);

            Assert.That(timeRanges.Count(), Is.EqualTo(days));
            Assert.That(itemsPerDay, Is.EqualTo(shouldItemsPerDay));
            Assert.That(daysAreEqual, Is.True);
        }
Exemplo n.º 10
0
 public TimeRange GetTimeRange()
 {
     return(TimeRange.Create(DateTime.UtcNow.AddMinutes(-this.MinutesSince), DateTime.UtcNow));
 }
Exemplo n.º 11
0
 public TimeRange GetTimeRange() => TimeRange.Create(DateTime.UtcNow.AddMinutes(-MinutesSince), DateTime.UtcNow);