public IEnumerable <Sales> Get( string StartDT = null, string EndDT = null, DateGroupType group = DateGroupType.Day) { if (StartDT == null) { StartDateTime = DateTime.MinValue; } else { DateTime.TryParseExact(StartDT, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out StartDateTime); } if (EndDT == null) { EndDateTime = DateTime.MaxValue; } else { DateTime.TryParseExact(EndDT, "yyyy-MM-dd", CultureInfo.InvariantCulture, DateTimeStyles.None, out EndDateTime); } return(MathGroup.GetSales(StartDateTime, EndDateTime, group, db)); }
// GET: Admin public ActionResult Index() { if (!User.Identity.IsAuthenticated) { return(RedirectToAction("Login", "Account", new { returnUrl = "/Roles/Index" })); } AdminViewModel vm; using (var db = new ZapContext()) { var globals = db.ZapreadGlobals.Where(g => g.Id == 1) .AsNoTracking() .FirstOrDefault(); var gd = db.Groups.Sum(g => g.TotalEarnedToDistribute); var LNdep = Convert.ToDouble(db.LightningTransactions.Where(t => t.IsSettled && t.IsDeposit).Sum(t => t.Amount)) / 100000000.0; var LNwth = Convert.ToDouble(db.LightningTransactions.Where(t => t.IsSettled && !t.IsDeposit).Sum(t => t.Amount)) / 100000000.0; // Calculate post and comment stats. var startDate = DateTime.Now.AddDays(-1 * 31); var allPosts = db .Posts .Where(x => x.TimeStamp > startDate); DateGroupType group = DateGroupType.Day; var groupedStats = GroupPostsByDate(allPosts, group, startDate); DateTime epochUTC = new DateTime(1970, 1, 1, 0, 0, 0, kind: DateTimeKind.Utc); var stats = groupedStats.Select(x => new { x.Key, Count = x.Count() }) .ToList() .Select(x => new Stat { //TimeStamp = GetDate(group, x.Key.Value, startDate), TimeStampUtc = Convert.ToInt64((GetDate(group, x.Key.Value, startDate) - epochUTC).TotalMilliseconds), Count = x.Count }) .OrderBy(x => x.TimeStampUtc) .ToList(); vm = new AdminViewModel() { Globals = globals, PendingGroupToDistribute = gd, LNTotalDeposited = LNdep, LNTotalWithdrawn = LNwth, }; return(View(vm)); } }
public void HistorySaleController_Get_Sales(string StartDT, string EndDT, DateGroupType group, int expCount) { Mock <ApplicationContext> mockContext = new Mock <ApplicationContext>(); var list = new List <HistorySale>() { new HistorySale() { DateSale = DateTime.Parse("2020-06-20") }, new HistorySale() { DateSale = DateTime.Parse("2020-04-10") }, new HistorySale() { DateSale = DateTime.Parse("2020-03-15") }, new HistorySale() { DateSale = DateTime.Parse("2020-02-11") }, new HistorySale() { DateSale = DateTime.Parse("2019-06-20") }, new HistorySale() { DateSale = DateTime.Parse("2018-06-20") }, new HistorySale() { DateSale = DateTime.Parse("2019-06-20") } }; DbSet <HistorySale> dbSetHistorySale = GetQueryableMockDbSet <HistorySale>(list); mockContext.Setup(x => x.HistorySales).Returns(dbSetHistorySale); HistorySaleController controller = new HistorySaleController(mockContext.Object); var result = controller.Get(StartDT, EndDT, group); Assert.Equal(expCount, result.Count()); }
public IEnumerable <Sales> Get( DateTime?StartDateTime = null, DateTime?EndDateTime = null, DateGroupType group = DateGroupType.Quarter) { IEnumerable <Sales> Group; if (StartDateTime == null) { StartDateTime = DateTime.MinValue; } if (EndDateTime == null) { EndDateTime = DateTime.MaxValue; } switch (group) { case DateGroupType.Week: { Group = MathGroup.GetHistorySalesWeek(db.HistorySales.Where(x => x.DateSale >= StartDateTime).Where(x => x.DateSale <= EndDateTime).OrderBy(x => x.DateSale).ToList()); break; } case DateGroupType.Month: { Group = MathGroup.GetHistorySalesMonth(db.HistorySales.Where(x => x.DateSale >= StartDateTime).Where(x => x.DateSale <= EndDateTime).OrderBy(x => x.DateSale).ToList()); break; } case DateGroupType.Quarter: { Group = MathGroup.GetHistorySalesQuarter(db.HistorySales.Where(x => x.DateSale >= StartDateTime).Where(x => x.DateSale <= EndDateTime).OrderBy(x => x.DateSale).ToList()); break; } default: { Group = MathGroup.GetHistorySalesDay(db.HistorySales.Where(x => x.DateSale >= StartDateTime).Where(x => x.DateSale <= EndDateTime).OrderBy(x => x.DateSale).ToList()); break; } } return(Group); }
private DateTime GetDate(DateGroupType group, int diff, DateTime startDate) { switch (group) { case DateGroupType.Day: return(startDate.AddDays(diff)); case DateGroupType.Week: return(startDate.AddDays(diff * 7)); case DateGroupType.Month: return(startDate.AddMonths(diff)); case DateGroupType.Quarter: return(startDate.AddMonths(diff * 3)); case DateGroupType.Year: return(startDate.AddYears(diff)); default: throw new NotSupportedException($"Grouping by '{group}' is not supported"); } }
public static IEnumerable <Sales> GetSales(DateTime startDateTime, DateTime endDateTime, DateGroupType group, DBConnection.ApplicationContext db) { IEnumerable <Sales> GroupSales; switch (group) { case DateGroupType.Week: { GroupSales = MathGroup.GetHistorySalesWeek(db.HistorySales.Where(x => x.DateSale >= startDateTime).Where(x => x.DateSale <= endDateTime).OrderBy(x => x.DateSale).ToList()); break; } case DateGroupType.Month: { GroupSales = MathGroup.GetHistorySalesMonth(db.HistorySales.Where(x => x.DateSale >= startDateTime).Where(x => x.DateSale <= endDateTime).OrderBy(x => x.DateSale).ToList()); break; } case DateGroupType.Quarter: { GroupSales = MathGroup.GetHistorySalesQuarter(db.HistorySales.Where(x => x.DateSale >= startDateTime).Where(x => x.DateSale <= endDateTime).OrderBy(x => x.DateSale).ToList()); break; } default: { GroupSales = MathGroup.GetHistorySalesDay(db.HistorySales.Where(x => x.DateSale >= startDateTime).Where(x => x.DateSale <= endDateTime).OrderBy(x => x.DateSale).ToList()); break; } } return(GroupSales); }
private IQueryable <IGrouping <int?, SpendingEvent> > GroupSpendsByDate(IQueryable <SpendingEvent> products, DateGroupType group, DateTime startDate) { switch (group) { case DateGroupType.Day: return(products.GroupBy(x => SqlFunctions.DateDiff("dd", startDate, x.TimeStamp))); case DateGroupType.Week: return(products.GroupBy(x => SqlFunctions.DateDiff("ww", startDate, x.TimeStamp))); case DateGroupType.Month: return(products.GroupBy(x => SqlFunctions.DateDiff("mm", startDate, x.TimeStamp))); case DateGroupType.Quarter: return(products.GroupBy(x => SqlFunctions.DateDiff("qq", startDate, x.TimeStamp))); case DateGroupType.Year: return(products.GroupBy(x => SqlFunctions.DateDiff("yy", startDate, x.TimeStamp))); default: throw new NotSupportedException($"Grouping by '{group}' is not supported"); } }
public async Task <JsonResult> GetPostStats() { var endDate = DateTime.UtcNow; // The starting point for statistics var startDate = endDate.AddDays(-1 * 31); // The binning DateGroupType bin = DateGroupType.Day; using (var db = new ZapContext()) { var allPosts = db .Posts .Where(x => x.TimeStamp > startDate && x.TimeStamp <= endDate); var allComments = db .Comments .Where(x => x.TimeStamp > startDate && x.TimeStamp <= endDate); var allSpends = db.SpendingEvents.Where(x => x.TimeStamp > startDate && x.TimeStamp <= endDate); var binnedPostStats = GroupPostsByDate(allPosts, bin, startDate); var binnedCommentStats = GroupCommentsByDate(allComments, bin, startDate); var binnedSpendingStats = GroupSpendsByDate(allSpends, bin, startDate); DateTime epochUTC = new DateTime(1970, 1, 1, 0, 0, 0, kind: DateTimeKind.Utc); var postStats = binnedPostStats.Select(x => new { x.Key, Count = x.Count() }).ToList() .Select(x => new Stat { //TimeStamp = GetDate(bin, x.Key.Value, startDate), TimeStampUtc = Convert.ToInt64((GetDate(bin, x.Key.Value, startDate) - epochUTC).TotalMilliseconds), Count = x.Count }) .OrderBy(x => x.TimeStampUtc) .ToList(); var commentStats = binnedCommentStats.Select(x => new { x.Key, Count = x.Count() }).ToList() .Select(x => new Stat { //TimeStamp = GetDate(bin, x.Key.Value, startDate), TimeStampUtc = Convert.ToInt64((GetDate(bin, x.Key.Value, startDate) - epochUTC).TotalMilliseconds), Count = x.Count }) .OrderBy(x => x.TimeStampUtc) .ToList(); var spendingStats = binnedSpendingStats.Select(x => new { x.Key, //Count = x.Count() Sum = x.Sum(y => y.Amount) }).ToList() .Select(x => new Stat { //TimeStamp = GetDate(bin, x.Key.Value, startDate), TimeStampUtc = Convert.ToInt64((GetDate(bin, x.Key.Value, startDate) - epochUTC).TotalMilliseconds), Count = Convert.ToInt32(x.Sum) }) .OrderBy(x => x.TimeStampUtc) .ToList(); var maxPosts = postStats.Max(x => x.Count); var maxComments = commentStats.Max(x => x.Count); var maxPostComments = maxPosts > maxComments ? maxPosts : maxComments; var maxSpent = spendingStats.Max(x => x.Count); return(Json(new { postStats, commentStats, spendingStats, maxPostComments, maxSpent }, JsonRequestBehavior.AllowGet)); } }