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/Command/RefreshLatestActiveJobOrderCacheCommand.php b/src/Command/RefreshLatestActiveJobOrderCacheCommand.php new file mode 100644 index 00000000..d37636de --- /dev/null +++ b/src/Command/RefreshLatestActiveJobOrderCacheCommand.php @@ -0,0 +1,75 @@ +em = $om; + $this->jo_cache = $jo_cache; + + parent::__construct(); + } + + protected function configure() + { + $this->setName('joborder:refresh_latest_cache') + ->setDescription('Refresh latest active job order cache from database.') + ->setHelp('Refresh latest active job order cache from database.'); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + // remove all entries in cache + $this->jo_cache->clearLatestActiveJobOrderCache(); + + $date = new DateTime(); + $date->modify('-1 day'); + + $status_list = [ + JOStatus::PENDING, + JOStatus::RIDER_ASSIGN, + JOStatus::ASSIGNED, + JOStatus::IN_TRANSIT, + JOStatus::IN_PROGRESS, + ]; + + $qb = $this->em->getRepository(JobOrder::class) + ->createQueryBuilder('jo'); + $res = $qb->select('jo') + ->where('jo.status IN (:statuses)') + ->andWhere('jo.date_schedule >= :date') + ->setParameter('statuses', $status_list, Connection::PARAM_STR_ARRAY) + ->setParameter('date', $date) + ->getQuery() + ->execute(); + + // add each to latest active cache + foreach ($res as $jo) + { + $this->jo_cache->addLatestActiveJoborder($jo); + } + + return 0; + } +} diff --git a/src/Controller/HomeController.php b/src/Controller/HomeController.php index 1a157b78..234e6bbc 100644 --- a/src/Controller/HomeController.php +++ b/src/Controller/HomeController.php @@ -17,6 +17,9 @@ use App\Entity\JobOrder; use App\Ramcar\TransactionOrigin; +use DateTime; +use DateInterval; + class HomeController extends Controller { /** @@ -44,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 diff --git a/src/EventListener/JobOrderActiveCacheListener.php b/src/EventListener/JobOrderActiveCacheListener.php index 05a6a612..d02bbd09 100644 --- a/src/EventListener/JobOrderActiveCacheListener.php +++ b/src/EventListener/JobOrderActiveCacheListener.php @@ -81,6 +81,9 @@ class JobOrderActiveCacheListener // save in cache $this->jo_cache->addActiveJobOrder($jo); + // save in latest JO cache + $this->jo_cache->addLatestActiveJoborder($jo); + // publish to mqtt $coords = $jo->getCoordinates(); @@ -116,6 +119,9 @@ class JobOrderActiveCacheListener // remove from redis cache $this->jo_cache->removeActiveJobOrder($jo); + // remove from latest JO cache + $this->jo_cache->removeLatestActiveJobOrder($jo); + // publish to mqtt // send jo status $this->mqtt->publish( diff --git a/src/Service/JobOrderCache.php b/src/Service/JobOrderCache.php index 488db29d..c606be73 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) @@ -28,6 +31,22 @@ class JobOrderCache ); } + public function addLatestActiveJoborder(JobOrder $jo) + { + $coords = $jo->getCoordinates(); + + // 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() { $all_jo = $this->redis->georadius( @@ -56,6 +75,38 @@ 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( @@ -63,4 +114,19 @@ class JobOrderCache $jo->getID() ); } + + public function removeLatestActiveJobOrder(JobOrder $jo) + { + // remove key from latest active jo + $this->redis->hdel($this->latest_active_jo_key, $jo->getID()); + } + + public function clearLatestActiveJobOrderCache() + { + $keys = $this->redis->hkeys($this->latest_active_jo_key); + foreach ($keys as $key) + { + $this->redis->hdel($this->latest_active_jo_key, $key); + } + } }