Add arranging of hubs, based on job orders assigned to hub. #543

This commit is contained in:
Korina Cordero 2021-03-16 06:33:40 +00:00
parent 1abaad1727
commit 0115328801
3 changed files with 56 additions and 17 deletions

View file

@ -37,6 +37,7 @@ use App\Service\MapTools;
use App\Service\InventoryManager;
use App\Service\RiderAssignmentHandlerInterface;
use App\Service\HubSelector;
use App\Service\HubDistributor;
use App\Entity\MobileSession;
use App\Entity\Customer;
@ -2520,9 +2521,9 @@ class APIController extends Controller implements LoggedController
// TODO: set this properly. This is test data
$hub_criteria = new HubCriteria();
$hub_criteria->setPoint($jo->getCoordinates())
->setLimitResults(1)
->setLimitDistance(5)
->setInventoryCheck(true)
->setLimitResults(50)
->setLimitDistance(100)
->setInventoryCheck(false)
->setJoType($jo->getServiceType())
->setDateTime(new DateTime());

View file

@ -2,8 +2,6 @@
namespace App\Service;
use Doctrine\ORM\EntityManagerInterface;
use App\Service\RedisClientProvider;
use App\Entity\Hub;
@ -13,8 +11,7 @@ class HubDistributor
protected $redis;
protected $hub_jo_key;
public function __construct(RedisClientProvider $redis,
$hub_jo_key)
public function __construct(RedisClientProvider $redis, $hub_jo_key)
{
$this->redis = $redis->getRedisClient();
$this->hub_jo_key = $hub_jo_key;
@ -22,33 +19,69 @@ class HubDistributor
public function addJoCountForHub(Hub $hub)
{
$key = $hub->gtID();
$key = $hub->getID();
// get current count
$result = $this->redis->hget($this->hub_jo_key, $key);
if ($result == true)
{
// hub exist in hash
// add to count
}
else
$jo_count = $this->redis->hget($this->hub_jo_key, $key);
if ($jo_count == false)
{
// hub not in hash
// add hub to hash
// set to 1 since this is first jo for hub
$this->redis->hset($this->hub_jo_key, $key, 1);
}
else
{
// hub exist in hash
// add to count
$this->redis->hset($this->hub_jo_key, $key, $jo_count + 1);
}
}
public function arrangeHubs($hubs)
{
if (count($hubs) == 1)
return $hubs;
$arranged_hubs = [];
foreach ($hubs as $hub_data)
{
$hub = $hub_data['hub'];
// get hub in hash
// need the id of hub
$key = $hub->getID();
// get jo count of hub
$hub_jo_count = $this->redis->hget($this->hub_jo_key, $key);
// 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;
}
}
usort($arranged_hubs, function($a, $b) {
if ($a['jo_count'] == $b['jo_count'])
return 0;
if ($a['jo_count'] < $b['jo_count'])
return -1;
else
return 1;
});
//error_log('arranged hubs ' . json_encode($arranged_hubs));
return $arranged_hubs;
}
}

View file

@ -60,12 +60,16 @@ class HubSelector
$hubs_round_robin = $this->filterHubsByRoundRobin($filtered_hubs);
$filtered_hubs = $hubs_round_robin;
//error_log(json_encode($filtered_hubs));
// max results filter
$hubs_max_result = $this->filterHubsByMaxResults($filtered_hubs, $limit_results);
$filtered_hubs = $hubs_max_result;
$results = $filtered_hubs;
//error_log(json_encode($results));
return $results;
}
@ -93,7 +97,8 @@ class HubSelector
$results = [];
for ($i = 0; $i < $limit_result; $i++)
{
$results[] = $hubs[$i];
if ($i < count($hubs))
$results[] = $hubs[$i];
}
return $results;