private ComputerReport CreateComputerInLabReport(DateTime startDate, DateTime endDate, DateTime startHour, DateTime endHour, ComputerLab comp, bool weekends) { TimeSpan computerTotalActiveTime = TimeSpan.Zero; TimeSpan computerTotalActiveTimeWithClasses = TimeSpan.Zero; DateTime compEnterence = comp.Entrance; DateTime compExit = DateTime.Now; if (comp.Exit.HasValue) { compExit = comp.Exit.Value; } DateTime newStartDate = new DateTime(Math.Max(compEnterence.Ticks, startDate.Ticks)); DateTime newEndDate = new DateTime(Math.Min(compExit.Ticks, endDate.Ticks)); List <Activity> compAct; try { compAct = comp.Computer.Activities.Where(e => (e.Login >= newStartDate && (e.Logout <= endDate || !e.Logout.HasValue)) && //activities in the report time range (e.Mode.Equals(ActivityType.User) || e.Mode.Equals(ActivityType.Class)) && // user or class activity !((e.Login.TimeOfDay >= endHour.TimeOfDay) || (e.Logout.HasValue && e.Logout.Value.TimeOfDay <= startHour.TimeOfDay))).ToList(); //hour range; } catch { compAct = _lController.GetComputerActivitiesInDateRange(comp.ComputerId, newStartDate, newEndDate, startHour, endHour); } if (!weekends) { compAct = compAct.Where(e => !(e.Weekend)).ToList(); } compAct = compAct.OrderBy(e => e.Login).ToList(); DateTime timePointWithClasses = startDate; DateTime timePoint = startDate; foreach (var act in compAct) { DateTime endOfActivity = DateTime.Now; if (act.Logout.HasValue) { endOfActivity = act.Logout.Value; } DateTime startOfTimeReport = new DateTime(Math.Max(act.Login.Ticks, act.Login.Date.Add(startHour.TimeOfDay).Ticks)); DateTime enfOfTimeReport = new DateTime(Math.Min(endOfActivity.Ticks, endOfActivity.Date.Add(endHour.TimeOfDay).Ticks)); //if its user activity add it the activity-time-no-classes if (act.Mode.Equals(ActivityType.User)) { TimeSpan timeToAdd = enfOfTimeReport - startOfTimeReport; computerTotalActiveTime = computerTotalActiveTime.Add(timeToAdd); timePoint = enfOfTimeReport; } //the time point is before the end of the activity there is time to add if (enfOfTimeReport > timePointWithClasses) { // strart from the latest point out of the two (start point and time point) startOfTimeReport = new DateTime(Math.Max(startOfTimeReport.Ticks, timePointWithClasses.Ticks)); TimeSpan timeToAdd = enfOfTimeReport - startOfTimeReport; //add to activity-time-with-classes (for both user and cass activity) computerTotalActiveTimeWithClasses = computerTotalActiveTimeWithClasses.Add(timeToAdd); timePointWithClasses = enfOfTimeReport; } } Computer computer = comp.Computer; if (computer == null) { computer = _lController.GetComputer(comp.ComputerId); } // number of hours the computer was in the lab (during the report duration) double computerInLabTime = CalculateHoursInReportForComputer(newStartDate, newEndDate, startHour, endHour, weekends); ComputerReport cR = new ComputerReport(computer, computerTotalActiveTime, computerTotalActiveTimeWithClasses, computerInLabTime); return(cR); }
private List <Notification> GetNotifications() { List <Notification> notifications = new List <Notification>(); List <Department> departments = _lController.GetUserViewDepartments(User.UserId); foreach (var department in departments) { foreach (var lab in department.Labs) { List <Computer> comps; try { comps = lab.Computers.ToList(); } catch { comps = _lController.GetLabComputers(lab.LabId); } foreach (var comp in comps) { //check for disconected notification if (User.DisconnectedPeriod != null) { int days = User.DisconnectedPeriod.Value; DateTime disconectedFrom = DateTime.Now.Date.AddDays(-days); List <Activity> offAct; try { offAct = comp.Activities.Where(e => !e.Logout.HasValue && e.Mode == ActivityType.Off && e.Login <= disconectedFrom).ToList(); } catch { offAct = _lController.GetCurrentDisconnectedActivity(comp.ComputerId, disconectedFrom); } if (offAct.Count > 0) { //for how long the computer is disconnected days = (int)(DateTime.Now.Date - offAct[0].Login.Date).TotalDays; Notification ntf = new Notification(comp, lab, department.DepartmentName, Constant.NotificationType.Disconnected, days); notifications.Add(ntf); continue; } } //check for unused notification if (User.NotActivePeriod != null) { Activity userAct; try { userAct = comp.Activities.Where(e => !e.Logout.HasValue && e.Mode == ActivityType.User).ToList().FirstOrDefault(); } catch { userAct = _lController.GetUserActivity(comp.ComputerId); } //if there is no user connected rigth now if (userAct == null) { //find the last time a user loged out DateTime?lastLogout; try { lastLogout = comp.Activities.Where(e => e.Mode == ActivityType.User).Max(e => e.Logout); } catch { lastLogout = _lController.GetLastLogOutTime(comp.ComputerId); } if (lastLogout == null) { ComputerLab cl = comp.ComputerLabs.Where(e => e.LabId == lab.LabId && !e.Exit.HasValue).ToList().FirstOrDefault(); if (cl != null) { //time from computer enterance to lab int days = (int)(DateTime.Now.Date - cl.Entrance.Date).TotalDays; //if the days passed from the computer enterance bigger than the given days- make notification if (days >= User.NotActivePeriod.Value) { Notification ntf = new Notification(comp, lab, department.DepartmentName, Constant.NotificationType.NotUsed, days); notifications.Add(ntf); } } } else if (lastLogout.HasValue) { //time from last logout int days = (int)(DateTime.Now.Date - lastLogout.Value.Date).TotalDays; //if the days passed from the last user activity bigger than the given days- make notification if (days >= User.NotActivePeriod.Value) { Notification ntf = new Notification(comp, lab, department.DepartmentName, Constant.NotificationType.NotUsed, days); notifications.Add(ntf); } } } } } } } return(notifications); }