Add redis cache to have the JOs from the last 24 hours. #535

This commit is contained in:
Korina Cordero 2021-02-08 08:38:50 +00:00
parent 9473609459
commit 4231c144d0
4 changed files with 59 additions and 17 deletions

View file

@ -55,6 +55,9 @@ CVU_BRAND_ID=insert_brandid_for_unknown_vehicles
# country code prefix # country code prefix
COUNTRY_CODE=+insert_country_code_here COUNTRY_CODE=+insert_country_code_here
# redis hash
LATEST_ACTIVE_JO=latest_active_jo
# dashboard # dashboard
DASHBOARD_ENABLE=set_to_true_or_false DASHBOARD_ENABLE=set_to_true_or_false

View file

@ -223,6 +223,7 @@ services:
arguments: arguments:
$redis_prov: "@App\\Service\\RedisClientProvider" $redis_prov: "@App\\Service\\RedisClientProvider"
$active_jo_key: "%env(LOCATION_JO_ACTIVE_KEY)%" $active_jo_key: "%env(LOCATION_JO_ACTIVE_KEY)%"
$latest_jo_key: "%env(LATEST_ACTIVE_JO)%"
App\Service\RiderCache: App\Service\RiderCache:
arguments: arguments:

View file

@ -47,7 +47,8 @@ class HomeController extends Controller
public function getRiderLocations(JobOrderCache $jo_cache, RiderCache $rider_cache, EntityManagerInterface $em, RiderTracker $rider_tracker) public function getRiderLocations(JobOrderCache $jo_cache, RiderCache $rider_cache, EntityManagerInterface $em, RiderTracker $rider_tracker)
{ {
// get active JOs from cache // get active JOs from cache
$active_jos = $jo_cache->getAllActiveJobOrders(); // $active_jos = $jo_cache->getAllActiveJobOrders();
$active_jos = $jo_cache->getAllLatestActiveJobOrders();
$riders = $rider_cache->getAllActiveRiders(); $riders = $rider_cache->getAllActiveRiders();
// TODO: optimize this // TODO: optimize this
@ -78,25 +79,13 @@ class HomeController extends Controller
continue; continue;
} }
// check if JO's date schedule is from the last 24 hours if ($jo->getSource() == TransactionOrigin::MOBILE_APP)
$date = new DateTime();
$date_interval = new DateInterval('P1D');
$date->sub($date_interval);
if ($jo->getDateSchedule() < $date)
{ {
unset($active_jos[$jo_id]); $active_jos[$jo_id]['is_mobile'] = true;
} }
else else
{ {
if ($jo->getSource() == TransactionOrigin::MOBILE_APP) $active_jos[$jo_id]['is_mobile'] = false;
{
$active_jos[$jo_id]['is_mobile'] = true;
}
else
{
$active_jos[$jo_id]['is_mobile'] = false;
}
} }
} }

View file

@ -9,11 +9,14 @@ class JobOrderCache
{ {
protected $redis; protected $redis;
protected $active_jo_key; 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->redis = $redis_prov->getRedisClient();
$this->active_jo_key = $active_jo_key; $this->active_jo_key = $active_jo_key;
$this->latest_active_jo_key = $latest_jo_key;
} }
public function addActiveJobOrder(JobOrder $jo) public function addActiveJobOrder(JobOrder $jo)
@ -26,6 +29,17 @@ class JobOrderCache
$coords->getLatitude(), $coords->getLatitude(),
$jo->getID() $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() public function getAllActiveJobOrders()
@ -56,11 +70,46 @@ class JobOrderCache
return $jo_locs; 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) public function removeActiveJobOrder(JobOrder $jo)
{ {
$this->redis->zrem( $this->redis->zrem(
$this->active_jo_key, $this->active_jo_key,
$jo->getID() $jo->getID()
); );
// remove key from latest active jo
$this->redis->hdel($this->latest_active_jo_key, $jo->getID());
} }
} }