public async Task <PagesDay> GetUsersByDay(DateTime?start, DateTime?end) { var NavContext = new NavigationContext(); if (start == null) { start = startDate; } if (end == null) { end = endDate; } var visitsbyCountry = await NavContext.Pages.Aggregate(new AggregateOptions { AllowDiskUse = true }) .Match(x => x.Insertdate > start && x.Insertdate <= end) .Group(r => new { groupedYear = r.Insertdate.Year, groupedMonth = r.Insertdate.Month, groupedDay = r.Insertdate.Day, users = r.Atm_ID }, g => new { Key = g.Key, total = g.Count() }) .Project(r => new DailyUserStat() { Day = r.Key.groupedDay, Month = r.Key.groupedMonth, Year = r.Key.groupedYear, User = r.Key.users }) .Group(y => new { groupedYear = y.Year, groupedMonth = y.Month, groupedDay = y.Day }, o => new { Key = o.Key, total = o.Count() }) .Project(r => new DailyStat() { Day = r.Key.groupedDay, Month = r.Key.groupedMonth, Year = r.Key.groupedYear, Total = r.total }) .SortBy(x => x.Day).SortBy(x => x.Month).SortBy(x => x.Year) .ToListAsync(); if (visitsbyCountry.Count == 0) { return(new PagesDay()); } DateTime aux = new DateTime(visitsbyCountry[0].Year, visitsbyCountry[0].Month, visitsbyCountry[0].Day); if (aux < start) { visitsbyCountry.RemoveAt(0); aux = new DateTime(visitsbyCountry[0].Year, visitsbyCountry[0].Month, visitsbyCountry[0].Day); } List <ProjectionKeyDateValue> pagesDate = NormalizeDayList(visitsbyCountry, aux); /*foreach (var item in visitsbyCountry) * { * DateTime dt = new DateTime(item.Year, item.Month, item.Day); * if (dt == aux) * { * ProjectionKeyDateValue pkdv = new ProjectionKeyDateValue() * { * Data = dt, * Count = item.Total * }; * pagesDate.Add(pkdv); * aux = dt.AddDays(1); * } * else * { * int sparedays = dt.Subtract(aux).Days; * for (int i = 0; i < sparedays; i++) * { * DateTime newday = aux.AddDays(1); * aux = newday; * if (newday == dt) * { * ProjectionKeyDateValue pkdv = new ProjectionKeyDateValue() * { * Data = dt, * Count = item.Total * }; * pagesDate.Add(pkdv); * } * else * { * ProjectionKeyDateValue pkdv = new ProjectionKeyDateValue() * { * Data = newday, * Count = 0 * }; * pagesDate.Add(pkdv); * } * } * } * * * }*/ PagesDay pd = new PagesDay() { Total = pagesDate.Sum(x => x.Count), pagesDay = pagesDate.OrderBy(x => x.Data) }; return(pd); }
public async Task <PagesDay> GetPagesByDay(DateTime?start, DateTime?end) { var NavContext = new NavigationContext(); //https://www.mongodb.com/presentations/results?page=1&search=Back%20to%20basics%20webinar //.Match("'Insertdate' : { $gte: ISODate('2015-10-01T00:00:00.000Z'), $lt: ISODate('2015-10-15T00:00:00.000Z')}" ) // .Match(x => x.Insertdate >= Convert.ToDateTime("2015-10-01T00:00:00.000Z") && x.Insertdate <= Convert.ToDateTime("2015-11-30T00:00:00.000Z")) //@"{ // url: '$Url', // yearMonthDay: { $dateToString: { format: '%Y-%m-%d', date: '$Insertdate' } }, // time: { $dateToString: { format: '%H:%M:%S:%L', date: '$Insertdate' } }}") // .Match(x => x.Insertdate <= new DateTime(2015, 10, 1, 00,00,00, 00) && x.Insertdate >= new DateTime(2015, 10, 15, 00, 00, 00, 00)) //var visitsbyCountry = await NavContext.Pages.Aggregate() // .Match(x => x.Insertdate <= Convert.ToDateTime("2015-10-01T00:00:00.000Z") && x.Insertdate >= Convert.ToDateTime("2015 - 10 - 15T00: 00:00.000Z")) // .Project(x => new { _id = x.Url, yearMonthDay = x.Insertdate.Date }) // .Group<ProjectionKeyValue>("{ _id: '$Atm_Country', Count: { $sum: 1 } }") // .ToListAsync(); if (start == null) { start = startDate; } if (end == null) { end = endDate; } var visitsbyCountry = await NavContext.Pages.Aggregate() .Match(x => x.Insertdate > start && x.Insertdate <= end) //.Match("{ \"Atm_Country\" : \"ES\" }") .Group(r => new { groupedYear = r.Insertdate.Year, groupedMonth = r.Insertdate.Month, groupedDay = r.Insertdate.Day }, g => new { Key = g.Key, total = g.Count() }) .Project(r => new DailyStat() { Day = r.Key.groupedDay, Month = r.Key.groupedMonth, Year = r.Key.groupedYear, //Data = new DateTime( r.Key.groupedYear, r.Key.groupedMonth, r.Key.groupedDay) , Total = r.total }) .SortBy(x => x.Day).SortBy(x => x.Month).SortBy(x => x.Year) .ToListAsync(); if (visitsbyCountry.Count == 0) { return(new PagesDay()); } DateTime aux = new DateTime(visitsbyCountry[0].Year, visitsbyCountry[0].Month, visitsbyCountry[0].Day); if (aux < start) { visitsbyCountry.RemoveAt(0); aux = new DateTime(visitsbyCountry[0].Year, visitsbyCountry[0].Month, visitsbyCountry[0].Day); } List <ProjectionKeyDateValue> pagesDate = NormalizeDayList(visitsbyCountry, aux); /*List<ProjectionKeyDateValue> pagesDate = new List<ProjectionKeyDateValue>(); * foreach (var item in visitsbyCountry) * { * DateTime dt = new DateTime(item.Year, item.Month, item.Day); * if (dt == aux) * { * ProjectionKeyDateValue pkdv = new ProjectionKeyDateValue() * { * Data = dt, * Count = item.Total * }; * pagesDate.Add(pkdv); * aux = dt.AddDays(1); * } * else * { * int sparedays = dt.Subtract(aux).Days; * for (int i = 0; i <= sparedays; i++) * { * DateTime newday = aux.AddDays(1); * aux = newday; * if (newday == dt) * { * ProjectionKeyDateValue pkdv = new ProjectionKeyDateValue() * { * Data = dt, * Count = item.Total * }; * pagesDate.Add(pkdv); * } * else * { * ProjectionKeyDateValue pkdv = new ProjectionKeyDateValue() * { * Data = newday, * Count = 0 * }; * pagesDate.Add(pkdv); * } * } * } * }*/ PagesDay pd = new PagesDay() { Total = pagesDate.Sum(x => x.Count), pagesDay = pagesDate.OrderBy(x => x.Data) }; return(pd); }