public static void AverageTime(ref AnalyticViewModel avm, List <Analytic> analytics) { double totalTimeWatched = 0.0; double totalTime = 0.0; int count = 0; foreach (var record in analytics) { if (record.Start != null && record.Stop != null) { if (record.Stop != 0 && record.Stop != 1) { totalTimeWatched += (record.Stop - record.Start); count++; } } if (record.Duration != null) { if (record.Duration != 0) { totalTime += record.Duration; } } } avm.AverageMinWatched = Math.Round(((totalTimeWatched / count) / 60), 2); avm.AverageDuration = Math.Round(((totalTimeWatched / totalTime) * 100), 2); avm.TotalMinWatched = Math.Round((totalTimeWatched / 60), 2); totalTime /= count; avm.TotalAverageMin = (int)Math.Round(totalTime / 60); avm.MajorTick = (int)(avm.TotalAverageMin / 10); avm.MinorTick = (int)((avm.TotalAverageMin / 10) / 2); }
public static void PageViewsForService(ref AnalyticViewModel avm, List <Analytic> analytics) { DateTime today = DateTime.Now; int totalViews = analytics.Count(); int todayViews = analytics.Where(a => a.CreateDate > today.AddDays(-1) && a.CreateDate < today).Count(); int weekViews = analytics.Where(a => a.CreateDate > today.AddDays(-7) && a.CreateDate < today).Count(); int monthViews = analytics.Where(a => a.CreateDate > today.AddMonths(-1) && a.CreateDate < today).Count(); avm.TodayViews = todayViews; avm.WeekViews = weekViews; avm.MonthViews = monthViews; }
public static void linechart(ref AnalyticViewModel avm, List <Analytic> analytics, bool pad = false) { List <DateTime> dayTimeRange = new List <DateTime>(); List <double> dayViews = new List <double>(); List <DateTime> monthTimeRange = new List <DateTime>(); List <double> monthViews = new List <double>(); List <DateTime> yearTimeRange = new List <DateTime>(); List <double> yearViews = new List <double>(); // figure the time range for days DateTime day = DateTime.Now; StringBuilder dayTimeRangeString = new StringBuilder(); string quote = "\""; dayTimeRangeString.Append("["); for (int i = 0; i < 10; i++) { dayTimeRange.Add(day); dayTimeRangeString.Append("new Date(" + quote + day.ToShortDateString() + quote + "),"); day = day.AddDays(-1); } string dayTimeRangeArray = dayTimeRangeString.ToString(); dayTimeRangeArray = dayTimeRangeArray.Substring(0, dayTimeRangeArray.Length - 1) + "]"; // ************* end the time range for DAYS *************// //figure out the views for the day range double todayViews = 0; StringBuilder dayViewString = new StringBuilder(); dayViewString.Append("["); foreach (var d in dayTimeRange) { todayViews = analytics.Where(a => a.CreateDate > d.AddDays(-1) && a.CreateDate < d).Count(); if (pad == true) { todayViews = todayViews * 1.5; todayViews = Math.Round(todayViews); } dayViewString.Append(todayViews.ToString() + ","); dayViews.Add(todayViews); } string dayViewsArray = dayViewString.ToString(); dayViewsArray = dayViewsArray.Substring(0, dayViewsArray.Length - 1) + "]"; // *************end the views for DAYS************* // // figure the time range for months DateTime month = DateTime.Now; StringBuilder monthTimeRangeString = new StringBuilder(); monthTimeRangeString.Append("["); for (int i = 0; i < 10; i++) { monthTimeRange.Add(month); monthTimeRangeString.Append("new Date(" + quote + month.ToShortDateString() + quote + "),"); month = month.AddMonths(-1); } string monthTimeRangeArray = monthTimeRangeString.ToString(); monthTimeRangeArray = monthTimeRangeArray.Substring(0, monthTimeRangeArray.Length - 1) + "]"; // *************end the time range for MONTHS************* // //figure out the views for the month range double mViews = 0; StringBuilder monthViewString = new StringBuilder(); monthViewString.Append("["); foreach (var d in monthTimeRange) { mViews = analytics.Where(a => a.CreateDate > d.AddMonths(-1) && a.CreateDate < d).Count(); if (pad == true) { mViews = mViews * 1.5; mViews = Math.Round(mViews); } monthViewString.Append(mViews.ToString() + ","); monthViews.Add(mViews); } string monthViewsArray = monthViewString.ToString(); monthViewsArray = monthViewsArray.Substring(0, monthViewsArray.Length - 1) + "]"; // *************end the views for month************* // // figure the time range for year DateTime year = DateTime.Now; StringBuilder yearTimeRangeString = new StringBuilder(); yearTimeRangeString.Append("["); for (int i = 0; i < 10; i++) { yearTimeRange.Add(year); yearTimeRangeString.Append("new Date(" + quote + year.ToShortDateString() + quote + "),"); year = year.AddYears(-1); } string yearTimeRangeArray = yearTimeRangeString.ToString(); yearTimeRangeArray = yearTimeRangeArray.Substring(0, yearTimeRangeArray.Length - 1) + "]"; // *************end the time range for year************* // //figure out the views for the year range double yViews = 0; StringBuilder yearViewString = new StringBuilder(); yearViewString.Append("["); foreach (var d in yearTimeRange) { yViews = analytics.Where(a => a.CreateDate > d.AddYears(-1) && a.CreateDate < d).Count(); if (pad == true) { yViews = yViews * 1.5; yViews = Math.Round(yViews); } yearViewString.Append(yViews.ToString() + ","); yearViews.Add(yViews); } string yearViewsArray = yearViewString.ToString(); yearViewsArray = yearViewsArray.Substring(0, yearViewsArray.Length - 1) + "]"; // *************end the views for year************* // avm.MonthTimeRange = monthTimeRangeArray; avm.MonthViewsArray = monthViewsArray; avm.DayTimeRange = dayTimeRangeArray; avm.DayViewsArray = dayViewsArray; avm.YearTimeRange = yearTimeRangeArray; avm.YearViewsArray = yearViewsArray; }
public static void PageViews(ref AnalyticViewModel avm, List <Analytic> analytics, List <FuneralHome> homes) { List <Service> services = new List <Service>(); //List<string> owernames = new List<string>(); foreach (var home in homes) { foreach (var s in home.Services.ToList()) { services.Add(s); //owernames.Add(s.FuneralHome.Owner.Name); } } DateTime today = DateTime.Now; int totalViews = analytics.Count(); int todayViews = analytics.Where(a => a.CreateDate > today.AddDays(-1) && a.CreateDate < today).Count(); int weekViews = analytics.Where(a => a.CreateDate > today.AddDays(-7) && a.CreateDate < today).Count(); int monthViews = analytics.Where(a => a.CreateDate > today.AddMonths(-1) && a.CreateDate < today).Count(); int VideoCount = 0; int PDFCount = 0; int VideoAndPdf = 0; int totalServices = 0; foreach (var s in services) { if (s.FuneralHome.Owner != null && s.FuneralHome.Owner.Name != "DevHome") { totalServices++; if (s.Video != null) { VideoCount++; } if (s.PDF != null) { PDFCount++; } if (s.Video != null && s.PDF != null) { VideoAndPdf++; } } } double percentWithVideos = Math.Round(((double)VideoCount / (double)totalServices) * 100, 0); double percentWithPdf = Math.Round(((double)PDFCount / (double)totalServices) * 100, 0); double percentWithBoth = Math.Round(((double)VideoAndPdf / (double)totalServices) * 100, 0); avm.TotalVideoViews = totalViews; avm.TotalFuneralVideos = VideoCount; avm.TotalFuneralPDFs = PDFCount; avm.FuneralsWithVideos = percentWithVideos; avm.FuneralsWithPDFs = percentWithPdf; avm.FuneralsWithBoth = percentWithBoth; avm.TodayViews = todayViews; avm.WeekViews = weekViews; avm.MonthViews = monthViews; }
public async Task <ActionResult> Dashboard(int?OwnerId, int?HomeId, int?ServiceId) { if (!User.IsInRole("Admin")) { return(View("NotFound")); } List <FuneralHome> Homes = db.FuneralHomes.Where(f => f.DevHome == false).ToList(); List <Analytic> Analytics = new List <Analytic>(); AnalyticViewModel AVM = new AnalyticViewModel(); AnalyticDashboardViewModel ADVM = new AnalyticDashboardViewModel(); List <Service> Services = new List <Service>(); ADVM.TotalVideoViews = db.Analytics.Count(); DateTime OneMonth = DateTime.Now.AddDays(-30); ADVM.MonthViews = db.Analytics.Where(a => a.CreateDate > OneMonth).Count(); if (OwnerId == null && HomeId == null && ServiceId == null) { // Analytics = db.Analytics.Where(a=>a.Video.Service.FuneralHome.DevHome==false).ToList(); Services = db.Services.Where(s => s.FuneralHome.DevHome == false).ToList(); } else { if (OwnerId != null) { // Analytics = db.Analytics.Where(a=>a.Video.Service.FuneralHome.OwnerId==OwnerId).ToList(); Services = db.Services.Where(a => a.FuneralHome.OwnerId == OwnerId).ToList(); } else { if (HomeId != null) { // Analytics = db.Analytics.Where(a => a.Video.Service.FuneralHome.Id == HomeId).ToList(); Services = db.Services.Where(a => a.FuneralHome.Id == HomeId).ToList(); } else { // Analytics = db.Analytics.Where(a => a.Video.ServiceId == ServiceId).ToList(); } } } //List<IpAddressObject> addresses = new List<IpAddressObject>(); //foreach (var an in Analytics) //{ // if (an.Latitude != 0 && an.Longitude != 0) // { // //Database got mucked up with a bunch of hard coded values // //So lets exclude those IP addresses from the heat map // if (an.IPAddress != "174.17.56.90") // { // IpAddressObject add = new IpAddressObject(); // add.IpAdd = an.IPAddress; // add.latitude = an.Latitude; // add.longitude = an.Longitude; // addresses.Add(add); // } // } //} DateTime LastWeek = DateTime.Now.AddDays(-7); List <Analytic> thisWeekAnalytics = Analytics.Where(a => a.CreateDate > LastWeek).ToList(); string mostViewedHome = ""; int HighestVIews = 0; int popularhomeId = 0; foreach (var home in Homes) { int homeViews = thisWeekAnalytics.Where(a => a.Video.Service.FuneralHome.Id == home.Id).Count(); if (homeViews > HighestVIews) { mostViewedHome = home.Name; HighestVIews = homeViews; popularhomeId = home.Id; } } //CRMFuneralHome popFH = db.CRMFuneralHome.Where(f => f.FuneralHomeId == popularhomeId).FirstOrDefault(); //CRMContact PopularHomePicCont = null; //if(popFH !=null) //{ // PopularHomePicCont = popFH.PrimaryCRMContact; //} // if(PopularHomePicCont!=null) // { // ADVM.PopularHomeOfTheWeekImage = PopularHomePicCont.PictureFileName; // } // ADVM.PopularHomeOfTheWeek = mostViewedHome; //ADVM.PopularHomeNumOfViews = HighestVIews; //ADVM.ServicesBarChartArray = Admin.GetServicesBarChart(Services, DateTime.Now.AddMonths(-3), DateTime.Now); //AVM.Details = addresses; //CalculateAnalytics.PageViews(ref ADVM, Analytics, Homes); // CalculateAnalytics.AverageTime(ref AVM, Analytics); //ADVM.NeedsToBeContacted = Admin.GetContactsThatNeedToBeContacted(db); //Admin.dashboardlinechart(ref ADVM, Analytics); //CalculateAnalytics.linechart(ref AVM, Analytics); ADVM.TotalFuneralsWithVideos = Admin.GetTotalServicesWithVideo(Services); ADVM.MonthlyFuneralsWithVideos = Admin.GetServicesThisMonthWithVideo(Services); ADVM.LowUsageHomes = CalculateAnalytics.LowUsageHomes(Homes); return(View(ADVM)); }
// GET: Analytics // id is for service public async Task <ActionResult> Index(int?id) { //if (!User.IsInRole("Admin")) //{ // return View("NotFound"); //} List <FuneralHome> homes = new List <FuneralHome>(); List <Analytic> Analytics = new List <Analytic>(); AnalyticViewModel AVM = new AnalyticViewModel(); if (id == null) { if (User.IsInRole("FuneralHome")) { var userId = User.Identity.GetUserId(); FuneralHome fh = db.FuneralHomes.Where(u => u.UserId == userId).FirstOrDefault(); Analytics.AddRange(db.Analytics.Where(a => a.Video.Service.FuneralHome.Id == fh.Id).ToList()); homes.Add(fh); CalculateAnalytics.PageViews(ref AVM, Analytics, homes); } else { //homes.AddRange(db.FuneralHomes.Where(f => f.DevHome == false).ToList()); //Analytics.AddRange(db.Analytics.ToList()); } } else { Service serv = db.Services.Where(s => s.Id == id).FirstOrDefault(); if (!Authorize(serv)) { return(View("NotFound")); } AVM.FirstName = serv.FirstName; AVM.LastName = serv.LastName; homes.Add(db.FuneralHomes.Where(f => f.Id == serv.FuneralHome.Id).FirstOrDefault()); Analytics.AddRange(db.Analytics.Where(a => a.Video.Service.Id == serv.Id).ToList()); CalculateAnalytics.PageViewsForService(ref AVM, Analytics); AVM.TotalVideoViews = Analytics.Count(); } List <IpAddressObject> addresses = new List <IpAddressObject>(); foreach (var an in Analytics) { if (an.Video != null) { if (an.Latitude != 0 && an.Longitude != 0) { //Database got mucked up with a bunch of hard coded values //So lets exclude those IP addresses from the heat map if (an.IPAddress != "174.17.56.90") { IpAddressObject add = new IpAddressObject(); add.IpAdd = an.IPAddress; add.latitude = an.Latitude; add.longitude = an.Longitude; if (an.Video != null && an.Video.Service != null && an.Video.Service.VideoTitle == null) { add.VideoTitle = an.Video.Service.FirstName + " " + an.Video.Service.LastName + "'s Service"; } else { add.VideoTitle = an.Video.Service.VideoTitle; } add.ViewingDate = an.CreateDate.ToShortDateString(); add.Name = an.Video.Service.FirstName + " " + an.Video.Service.LastName; int age = an.Video.Service.DeathDay.Year - an.Video.Service.Birthday.Year; add.Age = age.ToString(); add.FuneralHomeName = an.Video.Service.FuneralHome.Name; add.ServiceDate = an.Video.Service.ServiceDate.ToShortDateString(); add.DaysAfterService = (an.CreateDate - an.Video.Service.ServiceDate).Days.ToString(); add.city = an.City; if (an.Stop < 280) { add.AmountWatched = "NA"; } else { add.AmountWatched = Math.Round(((double)an.Stop / 60), 0).ToString(); } addresses.Add(add); } } } } AVM.Details = addresses; CalculateAnalytics.AverageTime(ref AVM, Analytics); CalculateAnalytics.linechart(ref AVM, Analytics, false); return(View(AVM)); }
public ActionResult Dashboard() { if (!User.IsInRole("Admin")) { return(View("NotFound")); } //TODO: write logic to derive the owner id based on user that is logged in int ownerId = 2; List <FuneralHome> Homes = db.FuneralHomes.Where(f => f.DevHome == false && f.OwnerId == ownerId).ToList(); List <Analytic> Analytics = new List <Analytic>(); AnalyticViewModel AVM = new AnalyticViewModel(); AnalyticDashboardViewModel ADVM = new AnalyticDashboardViewModel(); List <Service> Services = new List <Service>(); Analytics = db.Analytics.Where(a => a.Video.Service.FuneralHome.DevHome == false && a.Video.Service.FuneralHome.OwnerId == ownerId).ToList(); Services = db.Services.Where(s => s.FuneralHome.DevHome == false && s.FuneralHome.OwnerId == ownerId).ToList(); List <IpAddressObject> addresses = new List <IpAddressObject>(); foreach (var an in Analytics) { if (an.Latitude != 0 && an.Longitude != 0) { //Database got mucked up with a bunch of hard coded values //So lets exclude those IP addresses from the heat map if (an.IPAddress != "174.17.56.90") { IpAddressObject add = new IpAddressObject(); add.IpAdd = an.IPAddress; add.latitude = an.Latitude; add.longitude = an.Longitude; addresses.Add(add); } } } DateTime LastWeek = DateTime.Now.AddDays(-7); List <Analytic> thisWeekAnalytics = Analytics.Where(a => a.CreateDate > LastWeek).ToList(); string mostViewedHome = ""; int HighestVIews = 0; int popularhomeId = 0; foreach (var home in Homes) { int homeViews = thisWeekAnalytics.Where(a => a.Video.Service.FuneralHome.Id == home.Id).Count(); if (homeViews > HighestVIews) { mostViewedHome = home.Name; HighestVIews = homeViews; popularhomeId = home.Id; } } CRMFuneralHome popFH = db.CRMFuneralHome.Where(f => f.FuneralHomeId == popularhomeId).FirstOrDefault(); CRMContact PopularHomePicCont = null; if (popFH != null) { PopularHomePicCont = popFH.PrimaryCRMContact; } if (PopularHomePicCont != null) { ADVM.PopularHomeOfTheWeekImage = PopularHomePicCont.PictureFileName; } ADVM.PopularHomeOfTheWeek = mostViewedHome; ADVM.PopularHomeNumOfViews = HighestVIews; ADVM.ServicesBarChartArray = Admin.GetServicesBarChart(Services, DateTime.Now.AddMonths(-3), DateTime.Now); AVM.Details = addresses; CalculateAnalytics.PageViews(ref ADVM, Analytics, Homes); CalculateAnalytics.AverageTime(ref AVM, Analytics); Admin.dashboardlinechart(ref ADVM, Analytics); CalculateAnalytics.linechart(ref AVM, Analytics); ADVM.TotalFuneralsWithVideos = Admin.GetTotalServicesWithVideo(Services); ADVM.MonthlyFuneralsWithVideos = Admin.GetServicesThisMonthWithVideo(Services); ADVM.LowUsageHomes = CalculateAnalytics.LowUsageHomes(Homes); return(View(ADVM)); }