From e306b5931ddb8d195aedd56f9f92cca1aed701dc Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 1 Feb 2021 09:33:19 +0000 Subject: [PATCH] Modify the hours_shift arguments for the script. Populate the hours shift array using selected schedule. #534 --- src/Controller/AnalyticsController.php | 140 +++++++++++++++---------- utils/schedule_solver/solver.py | 16 +-- 2 files changed, 91 insertions(+), 65 deletions(-) diff --git a/src/Controller/AnalyticsController.php b/src/Controller/AnalyticsController.php index d18b10bb..68e703ed 100644 --- a/src/Controller/AnalyticsController.php +++ b/src/Controller/AnalyticsController.php @@ -21,8 +21,7 @@ use DateInterval; use App\Entity\JobOrder; use App\Entity\Hub; - -use App\Ramcar\ShiftSchedule; +use App\Entity\ShiftSchedule; class AnalyticsController extends Controller { @@ -93,10 +92,19 @@ class AnalyticsController extends Controller $hub_list[$hub->getID()] = $hub->getName() . ' - ' . $hub->getBranch(); } + // get saved shift schedules + $all_shift_schedules = $em->getRepository(ShiftSchedule::class)->findAll(); + + $shift_sched_list = []; + foreach ($all_shift_schedules as $sched) + { + $shift_sched_list[$sched->getID()] = $sched->getName(); + } + $params = [ 'hub_list' => $hub_list, 'default_hubs' => $hub_ids, - 'shift_schedules' => ShiftSchedule::getCollection(), + 'shift_schedules' => $shift_sched_list, ]; return $this->render('analytics/forecast_form.html.twig', $params); @@ -123,8 +131,9 @@ class AnalyticsController extends Controller $shift = $req->request->get('shift_schedule'); // TODO: populate the hour_shift array, depending on the shift selected - $hour_shifts = $this->populateHourShift($shift); + $hour_shifts = $this->populateHourShift($em, $shift); + error_log('reference'); error_log(print_r($hour_shifts, true)); // error_log(print_r($hub_list, true)); @@ -177,7 +186,7 @@ class AnalyticsController extends Controller // error_log(print_r($scheduler_data, true)); // run scheduler - $sched_res = $this->runScheduler($scheduler_data, $hour_shifts, $shift); + $sched_res = $this->runScheduler($scheduler_data, $hour_shifts); // tally total JOs for the month foreach ($scheduler_data as $sday_data) @@ -252,7 +261,7 @@ class AnalyticsController extends Controller return $day_data; } - protected function runScheduler($scheduler_data, $hour_shifts, $shift) + protected function runScheduler($scheduler_data, $hour_shifts) { // run python script to solve scheduling for riders @@ -267,8 +276,37 @@ class AnalyticsController extends Controller foreach ($scheduler_data as $weekday_data) $args[] = implode('-', $weekday_data); - // add shift - $args[] = $shift; + // add length of hour_shifts + $args[] = count($hour_shifts); + + // form hour_shifts argument for the python script + foreach ($hour_shifts as $hour_shift) + { + $shift = ''; + foreach ($hour_shift as $entry) + { + // check if entry has format '00:00 - 00:00' + if (strpos($entry, ':')) + { + // need to modify to 00 - 00 + $hours = explode('-', $entry); + $start = trim($hours[0]); + $end = trim($hours[1]); + + // get the hour only + $s_parts = explode(':', $start); + $e_parts = explode(':', $end); + $start_hour = trim($s_parts[0]); + $end_hour = trim($s_parts[1]); + + $shift = '\'' . $start_hour . ' - ' . $end_hour . '\''; + } + else + $shift = $shift . ',' . $entry; + } + + $args[] = $shift; + } //error_log(print_r($args, true)); @@ -676,55 +714,51 @@ class AnalyticsController extends Controller { } - protected function populateHourShift($shift) + protected function populateHourShift(EntityManagerInterface $em, $id) { $hour_shift = []; - if ($shift == '24_7') { - $hour_shift = [ - ['00:00 - 09:00', 0, 1, 2, 3, 4, 5, 6, 7, 8], - ['01:00 - 10:00', 1, 2, 3, 4, 5, 6, 7, 8, 9], - ['02:00 - 11:00', 2, 3, 4, 5, 6, 7, 8, 9, 10], - ['03:00 - 12:00', 3, 4, 5, 6, 7, 8, 9, 10, 11], - ['04:00 - 13:00', 4, 5, 6, 7, 8, 9, 10, 11, 12], - ['05:00 - 14:00', 5, 6, 7, 8, 9, 10, 11, 12, 13], - ['06:00 - 15:00', 6, 7, 8, 9, 10, 11, 12, 13, 14], - ['07:00 - 16:00', 7, 8, 9, 10, 11, 12, 13, 14, 15], - ['08:00 - 17:00', 8, 9, 10, 11, 12, 13, 14, 15, 16], - ['09:00 - 18:00', 9, 10, 11, 12, 13, 14, 15, 16, 17], - ['10:00 - 19:00', 10, 11, 12, 13, 14, 15, 16, 17, 18], - ['11:00 - 20:00', 11, 12, 13, 14, 15, 16, 17, 18, 19], - ['12:00 - 21:00', 12, 13, 14, 15, 16, 17, 18, 19, 20], - ['13:00 - 22:00', 13, 14, 15, 16, 17, 18, 19, 20, 21], - ['14:00 - 23:00', 14, 15, 16, 17, 18, 19, 20, 21, 22], - ['15:00 - 00:00', 15, 16, 17, 18, 19, 20, 21, 22, 23], - ['16:00 - 01:00', 16, 17, 18, 19, 20, 21, 22, 23, 0], - ['17:00 - 02:00', 17, 18, 19, 20, 21, 22, 23, 0, 1], - ['18:00 - 03:00', 18, 19, 20, 21, 22, 23, 0, 1, 2], - ['19:00 - 04:00', 19, 20, 21, 22, 23, 0, 1, 2, 3], - ['20:00 - 05:00', 20, 21, 22, 23, 0, 1, 2, 3, 4], - ['21:00 - 06:00', 21, 22, 23, 0, 1, 2, 3, 4, 5], - ['22:00 - 07:00', 22, 23, 0, 1, 2, 3, 4, 5, 6], - ['23:00 - 08:00', 23, 0, 1, 2, 3, 4, 5, 6, 7], - ]; - } - if ($shift == '8AM_5PM') { - $hour_shift = [ - ['07:00 - 16:00', 7, 8, 9, 10, 11, 12, 13, 14, 15], - ['08:00 - 17:00', 8, 9, 10, 11, 12, 13, 14, 15, 16], - ]; - } + // get the hour shifts for the selected shift scheduled + $shift = $em->getRepository(ShiftSchedule::class)->find($id); - if ($shift == '7AM_10PM') { - $hour_shift = [ - ['07:00 - 16:00', 7, 8, 9, 10, 11, 12, 13, 14, 15], - ['08:00 - 17:00', 8, 9, 10, 11, 12, 13, 14, 15, 16], - ['09:00 - 18:00', 9, 10, 11, 12, 13, 14, 15, 16, 17], - ['10:00 - 19:00', 10, 11, 12, 13, 14, 15, 16, 17, 18], - ['11:00 - 20:00', 11, 12, 13, 14, 15, 16, 17, 18, 19], - ['12:00 - 21:00', 12, 13, 14, 15, 16, 17, 18, 19, 20], - ['13:00 - 22:00', 13, 14, 15, 16, 17, 18, 19, 20, 21] - ]; + if (empty($shift)) + throw $this->createNotFoundException('The item does not exist'); + + $shifts = $shift->getHourShifts(); + + foreach ($shifts as $shift) + { + $shift_entry = []; + // format of hour_shift: [['start time - end time', start time, ... , end time - 1]] + // first hour of hour shift is the start time + // form the first entry of the hour shift + $shift_time = $shift['start'] . ' - ' . $shift['end']; + + $shift_entry[] = $shift_time; + + $s_times = explode(':', $shift['start']); + $e_times = explode(':', $shift['end']); + + $shift_start = intval($s_times[0]); + $shift_end = intval($e_times[0]); + + if ($shift_start == 23) + { + $shift_entry[] = 23; + for ($hour = 0; $hour < $shift_end; $hour++) + { + $shift_entry[] = $hour; + } + } + for($hour = $shift_start; $hour < $shift_end; $hour++) + { + $shift_entry[] = $hour; + + if ($hour == 23) + $hour = 0; + } + + $hour_shift[] = $shift_entry; } return $hour_shift; diff --git a/utils/schedule_solver/solver.py b/utils/schedule_solver/solver.py index 62a1b059..aa49d70c 100644 --- a/utils/schedule_solver/solver.py +++ b/utils/schedule_solver/solver.py @@ -56,18 +56,10 @@ def main(): # form list within the list hour_shift_item = [] - # get the shifts from the arguments shift = sys.argv[argv_index+1] - split_shift = shift.split(',') - - # surround the first item in list with single quotes - shift_index = "\'" + split_shift[0] + "\'" - - # append the first item to the rest of the hour shift informaton - hour_shift = shift_index + ", " + split_shift[1] + ", " + split_shift[2] + ", " + split_shift[3] + ", " + split_shift[4] + ", " + split_shift[5] + ", " + split_shift[6] + ", " + split_shift[7] + ", " + split_shift[8] + ", " + split_shift[9] # append to list - hour_shift_item.append(hour_shift) + hour_shift_item.append(shift) # append the list to the list hour_shifts.append(hour_shift_item) @@ -131,11 +123,11 @@ def main(): # solve it! status = solver.Solve() - print('Number of variables =', solver.NumVariables()) - print('Number of constraints =', solver.NumConstraints()) + #print('Number of variables =', solver.NumVariables()) + #print('Number of constraints =', solver.NumConstraints()) if status == solver.OPTIMAL: - print('Optimal solution found!') + #print('Optimal solution found!') for day_index in range(0, len(day_shifts)): for hour_index in range(0, len(hour_shifts)): result = solv_shifts[day_index][hour_index].solution_value()