Skip to content

Instantly share code, notes, and snippets.

@alexdresko
Last active December 31, 2015 23:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexdresko/8059762 to your computer and use it in GitHub Desktop.
Save alexdresko/8059762 to your computer and use it in GitHub Desktop.
// To use, just do something like :
// ViewBag.Stats = GetClicks(context);
private static List<Stats> GetClicks(AppContext context)
{
var result = new List<Stats>();
results.Add(GetStats(context, Period.Week))
results.Add(GetStats(context, Period.Month))
results.Add(GetStats(context, Period.Today))
return results;;
}
private static Stats GetStats(AppContext context, Period period)
{
var stats = new Stats();
switch (period)
{
var query = context.EmailLinkClicks.AsQueryable();
case Period.Today:
query = query.Where(c => c.CreatedOn == DateTime.Today);
break;
case Period.Week:
query = query.Where(c => c.CreatedOn > System.Data.Entity.DbFunctions.AddDays(c.CreatedOn, -7));
break;
case Period.Month:
query = query.Where(c => c.CreatedOn.Month == DateTime.Today.Month);
break;
}
stats.Description = period.ToString();
var ipAddresses = query.Select(c => c.IpAddress);
stats.Total = query.Count();
stats.Unique = query.Distinct().Count();
return stats;
}
class Stats
{
public int Total { get; set; }
public int Unique { get; set; }
public string Description { get; set; }
}
enum Period
{
Today,
Week,
Month
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment