From e13e3c0dd5623a4be616365f0e2fc60c9be5bc49 Mon Sep 17 00:00:00 2001 From: Kendrick Chan Date: Sun, 14 Jun 2020 18:54:52 +0800 Subject: [PATCH] Fix bug when no JO comes up for hub in 2018 #409 --- src/Controller/AnalyticsController.php | 55 +++++++++++++------ templates/analytics/forecast_submit.html.twig | 8 +++ 2 files changed, 46 insertions(+), 17 deletions(-) diff --git a/src/Controller/AnalyticsController.php b/src/Controller/AnalyticsController.php index dbbdbf29..f78cbcf4 100644 --- a/src/Controller/AnalyticsController.php +++ b/src/Controller/AnalyticsController.php @@ -140,19 +140,28 @@ class AnalyticsController extends Controller // error_log(print_r($scheduler_data, true)); unset($chart_all_weekdays['scheduler_data']); - // run scheduler - // send 2018 + month data - $sched_res = $this->runScheduler($scheduler_data['2018'][$month]); - - // tally total JOs for the month $total_jos = 0; - foreach ($scheduler_data['2018'][$month] as $sday_data) + + // run scheduler + // check if 2018 data is available + if (isset($scheduler_data['2018'][$month])) { - foreach ($sday_data as $shour_data) - { - $total_jos += $shour_data; - } + // send 2018 + month data + $sched_res = $this->runScheduler($scheduler_data['2018'][$month]); + + // tally total JOs for the month + foreach ($scheduler_data['2018'][$month] as $sday_data) + foreach ($sday_data as $shour_data) + $total_jos += $shour_data; } + else + { + $sched_res = [ + 'weekday_shifts' => $this->initDayData(), + 'total_riders' => 0, + ]; + } + $hub_data[$hub_id]['data_weekday'] = $chart_weekday; $hub_data[$hub_id]['data_all_weekdays'] = $chart_all_weekdays; @@ -178,6 +187,23 @@ class AnalyticsController extends Controller return $this->render('analytics/forecast_submit.html.twig', $params); } + protected function initDayData() + { + $day_data = []; + // each weekday + for ($i = 0; $i < 7; $i++) + { + $day_data[$i] = [ + 'weekday' => $this->weekdays[$i], + 'total_jos' => 0, + 'total_riders' => 0, + 'shifts' => [], + ]; + } + + return $day_data; + } + protected function runScheduler($scheduler_data) { // run python script to solve scheduling for riders @@ -208,7 +234,7 @@ class AnalyticsController extends Controller // segregate into weekdays - $day_data = []; + $day_data = $this->initDayData(); $i = 0; foreach ($scheduler_data as $weekday_data) { @@ -216,12 +242,7 @@ class AnalyticsController extends Controller foreach ($weekday_data as $hourly_jo) $total_jos += $hourly_jo; - $day_data[$i] = [ - 'weekday' => $this->weekdays[$i], - 'total_jos' => $total_jos, - 'total_riders' => 0, - 'shifts' => [], - ]; + $day_data[$i]['total_jos'] = $total_jos; $i++; } diff --git a/templates/analytics/forecast_submit.html.twig b/templates/analytics/forecast_submit.html.twig index 2fafd57f..937fb659 100644 --- a/templates/analytics/forecast_submit.html.twig +++ b/templates/analytics/forecast_submit.html.twig @@ -131,14 +131,22 @@ {{ hub.data_shift[i].weekday }} {{ hub.data_shift[i].total_jos }} {{ hub.data_shift[i].total_riders }} + {% if hub.data_shift[i].total_riders == 0 %} + 0 + {% else %} {{ (hub.data_shift[i].total_jos / hub.data_shift[i].total_riders) | round(1, 'common') }} + {% endif %} {% endfor %} Overall {{ hub.total_jos }} {{ hub.total_riders }} + {% if hub.total_riders == 0 %} + 0 + {% else %} {{ (hub.total_jos / hub.total_riders) | round(1, 'common') }} + {% endif %}