Last active
December 31, 2015 23:19
-
-
Save alexdresko/8059762 to your computer and use it in GitHub Desktop.
In response to http://codereview.stackexchange.com/questions/37823/retrieving-statistics-about-url-clicks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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