Update hub_jo_count when hub is assigned or reassigned via admin panel. #543

This commit is contained in:
Korina Cordero 2021-03-16 07:50:18 +00:00
parent 0115328801
commit 50e15cd770
3 changed files with 59 additions and 13 deletions

View file

@ -2333,7 +2333,8 @@ class APIController extends Controller implements LoggedController
public function newRequestJobOrder(Request $req, InvoiceGeneratorInterface $ic, GeofenceTracker $geo,
MapTools $map_tools, InventoryManager $im, MQTTClient $mclient,
RiderAssignmentHandlerInterface $rah, HubSelector $hub_select)
RiderAssignmentHandlerInterface $rah, HubSelector $hub_select,
HubDistributor $hub_dist)
{
// check required parameters and api key
$required_params = [
@ -2579,6 +2580,9 @@ class APIController extends Controller implements LoggedController
$jo->setStatusAutoAssign(AutoAssignStatus::HUB_AND_RIDER_ASSIGNED);
$assigned_rider->setAvailable(false);
// update redis hub_jo_count for hub
$hub_dist->incrementJoCountForHub($nearest_hubs[0]['hub']);
}
}
}
@ -2601,7 +2605,10 @@ class APIController extends Controller implements LoggedController
$jo->setStatusAutoAssign(AutoAssignStatus::HUB_ASSIGNED);
if ($date_schedule != null)
$jo->setDateSchedule($date_schedule);
$jo->setDateSchedule($date_schedule);
// update redis hub_jo_count for hub
$hub_dist->incrementJoCountForHub($hub);
}
$em->persist($jo);

View file

@ -17,7 +17,7 @@ class HubDistributor
$this->hub_jo_key = $hub_jo_key;
}
public function addJoCountForHub(Hub $hub)
public function incrementJoCountForHub(Hub $hub)
{
$key = $hub->getID();
@ -38,6 +38,20 @@ class HubDistributor
}
}
public function decrementJoCountForHub(Hub $hub)
{
$key = $hub->getID();
// get current count
$jo_count = $this->redis->hget($this->hub_jo_key, $key);
if ($jo_count)
{
// hub exist in hash
// decrement count
$this->redis->hset($this->hub_jo_key, $key, $jo_count - 1);
}
}
public function arrangeHubs($hubs)
{
if (count($hubs) == 1)
@ -57,17 +71,25 @@ class HubDistributor
// check if hub is in hash. if not, hub has no jobs
// but should still be added to results
$arranged_hubs[] = [
'hub' => $hub,
'db_distance' => $hub_data['db_distance'],
'distance' => $hub_data['distance'],
'duration' => $hub_data['duration'],
'jo_count' => 0,
];
if ($hub_jo_count != null)
{
$arranged_hubs['jo_count'] = $hub_jo_count;
$arranged_hubs[] = [
'hub' => $hub,
'db_distance' => $hub_data['db_distance'],
'distance' => $hub_data['distance'],
'duration' => $hub_data['duration'],
'jo_count' => $hub_jo_count,
];
}
else
{
$arranged_hubs[] = [
'hub' => $hub,
'db_distance' => $hub_data['db_distance'],
'distance' => $hub_data['distance'],
'duration' => $hub_data['duration'],
'jo_count' => 0,
];
}
}

View file

@ -49,6 +49,7 @@ use App\Service\MQTTClient;
use App\Service\APNSClient;
use App\Service\MapTools;
use App\Service\RisingTideGateway;
use App\Service\HubDistributor;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
@ -70,13 +71,15 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
protected $country_code;
protected $wh;
protected $rt;
protected $hub_dist;
protected $template_hash;
public function __construct(Security $security, EntityManagerInterface $em,
InvoiceGeneratorInterface $ic, ValidatorInterface $validator,
TranslatorInterface $translator, RiderAssignmentHandlerInterface $rah,
string $country_code, WarrantyHandler $wh, RisingTideGateway $rt)
string $country_code, WarrantyHandler $wh, RisingTideGateway $rt,
HubDistributor $hub_dist)
{
$this->em = $em;
$this->ic = $ic;
@ -87,6 +90,7 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
$this->country_code = $country_code;
$this->wh = $wh;
$this->rt = $rt;
$this->hub_dist = $hub_dist;
$this->loadTemplates();
}
@ -753,6 +757,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
'event' => 'outlet_assign'
];
$mclient->sendEvent($obj, $payload);
// update redis hub jo count
$this->hub_dist->incrementJoCountForHub($hub);
}
return $error_array;
@ -1122,6 +1129,9 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
$more_reason = $req->request->get('not_wait_notes');
}
// get previously assigned hub, if any
$old_hub = $obj->getHub();
if (empty($error_array))
{
// rider mqtt event
@ -1195,6 +1205,13 @@ class ResqJobOrderHandler implements JobOrderHandlerInterface
'event' => 'outlet_assign'
];
$mclient->sendEvent($obj, $payload);
// update redis hub_jo_count for hub
// decrement old hub's count and increment new hub's count
if ($old_hub != null)
$this->hub_dist->decrementJoCountForHub($old_hub);
if ($hub != null)
$this->hub_dist->incrementJoCountForHub($hub);
}
return $error_array;