public LabReport CreateLabReport(DateTime startDate, DateTime endDate, DateTime startHour, DateTime endHour, Lab lab, bool weekends) { LabReport labReport = new LabReport(lab); List <Task> tasks = new List <Task>(); List <ComputerLab> cL = getComputerInLab(lab, startDate, endDate); foreach (var item in cL) { // Task t = Task.Factory.StartNew(() => // { ComputerReport cR = CreateComputerInLabReport(startDate, endDate, startHour, endHour, item, weekends); //add data to labreport lock (reportLock) { labReport.ComputersReport.Add(cR); labReport.AddToLabTotalActivityTime(cR.GetComputerTotalActiveTime()); labReport.AddToLabTotalActivityTimeWithClasses(cR.GetComputerTotalActiveTimeWithClasses()); labReport.AddToLabTotalHours(cR.GetComputerTotalTime()); } // }); // tasks.Add(t); } Task.WaitAll(tasks.ToArray()); return(labReport); }
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); }