public async void CreateNewSchedule_Has_Statistics() { // Ref date, Monday August 20, 2018 var date = new DateTime(2018, 8, 20); var teamShiftScheduler = new TeamShiftScheduler(); var schedules = await teamShiftScheduler.CreateNewScheduleAsync(GetDefaultTransitionSet().RuleSet, GetEmployees(), date, 14, 2, 2, 7, 12, 1); // We have statistics for debugging Assert.NotEmpty(teamShiftScheduler.StatisticalResults); }
public async void CreateNewSchedule_Has_Result() { // Ref date, Monday August 20, 2018 var date = new DateTime(2018, 8, 20); var teamShiftScheduler = new TeamShiftScheduler(); var schedules = await teamShiftScheduler.CreateNewScheduleAsync(GetDefaultTransitionSet().RuleSet, GetEmployees(), date, 14, 2, 2, 7, 12, 1); // We have one schedule solution in the results Assert.Single(schedules); }
public async void CreateNewSchedule_Has_Valid_Params() { // Ref date, Monday August 20, 2018 var date = new DateTime(2018, 8, 20); var teamShiftScheduler = new TeamShiftScheduler(); var employees = GetEmployees(); var schedules = await teamShiftScheduler.CreateNewScheduleAsync(GetDefaultTransitionSet().RuleSet, employees, date, 14, 2, 2, 7, 12, 3); // We have 3 solutions, dates match and we have 14 days * 10 employee shifts Assert.Equal(3, schedules.Count); foreach (var schedule in schedules) { Assert.Equal(14, schedule.Days); Assert.Equal(date, schedule.StartDate); Assert.Equal(date.AddDays(14), schedule.EndDate); Assert.Equal(140, schedule.Shifts.Count); foreach (var shift in schedule.Shifts) { if (shift.Type == ShiftType.Day) { Assert.Equal(7, shift.StartDate.Hour); } if (shift.Type == ShiftType.Night) { Assert.Equal(19, shift.StartDate.Hour); } if (shift.Type == ShiftType.Off) { Assert.Equal(0, shift.StartDate.Hour); } if (shift.Type == ShiftType.Day) { Assert.Equal(19, shift.EndDate.Hour); } if (shift.Type == ShiftType.Night) { Assert.Equal(7, shift.EndDate.Hour); } if (shift.Type == ShiftType.Off) { Assert.Equal(0, shift.EndDate.Hour); } Assert.Contains(shift.Employee, employees); } } }
/// <summary> /// Create a new schedule from a given day. /// Some demo settings are in place here. /// </summary> /// <param name="scheduleRequest">Parameters for the search</param> /// <returns>Scheduled shifts</returns> public async Task <ScheduleDto> GetNewScheduleAsync(ScheduleRequestDto scheduleRequest) { // Get team members from repository var teamMembers = await _employeeRepository.QueryNoTracking() .Where(w => w.IsActive) // Active ones for next schedule .OrderBy(c => new Random().Next()) // Shuffle staff order .Take(scheduleRequest.NumberOfEmployees) // we take a number from all employees in demo .ToListAsync(); // Get transition set rules from repository var transitionSet = await _transitionSetRepository.QueryNoTracking() .Where(w => w.Id == scheduleRequest.TransitionSetId && w.IsActive) // active transition set .FirstOrDefaultAsync(); // We only have that number of employees if (teamMembers.Count < scheduleRequest.NumberOfEmployees) { throw new ArgumentOutOfRangeException($"Invalid number of employees for scheduling."); } // Check we have the active transition set if (transitionSet == null) { throw new ArgumentOutOfRangeException($"Invalid transition set defined."); } var ruleSet = TransitionSet.FromRuleSetString(transitionSet.Name, transitionSet.RuleSetString).RuleSet; // Take 5 solutions as a demo result var schedules = await _teamShiftScheduler.CreateNewScheduleAsync(ruleSet, teamMembers, scheduleRequest.StartDate.Date, scheduleRequest.Days, scheduleRequest.TeamSize, scheduleRequest.MinShiftsPerCycle, scheduleRequest.StartHour, scheduleRequest.ShiftHours, 5); // Play with results for demo if (schedules.Count > 0) { return(ScheduleDto.FromEntity(schedules[new Random().Next(schedules.Count)], GetLatestStatistics())); } // Return default with statistics on model error return(new ScheduleDto() { Statistics = GetLatestStatistics(), Error = GetLatestError() }); // Or just return first. // return ScheduleToDto(schedules.FirstOrDefault()); }
public async void CreateNewSchedule_Has_Valid_Result() { // Ref date, Monday August 20, 2018 var date = new DateTime(2018, 8, 20); var teamShiftScheduler = new TeamShiftScheduler(); var employees = GetEmployees(); var schedules = await teamShiftScheduler.CreateNewScheduleAsync(GetDefaultTransitionSet().RuleSet, employees, date, 14, 2, 2, 7, 12, 3); var schedule = schedules.FirstOrDefault(); Assert.NotNull(schedule); var shifts = schedule?.Shifts.ToList(); Assert.IsType <Schedule>(schedule); Assert.IsType <List <Shift> >(shifts); // Check the result for rules for (int i = 0; i < 14; i++) { var currentDate = date.AddDays(i); var dateShifts = shifts.Where(w => w.StartDate.Date == currentDate).ToList(); Assert.Equal(10, dateShifts.Count); Assert.Equal(2, dateShifts.Count(w => w.Type == ShiftType.Day)); Assert.Equal(2, dateShifts.Count(w => w.Type == ShiftType.Night)); Assert.Equal(6, dateShifts.Count(w => w.Type == ShiftType.Off)); Assert.Equal(10, dateShifts.Select(s => s.Employee).Distinct().Count()); } // And more... }