How To Store Information About The Sessions in Silverlight Applications For The Analysis – Part 3

Please begin by reading Part 1

and Part 2.

Next, implement WCF service to get analysis results and display them on UI. It may look like the image below:


The WCF service may look like:

[ServiceContract]
public interface ISessionStatisticsService
{

[OperationContract]

SessionStatisticsResponse GetStatistics();

}

public class SessionStatisticsService : ISessionStatisticsService
{

public SessionStatisticsResponse GetStatistics()

{

DateTime todayDate = DateTime.Now.Date;

DateTime tomorrowDate = DateTime.Now.Date.AddDays(1);

DateTime yesterdayDate = DateTime.Now.Date.AddDays(-1);

DateTime last7DaysDate = DateTime.Now.Date.AddDays(-7);

DateTime last30DaysDate = DateTime.Now.Date.AddDays(-30);

SessionStatisticsResponse result = new SessionStatisticsResponse();

result.SessionsOnline = database.SessionStatistics.Where(x => x.Ended == null).Count();

result.SessionsTotal = database.SessionStatistics.Count();

result.SessionsToday = database.SessionStatistics.Where(x => x.Started >= todayDate && x.Started < tomorrowDate).Count();

result.SessionsYesterday = database.SessionStatistics.Where(x => x.Started >= yesterdayDate && x.Started < todayDate).Count();

result.SessionsLast7Days = database.SessionStatistics.Where(x => x.Started >= last7DaysDate && x.Started < todayDate).Count();

result.SessionsLast30Days = database.SessionStatistics.Where(x => x.Started >=last30DaysDate && x.Started < todayDate).Count();

result.AvgSessionsTimeTotal = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null).Average(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.AvgSessionsTimeToday = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= todayDate && x.Started < tomorrowDate).Average(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.AvgSessionsTimeYesterday = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= yesterdayDate && x.Started < todayDate).Average(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.AvgSessionsTimeLast7Days = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= last7DaysDate && x.Started < todayDate).Average(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.AvgSessionsTimeLast30Days = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= last30DaysDate && x.Started < todayDate).Average(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.MaxSessionsTimeTotal = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null).Max(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.MaxSessionsTimeToday = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= todayDate && x.Started < tomorrowDate).Max(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.MaxSessionsTimeYesterday = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= yesterdayDate && x.Started < todayDate).Max(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.MaxSessionsTimeLast7Days = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= last7DaysDate && x.Started < todayDate).Max(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.MaxSessionsTimeLast30Days = Convert.ToInt32(database.SessionStatistics.Where(x => x.Ended != null && x.Started >= last30DaysDate && x.Started < todayDate).Max(x => SqlFunctions.DateDiff(“mi”, x.Started, x.Ended)));

result.GeoStatisticsTotal = database.SessionStatistics.GroupBy(x => x.Country).Select(group => new SessionGeoStatisticsResponse() { Country = group.Key, SessionsCount = group.Count() }).OrderByDescending(x => x.SessionsCount).ToArray();

result.GeoStatisticsToday = database.SessionStatistics.Where(x => x.Started >= todayDate && x.Started < tomorrowDate).GroupBy(x => x.Country).Select(group => new SessionGeoStatisticsResponse() { Country = group.Key, SessionsCount = group.Count() }).OrderByDescending(x => x.SessionsCount).ToArray();

result.GeoStatisticsYesterday = database.SessionStatistics.Where(x => x.Started >= yesterdayDate && x.Started < todayDate).GroupBy(x => x.Country).Select(group => new SessionGeoStatisticsResponse() { Country = group.Key, SessionsCount = group.Count() }).OrderByDescending(x => x.SessionsCount).ToArray();

result.GeoStatisticsLast7Days = database.SessionStatistics.Where(x => x.Started >= last7DaysDate && x.Started < todayDate).GroupBy(x => x.Country).Select(group => new SessionGeoStatisticsResponse() { Country = group.Key, SessionsCount = group.Count() }).OrderByDescending(x => x.SessionsCount).ToArray();

result.GeoStatisticsLast30Days = database.SessionStatistics.Where(x => x.Started >= last30DaysDate && x.Started < todayDate).GroupBy(x => x.Country).Select(group => new SessionGeoStatisticsResponse() { Country = group.Key, SessionsCount = group.Count() }).OrderByDescending(x => x.SessionsCount).ToArray();

return result;

}

}

And that’s all! You now are able to store information about the sessions in Silerlight Applications for analysis!

Looking for quality Silverlight Hosting

? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under 3rd Party Software, ASP .NET 3.5, Programming/Coding | RSS 2.0

Leave a Reply

Your email address will not be published. Required fields are marked *