// Get average time watched per day in hours public double GetAverageDailyWatchTime() { var dates = HistoryVideos.Select(v => v.GetTime().Date).Distinct().ToList(); var dailyTimesMinutes = dates.Select(d => HistoryVideos.Where(h => h.GetTime().Date.Equals(d.Date)).Sum(v => VideoViewModelsDict[v.GetVideoID()].GetDuration().TotalMinutes)); double averageTimeMinutes = dailyTimesMinutes.Average(); double averageTimeHours = Math.Round(averageTimeMinutes / 60); return(averageTimeHours); }
// Get month with the most time watched public List <TimeWatchedPerMonthViewModel> GetTimeWatchedPerMonth() { List <MonthYearViewModel> monthViewModels = HistoryVideos.Select(v => new MonthYearViewModel(v.GetTime().Month, v.GetTime().Year)).GroupBy(vm => vm.Month + " " + vm.Year).Select(x => x.FirstOrDefault()).ToList(); List <double> monthlyHours = monthViewModels.Select(vm => HistoryVideos.Where(h => new MonthYearViewModel(h.GetTime().Month, h.GetTime().Year) == vm).Sum(v => VideoViewModelsDict[v.GetVideoID()].GetDuration().TotalHours)).ToList(); List <TimeWatchedPerMonthViewModel> viewModels = new List <TimeWatchedPerMonthViewModel>(); for (int i = 0; i < monthViewModels.Count; i++) { viewModels.Add(new TimeWatchedPerMonthViewModel(monthViewModels[i], Math.Round(monthlyHours[i]))); } return(viewModels.OrderByDescending(vm => vm.HoursWatched).ToList()); }
// Calculate how many hours the user watched per day of their history public List <HoursPerDayViewModel> GetHoursPerDay() { var dates = HistoryVideos.Select(v => v.GetTime().Date).Distinct().ToList(); List <TimeSpan> timeWatched = new List <TimeSpan>(); foreach (DateTime date in dates) { var videosOnDay = HistoryVideos.Where(h => h.GetTime().Date == date.Date).ToList(); TimeSpan timeForDay = new TimeSpan(0, 0, 0); foreach (HistoryVideo video in videosOnDay) { VideoViewModel viewModel = VideoViewModelsDict[video.GetVideoID()]; timeForDay = timeForDay.Add(viewModel.GetDuration()); } timeWatched.Add(timeForDay); } List <HoursPerDayViewModel> hoursPerDayViewModels = new List <HoursPerDayViewModel>(); for (int i = 0; i < timeWatched.Count; i++) { hoursPerDayViewModels.Add(new HoursPerDayViewModel() { Date = dates[i], HoursWatched = timeWatched[i] }); } //using (StreamWriter sw = new StreamWriter(UriHelper.CALCULATIONS_OUTPUT_DIR + "/Hours Per Day - " + DateTime.Now.ToFileTime() + ".csv")) //{ // foreach (var viewModel in hoursPerDayViewModels) // { // sw.WriteLine(viewModel.Date.ToShortDateString() + ";" + viewModel.HoursWatched.Hours + " Hours, " + viewModel.HoursWatched.Minutes + " Minutes and " + viewModel.HoursWatched.Seconds + " Seconds"); // } //} return(hoursPerDayViewModels); }
// Remove all videos that don't have data in the dictionary private void CleanseHistoryVideos() { HistoryVideos = HistoryVideos.Where(v => VideoViewModelsDict.ContainsKey(v.GetVideoID())).ToList(); }