From 4231c144d029420cca2a278e5842f0a4cdca7fce Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 8 Feb 2021 08:38:50 +0000 Subject: [PATCH] Add redis cache to have the JOs from the last 24 hours. #535 --- .env.dist | 3 ++ config/services.yaml | 1 + src/Controller/HomeController.php | 21 +++---------- src/Service/JobOrderCache.php | 51 ++++++++++++++++++++++++++++++- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/.env.dist b/.env.dist index 9fc0a002..f6d35eec 100644 --- a/.env.dist +++ b/.env.dist @@ -55,6 +55,9 @@ CVU_BRAND_ID=insert_brandid_for_unknown_vehicles # country code prefix COUNTRY_CODE=+insert_country_code_here +# redis hash +LATEST_ACTIVE_JO=latest_active_jo + # dashboard DASHBOARD_ENABLE=set_to_true_or_false diff --git a/config/services.yaml b/config/services.yaml index c2388e79..75c83d1d 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -223,6 +223,7 @@ services: arguments: $redis_prov: "@App\\Service\\RedisClientProvider" $active_jo_key: "%env(LOCATION_JO_ACTIVE_KEY)%" + $latest_jo_key: "%env(LATEST_ACTIVE_JO)%" App\Service\RiderCache: arguments: diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 2e56d128..234e6bbc 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -47,7 +47,8 @@ class HomeController extends Controller public function getRiderLocations(JobOrderCache $jo_cache, RiderCache $rider_cache, EntityManagerInterface $em, RiderTracker $rider_tracker) { // get active JOs from cache - $active_jos = $jo_cache->getAllActiveJobOrders(); + // $active_jos = $jo_cache->getAllActiveJobOrders(); + $active_jos = $jo_cache->getAllLatestActiveJobOrders(); $riders = $rider_cache->getAllActiveRiders(); // TODO: optimize this @@ -78,25 +79,13 @@ class HomeController extends Controller continue; } - // check if JO's date schedule is from the last 24 hours - $date = new DateTime(); - $date_interval = new DateInterval('P1D'); - $date->sub($date_interval); - - if ($jo->getDateSchedule() < $date) + if ($jo->getSource() == TransactionOrigin::MOBILE_APP) { - unset($active_jos[$jo_id]); + $active_jos[$jo_id]['is_mobile'] = true; } else { - if ($jo->getSource() == TransactionOrigin::MOBILE_APP) - { - $active_jos[$jo_id]['is_mobile'] = true; - } - else - { - $active_jos[$jo_id]['is_mobile'] = false; - } + $active_jos[$jo_id]['is_mobile'] = false; } } diff --git a/src/Service/JobOrderCache.php b/src/Service/JobOrderCache.php index 488db29d..b506b68f 100644 --- a/src/Service/JobOrderCache.php +++ b/src/Service/JobOrderCache.php @@ -9,11 +9,14 @@ class JobOrderCache { protected $redis; protected $active_jo_key; + protected $latest_active_jo_key; - public function __construct(RedisClientProvider $redis_prov, $active_jo_key) + public function __construct(RedisClientProvider $redis_prov, $active_jo_key, + $latest_jo_key) { $this->redis = $redis_prov->getRedisClient(); $this->active_jo_key = $active_jo_key; + $this->latest_active_jo_key = $latest_jo_key; } public function addActiveJobOrder(JobOrder $jo) @@ -26,6 +29,17 @@ class JobOrderCache $coords->getLatitude(), $jo->getID() ); + + // add to JO cache with expiry date + $key = $jo->getID(); + $data = [ + 'id' => $key, + 'latitude' => $coords->getLatitude(), + 'longitude' => $coords->getLongitude(), + ]; + $value = json_encode($data); + + $this->redis->hset($this->latest_active_jo_key, $key, $value); } public function getAllActiveJobOrders() @@ -56,11 +70,46 @@ class JobOrderCache return $jo_locs; } + public function getAllLatestActiveJobOrders() + { + // get all fields in latest_active_jo hash + $latest_active_jos = $this->redis->hgetall($this->latest_active_jo_key); + + $jo_locations = []; + foreach ($latest_active_jos as $active_jo) + { + $jo_data = json_decode($active_jo, true); + $id = ''; + $lat = ''; + $lng = ''; + foreach ($jo_data as $key => $value) + { + if ($key == 'id') + $id = $value; + if ($key == 'longitude') + $lng = $value; + if ($key == 'latitude') + $lat = $value; + + } + $jo_locations[$id] = [ + 'longitude' => $lng, + 'latitude' => $lat, + ]; + } + + //error_log(print_r($jo_locations, true)); + return $jo_locations; + } + public function removeActiveJobOrder(JobOrder $jo) { $this->redis->zrem( $this->active_jo_key, $jo->getID() ); + + // remove key from latest active jo + $this->redis->hdel($this->latest_active_jo_key, $jo->getID()); } }