Merge branch '535-display-open-jos-in-the-last-24-hours' into 'master'

Resolve "Display open JOs in the last 24 hours"

Closes #535

See merge request jankstudio/resq!620
This commit is contained in:
Kendrick Chan 2021-02-08 09:20:00 +00:00
commit a9dfebf958
6 changed files with 157 additions and 2 deletions

View file

@ -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

View file

@ -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:

View file

@ -0,0 +1,75 @@
<?php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Doctrine\DBAL\Connection;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\JobOrderCache;
use App\Entity\JobOrder;
use App\Ramcar\JOStatus;
use DateTime;
class RefreshLatestActiveJobOrderCacheCommand extends Command
{
protected $em;
protected $jo_cache;
public function __construct(EntityManagerInterface $om, JobOrderCache $jo_cache)
{
$this->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;
}
}

View file

@ -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

View file

@ -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(

View file

@ -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);
}
}
}