public List <LabOccupancyReport> CreateOccupancyLabReport(DateTime startDate, DateTime endDate, DateTime startHour, DateTime endHour, List <int> labsIds, bool weekends) { List <LabOccupancyReport> reports = new List <LabOccupancyReport>(); List <Task> tasks = new List <Task>(); endDate = endDate.AddDays(1); foreach (var id in labsIds) { Task t = Task.Factory.StartNew(() => { LabOccupancyReport labReport = CreateOccupancyLabReport(startDate, endDate, startHour, endHour, id, weekends); reports.Add(labReport); }); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); return(reports); }
public LabOccupancyReport CreateOccupancyLabReport(DateTime startDate, DateTime endDate, DateTime startHour, DateTime endHour, Lab lab, bool weekends) { LabOccupancyReport labReport = new LabOccupancyReport(lab); TimeSpan labTotalActiveTime = TimeSpan.Zero; List <ComputerLab> cL = getComputerInLab(lab, startDate, endDate); List <int> hoursToCheck = new List <int>(); Dictionary <int, LabHourOccupancyReport> hourReports = new Dictionary <int, LabHourOccupancyReport>(); for (int start = startHour.Hour; start < endHour.Hour; start++) { hoursToCheck.Add(start); hourReports.Add(start, new LabHourOccupancyReport(start)); } Dictionary <DayOfWeek, LabDayOfWeekReport> weekDayReports = new Dictionary <DayOfWeek, LabDayOfWeekReport>(); for (int i = 0; i < 7; i++) { if (!weekends && isWeekend((DayOfWeek)i)) { continue; } weekDayReports.Add((DayOfWeek)i, new LabDayOfWeekReport((DayOfWeek)i)); } //for each day for (DateTime date = startDate.Date; date < endDate.Date; date = date.AddDays(1)) { //skip the weekends dates if weekends unincluded if (!weekends && isWeekend(date.DayOfWeek)) { continue; } double maxCompNum = 0; double minCompNum = int.MaxValue; double sum = 0; //for each hour check how many computers were on foreach (var hour in hoursToCheck.ToList()) { int computerCount = 0; int computers = 0; double occPrecent = 0; foreach (var item in cL.ToList()) { //if the computer were in the lab in this specific day if (date >= item.Entrance.Date && (!item.Exit.HasValue || date <= item.Exit.Value.Date)) { if (IsComputerActive(date, hour, item.ComputerId)) { computerCount++; } computers++; } } if (computers > 0) { occPrecent = (double)computerCount / computers; } maxCompNum = Math.Max(maxCompNum, occPrecent); minCompNum = Math.Min(minCompNum, occPrecent); sum += occPrecent; hourReports[hour].AddDay(occPrecent); } weekDayReports[date.DayOfWeek].AddDay(maxCompNum, minCompNum, sum / hoursToCheck.Count); } foreach (var item in weekDayReports) { labReport.AddByDayReport(item.Value); } foreach (var item in hourReports) { labReport.AddByHourReport(item.Value); } return(labReport); }